forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder_change_trust.go
55 lines (45 loc) · 1.43 KB
/
builder_change_trust.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
package bridge
import (
"github.com/stellar/go/amount"
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"
"github.com/stellar/go/txnbuild"
)
// ChangeTrustOperationBody represents change_trust operation
type ChangeTrustOperationBody struct {
Source *string
Asset protocols.Asset
// nil means max limit
Limit *string
}
// Build returns a txnbuild.Operation
func (op ChangeTrustOperationBody) Build() txnbuild.Operation {
txnOp := txnbuild.ChangeTrust{
Line: txnbuild.CreditAsset{Code: op.Asset.Code, Issuer: op.Asset.Issuer},
}
if op.Limit != nil {
txnOp.Limit = *op.Limit
}
if op.Source != nil {
txnOp.SourceAccount = &txnbuild.SimpleAccount{AccountID: *op.Source}
}
return &txnOp
}
// Validate validates if operation body is valid.
func (op ChangeTrustOperationBody) Validate() error {
err := op.Asset.Validate()
if err != nil {
return helpers.NewInvalidParameterError("asset", err.Error())
}
if op.Limit != nil {
_, err := amount.Parse(*op.Limit)
if err != nil {
return helpers.NewInvalidParameterError("limit", "Limit is not a valid amount.")
}
}
if op.Source != nil && !shared.IsValidAccountID(*op.Source) {
return helpers.NewInvalidParameterError("source", "Source must be a public key (starting with `G`).")
}
return nil
}