forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
change_trust.go
63 lines (54 loc) · 1.74 KB
/
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
59
60
61
62
63
package txnbuild
import (
"math"
"github.com/stellar/go/amount"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// ChangeTrust represents the Stellar change trust operation. See
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html.
// If Limit is omitted, it defaults to txnbuild.MaxTrustlineLimit.
type ChangeTrust struct {
Line Asset
Limit string
SourceAccount Account
}
// MaxTrustlineLimit represents the maximum value that can be set as a trustline limit.
var MaxTrustlineLimit = amount.StringFromInt64(math.MaxInt64)
// RemoveTrustlineOp returns a ChangeTrust operation to remove the trustline of the described asset,
// by setting the limit to "0".
func RemoveTrustlineOp(issuedAsset Asset) ChangeTrust {
return ChangeTrust{
Line: issuedAsset,
Limit: "0",
}
}
// BuildXDR for ChangeTrust returns a fully configured XDR Operation.
func (ct *ChangeTrust) BuildXDR() (xdr.Operation, error) {
if ct.Line.IsNative() {
return xdr.Operation{}, errors.New("trustline cannot be extended to a native (XLM) asset")
}
xdrLine, err := ct.Line.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "can't convert trustline asset to XDR")
}
if ct.Limit == "" {
ct.Limit = MaxTrustlineLimit
}
xdrLimit, err := amount.Parse(ct.Limit)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse limit amount")
}
opType := xdr.OperationTypeChangeTrust
xdrOp := xdr.ChangeTrustOp{
Line: xdrLine,
Limit: xdrLimit,
}
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, ct.SourceAccount)
return op, nil
}