mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-14 12:05:18 +00:00
61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
|
|
|
"github.com/kava-labs/kava/x/committee/types"
|
|
)
|
|
|
|
// Note: QueryProposer is copied in from the gov module
|
|
|
|
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)
|
|
}
|
|
|
|
// QueryProposer will query for a proposer of a governance proposal by ID.
|
|
func QueryProposer(cliCtx context.CLIContext, proposalID uint64) (Proposer, error) {
|
|
events := []string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgSubmitProposal),
|
|
fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalSubmit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))),
|
|
}
|
|
|
|
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
|
|
// support configurable pagination.
|
|
searchResult, err := utils.QueryTxsByEvents(cliCtx, events, defaultPage, defaultLimit)
|
|
if err != nil {
|
|
return Proposer{}, err
|
|
}
|
|
|
|
for _, info := range searchResult.Txs {
|
|
for _, msg := range info.Tx.GetMsgs() {
|
|
// there should only be a single proposal under the given conditions
|
|
if msg.Type() == types.TypeMsgSubmitProposal {
|
|
subMsg := msg.(types.MsgSubmitProposal)
|
|
return NewProposer(proposalID, subMsg.Proposer.String()), nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return Proposer{}, fmt.Errorf("failed to find the proposer for proposalID %d", proposalID)
|
|
}
|