-
Notifications
You must be signed in to change notification settings - Fork 557
/
query.go
263 lines (227 loc) · 8.65 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
package cli
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/osmosis-labs/osmosis/osmoutils/osmocli"
"github.com/osmosis-labs/osmosis/v25/x/incentives/types"
lockuptypes "github.com/osmosis-labs/osmosis/v25/x/lockup/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
)
// GetQueryCmd returns the query commands for this module.
func GetQueryCmd() *cobra.Command {
// group incentives queries under a subcommand
cmd := osmocli.QueryIndexCmd(types.ModuleName)
qcGetter := types.NewQueryClient
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdGauges)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdToDistributeCoins)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdGaugeByID)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdActiveGauges)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdActiveGaugesPerDenom)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdUpcomingGauges)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdUpcomingGaugesPerDenom)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdAllGroups)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdAllGroupsGauges)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdAllGroupsWithGauge)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdGroupByGroupGaugeID)
osmocli.AddQueryCmd(cmd, qcGetter, GetCmdCurrentWeightByGroupGaugeID)
cmd.AddCommand(GetCmdRewardsEst())
return cmd
}
// GetCmdGauges returns all available gauges.
func GetCmdGauges() (*osmocli.QueryDescriptor, *types.GaugesRequest) {
return &osmocli.QueryDescriptor{
Use: "gauges",
Short: "Query all available gauges",
Long: "{{.Short}}",
}, &types.GaugesRequest{}
}
// GetCmdToDistributeCoins returns coins that are going to be distributed.
func GetCmdToDistributeCoins() (*osmocli.QueryDescriptor, *types.ModuleToDistributeCoinsRequest) {
return &osmocli.QueryDescriptor{
Use: "to-distribute-coins",
Short: "Query coins that is going to be distributed",
Long: `{{.Short}}`,
}, &types.ModuleToDistributeCoinsRequest{}
}
// GetCmdGaugeByID returns a gauge by ID.
func GetCmdGaugeByID() (*osmocli.QueryDescriptor, *types.GaugeByIDRequest) {
return &osmocli.QueryDescriptor{
Use: "gauge-by-id",
Short: "Query gauge by id.",
Long: `{{.Short}}{{.ExampleHeader}}
{{.CommandPrefix}} gauge-by-id 1
`,
}, &types.GaugeByIDRequest{}
}
// GetCmdActiveGauges returns active gauges.
func GetCmdActiveGauges() (*osmocli.QueryDescriptor, *types.ActiveGaugesRequest) {
return &osmocli.QueryDescriptor{
Use: "active-gauges",
Short: "Query active gauges",
Long: `{{.Short}}`,
}, &types.ActiveGaugesRequest{}
}
// GetCmdActiveGaugesPerDenom returns active gauges for a specified denom.
func GetCmdActiveGaugesPerDenom() (*osmocli.QueryDescriptor, *types.ActiveGaugesPerDenomRequest) {
return &osmocli.QueryDescriptor{
Use: "active-gauges-per-denom",
Short: "Query active gauges per denom",
Long: `{{.Short}}{{.ExampleHeader}}
{{.CommandPrefix}} active-gauges-per-denom gamm/pool/1`,
}, &types.ActiveGaugesPerDenomRequest{}
}
// GetCmdUpcomingGauges returns scheduled gauges.
func GetCmdUpcomingGauges() (*osmocli.QueryDescriptor, *types.UpcomingGaugesRequest) {
return &osmocli.QueryDescriptor{
Use: "upcoming-gauges",
Short: "Query upcoming gauges",
Long: `{{.Short}}`,
}, &types.UpcomingGaugesRequest{}
}
// GetCmdUpcomingGaugesPerDenom returns scheduled gauges for specified denom..
func GetCmdUpcomingGaugesPerDenom() (*osmocli.QueryDescriptor, *types.UpcomingGaugesPerDenomRequest) {
return &osmocli.QueryDescriptor{
Use: "upcoming-gauges-per-denom",
Short: "Query scheduled gauges per denom",
Long: `{{.Short}}`,
}, &types.UpcomingGaugesPerDenomRequest{}
}
// GetCmdCurrentWeightByGroupGaugeID returns current weight for each gauge respectively since the last epoch from a group gauge ID.
func GetCmdCurrentWeightByGroupGaugeID() (*osmocli.QueryDescriptor, *types.QueryCurrentWeightByGroupGaugeIDRequest) {
return &osmocli.QueryDescriptor{
Use: "current-weight-by-group-gauge-id",
Short: "Query current incentives distribution weight since epoch for each gauge respectively from a group gauge ID",
Long: `{{.Short}}`,
}, &types.QueryCurrentWeightByGroupGaugeIDRequest{}
}
// GetCmdRewardsEst returns rewards estimation.
func GetCmdRewardsEst() *cobra.Command {
cmd := &cobra.Command{
Use: "rewards-estimation",
Short: "Query rewards estimation",
Long: strings.TrimSpace(
fmt.Sprintf(`Query rewards estimation.
Example:
$ %s query incentives rewards-estimation
`,
version.AppName,
),
),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var res *lockuptypes.AccountLockedLongerDurationResponse
ownerLocks := []uint64{}
lockIds := []uint64{}
owner, err := cmd.Flags().GetString(FlagOwner)
if err != nil {
return err
}
lockIdsCombined, err := cmd.Flags().GetString(FlagLockIds)
if err != nil {
return err
}
lockIdStrs := strings.Split(lockIdsCombined, ",")
endEpoch, err := cmd.Flags().GetInt64(FlagEndEpoch)
if err != nil {
return err
}
// if user doesn't provide at least one of the lock ids or owner, we don't have enough information to proceed.
if lockIdsCombined == "" && owner == "" {
return fmt.Errorf("either one of owner flag or lock IDs must be provided")
// if user provides lockIDs, use these lockIDs in our rewards estimation
} else if owner == "" {
for _, lockIdStr := range lockIdStrs {
lockId, err := strconv.ParseUint(lockIdStr, 10, 64)
if err != nil {
return err
}
lockIds = append(lockIds, lockId)
}
// if no lockIDs are provided but an owner is provided, we query the rewards for all of the locks the owner has
} else if lockIdsCombined == "" {
lockIds = append(lockIds, ownerLocks...)
}
// if lockIDs are provided and an owner is provided, only query the lockIDs that are provided
// if a lockID was provided and it doesn't belong to the owner, return an error
if len(lockIds) != 0 && owner != "" {
for _, lockId := range lockIds {
validInputLockId := contains(ownerLocks, lockId)
if !validInputLockId {
return fmt.Errorf("lock-id %v does not belong to %v", lockId, owner)
}
}
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
if owner != "" {
queryClientLockup := lockuptypes.NewQueryClient(clientCtx)
res, err = queryClientLockup.AccountLockedLongerDuration(cmd.Context(), &lockuptypes.AccountLockedLongerDurationRequest{Owner: owner, Duration: time.Millisecond})
if err != nil {
return err
}
for _, lockId := range res.Locks {
ownerLocks = append(ownerLocks, lockId.ID)
}
}
// TODO: Fix accumulation store bug. For now, we return a graceful error when attempting to query bugged gauges
rewardsEstimateResult, err := queryClient.RewardsEst(cmd.Context(), &types.RewardsEstRequest{
Owner: owner, // owner is used only when lockIds are empty
LockIds: lockIds,
EndEpoch: endEpoch,
})
if err != nil {
return err
}
return clientCtx.PrintProto(rewardsEstimateResult)
},
}
flags.AddQueryFlagsToCmd(cmd)
cmd.Flags().String(FlagOwner, "", "Owner to receive rewards, optionally used when lock-ids flag is NOT set")
cmd.Flags().String(FlagLockIds, "", "the lock ids to receive rewards, when it is empty, all lock ids of the owner are used")
cmd.Flags().Int64(FlagEndEpoch, 0, "the end epoch number to participate in rewards calculation")
return cmd
}
func contains(s []uint64, value uint64) bool {
for _, v := range s {
if v == value {
return true
}
}
return false
}
func GetCmdAllGroups() (*osmocli.QueryDescriptor, *types.QueryAllGroupsRequest) {
return &osmocli.QueryDescriptor{
Use: "all-groups",
Short: "Query all groups",
Long: `{{.Short}}`,
}, &types.QueryAllGroupsRequest{}
}
func GetCmdAllGroupsGauges() (*osmocli.QueryDescriptor, *types.QueryAllGroupsGaugesRequest) {
return &osmocli.QueryDescriptor{
Use: "all-groups-gauges",
Short: "Query all group gauges",
Long: `{{.Short}}`,
}, &types.QueryAllGroupsGaugesRequest{}
}
func GetCmdAllGroupsWithGauge() (*osmocli.QueryDescriptor, *types.QueryAllGroupsWithGaugeRequest) {
return &osmocli.QueryDescriptor{
Use: "all-groups-with-gauge",
Short: "Query all groups with their respective group gauge",
Long: `{{.Short}}`,
}, &types.QueryAllGroupsWithGaugeRequest{}
}
func GetCmdGroupByGroupGaugeID() (*osmocli.QueryDescriptor, *types.QueryGroupByGroupGaugeIDRequest) {
return &osmocli.QueryDescriptor{
Use: "group-by-group-gauge-id [group-gauge-id]",
Short: "Query a group it's respective group gauge ID",
Long: `{{.Short}}`,
}, &types.QueryGroupByGroupGaugeIDRequest{}
}