|
| 1 | +package gov |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" |
| 8 | + "github.com/forbole/juno/v2/cmd/parse" |
| 9 | + "github.com/forbole/juno/v2/types/config" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + "github.com/forbole/bdjuno/v2/database" |
| 13 | + "github.com/forbole/bdjuno/v2/modules" |
| 14 | + "github.com/forbole/bdjuno/v2/modules/gov" |
| 15 | + "github.com/forbole/bdjuno/v2/utils" |
| 16 | +) |
| 17 | + |
| 18 | +// proposalCmd returns the Cobra command allowing to fix all things related to a proposal |
| 19 | +func proposalCmd(parseConfig *parse.Config) *cobra.Command { |
| 20 | + return &cobra.Command{ |
| 21 | + Use: "proposal [id]", |
| 22 | + Short: "Get the description, votes and everything related to a proposal given its id", |
| 23 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | + proposalID := args[0] |
| 25 | + |
| 26 | + parseCtx, err := parse.GetParsingContext(parseConfig) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + sources, err := modules.BuildSources(config.Cfg.Node, parseCtx.EncodingConfig) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + // Get the database |
| 37 | + db := database.Cast(parseCtx.Database) |
| 38 | + |
| 39 | + // Build the gov module |
| 40 | + govModule := gov.NewModule(parseCtx.EncodingConfig.Marshaler, sources.GovSource, nil, nil, nil, db) |
| 41 | + |
| 42 | + err = refreshProposalDetails(parseCtx, proposalID, govModule) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + err = refreshProposalDeposits(parseCtx, proposalID, govModule) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + err = refreshProposalVotes(parseCtx, proposalID, govModule) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func refreshProposalDetails(parseCtx *parse.Context, proposalID string, govModule *gov.Module) error { |
| 63 | + // Get the tx that created the proposal |
| 64 | + txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("submit_proposal.proposal_id=%s", proposalID)) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + if len(txs) > 1 { |
| 70 | + return fmt.Errorf("expecting only one create proposal transaction, found %d", len(txs)) |
| 71 | + } |
| 72 | + |
| 73 | + // Get the tx details |
| 74 | + tx, err := parseCtx.Node.Tx(hex.EncodeToString(txs[0].Tx.Hash())) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + // Handle the MsgSubmitProposal messages |
| 80 | + for index, msg := range tx.GetMsgs() { |
| 81 | + if _, ok := msg.(*govtypes.MsgSubmitProposal); !ok { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + err = govModule.HandleMsg(index, msg, tx) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("error while handling MsgSubmitProposal: %s", err) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func refreshProposalDeposits(parseCtx *parse.Context, proposalID string, govModule *gov.Module) error { |
| 95 | + // Get the tx that deposited to the proposal |
| 96 | + txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_deposit.proposal_id=%s", proposalID)) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + for _, tx := range txs { |
| 102 | + // Get the tx details |
| 103 | + junoTx, err := parseCtx.Node.Tx(hex.EncodeToString(tx.Tx.Hash())) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + // Handle the MsgDeposit messages |
| 109 | + for index, msg := range junoTx.GetMsgs() { |
| 110 | + if _, ok := msg.(*govtypes.MsgDeposit); !ok { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + err = govModule.HandleMsg(index, msg, junoTx) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("error while handling MsgDeposit: %s", err) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func refreshProposalVotes(parseCtx *parse.Context, proposalID string, govModule *gov.Module) error { |
| 125 | + // Get the tx that voted the proposal |
| 126 | + txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_vote.proposal_id=%s", proposalID)) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + for _, tx := range txs { |
| 132 | + // Get the tx details |
| 133 | + junoTx, err := parseCtx.Node.Tx(hex.EncodeToString(tx.Tx.Hash())) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + // Handle the MsgVote messages |
| 139 | + for index, msg := range junoTx.GetMsgs() { |
| 140 | + if _, ok := msg.(*govtypes.MsgVote); !ok { |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + err = govModule.HandleMsg(index, msg, junoTx) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("error while handling MsgVote: %s", err) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
0 commit comments