-
Notifications
You must be signed in to change notification settings - Fork 181
/
query.go
261 lines (227 loc) · 7.31 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
package cli
import (
"fmt"
"strconv"
"strings"
"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"
sdkGovCli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
"github.com/spf13/cobra"
"github.com/okex/okexchain/x/gov/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// Group gov queries under a subcommand
govQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the governance module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
govQueryCmd.AddCommand(client.GetCommands(
sdkGovCli.GetCmdQueryProposal(queryRoute, cdc),
sdkGovCli.GetCmdQueryProposals(queryRoute, cdc),
getCmdQueryVote(queryRoute, cdc),
getCmdQueryVotes(queryRoute, cdc),
sdkGovCli.GetCmdQueryParam(queryRoute, cdc),
sdkGovCli.GetCmdQueryParams(queryRoute, cdc),
sdkGovCli.GetCmdQueryProposer(queryRoute, cdc),
getCmdQueryDeposit(queryRoute, cdc),
getCmdQueryDeposits(queryRoute, cdc),
sdkGovCli.GetCmdQueryTally(queryRoute, cdc))...)
return govQueryCmd
}
// Command to Get a Proposal Information
// getCmdQueryVote implements the query proposal vote command.
func getCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "vote [proposal-id] [voter-addr]",
Args: cobra.ExactArgs(2),
Short: "Query details of a single vote",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details for a single vote on a proposal given its identifier.
Example:
$ %s query gov vote 1 okexchain1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
voterAddr, proposalID, _, err := parse(cliCtx, queryRoute, args)
if err != nil {
return err
}
params := types.NewQueryVoteParams(proposalID, voterAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", queryRoute), bz)
if err != nil {
return err
}
var vote types.Vote
if err := cdc.UnmarshalJSON(res, &vote); err != nil {
return err
}
if vote.Empty() {
res, err = gcutils.QueryVoteByTxQuery(cliCtx, params)
if err != nil {
return err
}
if err := cdc.UnmarshalJSON(res, &vote); err != nil {
return err
}
}
return cliCtx.PrintOutput(vote) //nolint:errcheck
},
}
}
func getDepositsOrVotes(cdc *codec.Codec, queryRoute string, args []string, isDeposits bool) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
_, proposalID, res, err := parse(cliCtx, queryRoute, args)
if err != nil {
return err
}
params := types.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
var proposal types.Proposal
cdc.MustUnmarshalJSON(res, &proposal)
propStatus := proposal.Status
if isDeposits {
if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) {
res, err = gcutils.QueryDepositsByTxQuery(cliCtx, params)
} else {
res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz)
}
} else {
if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) {
res, err = gcutils.QueryVotesByTxQuery(cliCtx, params)
} else {
res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz)
}
}
if err != nil {
return err
}
type DepositVotes = types.Votes
if isDeposits {
type DepositVotes = types.Deposits
}
var dep DepositVotes
cdc.MustUnmarshalJSON(res, &dep)
return cliCtx.PrintOutput(dep)
}
// getCmdQueryVotes implements the command to query for proposal votes.
func getCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "votes [proposal-id]",
Args: cobra.ExactArgs(1),
Short: "Query votes on a proposal",
Long: strings.TrimSpace(
fmt.Sprintf(`Query vote details for a single proposal by its identifier.
Example:
$ %s query gov votes 1
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
return getDepositsOrVotes(cdc, queryRoute, args, false)
},
}
}
// Command to Get a specific deposit Information
// getCmdQueryDeposit implements the query proposal deposit command.
func getCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "deposit [proposal-id] [depositer-addr]",
Args: cobra.ExactArgs(2),
Short: "Query details of a deposit",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details for a single proposal deposit on a proposal by its identifier.
Example:
$ %s query gov deposit 1 okexchain1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
depositorAddr, proposalID, _, err := parse(cliCtx, queryRoute, args)
if err != nil {
return err
}
params := types.NewQueryDepositParams(proposalID, depositorAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", queryRoute), bz)
if err != nil {
return err
}
var deposit types.Deposit
cdc.MustUnmarshalJSON(res, &deposit)
if deposit.Empty() {
res, err = gcutils.QueryDepositByTxQuery(cliCtx, params)
if err != nil {
return err
}
cdc.MustUnmarshalJSON(res, &deposit)
}
return cliCtx.PrintOutput(deposit)
},
}
}
// getCmdQueryDeposits implements the command to query for proposal deposits.
func getCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "deposits [proposal-id]",
Args: cobra.ExactArgs(1),
Short: "Query deposits on a proposal",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details for all deposits on a proposal.
You can find the proposal-id by running "%s query gov proposals".
Example:
$ %s query gov deposits 1
`,
version.ClientName, version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
return getDepositsOrVotes(cdc, queryRoute, args, true)
},
}
}
func parse(cliCtx client.CLIContext, queryRoute string, args []string) (sdk.AccAddress, uint64, []byte, error) {
// validate that the proposal id is a uint
proposalID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return nil, 0, []byte{},
fmt.Errorf("proposal-id %s not a valid uint, please input a valid proposal-id", args[0])
}
// check to see if the proposal is in the store
res, err := gcutils.QueryProposalByID(proposalID, cliCtx, queryRoute)
if err != nil {
return nil, proposalID, []byte{}, fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err)
}
var addr sdk.AccAddress
if len(args) > 1 {
addr, err = sdk.AccAddressFromBech32(args[1])
if err != nil {
return addr, proposalID, res, fmt.Errorf("invalid address:%s", args[1])
}
}
return addr, proposalID, res, nil
}
// DONTCOVER