-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment.go
53 lines (46 loc) · 1.39 KB
/
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
package txnbuild
import (
"github.com/stellar/go/amount"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// Payment represents the Stellar payment operation. See
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html
type Payment struct {
Destination string
Amount string
Asset Asset
SourceAccount Account
}
// BuildXDR for Payment returns a fully configured XDR Operation.
func (p *Payment) BuildXDR() (xdr.Operation, error) {
var destAccountID xdr.AccountId
err := destAccountID.SetAddress(p.Destination)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set destination address")
}
xdrAmount, err := amount.Parse(p.Amount)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse amount")
}
if p.Asset == nil {
return xdr.Operation{}, errors.New("you must specify an asset for payment")
}
xdrAsset, err := p.Asset.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set asset type")
}
opType := xdr.OperationTypePayment
xdrOp := xdr.PaymentOp{
Destination: destAccountID,
Amount: xdrAmount,
Asset: xdrAsset,
}
body, err := xdr.NewOperationBody(opType, xdrOp)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to build XDR Operation")
}
op := xdr.Operation{Body: body}
SetOpSourceAccount(&op, p.SourceAccount)
return op, nil
}