-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
388 lines (332 loc) · 10.9 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package cli
import (
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/fastock/fastock-chain/x/staking/types"
)
// GetQueryCmd returns the cli query commands for staking module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
stakingQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the staking module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
stakingQueryCmd.AddCommand(flags.GetCommands(
GetCmdQueryDelegator(queryRoute, cdc),
GetCmdQueryValidatorShares(queryRoute, cdc),
GetCmdQueryValidator(queryRoute, cdc),
GetCmdQueryValidators(queryRoute, cdc),
GetCmdQueryProxy(queryRoute, cdc),
GetCmdQueryParams(queryRoute, cdc),
GetCmdQueryPool(queryRoute, cdc))...)
return stakingQueryCmd
}
// GetCmdQueryValidator gets the validator query command.
func GetCmdQueryValidator(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "validator [validator-addr]",
Short: "query a validator",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details about an individual validator.
Example:
$ %s query staking validator blockchainvaloper1alq9na49n9yycysh889rl90g9nhe58lcqkfpfg
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
addr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
res, _, err := cliCtx.QueryStore(types.GetValidatorKey(addr), storeName)
if err != nil {
return err
}
if len(res) == 0 {
return fmt.Errorf("no validator found with address %s", addr)
}
//return cliCtx.PrintOutput(types.MustUnmarshalValidator(cdc, res))
return cliCtx.PrintOutput(types.MustUnmarshalValidator(cdc, res).Standardize())
},
}
}
// GetCmdQueryValidators gets the query all validators command.
func GetCmdQueryValidators(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "validators",
Short: "query for all validators",
Args: cobra.NoArgs,
Long: strings.TrimSpace(
fmt.Sprintf(`Query details about all validators on a network.
Example:
$ %s query staking validators
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
resKVs, _, err := cliCtx.QuerySubspace(types.ValidatorsKey, storeName)
if err != nil {
return err
}
var validators types.Validators
for _, kv := range resKVs {
validators = append(validators, types.MustUnmarshalValidator(cdc, kv.Value))
}
//return cliCtx.PrintOutput(validators)
return cliCtx.PrintOutput(validators.Standardize())
},
}
}
// GetCmdQueryPool gets the pool query command.
func GetCmdQueryPool(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "pool",
Args: cobra.NoArgs,
Short: "query the current staking pool values",
Long: strings.TrimSpace(
fmt.Sprintf(`Query values for amounts stored in the staking pool.
Example:
$ %s query staking pool
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/pool", storeName), nil)
if err != nil {
return err
}
var pool types.Pool
if err := cdc.UnmarshalJSON(bz, &pool); err != nil {
return err
}
return cliCtx.PrintOutput(pool)
},
}
}
// GetCmdQueryParams gets the params query command.
func GetCmdQueryParams(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Args: cobra.NoArgs,
Short: "query the current staking parameters information",
Long: strings.TrimSpace(
fmt.Sprintf(`Query values set as staking parameters.
Example:
$ %s query staking params
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []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)
},
}
}
// GetCmdQueryProxy gets command for querying the delegators by a specific proxy
func GetCmdQueryProxy(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "proxy [address]",
Short: "query the delegator addresses by a proxy",
Args: cobra.ExactArgs(1),
Long: strings.TrimSpace(
fmt.Sprintf(`Query the addresses of delegators by a specific proxy
Example:
$ %s query staking proxy blockchain1hw4r48aww06ldrfeuq2v438ujnl6alsz0685a0
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proxyAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return fmt.Errorf("invalid address:%s", args[0])
}
bytes, err := cdc.MarshalJSON(types.NewQueryDelegatorParams(proxyAddr))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryProxy)
resp, _, err := cliCtx.QueryWithData(route, bytes)
if err != nil {
return err
}
var delAddrs Delegators
if err := cdc.UnmarshalJSON(resp, &delAddrs); err != nil {
return err
}
return cliCtx.PrintOutput(delAddrs)
},
}
}
// Delegators is a type alias of sdk.AccAddress slice
type Delegators []sdk.AccAddress
// String returns a human readable string representation of Delegators
func (as Delegators) String() (strFormat string) {
for _, a := range as {
strFormat = fmt.Sprintf("%s%s\n", strFormat, a.String())
}
return strings.TrimSpace(strFormat)
}
// GetCmdQueryDelegator gets command for querying the info of delegator about delegation and shares
func GetCmdQueryDelegator(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "delegator [address]",
Short: "query the information about a delegator",
Args: cobra.ExactArgs(1),
Long: strings.TrimSpace(
fmt.Sprintf(`Query the information of delegations and all shares recently added by a delegator
Example:
$ %s query staking delegator blockchain1hw4r48aww06ldrfeuq2v438ujnl6alsz0685a0
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
delAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return fmt.Errorf("invalid address:%s", args[0])
}
delegator, undelegation := types.NewDelegator(delAddr), types.DefaultUndelegation()
resp, _, err := cliCtx.QueryStore(types.GetDelegatorKey(delAddr), storeName)
if err != nil {
return err
}
if len(resp) != 0 {
cdc.MustUnmarshalBinaryLengthPrefixed(resp, &delegator)
}
// query for the undelegation info
bytes, err := cdc.MarshalJSON(types.NewQueryDelegatorParams(delAddr))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryUnbondingDelegation)
res, _, err := cliCtx.QueryWithData(route, bytes)
// if err!= nil , we treat it as there's no undelegation of the delegator
if err == nil {
if err := cdc.UnmarshalJSON(res, &undelegation); err != nil {
return err
}
}
return cliCtx.PrintOutput(convertToDelegatorResp(delegator, undelegation))
},
}
}
// DelegatorResponse is designed for delegator info query
type DelegatorResponse struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorAddresses []sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
Shares sdk.Dec `json:"shares" yaml:"shares"`
Tokens sdk.Dec `json:"tokens" yaml:"tokens"`
UnbondedTokens sdk.Dec `json:"unbonded_tokens" yaml:"unbonded_tokens"`
CompletionTime time.Time `json:"completion_time" yaml:"completion_time"`
IsProxy bool `json:"is_proxy" yaml:"is_proxy"`
TotalDelegatedTokens sdk.Dec `json:"total_delegated_tokens" yaml:"total_delegated_tokens"`
ProxyAddress sdk.AccAddress `json:"proxy_address" yaml:"proxy_address"`
}
// String returns a human readable string representation of DelegatorResponse
func (dr DelegatorResponse) String() (output string) {
n := len(dr.ValidatorAddresses)
if n > 0 {
output = fmt.Sprintf("%s\n", dr.ValidatorAddresses[0].String())
for i := 1; i < n; i++ {
output = fmt.Sprintf("%s %s\n", output, dr.ValidatorAddresses[i].String())
}
}
proxy := "No"
if dr.IsProxy {
proxy = "Yes"
}
proxied := "No"
if dr.ProxyAddress != nil {
proxied = "Yes\n Proxied by " + dr.ProxyAddress.String() + "\n"
}
output = fmt.Sprintf(`Delegator:
DelegatorAddress: %s
ValidatorAddresses: %s
Shares: %s
Tokens: %s
UnbondedTokens: %s
CompletionTime: %s
IsProxied: %s
IsProxy: %s`,
dr.DelegatorAddress, output, dr.Shares, dr.Tokens, dr.UnbondedTokens, dr.CompletionTime, proxied, proxy)
return
}
func convertToDelegatorResp(delegator types.Delegator, undelegation types.UndelegationInfo,
) DelegatorResponse {
return DelegatorResponse{
delegator.DelegatorAddress,
delegator.ValidatorAddresses,
delegator.Shares,
delegator.Tokens,
undelegation.Quantity,
undelegation.CompletionTime,
delegator.IsProxy,
delegator.TotalDelegatedTokens,
delegator.ProxyAddress,
}
}
// GetCmdQueryValidatorShares gets command for querying all shares added to a validator
func GetCmdQueryValidatorShares(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "shares-added-to [validator-addr]",
Short: "query all shares added to a validator",
Args: cobra.ExactArgs(1),
Long: strings.TrimSpace(
fmt.Sprintf(`Query all shares added to a validator.
Example:
$ %s query staking shares-added-to blockchainvaloper1alq9na49n9yycysh889rl90g9nhe58lcqkfpfg
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
bytes, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr))
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorAllShares)
resp, _, err := cliCtx.QueryWithData(route, bytes)
if err != nil {
return err
}
var sharesResponses types.SharesResponses
if err := cdc.UnmarshalJSON(resp, &sharesResponses); err != nil {
return err
}
return cliCtx.PrintOutput(sharesResponses)
},
}
}