-
Notifications
You must be signed in to change notification settings - Fork 368
/
query.go
323 lines (279 loc) · 9.54 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
package cli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/kava-labs/kava/x/incentive/types"
)
const (
flagOwner = "owner"
flagType = "type"
flagUnsynced = "unsynced"
flagDenom = "denom"
typeDelegator = "delegator"
typeHard = "hard"
typeUSDXMinting = "usdx-minting"
typeSwap = "swap"
)
var rewardTypes = []string{typeDelegator, typeHard, typeUSDXMinting, typeSwap}
// GetQueryCmd returns the cli query commands for the incentive module
func GetQueryCmd() *cobra.Command {
incentiveQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the incentive module",
}
cmds := []*cobra.Command{
queryParamsCmd(),
queryRewardsCmd(),
queryRewardFactorsCmd(),
}
for _, cmd := range cmds {
flags.AddQueryFlagsToCmd(cmd)
}
incentiveQueryCmd.AddCommand(cmds...)
return incentiveQueryCmd
}
func queryRewardsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "rewards",
Short: "query claimable rewards",
Long: strings.TrimSpace(
fmt.Sprintf(`Query rewards with optional flags for owner and type
Example:
$ %s query %s rewards
$ %s query %s rewards --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
$ %s query %s rewards --type hard
$ %s query %s rewards --type usdx-minting
$ %s query %s rewards --type delegator
$ %s query %s rewards --type swap
$ %s query %s rewards --type hard --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
$ %s query %s rewards --type hard --unsynced
`,
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
version.AppName, types.ModuleName, version.AppName, types.ModuleName)),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)
strOwner, _ := cmd.Flags().GetString(flagOwner)
strType, _ := cmd.Flags().GetString(flagType)
boolUnsynced, _ := cmd.Flags().GetBool(flagUnsynced)
// Prepare params for querier
var owner sdk.AccAddress
if strOwner != "" {
if owner, err = sdk.AccAddressFromBech32(strOwner); err != nil {
return err
}
}
switch strings.ToLower(strType) {
case typeHard:
params := types.NewQueryRewardsParams(page, limit, owner, boolUnsynced)
claims, err := executeHardRewardsQuery(cliCtx, params)
if err != nil {
return err
}
return cliCtx.PrintObjectLegacy(claims)
case typeUSDXMinting:
params := types.NewQueryRewardsParams(page, limit, owner, boolUnsynced)
claims, err := executeUSDXMintingRewardsQuery(cliCtx, params)
if err != nil {
return err
}
return cliCtx.PrintObjectLegacy(claims)
case typeDelegator:
params := types.NewQueryRewardsParams(page, limit, owner, boolUnsynced)
claims, err := executeDelegatorRewardsQuery(cliCtx, params)
if err != nil {
return err
}
return cliCtx.PrintObjectLegacy(claims)
case typeSwap:
params := types.NewQueryRewardsParams(page, limit, owner, boolUnsynced)
claims, err := executeSwapRewardsQuery(cliCtx, params)
if err != nil {
return err
}
return cliCtx.PrintObjectLegacy(claims)
default:
params := types.NewQueryRewardsParams(page, limit, owner, boolUnsynced)
hardClaims, err := executeHardRewardsQuery(cliCtx, params)
if err != nil {
return err
}
usdxMintingClaims, err := executeUSDXMintingRewardsQuery(cliCtx, params)
if err != nil {
return err
}
delegatorClaims, err := executeDelegatorRewardsQuery(cliCtx, params)
if err != nil {
return err
}
swapClaims, err := executeSwapRewardsQuery(cliCtx, params)
if err != nil {
return err
}
if len(hardClaims) > 0 {
if err := cliCtx.PrintObjectLegacy(hardClaims); err != nil {
return err
}
}
if len(usdxMintingClaims) > 0 {
if err := cliCtx.PrintObjectLegacy(usdxMintingClaims); err != nil {
return err
}
}
if len(delegatorClaims) > 0 {
if err := cliCtx.PrintObjectLegacy(delegatorClaims); err != nil {
return err
}
}
if len(swapClaims) > 0 {
if err := cliCtx.PrintObjectLegacy(swapClaims); err != nil {
return err
}
}
}
return nil
},
}
cmd.Flags().String(flagOwner, "", "(optional) filter by owner address")
cmd.Flags().String(flagType, "", fmt.Sprintf("(optional) filter by a reward type: %s", strings.Join(rewardTypes, "|")))
cmd.Flags().Bool(flagUnsynced, false, "(optional) get unsynced claims")
cmd.Flags().Int(flags.FlagPage, 1, "pagination page rewards of to to query for")
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of rewards to query for")
return cmd
}
func queryParamsCmd() *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "get the incentive module parameters",
Long: "Get the current global incentive module parameters.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
// Query
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetParams)
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
cliCtx = cliCtx.WithHeight(height)
// Decode and print results
var params types.Params
if err := cliCtx.LegacyAmino.UnmarshalJSON(res, ¶ms); err != nil {
return fmt.Errorf("failed to unmarshal params: %w", err)
}
return cliCtx.PrintObjectLegacy(params)
},
}
}
func queryRewardFactorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "reward-factors",
Short: "get current global reward factors",
Long: `Get current global reward factors for all reward types.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
// Execute query
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetRewardFactors)
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
cliCtx = cliCtx.WithHeight(height)
// Decode and print results
var response types.QueryGetRewardFactorsResponse
if err := cliCtx.LegacyAmino.UnmarshalJSON(res, &response); err != nil {
return fmt.Errorf("failed to unmarshal reward factors: %w", err)
}
return cliCtx.PrintObjectLegacy(response)
},
}
cmd.Flags().String(flagDenom, "", "(optional) filter reward factors by denom")
return cmd
}
func executeHardRewardsQuery(cliCtx client.Context, params types.QueryRewardsParams) (types.HardLiquidityProviderClaims, error) {
bz, err := cliCtx.LegacyAmino.MarshalJSON(params)
if err != nil {
return types.HardLiquidityProviderClaims{}, err
}
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetHardRewards)
res, height, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return types.HardLiquidityProviderClaims{}, err
}
cliCtx = cliCtx.WithHeight(height)
var claims types.HardLiquidityProviderClaims
if err := cliCtx.LegacyAmino.UnmarshalJSON(res, &claims); err != nil {
return types.HardLiquidityProviderClaims{}, fmt.Errorf("failed to unmarshal claims: %w", err)
}
return claims, nil
}
func executeUSDXMintingRewardsQuery(cliCtx client.Context, params types.QueryRewardsParams) (types.USDXMintingClaims, error) {
bz, err := cliCtx.LegacyAmino.MarshalJSON(params)
if err != nil {
return types.USDXMintingClaims{}, err
}
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetUSDXMintingRewards)
res, height, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return types.USDXMintingClaims{}, err
}
cliCtx = cliCtx.WithHeight(height)
var claims types.USDXMintingClaims
if err := cliCtx.LegacyAmino.UnmarshalJSON(res, &claims); err != nil {
return types.USDXMintingClaims{}, fmt.Errorf("failed to unmarshal claims: %w", err)
}
return claims, nil
}
func executeDelegatorRewardsQuery(cliCtx client.Context, params types.QueryRewardsParams) (types.DelegatorClaims, error) {
bz, err := cliCtx.LegacyAmino.MarshalJSON(params)
if err != nil {
return types.DelegatorClaims{}, err
}
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetDelegatorRewards)
res, height, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return types.DelegatorClaims{}, err
}
cliCtx = cliCtx.WithHeight(height)
var claims types.DelegatorClaims
if err := cliCtx.LegacyAmino.UnmarshalJSON(res, &claims); err != nil {
return types.DelegatorClaims{}, fmt.Errorf("failed to unmarshal claims: %w", err)
}
return claims, nil
}
func executeSwapRewardsQuery(cliCtx client.Context, params types.QueryRewardsParams) (types.SwapClaims, error) {
bz, err := cliCtx.LegacyAmino.MarshalJSON(params)
if err != nil {
return types.SwapClaims{}, err
}
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetSwapRewards)
res, height, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return types.SwapClaims{}, err
}
cliCtx = cliCtx.WithHeight(height)
var claims types.SwapClaims
if err := cliCtx.LegacyAmino.UnmarshalJSON(res, &claims); err != nil {
return types.SwapClaims{}, fmt.Errorf("failed to unmarshal claims: %w", err)
}
return claims, nil
}