-
Notifications
You must be signed in to change notification settings - Fork 501
/
payment.go
103 lines (84 loc) · 2.65 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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://developers.stellar.org/docs/start/list-of-operations/
type Payment struct {
Destination string
Amount string
Asset Asset
SourceAccount string
}
// BuildXDR for Payment returns a fully configured XDR Operation.
func (p *Payment) BuildXDR() (xdr.Operation, error) {
var destMuxedAccount xdr.MuxedAccount
err := destMuxedAccount.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: destMuxedAccount,
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
}
// FromXDR for Payment initialises the txnbuild struct from the corresponding xdr Operation.
func (p *Payment) FromXDR(xdrOp xdr.Operation) error {
result, ok := xdrOp.Body.GetPaymentOp()
if !ok {
return errors.New("error parsing payment operation from xdr")
}
p.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
p.Destination = result.Destination.Address()
p.Amount = amount.String(result.Amount)
asset, err := assetFromXDR(result.Asset)
if err != nil {
return errors.Wrap(err, "error parsing asset in payment operation")
}
p.Asset = asset
return nil
}
// Validate for Payment validates the required struct fields. It returns an error if any
// of the fields are invalid. Otherwise, it returns nil.
func (p *Payment) Validate() error {
_, err := xdr.AddressToMuxedAccount(p.Destination)
if err != nil {
return NewValidationError("Destination", err.Error())
}
err = validateStellarAsset(p.Asset)
if err != nil {
return NewValidationError("Asset", err.Error())
}
err = validateAmount(p.Amount)
if err != nil {
return NewValidationError("Amount", err.Error())
}
return nil
}
// GetSourceAccount returns the source account of the operation, or the empty string if not
// set.
func (p *Payment) GetSourceAccount() string {
return p.SourceAccount
}