/
query.go
361 lines (316 loc) · 9.76 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
package cli
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
"strings"
"github.com/cosmos/cosmos-sdk/client/context"
client "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/fastock/fastock-chain/x/farm/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// Group farm queries under a subcommand
farmQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
}
farmQueryCmd.AddCommand(
client.GetCommands(
GetCmdQueryPool(queryRoute, cdc),
GetCmdQueryPools(queryRoute, cdc),
GetCmdQueryPoolNum(queryRoute, cdc),
GetCmdQueryLockInfo(queryRoute, cdc),
GetCmdQueryEarnings(queryRoute, cdc),
GetCmdQueryAccount(queryRoute, cdc),
GetCmdQueryAccountsLockedTo(queryRoute, cdc),
GetCmdQueryWhitelist(queryRoute, cdc),
GetCmdQueryParams(queryRoute, cdc),
)...,
)
return farmQueryCmd
}
// GetCmdQueryPool gets the pool query command.
func GetCmdQueryPool(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "pool [pool-name]",
Short: "query a pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details about the kind of coins as reward, the balance and the amount to farm in one block.
Example:
$ %s query farm pool pool-eth-xxb
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bytes, err := cdc.MarshalJSON(types.NewQueryPoolParams(args[0]))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryPool)
resp, _, err := cliCtx.QueryWithData(route, bytes)
if err != nil {
return err
}
var pool types.FarmPool
cdc.MustUnmarshalJSON(resp, &pool)
return cliCtx.PrintOutput(pool)
},
}
}
// GetCmdQueryPools gets the pools query command.
func GetCmdQueryPools(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "pools",
Short: "query for all pools",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details about all pools.
Example:
$ %s query farm pools
`,
version.ClientName,
),
),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
// fixed to all pools query
jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolsParams(1, 0))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryPools)
bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
if err != nil {
return err
}
var pools types.FarmPools
cdc.MustUnmarshalJSON(bz, &pools)
return cliCtx.PrintOutput(pools)
},
}
}
// GetCmdQueryEarnings gets the earnings query command.
func GetCmdQueryEarnings(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "rewards [pool-name] [address]",
Short: "query the current rewards of an account",
Long: strings.TrimSpace(
fmt.Sprintf(`Query available rewards for an address.
Example:
$ %s query farm rewards pool-eth-xxb blockchain1hw4r48aww06ldrfeuq2v438ujnl6alsz0685a0
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
accAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}
jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolAccountParams(args[0], accAddr))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryEarnings)
bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
if err != nil {
return err
}
var earnings types.Earnings
cdc.MustUnmarshalJSON(bz, &earnings)
return cliCtx.PrintOutput(earnings)
},
}
}
// GetCmdQueryParams gets the pools query command.
func GetCmdQueryParams(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "query the current farm parameters information",
Long: strings.TrimSpace(
fmt.Sprintf(`Query values set as farm parameters.
Example:
$ %s query farm params
`,
version.ClientName,
),
),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryParameters)
bz, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var params types.Params
cdc.MustUnmarshalJSON(bz, ¶ms)
return cliCtx.PrintOutput(params)
},
}
}
// GetCmdQueryWhitelist gets the whitelist query command.
func GetCmdQueryWhitelist(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "whitelist",
Short: "query the whitelist of pools to farm okt",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the current whitelist of pools which are approved to farm okt.
Example:
$ %s query farm whitelist
`,
version.ClientName,
),
),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryWhitelist)
bz, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var whitelist types.PoolNameList
cdc.MustUnmarshalJSON(bz, &whitelist)
return cliCtx.PrintOutput(whitelist)
},
}
}
// GetCmdQueryAccount gets the account query command.
func GetCmdQueryAccount(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "account [address]",
Short: "query the name of pools that an account has locked coins in",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the names of all pools that an account has locked coins in.
Example:
$ %s query farm account blockchain1hw4r48aww06ldrfeuq2v438ujnl6alsz0685a0
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
accAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
jsonBytes, err := cdc.MarshalJSON(types.NewQueryAccountParams(accAddr))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryAccount)
bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
if err != nil {
return err
}
var poolNameList types.PoolNameList
cdc.MustUnmarshalJSON(bz, &poolNameList)
return cliCtx.PrintOutput(poolNameList)
},
}
}
// GetCmdQueryPoolNum gets the pool number query command.
func GetCmdQueryPoolNum(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "pool-num",
Short: "query the number of pools",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the number of pools that already exist.
Example:
$ %s query farm pool-num
`,
version.ClientName,
),
),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryPoolNum)
bz, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var poolNum types.PoolNum
cdc.MustUnmarshalJSON(bz, &poolNum)
return cliCtx.PrintOutput(poolNum)
},
}
}
// GetCmdQueryAccountsLockedTo gets all addresses of accounts that locked coins in a specific pool
func GetCmdQueryAccountsLockedTo(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "accounts-locked-to [pool-name]",
Short: "query the addresses of accounts locked in a pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all the addresses of accounts that have locked coins in a specific pool.
Example:
$ %s query farm accounts-locked-to pool-eth-xxb
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolParams(args[0]))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryAccountsLockedTo)
bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
if err != nil {
return err
}
var accAddrList types.AccAddrList
cdc.MustUnmarshalJSON(bz, &accAddrList)
return cliCtx.PrintOutput(accAddrList)
},
}
}
// GetCmdQueryLockInfo gets the lock info of an account's token locking on a specific pool
func GetCmdQueryLockInfo(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "lock-info [pool-name] [address]",
Short: "query the lock info of an account on a pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the lock info of an account's token locking on a specific pool.
Example:
$ %s query farm lock-info pool-eth-xxb blockchain1hw4r48aww06ldrfeuq2v438ujnl6alsz0685a0
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
accAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}
jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolAccountParams(args[0], accAddr))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryLockInfo)
bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
if err != nil {
return err
}
var lockInfo types.LockInfo
cdc.MustUnmarshalJSON(bz, &lockInfo)
return cliCtx.PrintOutput(lockInfo)
},
}
}