2018-12-14 11:13:43 -08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-01-17 09:53:22 -08:00
|
|
|
|
2019-12-18 10:15:37 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-06-26 09:03:25 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-06-03 00:36:08 -07:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-03-01 07:57:28 -08:00
|
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
2021-12-13 10:48:44 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
2018-12-14 11:13:43 -08:00
|
|
|
)
|
|
|
|
|
2019-01-15 07:34:48 -08:00
|
|
|
const (
|
|
|
|
defaultPage = 1
|
|
|
|
defaultLimit = 30 // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19
|
|
|
|
)
|
|
|
|
|
2018-12-21 09:30:46 -08:00
|
|
|
// Proposer contains metadata of a governance proposal used for querying a
|
|
|
|
// proposer.
|
|
|
|
type Proposer struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"`
|
|
|
|
Proposer string `json:"proposer" yaml:"proposer"`
|
2018-12-21 09:30:46 -08:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:13:43 -08:00
|
|
|
// 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.
|
2021-12-13 10:48:44 -08:00
|
|
|
func QueryDepositsByTxQuery(clientCtx client.Context, params v1beta1.QueryProposalParams) ([]byte, error) {
|
|
|
|
var deposits []v1beta1.Deposit
|
2021-06-03 00:36:08 -07:00
|
|
|
|
|
|
|
// initial deposit was submitted with proposal, so must be queried separately
|
|
|
|
initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !initialDeposit.Amount.IsZero() {
|
|
|
|
deposits = append(deposits, initialDeposit)
|
|
|
|
}
|
|
|
|
|
2021-02-18 10:00:19 -08:00
|
|
|
searchResult, err := combineEvents(
|
|
|
|
clientCtx, defaultPage,
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query legacy Msgs event action
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgDeposit),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query proto Msgs event action
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgDeposit{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
|
|
|
)
|
2018-12-14 11:13:43 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-04 04:09:03 -07:00
|
|
|
for _, info := range searchResult.Txs {
|
2020-07-17 10:17:21 -07:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
2021-12-13 10:48:44 -08:00
|
|
|
if depMsg, ok := msg.(*v1beta1.MsgDeposit); ok {
|
|
|
|
deposits = append(deposits, v1beta1.Deposit{
|
2018-12-14 11:13:43 -08:00
|
|
|
Depositor: depMsg.Depositor,
|
2020-08-14 01:55:54 -07:00
|
|
|
ProposalId: params.ProposalID,
|
2018-12-14 11:13:43 -08:00
|
|
|
Amount: depMsg.Amount,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
bz, err := clientCtx.LegacyAmino.MarshalJSON(deposits)
|
2020-06-15 17:20:50 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
|
|
|
|
2019-12-24 09:04:36 -08:00
|
|
|
// 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
|
2019-12-18 10:15:37 -08:00
|
|
|
// marshalled result or any error that occurred.
|
2021-12-13 10:48:44 -08:00
|
|
|
func QueryVotesByTxQuery(clientCtx client.Context, params v1beta1.QueryProposalVotesParams) ([]byte, error) {
|
2019-12-18 10:15:37 -08:00
|
|
|
var (
|
2021-12-13 10:48:44 -08:00
|
|
|
votes []v1beta1.Vote
|
2019-12-18 10:15:37 -08:00
|
|
|
nextTxPage = defaultPage
|
|
|
|
totalLimit = params.Limit * params.Page
|
|
|
|
)
|
2021-02-18 10:00:19 -08:00
|
|
|
|
2019-12-18 10:15:37 -08:00
|
|
|
// query interrupted either if we collected enough votes or tx indexer run out of relevant txs
|
|
|
|
for len(votes) < totalLimit {
|
2021-04-30 04:00:47 -07:00
|
|
|
// Search for both (legacy) votes and weighted votes.
|
2021-02-18 10:00:19 -08:00
|
|
|
searchResult, err := combineEvents(
|
|
|
|
clientCtx, nextTxPage,
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query legacy Vote Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVote),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query Vote proto Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVote{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query legacy VoteWeighted Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVoteWeighted),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query VoteWeighted proto Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVoteWeighted{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
|
|
|
)
|
2019-12-18 10:15:37 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 10:00:19 -08:00
|
|
|
|
2019-12-18 10:15:37 -08:00
|
|
|
for _, info := range searchResult.Txs {
|
2020-07-17 10:17:21 -07:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
2021-12-13 10:48:44 -08:00
|
|
|
if voteMsg, ok := msg.(*v1beta1.MsgVote); ok {
|
|
|
|
votes = append(votes, v1beta1.Vote{
|
2020-11-03 03:48:53 -08:00
|
|
|
Voter: voteMsg.Voter,
|
|
|
|
ProposalId: params.ProposalID,
|
2021-12-13 10:48:44 -08:00
|
|
|
Options: v1beta1.NewNonSplitVoteOption(voteMsg.Option),
|
2020-11-03 03:48:53 -08:00
|
|
|
})
|
2021-02-18 10:00:19 -08:00
|
|
|
}
|
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
if voteWeightedMsg, ok := msg.(*v1beta1.MsgVoteWeighted); ok {
|
|
|
|
votes = append(votes, v1beta1.Vote{
|
2021-02-18 10:00:19 -08:00
|
|
|
Voter: voteWeightedMsg.Voter,
|
2020-08-14 01:55:54 -07:00
|
|
|
ProposalId: params.ProposalID,
|
2021-02-18 10:00:19 -08:00
|
|
|
Options: voteWeightedMsg.Options,
|
2019-12-18 10:15:37 -08:00
|
|
|
})
|
|
|
|
}
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
|
|
|
}
|
2019-12-18 10:15:37 -08:00
|
|
|
if len(searchResult.Txs) != defaultLimit {
|
|
|
|
break
|
|
|
|
}
|
2021-02-18 10:00:19 -08:00
|
|
|
|
|
|
|
nextTxPage++
|
2019-12-18 10:15:37 -08:00
|
|
|
}
|
|
|
|
start, end := client.Paginate(len(votes), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
2021-12-13 10:48:44 -08:00
|
|
|
votes = []v1beta1.Vote{}
|
2019-12-18 10:15:37 -08:00
|
|
|
} else {
|
|
|
|
votes = votes[start:end]
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
2020-06-15 17:20:50 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
bz, err := clientCtx.LegacyAmino.MarshalJSON(votes)
|
2020-06-15 17:20:50 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryVoteByTxQuery will query for a single vote via a direct txs tags query.
|
2021-12-13 10:48:44 -08:00
|
|
|
func QueryVoteByTxQuery(clientCtx client.Context, params v1beta1.QueryVoteParams) ([]byte, error) {
|
2021-02-18 10:00:19 -08:00
|
|
|
searchResult, err := combineEvents(
|
|
|
|
clientCtx, defaultPage,
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query legacy Vote Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVote),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query Vote proto Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVote{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter.String()),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query legacy VoteWeighted Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVoteWeighted),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter.String()),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query VoteWeighted proto Msgs
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVoteWeighted{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
|
|
|
)
|
2018-12-14 11:13:43 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 10:00:19 -08:00
|
|
|
|
2019-05-04 04:09:03 -07:00
|
|
|
for _, info := range searchResult.Txs {
|
2020-07-17 10:17:21 -07:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
2018-12-14 11:13:43 -08:00
|
|
|
// there should only be a single vote under the given conditions
|
2021-12-13 10:48:44 -08:00
|
|
|
var vote *v1beta1.Vote
|
|
|
|
if voteMsg, ok := msg.(*v1beta1.MsgVote); ok {
|
|
|
|
vote = &v1beta1.Vote{
|
2018-12-14 11:13:43 -08:00
|
|
|
Voter: voteMsg.Voter,
|
2020-08-14 01:55:54 -07:00
|
|
|
ProposalId: params.ProposalID,
|
2021-12-13 10:48:44 -08:00
|
|
|
Options: v1beta1.NewNonSplitVoteOption(voteMsg.Option),
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
2021-04-30 04:00:47 -07:00
|
|
|
}
|
2020-11-03 04:21:23 -08:00
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
if voteWeightedMsg, ok := msg.(*v1beta1.MsgVoteWeighted); ok {
|
|
|
|
vote = &v1beta1.Vote{
|
2021-04-30 04:00:47 -07:00
|
|
|
Voter: voteWeightedMsg.Voter,
|
2020-11-03 03:48:53 -08:00
|
|
|
ProposalId: params.ProposalID,
|
2021-04-30 04:00:47 -07:00
|
|
|
Options: voteWeightedMsg.Options,
|
2020-06-15 17:20:50 -07:00
|
|
|
}
|
2021-04-30 04:00:47 -07:00
|
|
|
}
|
2020-06-15 17:20:50 -07:00
|
|
|
|
2021-04-30 04:00:47 -07:00
|
|
|
if vote != nil {
|
2021-06-11 04:49:39 -07:00
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(vote)
|
2020-11-03 04:21:23 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-03 03:48:53 -08:00
|
|
|
|
2020-11-03 04:21:23 -08:00
|
|
|
return bz, nil
|
|
|
|
}
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:30:46 -08:00
|
|
|
return nil, fmt.Errorf("address '%s' did not vote on proposalID %d", params.Voter, params.ProposalID)
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryDepositByTxQuery will query for a single deposit via a direct txs tags
|
|
|
|
// query.
|
2021-12-13 10:48:44 -08:00
|
|
|
func QueryDepositByTxQuery(clientCtx client.Context, params v1beta1.QueryDepositParams) ([]byte, error) {
|
2021-06-03 00:36:08 -07:00
|
|
|
|
|
|
|
// initial deposit was submitted with proposal, so must be queried separately
|
|
|
|
initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !initialDeposit.Amount.IsZero() {
|
2021-06-11 04:49:39 -07:00
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(&initialDeposit)
|
2021-06-03 00:36:08 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 10:00:19 -08:00
|
|
|
searchResult, err := combineEvents(
|
|
|
|
clientCtx, defaultPage,
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query legacy Msgs event action
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgDeposit),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Depositor.String()),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query proto Msgs event action
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgDeposit{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Depositor.String()),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
|
|
|
)
|
2018-12-14 11:13:43 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-04 04:09:03 -07:00
|
|
|
for _, info := range searchResult.Txs {
|
2020-07-17 10:17:21 -07:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
2018-12-14 11:13:43 -08:00
|
|
|
// there should only be a single deposit under the given conditions
|
2021-12-13 10:48:44 -08:00
|
|
|
if depMsg, ok := msg.(*v1beta1.MsgDeposit); ok {
|
|
|
|
deposit := v1beta1.Deposit{
|
2018-12-14 11:13:43 -08:00
|
|
|
Depositor: depMsg.Depositor,
|
2020-08-14 01:55:54 -07:00
|
|
|
ProposalId: params.ProposalID,
|
2018-12-14 11:13:43 -08:00
|
|
|
Amount: depMsg.Amount,
|
|
|
|
}
|
|
|
|
|
2021-06-11 04:49:39 -07:00
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(&deposit)
|
2020-06-15 17:20:50 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:30:46 -08:00
|
|
|
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.
|
2020-06-01 05:46:03 -07:00
|
|
|
func QueryProposerByTxQuery(clientCtx client.Context, proposalID uint64) (Proposer, error) {
|
2021-02-18 10:00:19 -08:00
|
|
|
searchResult, err := combineEvents(
|
|
|
|
clientCtx,
|
|
|
|
defaultPage,
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query legacy Msgs event action
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgSubmitProposal),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
2021-04-30 04:00:47 -07:00
|
|
|
// Query proto Msgs event action
|
2021-02-18 10:00:19 -08:00
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgSubmitProposal{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
2021-02-18 10:00:19 -08:00
|
|
|
},
|
|
|
|
)
|
2018-12-21 09:30:46 -08:00
|
|
|
if err != nil {
|
2019-01-22 09:28:39 -08:00
|
|
|
return Proposer{}, err
|
2018-12-21 09:30:46 -08:00
|
|
|
}
|
|
|
|
|
2019-05-04 04:09:03 -07:00
|
|
|
for _, info := range searchResult.Txs {
|
2020-07-17 10:17:21 -07:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
2018-12-21 09:30:46 -08:00
|
|
|
// there should only be a single proposal under the given conditions
|
2021-12-13 10:48:44 -08:00
|
|
|
if subMsg, ok := msg.(*v1beta1.MsgSubmitProposal); ok {
|
2020-09-25 03:25:37 -07:00
|
|
|
return NewProposer(proposalID, subMsg.Proposer), nil
|
2018-12-21 09:30:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 08:43:15 -07:00
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
return Proposer{}, fmt.Errorf("failed to find the proposer for proposalID %d", proposalID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryProposalByID takes a proposalID and returns a proposal
|
2020-06-01 05:46:03 -07:00
|
|
|
func QueryProposalByID(proposalID uint64, clientCtx client.Context, queryRoute string) ([]byte, error) {
|
2021-12-13 10:48:44 -08:00
|
|
|
params := v1beta1.NewQueryProposalParams(proposalID)
|
2020-08-26 02:39:38 -07:00
|
|
|
bz, err := clientCtx.LegacyAmino.MarshalJSON(params)
|
2019-01-22 09:28:39 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-21 09:30:46 -08:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/proposal", queryRoute), bz)
|
2019-01-22 09:28:39 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-06 08:43:15 -07:00
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
return res, err
|
2018-12-14 11:13:43 -08:00
|
|
|
}
|
2021-02-18 10:00:19 -08:00
|
|
|
|
|
|
|
// combineEvents queries txs by events with all events from each event group,
|
|
|
|
// and combines all those events together.
|
|
|
|
//
|
|
|
|
// Tx are indexed in tendermint via their Msgs `Type()`, which can be:
|
|
|
|
// - via legacy Msgs (amino or proto), their `Type()` is a custom string,
|
2021-04-30 04:00:47 -07:00
|
|
|
// - via ADR-031 proto msgs, their `Type()` is the protobuf FQ method name.
|
2021-02-18 10:00:19 -08:00
|
|
|
// In searching for events, we search for both `Type()`s, and we use the
|
|
|
|
// `combineEvents` function here to merge events.
|
|
|
|
func combineEvents(clientCtx client.Context, page int, eventGroups ...[]string) (*sdk.SearchTxsResult, error) {
|
|
|
|
// Only the Txs field will be populated in the final SearchTxsResult.
|
|
|
|
allTxs := []*sdk.TxResponse{}
|
|
|
|
for _, events := range eventGroups {
|
2021-03-01 07:57:28 -08:00
|
|
|
res, err := authtx.QueryTxsByEvents(clientCtx, events, page, defaultLimit, "")
|
2021-02-18 10:00:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
allTxs = append(allTxs, res.Txs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sdk.SearchTxsResult{Txs: allTxs}, nil
|
|
|
|
}
|
2021-06-03 00:36:08 -07:00
|
|
|
|
|
|
|
// queryInitialDepositByTxQuery will query for a initial deposit of a governance proposal by
|
|
|
|
// ID.
|
2021-12-13 10:48:44 -08:00
|
|
|
func queryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) (v1beta1.Deposit, error) {
|
2021-06-03 00:36:08 -07:00
|
|
|
searchResult, err := combineEvents(
|
|
|
|
clientCtx, defaultPage,
|
|
|
|
// Query legacy Msgs event action
|
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgSubmitProposal),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
2021-06-03 00:36:08 -07:00
|
|
|
},
|
|
|
|
// Query proto Msgs event action
|
|
|
|
[]string{
|
2021-12-13 10:48:44 -08:00
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgSubmitProposal{})),
|
2021-12-11 15:04:38 -08:00
|
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
2021-06-03 00:36:08 -07:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-13 10:48:44 -08:00
|
|
|
return v1beta1.Deposit{}, err
|
2021-06-03 00:36:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range searchResult.Txs {
|
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
|
|
// there should only be a single proposal under the given conditions
|
2021-12-13 10:48:44 -08:00
|
|
|
if subMsg, ok := msg.(*v1beta1.MsgSubmitProposal); ok {
|
|
|
|
return v1beta1.Deposit{
|
2021-06-03 00:36:08 -07:00
|
|
|
ProposalId: proposalID,
|
|
|
|
Depositor: subMsg.Proposer,
|
|
|
|
Amount: subMsg.InitialDeposit,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
return v1beta1.Deposit{}, sdkerrors.ErrNotFound.Wrapf("failed to find the initial deposit for proposalID %d", proposalID)
|
2021-06-03 00:36:08 -07:00
|
|
|
}
|