-
Notifications
You must be signed in to change notification settings - Fork 591
/
query.go
362 lines (318 loc) · 10.4 KB
/
query.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package cli
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"gopkg.in/yaml.v2"
"github.com/osmosis-labs/osmosis/osmoutils/osmocli"
"github.com/osmosis-labs/osmosis/v25/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v25/x/gamm/types"
)
// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd() *cobra.Command {
cmd := osmocli.QueryIndexCmd(types.ModuleName)
osmocli.AddQueryCmd(cmd, types.NewQueryClient, GetCmdSpotPrice)
osmocli.AddQueryCmd(cmd, types.NewQueryClient, GetCmdPool)
osmocli.AddQueryCmd(cmd, types.NewQueryClient, GetCmdPools)
osmocli.AddQueryCmd(cmd, types.NewQueryClient, GetCmdEstimateSwapExactAmountIn)
osmocli.AddQueryCmd(cmd, types.NewQueryClient, GetCmdEstimateSwapExactAmountOut)
osmocli.AddQueryCmd(cmd, types.NewQueryClient, GetConcentratedPoolIdLinkFromCFMMRequest)
osmocli.AddQueryCmd(cmd, types.NewQueryClient, GetCFMMConcentratedPoolLinksRequest)
cmd.AddCommand(
GetCmdNumPools(),
GetCmdPoolParams(),
GetCmdTotalShares(),
GetCmdQueryTotalLiquidity(),
GetCmdTotalPoolLiquidity(),
GetCmdQueryPoolsWithFilter(),
GetCmdPoolType(),
)
return cmd
}
var customRouterFlagOverride = map[string]string{
"router": FlagSwapRouteDenoms,
}
// Deprecated: use x/poolmanager's Pool query.
// nolint: staticcheck
func GetCmdPool() (*osmocli.QueryDescriptor, *types.QueryPoolRequest) {
return &osmocli.QueryDescriptor{
Use: "pool",
Short: "Query pool",
// Deprecated: use x/poolmanager's Pool query.
// nolint: staticcheck
Long: `{{.Short}}{{.ExampleHeader}}
{{.CommandPrefix}} pool 1`,
}, &types.QueryPoolRequest{}
}
// TODO: Push this to the SDK.
func writeOutputBoilerplate(ctx client.Context, out []byte) error {
writer := ctx.Output
if writer == nil {
writer = os.Stdout
}
_, err := writer.Write(out)
if err != nil {
return err
}
if ctx.OutputFormat != "text" {
// append new-line for formats besides YAML
_, err = writer.Write([]byte("\n"))
if err != nil {
return err
}
}
return nil
}
func GetCmdPools() (*osmocli.QueryDescriptor, *types.QueryPoolsRequest) {
return &osmocli.QueryDescriptor{
Use: "pools",
Short: "Query pools",
Long: `{{.Short}}{{.ExampleHeader}}
{{.CommandPrefix}} pools`,
}, &types.QueryPoolsRequest{}
}
// nolint: staticcheck
func GetCmdNumPools() *cobra.Command {
return osmocli.SimpleQueryCmd[*types.QueryNumPoolsRequest](
"num-pools",
"Query number of pools",
"{{.Short}}",
types.ModuleName, types.NewQueryClient,
)
}
// GetCmdPoolParams return pool params.
func GetCmdPoolParams() *cobra.Command {
cmd := &cobra.Command{
Use: "pool-params <poolID>",
Short: "Query pool-params",
Long: strings.TrimSpace(
fmt.Sprintf(`Query pool-params.
Example:
$ %s query gamm pool-params 1
`,
version.AppName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
poolID, err := strconv.Atoi(args[0])
if err != nil {
return err
}
res, err := queryClient.PoolParams(cmd.Context(), &types.QueryPoolParamsRequest{
PoolId: uint64(poolID),
})
if err != nil {
return err
}
if clientCtx.OutputFormat == "text" {
poolParams := &balancer.PoolParams{}
if err := poolParams.Unmarshal(res.GetParams().Value); err != nil {
return err
}
out, err := yaml.Marshal(poolParams)
if err != nil {
return err
}
return writeOutputBoilerplate(clientCtx, out)
} else {
out, err := clientCtx.Codec.MarshalJSON(res)
if err != nil {
return err
}
return writeOutputBoilerplate(clientCtx, out)
}
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func GetCmdTotalShares() *cobra.Command {
return osmocli.SimpleQueryCmd[*types.QueryTotalSharesRequest](
"total-share",
"Query total-share",
`Query total-share.
Example:
{{.CommandPrefix}} total-share 1
`,
types.ModuleName, types.NewQueryClient,
)
}
func GetCmdQueryTotalLiquidity() *cobra.Command {
return osmocli.SimpleQueryCmd[*types.QueryTotalLiquidityRequest](
"total-liquidity",
"Query total-liquidity",
`Query total-liquidity.
Example:
{{.CommandPrefix}} total-liquidity
`,
types.ModuleName, types.NewQueryClient,
)
}
// Deprecated: use alternate in x/poolmanager.
func GetCmdSpotPrice() (*osmocli.QueryDescriptor, *types.QuerySpotPriceRequest) {
return &osmocli.QueryDescriptor{
Use: "spot-price",
Short: "Query spot-price (LEGACY, arguments are reversed!!)",
Long: `Query spot price (Legacy).{{.ExampleHeader}}
{{.CommandPrefix}} spot-price 1 uosmo ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2
`,
}, &types.QuerySpotPriceRequest{}
}
// Deprecated: use alternate in x/poolmanager.
func GetCmdEstimateSwapExactAmountIn() (*osmocli.QueryDescriptor, *types.QuerySwapExactAmountInRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-swap-exact-amount-in",
Short: "Query estimate-swap-exact-amount-in",
Long: `Query estimate-swap-exact-amount-in.{{.ExampleHeader}}
{{.CommandPrefix}} estimate-swap-exact-amount-in 1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 1000stake --swap-route-pool-ids=2 --swap-route-pool-ids=3`,
ParseQuery: EstimateSwapExactAmountInParseArgs,
Flags: osmocli.FlagDesc{RequiredFlags: []*flag.FlagSet{FlagSetMultihopSwapRoutes()}},
QueryFnName: "EstimateSwapExactAmountIn",
CustomFlagOverrides: customRouterFlagOverride,
}, &types.QuerySwapExactAmountInRequest{}
}
// Deprecated: use alternate in x/poolmanager.
func GetCmdEstimateSwapExactAmountOut() (*osmocli.QueryDescriptor, *types.QuerySwapExactAmountOutRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-swap-exact-amount-out",
Short: "Query estimate-swap-exact-amount-out",
Long: `Query estimate-swap-exact-amount-out.{{.ExampleHeader}}
{{.CommandPrefix}} estimate-swap-exact-amount-out 1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 1000stake --swap-route-pool-ids=2 --swap-route-pool-ids=3`,
ParseQuery: EstimateSwapExactAmountOutParseArgs,
Flags: osmocli.FlagDesc{RequiredFlags: []*flag.FlagSet{FlagSetMultihopSwapRoutes()}},
QueryFnName: "EstimateSwapExactAmountOut",
CustomFlagOverrides: customRouterFlagOverride,
}, &types.QuerySwapExactAmountOutRequest{}
}
// nolint: staticcheck
func EstimateSwapExactAmountInParseArgs(args []string, fs *flag.FlagSet) (proto.Message, error) {
poolID, err := strconv.Atoi(args[0])
if err != nil {
return nil, err
}
routes, err := swapAmountInRoutes(fs)
if err != nil {
return nil, err
}
return &types.QuerySwapExactAmountInRequest{
Sender: args[1], // TODO: where sender is used?
PoolId: uint64(poolID), // TODO: is this poolId used?
TokenIn: args[2],
Routes: routes,
}, nil
}
// nolint: staticcheck
func EstimateSwapExactAmountOutParseArgs(args []string, fs *flag.FlagSet) (proto.Message, error) {
poolID, err := strconv.Atoi(args[0])
if err != nil {
return nil, err
}
routes, err := swapAmountOutRoutes(fs)
if err != nil {
return nil, err
}
return &types.QuerySwapExactAmountOutRequest{
Sender: args[1], // TODO: where sender is used?
PoolId: uint64(poolID), // TODO: is this poolId used?
Routes: routes,
TokenOut: args[2],
}, nil
}
// GetCmdQueryPoolsWithFilter returns pool with filter
func GetCmdQueryPoolsWithFilter() *cobra.Command {
cmd := &cobra.Command{
Use: "pools-with-filter <min_liquidity> <pool_type>",
Short: "Query pools with filter",
Long: strings.TrimSpace(
fmt.Sprintf(`Query pools with filter. The possible filter options are:
1. By Pool type: Either "Balancer" or "Stableswap"
2. By min pool liquidity by providing min coins
Note that if both filters are to be applied, "min_liquidity" always needs to be provided as the first argument.
Example:
$ %s query gamm pools-with-filter <min_liquidity> <pool_type>
`,
version.AppName,
),
),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
var pool_type string
min_liquidity := args[0]
if len(args) > 1 {
pool_type = args[1]
}
res, err := queryClient.PoolsWithFilter(cmd.Context(), &types.QueryPoolsWithFilterRequest{
MinLiquidity: min_liquidity,
PoolType: pool_type,
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdPoolType returns pool type given pool id.
func GetCmdPoolType() *cobra.Command {
return osmocli.SimpleQueryCmd[*types.QueryPoolTypeRequest](
"pool-type",
"Query pool type",
`Query pool type
Example:
{{.CommandPrefix}} pool-type <pool_id>
`,
types.ModuleName, types.NewQueryClient,
)
}
// GetConcentratedPoolIdLinkFromCFMMRequest returns concentrated pool id that is linked to the given cfmm pool id.
func GetConcentratedPoolIdLinkFromCFMMRequest() (*osmocli.QueryDescriptor, *types.QueryConcentratedPoolIdLinkFromCFMMRequest) {
return &osmocli.QueryDescriptor{
Use: "cl-pool-link-from-cfmm",
Short: "Query concentrated pool id link from cfmm pool id",
Long: `{{.Short}}{{.ExampleHeader}}
{{.CommandPrefix}} cl-pool-link-from-cfmm 1`,
}, &types.QueryConcentratedPoolIdLinkFromCFMMRequest{}
}
// GetCmdTotalPoolLiquidity returns total liquidity in pool.
// Deprecated: please use the alternative in x/poolmanager
// nolint: staticcheck
func GetCmdTotalPoolLiquidity() *cobra.Command {
return osmocli.SimpleQueryCmd[*types.QueryTotalPoolLiquidityRequest](
"total-pool-liquidity",
"Query total-pool-liquidity",
`Query total-pool-liquidity.
Example:
{{.CommandPrefix}} total-pool-liquidity 1
`,
types.ModuleName, types.NewQueryClient,
)
}
// GetConcentratedPoolIdLinkFromCFMMRequest returns all concentrated pool id to cfmm pool id links.
func GetCFMMConcentratedPoolLinksRequest() (*osmocli.QueryDescriptor, *types.QueryCFMMConcentratedPoolLinksRequest) {
return &osmocli.QueryDescriptor{
Use: "cfmm-cl-pool-links",
Short: "Query all concentrated pool and cfmm pool id links",
Long: `{{.Short}}{{.ExampleHeader}}
{{.CommandPrefix}} cfmm-cl-pool-links`,
}, &types.QueryCFMMConcentratedPoolLinksRequest{}
}