-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
query.go
252 lines (208 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
package utils
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/gov/tags"
)
const (
defaultPage = 1
defaultLimit = 30 // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19
)
// Proposer contains metadata of a governance proposal used for querying a
// proposer.
type Proposer struct {
ProposalID uint64 `json:"proposal_id"`
Proposer string `json:"proposer"`
}
// NewProposer returns a new Proposer given id and proposer
func NewProposer(proposalID uint64, proposer string) Proposer {
return Proposer{proposalID, proposer}
}
func (p Proposer) String() string {
return fmt.Sprintf("Proposal with ID %d was proposed by %s", p.ProposalID, p.Proposer)
}
// QueryDepositsByTxQuery will query for deposits via a direct txs tags query. It
// will fetch and build deposits directly from the returned txs and return a
// JSON marshalled result or any error that occurred.
//
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
func QueryDepositsByTxQuery(
cdc *codec.Codec, cliCtx context.CLIContext, params gov.QueryProposalParams,
) ([]byte, error) {
tags := []string{
fmt.Sprintf("%s='%s'", tags.Action, gov.MsgDeposit{}.Type()),
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))),
}
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
var deposits []gov.Deposit
for _, info := range infos {
for _, msg := range info.Tx.GetMsgs() {
if msg.Type() == gov.TypeMsgDeposit {
depMsg := msg.(gov.MsgDeposit)
deposits = append(deposits, gov.Deposit{
Depositor: depMsg.Depositor,
ProposalID: params.ProposalID,
Amount: depMsg.Amount,
})
}
}
}
if cliCtx.Indent {
return cdc.MarshalJSONIndent(deposits, "", " ")
}
return cdc.MarshalJSON(deposits)
}
// QueryVotesByTxQuery will query for votes via a direct txs tags query. It
// will fetch and build votes directly from the returned txs and return a JSON
// marshalled result or any error that occurred.
//
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
func QueryVotesByTxQuery(
cdc *codec.Codec, cliCtx context.CLIContext, params gov.QueryProposalParams,
) ([]byte, error) {
tags := []string{
fmt.Sprintf("%s='%s'", tags.Action, gov.MsgVote{}.Type()),
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))),
}
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
var votes []gov.Vote
for _, info := range infos {
for _, msg := range info.Tx.GetMsgs() {
if msg.Type() == gov.TypeMsgVote {
voteMsg := msg.(gov.MsgVote)
votes = append(votes, gov.Vote{
Voter: voteMsg.Voter,
ProposalID: params.ProposalID,
Option: voteMsg.Option,
})
}
}
}
if cliCtx.Indent {
return cdc.MarshalJSONIndent(votes, "", " ")
}
return cdc.MarshalJSON(votes)
}
// QueryVoteByTxQuery will query for a single vote via a direct txs tags query.
func QueryVoteByTxQuery(
cdc *codec.Codec, cliCtx context.CLIContext, params gov.QueryVoteParams,
) ([]byte, error) {
tags := []string{
fmt.Sprintf("%s='%s'", tags.Action, gov.MsgVote{}.Type()),
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))),
fmt.Sprintf("%s='%s'", tags.Voter, []byte(params.Voter.String())),
}
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
for _, info := range infos {
for _, msg := range info.Tx.GetMsgs() {
// there should only be a single vote under the given conditions
if msg.Type() == gov.TypeMsgVote {
voteMsg := msg.(gov.MsgVote)
vote := gov.Vote{
Voter: voteMsg.Voter,
ProposalID: params.ProposalID,
Option: voteMsg.Option,
}
if cliCtx.Indent {
return cdc.MarshalJSONIndent(vote, "", " ")
}
return cdc.MarshalJSON(vote)
}
}
}
return nil, fmt.Errorf("address '%s' did not vote on proposalID %d", params.Voter, params.ProposalID)
}
// QueryDepositByTxQuery will query for a single deposit via a direct txs tags
// query.
func QueryDepositByTxQuery(
cdc *codec.Codec, cliCtx context.CLIContext, params gov.QueryDepositParams,
) ([]byte, error) {
tags := []string{
fmt.Sprintf("%s='%s'", tags.Action, gov.MsgDeposit{}.Type()),
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))),
fmt.Sprintf("%s='%s'", tags.Depositor, []byte(params.Depositor.String())),
}
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
for _, info := range infos {
for _, msg := range info.Tx.GetMsgs() {
// there should only be a single deposit under the given conditions
if msg.Type() == gov.TypeMsgDeposit {
depMsg := msg.(gov.MsgDeposit)
deposit := gov.Deposit{
Depositor: depMsg.Depositor,
ProposalID: params.ProposalID,
Amount: depMsg.Amount,
}
if cliCtx.Indent {
return cdc.MarshalJSONIndent(deposit, "", " ")
}
return cdc.MarshalJSON(deposit)
}
}
}
return nil, fmt.Errorf("address '%s' did not deposit to proposalID %d", params.Depositor, params.ProposalID)
}
// QueryProposerByTxQuery will query for a proposer of a governance proposal by
// ID.
func QueryProposerByTxQuery(
cdc *codec.Codec, cliCtx context.CLIContext, proposalID uint64,
) (Proposer, error) {
tags := []string{
fmt.Sprintf("%s='%s'", tags.Action, gov.MsgSubmitProposal{}.Type()),
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", proposalID))),
}
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return Proposer{}, err
}
for _, info := range infos {
for _, msg := range info.Tx.GetMsgs() {
// there should only be a single proposal under the given conditions
if msg.Type() == gov.TypeMsgSubmitProposal {
subMsg := msg.(gov.MsgSubmitProposal)
return NewProposer(proposalID, subMsg.Proposer.String()), nil
}
}
}
return Proposer{}, fmt.Errorf("failed to find the proposer for proposalID %d", proposalID)
}
// QueryProposalByID takes a proposalID and returns a proposal
func QueryProposalByID(proposalID uint64, cliCtx context.CLIContext, cdc *codec.Codec, queryRoute string) ([]byte, error) {
params := gov.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return nil, err
}
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposal", queryRoute), bz)
if err != nil {
return nil, err
}
return res, err
}