forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_buy_offer.go
98 lines (85 loc) · 2.9 KB
/
manage_buy_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
89
90
91
92
93
94
95
96
97
98
package txnbuild
import (
"github.com/aiblocks/go/amount"
"github.com/aiblocks/go/support/errors"
"github.com/aiblocks/go/xdr"
)
// ManageBuyOffer represents the AiBlocks manage buy offer operation. See
// https://www.aiblocks.io/developers/guides/concepts/list-of-operations.html
type ManageBuyOffer struct {
Selling Asset
Buying Asset
Amount string
Price string
price price
OfferID int64
SourceAccount Account
}
// BuildXDR for ManageBuyOffer returns a fully configured XDR Operation.
func (mo *ManageBuyOffer) BuildXDR() (xdr.Operation, error) {
xdrSelling, err := mo.Selling.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set XDR 'Selling' field")
}
xdrBuying, err := mo.Buying.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set XDR 'Buying' field")
}
xdrAmount, err := amount.Parse(mo.Amount)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse 'Amount'")
}
if err = mo.price.parse(mo.Price); err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse 'Price'")
}
opType := xdr.OperationTypeManageBuyOffer
xdrOp := xdr.ManageBuyOfferOp{
Selling: xdrSelling,
Buying: xdrBuying,
BuyAmount: xdrAmount,
Price: mo.price.toXDR(),
OfferId: xdr.Int64(mo.OfferID),
}
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, mo.SourceAccount)
return op, nil
}
// FromXDR for ManageBuyOffer initialises the txnbuild struct from the corresponding xdr Operation.
func (mo *ManageBuyOffer) FromXDR(xdrOp xdr.Operation) error {
result, ok := xdrOp.Body.GetManageBuyOfferOp()
if !ok {
return errors.New("error parsing manage_buy_offer operation from xdr")
}
mo.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
mo.OfferID = int64(result.OfferId)
mo.Amount = amount.String(result.BuyAmount)
if result.Price != (xdr.Price{}) {
mo.price.fromXDR(result.Price)
mo.Price = mo.price.string()
}
buyingAsset, err := assetFromXDR(result.Buying)
if err != nil {
return errors.Wrap(err, "error parsing buying_asset in manage_buy_offer operation")
}
mo.Buying = buyingAsset
sellingAsset, err := assetFromXDR(result.Selling)
if err != nil {
return errors.Wrap(err, "error parsing selling_asset in manage_buy_offer operation")
}
mo.Selling = sellingAsset
return nil
}
// Validate for ManageBuyOffer validates the required struct fields. It returns an error if any
// of the fields are invalid. Otherwise, it returns nil.
func (mo *ManageBuyOffer) Validate() error {
return validateOffer(mo.Buying, mo.Selling, mo.Amount, mo.Price, mo.OfferID)
}
// GetSourceAccount returns the source account of the operation, or nil if not
// set.
func (mo *ManageBuyOffer) GetSourceAccount() Account {
return mo.SourceAccount
}