forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder_payment.go
55 lines (45 loc) · 1.55 KB
/
builder_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
package bridge
import (
"github.com/stellar/go/amount"
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"
"github.com/stellar/go/txnbuild"
)
// PaymentOperationBody represents payment operation
type PaymentOperationBody struct {
Source *string
Destination string
Amount string
Asset protocols.Asset
}
// Build returns a txnbuild.Operation
func (op PaymentOperationBody) Build() txnbuild.Operation {
txnOp := txnbuild.Payment{
Destination: op.Destination,
Amount: op.Amount,
Asset: txnbuild.CreditAsset{Code: op.Asset.Code, Issuer: op.Asset.Issuer},
}
if op.Source != nil {
txnOp.SourceAccount = &txnbuild.SimpleAccount{AccountID: *op.Source}
}
return &txnOp
}
// Validate validates if operation body is valid.
func (op PaymentOperationBody) Validate() error {
if !shared.IsValidAccountID(op.Destination) {
return helpers.NewInvalidParameterError("destination", "Destination must be a public key (starting with `G`).")
}
_, err := amount.Parse(op.Amount)
if err != nil {
return helpers.NewInvalidParameterError("amount", "Invalid amount.")
}
err = op.Asset.Validate()
if err != nil {
return helpers.NewInvalidParameterError("asset", "Invalid asset.")
}
if op.Source != nil && !shared.IsValidAccountID(*op.Source) {
return helpers.NewInvalidParameterError("source", "Source must be a public key (starting with `G`).")
}
return nil
}