-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_distr_proposal.go
174 lines (145 loc) · 4.99 KB
/
query_distr_proposal.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
package cli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/client/context"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/codec"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/version"
"github.com/furyaxyz/fuxchain/x/distribution/client/common"
"github.com/furyaxyz/fuxchain/x/distribution/types"
)
// GetCmdQueryDelegatorRewards implements the query delegator rewards command.
func GetCmdQueryDelegatorRewards(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "rewards [delegator-addr] [<validator-addr>]",
Args: cobra.RangeArgs(1, 2),
Short: "Query all distribution delegator rewards or rewards from a particular validator",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all rewards earned by a delegator, optionally restrict to rewards from a single validator.
Example:
$ %s query distr rewards did:fury:ex1j5mr2jhr9pf20e7yhln5zkcsgqtdt7cydr8x3y
$ %s query distr rewards did:fury:ex1j5mr2jhr9pf20e7yhln5zkcsgqtdt7cydr8x3y did:fury:exvaloper1pt7xrmxul7sx54ml44lvv403r06clrdkehd8z7
`,
version.ClientName, version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
// query for rewards from a particular delegation
if len(args) == 2 {
resp, _, err := common.QueryDelegationRewards(cliCtx, queryRoute, args[0], args[1])
if err != nil {
return err
}
var result sdk.DecCoins
if err = cdc.UnmarshalJSON(resp, &result); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}
return cliCtx.PrintOutput(result)
}
delegatorAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
params := types.NewQueryDelegatorParams(delegatorAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return fmt.Errorf("failed to marshal params: %w", err)
}
// query for delegator total rewards
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards)
res, _, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return err
}
var result types.QueryDelegatorTotalRewardsResponse
if err = cdc.UnmarshalJSON(res, &result); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}
return cliCtx.PrintOutput(result)
},
}
}
// GetCmdQueryValidatorOutstandingRewards implements the query validator outstanding rewards command.
func GetCmdQueryValidatorOutstandingRewards(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "outstanding-rewards [validator]",
Args: cobra.ExactArgs(1),
Short: "Query distribution outstanding (un-withdrawn) rewards for a validator and all their delegations",
Long: strings.TrimSpace(
fmt.Sprintf(`Query distribution outstanding (un-withdrawn) rewards
for a validator and all their delegations.
Example:
$ %s query distr outstanding-rewards did:fury:exvaloper1pt7xrmxul7sx54ml44lvv403r06clrdkehd8z7
`,
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
}
params := types.NewQueryValidatorOutstandingRewardsParams(valAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
resp, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorOutstandingRewards),
bz,
)
if err != nil {
return err
}
var outstandingRewards types.ValidatorOutstandingRewards
if err := cdc.UnmarshalJSON(resp, &outstandingRewards); err != nil {
return err
}
return cliCtx.PrintOutput(outstandingRewards)
},
}
}
// GetCmdQueryWithdrawAddr implements the query the delegator's withdraw address for commission and reward
func GetCmdQueryWithdrawAddr(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "withdraw-addr [delegator]",
Args: cobra.ExactArgs(1),
Short: "Query delegator's withdraw address",
Long: strings.TrimSpace(
fmt.Sprintf(`Query delegator's withdraw address.
Example:
$ %s query distr withdraw-addr did:fury:ex17kn7d20d85yymu45h79dqs5pxq9m3nyx2mdmcs
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
delegatorAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
params := types.NewQueryDelegatorWithdrawAddrParams(delegatorAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
resp, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryWithdrawAddr),
bz,
)
if err != nil {
return err
}
var accAddress sdk.AccAddress
if err := cdc.UnmarshalJSON(resp, &accAddress); err != nil {
return err
}
return cliCtx.PrintOutput(accAddress)
},
}
}