-
Notifications
You must be signed in to change notification settings - Fork 352
/
query.go
185 lines (163 loc) · 4.96 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
package cli
import (
"context"
"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/keeper"
"github.com/kava-labs/kava/x/incentive/types"
)
const (
flagOwner = "owner"
flagType = "type"
flagUnsynced = "unsynced"
flagDenom = "denom"
)
var rewardTypes = []string{
keeper.RewardTypeHard,
keeper.RewardTypeUSDXMinting,
keeper.RewardTypeDelegator,
keeper.RewardTypeSwap,
keeper.RewardTypeSavings,
keeper.RewardTypeEarn,
}
// 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(),
queryApyCmd(),
}
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:
$ %[1]s query %[2]s rewards
$ %[1]s query %[2]s rewards --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
$ %[1]s query %[2]s rewards --type hard
$ %[1]s query %[2]s rewards --type usdx-minting
$ %[1]s query %[2]s rewards --type delegator
$ %[1]s query %[2]s rewards --type swap
$ %[1]s query %[2]s rewards --type savings
$ %[1]s query %[2]s rewards --type earn
$ %[1]s query %[2]s rewards --type hard --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
$ %[1]s query %[2]s rewards --type hard --unsynced
`,
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
}
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
}
}
rewardType := strings.ToLower(strType)
queryClient := types.NewQueryClient(cliCtx)
request := types.QueryRewardsRequest{
RewardType: rewardType,
Owner: owner.String(),
Unsynchronized: boolUnsynced,
}
rewards, err := queryClient.Rewards(context.Background(), &request)
if err != nil {
return err
}
return cliCtx.PrintProto(rewards)
},
}
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 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
}
queryClient := types.NewQueryClient(cliCtx)
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}
return cliCtx.PrintProto(res)
},
}
}
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
}
queryClient := types.NewQueryClient(cliCtx)
res, err := queryClient.RewardFactors(context.Background(), &types.QueryRewardFactorsRequest{})
if err != nil {
return err
}
return cliCtx.PrintProto(res)
},
}
cmd.Flags().String(flagDenom, "", "(optional) filter reward factors by denom")
return cmd
}
func queryApyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "apy",
Short: "queries incentive reward apy for a reward",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(cliCtx)
res, err := queryClient.Apy(context.Background(), &types.QueryApyRequest{})
if err != nil {
return err
}
return cliCtx.PrintProto(res)
},
}
return cmd
}