/
query_params.go
144 lines (128 loc) · 3.91 KB
/
query_params.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package actions
import (
"fmt"
"strings"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/render/problem"
"github.com/stellar/go/xdr"
)
// SellingBuyingAssetQueryParams query struct for end-points requiring a selling
// and buying asset
type SellingBuyingAssetQueryParams struct {
SellingAssetType string `schema:"selling_asset_type" valid:"assetType,optional"`
SellingAssetIssuer string `schema:"selling_asset_issuer" valid:"accountID,optional"`
SellingAssetCode string `schema:"selling_asset_code" valid:"-"`
BuyingAssetType string `schema:"buying_asset_type" valid:"assetType,optional"`
BuyingAssetIssuer string `schema:"buying_asset_issuer" valid:"accountID,optional"`
BuyingAssetCode string `schema:"buying_asset_code" valid:"-"`
// allow selling and buying using an asset's canonical representation. We
// are keeping the former selling_* and buying_* for backwards compatibility
// but it should not be documented.
SellingAsset string `schema:"selling" valid:"asset,optional"`
BuyingAsset string `schema:"buying" valid:"asset,optional"`
}
// Validate runs custom validations buying and selling
func (q SellingBuyingAssetQueryParams) Validate() error {
ambiguousErr := "Ambiguous parameter, you can't include both `%[1]s` and `%[1]s_asset_type`. Remove all parameters of the form `%[1]s_`"
if len(q.SellingAssetType) > 0 && len(q.SellingAsset) > 0 {
return problem.MakeInvalidFieldProblem(
"selling_asset_type",
errors.New(fmt.Sprintf(ambiguousErr, "selling")),
)
}
err := validateAssetParams(q.SellingAssetType, q.SellingAssetCode, q.SellingAssetIssuer, "selling_")
if err != nil {
return err
}
if len(q.BuyingAssetType) > 0 && len(q.BuyingAsset) > 0 {
return problem.MakeInvalidFieldProblem(
"buying_asset_type",
errors.New(fmt.Sprintf(ambiguousErr, "buying")),
)
}
err = validateAssetParams(q.BuyingAssetType, q.BuyingAssetCode, q.BuyingAssetIssuer, "buying_")
if err != nil {
return err
}
return nil
}
// Selling returns an xdr.Asset representing the selling side of the offer.
func (q SellingBuyingAssetQueryParams) Selling() (*xdr.Asset, error) {
if len(q.SellingAsset) > 0 {
switch q.SellingAsset {
case "native":
asset := xdr.MustNewNativeAsset()
return &asset, nil
default:
parts := strings.Split(q.SellingAsset, ":")
if len(parts) != 2 {
return nil, problem.MakeInvalidFieldProblem(
"selling",
errors.New("missing colon"),
)
}
asset, err := xdr.NewCreditAsset(parts[0], parts[1])
if err != nil {
return nil, problem.MakeInvalidFieldProblem(
"selling",
err,
)
}
return &asset, err
}
}
if len(q.SellingAssetType) == 0 {
return nil, nil
}
selling, err := xdr.BuildAsset(
q.SellingAssetType,
q.SellingAssetIssuer,
q.SellingAssetCode,
)
if err != nil {
p := problem.BadRequest
p.Extras = map[string]interface{}{"reason": fmt.Sprintf("bad selling asset: %s", err.Error())}
return nil, p
}
return &selling, nil
}
// Buying returns an *xdr.Asset representing the buying side of the offer.
func (q SellingBuyingAssetQueryParams) Buying() (*xdr.Asset, error) {
if len(q.BuyingAsset) > 0 {
switch q.BuyingAsset {
case "native":
asset := xdr.MustNewNativeAsset()
return &asset, nil
default:
parts := strings.Split(q.BuyingAsset, ":")
if len(parts) != 2 {
return nil, problem.MakeInvalidFieldProblem(
"buying",
errors.New("missing colon"),
)
}
asset, err := xdr.NewCreditAsset(parts[0], parts[1])
if err != nil {
return nil, problem.MakeInvalidFieldProblem(
"buying",
err,
)
}
return &asset, err
}
}
if len(q.BuyingAssetType) == 0 {
return nil, nil
}
buying, err := xdr.BuildAsset(
q.BuyingAssetType,
q.BuyingAssetIssuer,
q.BuyingAssetCode,
)
if err != nil {
p := problem.BadRequest
p.Extras = map[string]interface{}{"reason": fmt.Sprintf("bad buying asset: %s", err.Error())}
return nil, p
}
return &buying, nil
}