-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_quote_options.go
134 lines (111 loc) · 3.98 KB
/
router_quote_options.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
package sqsclient
import (
"fmt"
"net/url"
"strconv"
)
// RouterQuoteOptions is the options for the /router/quote endpoint.
type RouterQuoteOptions struct {
// Out given in.
// TokenIn is the token in and denom to swap from.
// E.g. 10uosmo
TokenIn string
// TokenOutDenom is the denom to swap to.
TokenOutDenom string
// In given out.
// TokenOut is the token out and denom to swap to.
// E.g. 10uatom
TokenOut string
// TokenInDenom is the denom to swap from.
TokenInDenom string
// HumanDenoms is whether the input tokens are human readable denoms.
HumanDenoms bool
// IsSingleRoute is whether the quote is for a single route.
// If true, split routes are not returned.
// If false, split routes are attempted to be computed.
IsSingleRoute bool
// AppendBaseFee is whether the base fee is appended to the quote.
AppendBaseFee bool
}
// RouterQuoteOption is the type for the options for the /router/quote endpoint.
type RouterQuoteOption func(opts *RouterQuoteOptions)
// Validate validates the RouterQuoteOptions.
// It returns an error if the options are invalid.
func (o *RouterQuoteOptions) Validate() error {
if o.TokenIn == "" && o.TokenOut == "" {
return fmt.Errorf("token in or token out must be set")
}
if o.TokenInDenom == "" && o.TokenOutDenom == "" {
return fmt.Errorf("token in denom or token out denom must be set")
}
if o.TokenIn != "" && o.TokenOut != "" {
return fmt.Errorf("token in and token out cannot be set at the same time")
}
if o.TokenInDenom != "" && o.TokenOutDenom != "" {
return fmt.Errorf("token in denom and token out denom cannot be set at the same time")
}
return nil
}
// IsOutGivenIn returns true if the quote is for an out given in swap.
func (o *RouterQuoteOptions) IsOutGivenIn() bool {
return o.TokenIn != "" && o.TokenOutDenom != ""
}
// CreateQueryParams creates the query parameters for the /router/quote endpoint.
func (o *RouterQuoteOptions) CreateQueryParams() url.Values {
queryParams := url.Values{}
queryParams.Add("humanDenoms", strconv.FormatBool(o.HumanDenoms))
queryParams.Add("singleRoute", strconv.FormatBool(o.IsSingleRoute))
if o.IsOutGivenIn() {
queryParams.Add("tokenIn", o.TokenIn)
queryParams.Add("tokenOutDenom", o.TokenOutDenom)
} else {
queryParams.Add("tokenInDenom", o.TokenInDenom)
queryParams.Add("tokenOut", o.TokenOut)
}
if o.HumanDenoms {
queryParams.Add("humanDenoms", "true")
}
if o.IsSingleRoute {
queryParams.Add("singleRoute", "true")
}
if o.AppendBaseFee {
queryParams.Add("appendBaseFee", "true")
}
return queryParams
}
// WithOutGivenIn sets the options for an out given in swap for the /router/quote endpoint.
func WithOutGivenIn[T any](inAmount T, tokenInDenom string, tokenOutDenom string) RouterQuoteOption {
return func(opts *RouterQuoteOptions) {
opts.TokenIn = fmt.Sprintf("%v%s", inAmount, tokenInDenom)
opts.TokenOutDenom = tokenOutDenom
}
}
// WithInGivenOut sets the options for an in given out swap for the /router/quote endpoint.
func WithInGivenOut[T any](outAmount T, tokenOutDenom string, tokenInDenom string) RouterQuoteOption {
return func(opts *RouterQuoteOptions) {
opts.TokenInDenom = tokenInDenom
opts.TokenOut = fmt.Sprintf("%v%s", outAmount, tokenOutDenom)
}
}
// WithHumanDenomsQuote is an option to set the human denoms for the /router/quote endpoint.
func WithHumanDenoms() RouterQuoteOption {
return func(opts *RouterQuoteOptions) {
opts.HumanDenoms = true
}
}
// WithAppendBaseFee sets the options to append the base fee to the quote.
func WithAppendBaseFee() RouterQuoteOption {
return func(opts *RouterQuoteOptions) {
opts.AppendBaseFee = true
}
}
// WithIsSingleRoute sets the options for a single route for the /router/quote endpoint.
// If true, split routes are not returned.
// If false, split routes are attempted to be computed.
func WithIsSingleRoute() RouterQuoteOption {
return func(opts *RouterQuoteOptions) {
opts.IsSingleRoute = true
}
}
// WithIsSingleRoute sets the options for a single route.
var _ Options = &RouterQuoteOptions{}