forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_passive_offer.go
88 lines (75 loc) · 2.71 KB
/
create_passive_offer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package txnbuild
import (
"github.com/stellar/go/amount"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// CreatePassiveSellOffer represents the Stellar create passive offer operation. See
// https://developers.stellar.org/docs/start/list-of-operations/
type CreatePassiveSellOffer struct {
Selling Asset
Buying Asset
Amount string
Price xdr.Price
SourceAccount string
}
// BuildXDR for CreatePassiveSellOffer returns a fully configured XDR Operation.
func (cpo *CreatePassiveSellOffer) BuildXDR() (xdr.Operation, error) {
xdrSelling, err := cpo.Selling.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set XDR 'Selling' field")
}
xdrBuying, err := cpo.Buying.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set XDR 'Buying' field")
}
xdrAmount, err := amount.Parse(cpo.Amount)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse 'Amount'")
}
xdrOp := xdr.CreatePassiveSellOfferOp{
Selling: xdrSelling,
Buying: xdrBuying,
Amount: xdrAmount,
Price: cpo.Price,
}
opType := xdr.OperationTypeCreatePassiveSellOffer
body, err := xdr.NewOperationBody(opType, xdrOp)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to build XDR OperationBody")
}
op := xdr.Operation{Body: body}
SetOpSourceAccount(&op, cpo.SourceAccount)
return op, nil
}
// FromXDR for CreatePassiveSellOffer initialises the txnbuild struct from the corresponding xdr Operation.
func (cpo *CreatePassiveSellOffer) FromXDR(xdrOp xdr.Operation) error {
result, ok := xdrOp.Body.GetCreatePassiveSellOfferOp()
if !ok {
return errors.New("error parsing create_passive_sell_offer operation from xdr")
}
cpo.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
cpo.Amount = amount.String(result.Amount)
cpo.Price = result.Price
buyingAsset, err := assetFromXDR(result.Buying)
if err != nil {
return errors.Wrap(err, "error parsing buying_asset in create_passive_sell_offer operation")
}
cpo.Buying = buyingAsset
sellingAsset, err := assetFromXDR(result.Selling)
if err != nil {
return errors.Wrap(err, "error parsing selling_asset in create_passive_sell_offer operation")
}
cpo.Selling = sellingAsset
return nil
}
// Validate for CreatePassiveSellOffer validates the required struct fields. It returns an error if any
// of the fields are invalid. Otherwise, it returns nil.
func (cpo *CreatePassiveSellOffer) Validate() error {
return validatePassiveOffer(cpo.Buying, cpo.Selling, cpo.Amount, cpo.Price)
}
// GetSourceAccount returns the source account of the operation, or the empty string if not
// set.
func (cpo *CreatePassiveSellOffer) GetSourceAccount() string {
return cpo.SourceAccount
}