forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
create_passive_offer.go
57 lines (49 loc) · 1.56 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
package txnbuild
import (
"github.com/stellar/go/amount"
"github.com/stellar/go/price"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// CreatePassiveSellOffer represents the Stellar create passive offer operation. See
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html
type CreatePassiveSellOffer struct {
Selling Asset
Buying Asset
Amount string
Price string
SourceAccount Account
}
// 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'")
}
xdrPrice, err := price.Parse(cpo.Price)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse 'Price'")
}
xdrOp := xdr.CreatePassiveSellOfferOp{
Selling: xdrSelling,
Buying: xdrBuying,
Amount: xdrAmount,
Price: xdrPrice,
}
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
}