forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder_manage_offer.go
75 lines (61 loc) · 2.01 KB
/
builder_manage_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
package bridge
import (
"strconv"
"github.com/stellar/go/txnbuild"
shared "github.com/stellar/go/services/internal/bridge-compliance-shared"
"github.com/stellar/go/services/internal/bridge-compliance-shared/http/helpers"
"github.com/stellar/go/services/internal/bridge-compliance-shared/protocols"
)
// ManageOfferOperationBody represents manage_offer operation
type ManageOfferOperationBody struct {
PassiveOffer bool `json:"-"`
Source *string
Selling protocols.Asset
Buying protocols.Asset
Amount string
Price string
OfferID *string `json:"offer_id"`
}
// Build returns a txnbuild.Operation
func (op ManageOfferOperationBody) Build() txnbuild.Operation {
if op.PassiveOffer {
txnOp := txnbuild.CreatePassiveSellOffer{
Selling: txnbuild.CreditAsset{Code: op.Selling.Code, Issuer: op.Selling.Issuer},
Buying: txnbuild.CreditAsset{Code: op.Buying.Code, Issuer: op.Buying.Issuer},
Amount: op.Amount,
Price: op.Price,
}
if op.Source != nil {
txnOp.SourceAccount = &txnbuild.SimpleAccount{AccountID: *op.Source}
}
return &txnOp
}
txnOp := txnbuild.ManageSellOffer{
Selling: txnbuild.CreditAsset{Code: op.Selling.Code, Issuer: op.Selling.Issuer},
Buying: txnbuild.CreditAsset{Code: op.Buying.Code, Issuer: op.Buying.Issuer},
Amount: op.Amount,
Price: op.Price,
}
if op.OfferID != nil {
// Validated in Validate()
u, _ := strconv.ParseInt(*op.OfferID, 10, 64)
txnOp.OfferID = u
}
if op.Source != nil {
txnOp.SourceAccount = &txnbuild.SimpleAccount{AccountID: *op.Source}
}
return &txnOp
}
// Validate validates if operation body is valid.
func (op ManageOfferOperationBody) Validate() error {
if op.OfferID != nil {
_, err := strconv.ParseInt(*op.OfferID, 10, 64)
if err != nil {
return helpers.NewInvalidParameterError("offer_id", "Not a number.")
}
}
if op.Source != nil && !shared.IsValidAccountID(*op.Source) {
return helpers.NewInvalidParameterError("source", "Source must be a public key (starting with `G`).")
}
return nil
}