-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_payment.go
87 lines (78 loc) · 2.45 KB
/
path_payment.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
package txnbuild
import (
"github.com/stellar/go/amount"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// PathPayment represents the Stellar path payment operation. See
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html
type PathPayment struct {
SendAsset Asset
SendMax string
Destination string
DestAsset Asset
DestAmount string
Path []Asset
SourceAccount Account
}
// BuildXDR for Payment returns a fully configured XDR Operation.
func (pp *PathPayment) BuildXDR() (xdr.Operation, error) {
// Set XDR send asset
if pp.SendAsset == nil {
return xdr.Operation{}, errors.New("you must specify an asset to send for payment")
}
xdrSendAsset, err := pp.SendAsset.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set asset type")
}
// Set XDR send max
xdrSendMax, err := amount.Parse(pp.SendMax)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse maximum amount to send")
}
// Set XDR destination
var xdrDestination xdr.AccountId
err = xdrDestination.SetAddress(pp.Destination)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set destination address")
}
// Set XDR destination asset
if pp.DestAsset == nil {
return xdr.Operation{}, errors.New("you must specify an asset for destination account to receive")
}
xdrDestAsset, err := pp.DestAsset.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set asset type")
}
// Set XDR destination amount
xdrDestAmount, err := amount.Parse(pp.DestAmount)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse amount of asset destination account receives")
}
// Set XDR path
var xdrPath []xdr.Asset
var xdrPathAsset xdr.Asset
for _, asset := range pp.Path {
xdrPathAsset, err = asset.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set asset type")
}
xdrPath = append(xdrPath, xdrPathAsset)
}
opType := xdr.OperationTypePathPayment
xdrOp := xdr.PathPaymentOp{
SendAsset: xdrSendAsset,
SendMax: xdrSendMax,
Destination: xdrDestination,
DestAsset: xdrDestAsset,
DestAmount: xdrDestAmount,
Path: xdrPath,
}
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, pp.SourceAccount)
return op, nil
}