forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_payment.go
65 lines (54 loc) · 1.72 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
56
57
58
59
60
61
62
63
64
65
package bridge
import (
"github.com/stellar/go/amount"
b "github.com/stellar/go/build"
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"
)
// PaymentOperationBody represents payment operation
type PaymentOperationBody struct {
Source *string
Destination string
Amount string
Asset protocols.Asset
}
// ToTransactionMutator returns go-stellar-base TransactionMutator
func (op PaymentOperationBody) ToTransactionMutator() b.TransactionMutator {
mutators := []interface{}{
b.Destination{op.Destination},
}
if op.Asset.Code != "" && op.Asset.Issuer != "" {
mutators = append(
mutators,
b.CreditAmount{op.Asset.Code, op.Asset.Issuer, op.Amount},
)
} else {
mutators = append(
mutators,
b.NativeAmount{op.Amount},
)
}
if op.Source != nil {
mutators = append(mutators, b.SourceAccount{*op.Source})
}
return b.Payment(mutators...)
}
// 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
}