forked from stellar/go
/
builder_change_trust.go
58 lines (48 loc) · 1.52 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
56
57
58
package bridge
import (
"github.com/stellar/go/amount"
b "github.com/stellar/go/build"
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"
)
// ChangeTrustOperationBody represents change_trust operation
type ChangeTrustOperationBody struct {
Source *string
Asset protocols.Asset
// nil means max limit
Limit *string
}
// ToTransactionMutator returns go-stellar-base TransactionMutator
func (op ChangeTrustOperationBody) ToTransactionMutator() b.TransactionMutator {
mutators := []interface{}{
op.Asset.ToBaseAsset(),
}
if op.Limit == nil {
// Set MaxLimit
mutators = append(mutators, b.MaxLimit)
} else {
mutators = append(mutators, b.Limit(*op.Limit))
}
if op.Source != nil {
mutators = append(mutators, b.SourceAccount{*op.Source})
}
return b.ChangeTrust(mutators...)
}
// 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
}