## Description Closes: #10490 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
524 lines
18 KiB
Go
524 lines
18 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
|
|
)
|
|
|
|
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" yaml:"proposal_id"`
|
|
Proposer string `json:"proposer" yaml:"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(clientCtx client.Context, params v1beta2.QueryProposalParams) ([]byte, error) {
|
|
var deposits []v1beta2.Deposit
|
|
|
|
// initial deposit was submitted with proposal, so must be queried separately
|
|
initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !sdk.Coins(initialDeposit.Amount).IsZero() {
|
|
deposits = append(deposits, initialDeposit)
|
|
}
|
|
|
|
searchResult, err := combineEvents(
|
|
clientCtx, defaultPage,
|
|
// Query legacy Msgs event action
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgDeposit),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
// Query proto Msgs event action v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgDeposit{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
// Query proto Msgs event action v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgDeposit{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, info := range searchResult.Txs {
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
if depMsg, ok := msg.(*v1beta1.MsgDeposit); ok {
|
|
deposits = append(deposits, v1beta2.Deposit{
|
|
Depositor: depMsg.Depositor,
|
|
ProposalId: params.ProposalID,
|
|
Amount: depMsg.Amount,
|
|
})
|
|
}
|
|
|
|
if depMsg, ok := msg.(*v1beta2.MsgDeposit); ok {
|
|
deposits = append(deposits, v1beta2.Deposit{
|
|
Depositor: depMsg.Depositor,
|
|
ProposalId: params.ProposalID,
|
|
Amount: depMsg.Amount,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
bz, err := clientCtx.LegacyAmino.MarshalJSON(deposits)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
// 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.
|
|
func QueryVotesByTxQuery(clientCtx client.Context, params v1beta2.QueryProposalVotesParams) ([]byte, error) {
|
|
var (
|
|
votes []*v1beta2.Vote
|
|
nextTxPage = defaultPage
|
|
totalLimit = params.Limit * params.Page
|
|
)
|
|
|
|
// query interrupted either if we collected enough votes or tx indexer run out of relevant txs
|
|
for len(votes) < totalLimit {
|
|
// Search for both (legacy) votes and weighted votes.
|
|
searchResult, err := combineEvents(
|
|
clientCtx, nextTxPage,
|
|
// Query legacy Vote Msgs
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVote),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
// Query Vote proto Msgs v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVote{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
// Query Vote proto Msgs v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgVote{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
// Query legacy VoteWeighted Msgs
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVoteWeighted),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
// Query VoteWeighted proto Msgs v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVoteWeighted{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
// Query VoteWeighted proto Msgs v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgVoteWeighted{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, info := range searchResult.Txs {
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
if voteMsg, ok := msg.(*v1beta1.MsgVote); ok {
|
|
votes = append(votes, &v1beta2.Vote{
|
|
Voter: voteMsg.Voter,
|
|
ProposalId: params.ProposalID,
|
|
Options: v1beta2.NewNonSplitVoteOption(v1beta2.VoteOption(voteMsg.Option)),
|
|
})
|
|
}
|
|
|
|
if voteMsg, ok := msg.(*v1beta2.MsgVote); ok {
|
|
votes = append(votes, &v1beta2.Vote{
|
|
Voter: voteMsg.Voter,
|
|
ProposalId: params.ProposalID,
|
|
Options: v1beta2.NewNonSplitVoteOption(voteMsg.Option),
|
|
})
|
|
}
|
|
|
|
if voteWeightedMsg, ok := msg.(*v1beta1.MsgVoteWeighted); ok {
|
|
votes = append(votes, convertVote(voteWeightedMsg))
|
|
}
|
|
|
|
if voteWeightedMsg, ok := msg.(*v1beta2.MsgVoteWeighted); ok {
|
|
votes = append(votes, &v1beta2.Vote{
|
|
Voter: voteWeightedMsg.Voter,
|
|
ProposalId: params.ProposalID,
|
|
Options: voteWeightedMsg.Options,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
if len(searchResult.Txs) != defaultLimit {
|
|
break
|
|
}
|
|
|
|
nextTxPage++
|
|
}
|
|
start, end := client.Paginate(len(votes), params.Page, params.Limit, 100)
|
|
if start < 0 || end < 0 {
|
|
votes = []*v1beta2.Vote{}
|
|
} else {
|
|
votes = votes[start:end]
|
|
}
|
|
|
|
bz, err := clientCtx.LegacyAmino.MarshalJSON(votes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
// QueryVoteByTxQuery will query for a single vote via a direct txs tags query.
|
|
func QueryVoteByTxQuery(clientCtx client.Context, params v1beta2.QueryVoteParams) ([]byte, error) {
|
|
searchResult, err := combineEvents(
|
|
clientCtx, defaultPage,
|
|
// Query legacy Vote Msgs
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVote),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter),
|
|
},
|
|
// Query Vote proto Msgs v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVote{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter.String()),
|
|
},
|
|
// Query Vote proto Msgs v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgVote{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter.String()),
|
|
},
|
|
// Query legacy VoteWeighted Msgs
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgVoteWeighted),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter.String()),
|
|
},
|
|
// Query VoteWeighted proto Msgs v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgVoteWeighted{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter),
|
|
},
|
|
// Query VoteWeighted proto Msgs v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgVoteWeighted{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, info := range searchResult.Txs {
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
// there should only be a single vote under the given conditions
|
|
var vote *v1beta2.Vote
|
|
if voteMsg, ok := msg.(*v1beta1.MsgVote); ok {
|
|
vote = &v1beta2.Vote{
|
|
Voter: voteMsg.Voter,
|
|
ProposalId: params.ProposalID,
|
|
Options: v1beta2.NewNonSplitVoteOption(v1beta2.VoteOption(voteMsg.Option)),
|
|
}
|
|
}
|
|
|
|
if voteMsg, ok := msg.(*v1beta2.MsgVote); ok {
|
|
vote = &v1beta2.Vote{
|
|
Voter: voteMsg.Voter,
|
|
ProposalId: params.ProposalID,
|
|
Options: v1beta2.NewNonSplitVoteOption(voteMsg.Option),
|
|
}
|
|
}
|
|
|
|
if voteWeightedMsg, ok := msg.(*v1beta1.MsgVoteWeighted); ok {
|
|
vote = convertVote(voteWeightedMsg)
|
|
}
|
|
|
|
if voteWeightedMsg, ok := msg.(*v1beta2.MsgVoteWeighted); ok {
|
|
vote = &v1beta2.Vote{
|
|
Voter: voteWeightedMsg.Voter,
|
|
ProposalId: params.ProposalID,
|
|
Options: voteWeightedMsg.Options,
|
|
}
|
|
}
|
|
|
|
if vote != nil {
|
|
bz, err := clientCtx.Codec.MarshalJSON(vote)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
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(clientCtx client.Context, params v1beta2.QueryDepositParams) ([]byte, error) {
|
|
|
|
// initial deposit was submitted with proposal, so must be queried separately
|
|
initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !sdk.Coins(initialDeposit.Amount).IsZero() {
|
|
bz, err := clientCtx.Codec.MarshalJSON(&initialDeposit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
searchResult, err := combineEvents(
|
|
clientCtx, defaultPage,
|
|
// Query legacy Msgs event action
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgDeposit),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Depositor.String()),
|
|
},
|
|
// Query proto Msgs event action v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgDeposit{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Depositor.String()),
|
|
},
|
|
// Query proto Msgs event action v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgDeposit{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Depositor.String()),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, info := range searchResult.Txs {
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
// there should only be a single deposit under the given conditions
|
|
if depMsg, ok := msg.(*v1beta1.MsgDeposit); ok {
|
|
deposit := v1beta2.Deposit{
|
|
Depositor: depMsg.Depositor,
|
|
ProposalId: params.ProposalID,
|
|
Amount: depMsg.Amount,
|
|
}
|
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(&deposit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
if depMsg, ok := msg.(*v1beta2.MsgDeposit); ok {
|
|
deposit := v1beta2.Deposit{
|
|
Depositor: depMsg.Depositor,
|
|
ProposalId: params.ProposalID,
|
|
Amount: depMsg.Amount,
|
|
}
|
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(&deposit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
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(clientCtx client.Context, proposalID uint64) (Proposer, error) {
|
|
searchResult, err := combineEvents(
|
|
clientCtx,
|
|
defaultPage,
|
|
// Query legacy Msgs event action
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgSubmitProposal),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
|
},
|
|
// Query proto Msgs event action v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgSubmitProposal{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
|
},
|
|
// Query proto Msgs event action v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgSubmitProposal{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return Proposer{}, err
|
|
}
|
|
|
|
for _, info := range searchResult.Txs {
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
// there should only be a single proposal under the given conditions
|
|
if subMsg, ok := msg.(*v1beta1.MsgSubmitProposal); ok {
|
|
return NewProposer(proposalID, subMsg.Proposer), nil
|
|
}
|
|
if subMsg, ok := msg.(*v1beta2.MsgSubmitProposal); ok {
|
|
return NewProposer(proposalID, subMsg.Proposer), 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, clientCtx client.Context, queryRoute string) ([]byte, error) {
|
|
params := v1beta2.NewQueryProposalParams(proposalID)
|
|
bz, err := clientCtx.LegacyAmino.MarshalJSON(params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/proposal", queryRoute), bz)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return res, err
|
|
}
|
|
|
|
// 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,
|
|
// - via ADR-031 proto msgs, their `Type()` is the protobuf FQ method name.
|
|
// 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 {
|
|
res, err := authtx.QueryTxsByEvents(clientCtx, events, page, defaultLimit, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
allTxs = append(allTxs, res.Txs...)
|
|
}
|
|
|
|
return &sdk.SearchTxsResult{Txs: allTxs}, nil
|
|
}
|
|
|
|
// queryInitialDepositByTxQuery will query for a initial deposit of a governance proposal by
|
|
// ID.
|
|
func queryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) (v1beta2.Deposit, error) {
|
|
searchResult, err := combineEvents(
|
|
clientCtx, defaultPage,
|
|
// Query legacy Msgs event action
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, v1beta1.TypeMsgSubmitProposal),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
|
},
|
|
// Query proto Msgs event action v1beta1
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta1.MsgSubmitProposal{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
|
},
|
|
// Query proto Msgs event action v1beta2
|
|
[]string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&v1beta2.MsgSubmitProposal{})),
|
|
fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID),
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return v1beta2.Deposit{}, err
|
|
}
|
|
|
|
for _, info := range searchResult.Txs {
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
// there should only be a single proposal under the given conditions
|
|
if subMsg, ok := msg.(*v1beta1.MsgSubmitProposal); ok {
|
|
return v1beta2.Deposit{
|
|
ProposalId: proposalID,
|
|
Depositor: subMsg.Proposer,
|
|
Amount: subMsg.InitialDeposit,
|
|
}, nil
|
|
}
|
|
|
|
if subMsg, ok := msg.(*v1beta2.MsgSubmitProposal); ok {
|
|
return v1beta2.Deposit{
|
|
ProposalId: proposalID,
|
|
Depositor: subMsg.Proposer,
|
|
Amount: subMsg.InitialDeposit,
|
|
}, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return v1beta2.Deposit{}, sdkerrors.ErrNotFound.Wrapf("failed to find the initial deposit for proposalID %d", proposalID)
|
|
}
|
|
|
|
// convertVote converts a MsgVoteWeighted into a *v1beta2.Vote.
|
|
func convertVote(v *v1beta1.MsgVoteWeighted) *v1beta2.Vote {
|
|
opts := make([]*v1beta2.WeightedVoteOption, len(v.Options))
|
|
for i, o := range v.Options {
|
|
opts[i] = &v1beta2.WeightedVoteOption{
|
|
Option: v1beta2.VoteOption(o.Option),
|
|
Weight: o.Weight.String(),
|
|
}
|
|
}
|
|
return &v1beta2.Vote{
|
|
Voter: v.Voter,
|
|
ProposalId: v.ProposalId,
|
|
Options: opts,
|
|
}
|
|
}
|