Skip to content

Commit

Permalink
Voting hooks (needs checking)
Browse files Browse the repository at this point in the history
  • Loading branch information
Satchitananda committed Nov 26, 2023
1 parent b2fde24 commit 7532895
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
28 changes: 18 additions & 10 deletions hooks/emitter/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

Expand Down Expand Up @@ -186,21 +187,18 @@ func (h *Hook) AfterInitChain(ctx sdk.Context, req abci.RequestInitChain, res ab
}

// Gov module
var govState govtypes.GenesisState
var govState govv1.GenesisState
h.cdc.MustUnmarshalJSON(genesisState[govtypes.ModuleName], &govState)
for _, proposal := range govState.Proposals {
content := proposal.GetContent()
h.Write("NEW_PROPOSAL", common.JsDict{
"id": proposal.ProposalId,
"id": proposal.Id,
"proposer": nil,
"type": proposal.ProposalType(),
"title": content.GetTitle(),
"description": content.GetDescription(),
"proposal_route": content.ProposalRoute(),
"title": proposal.Title,
"description": proposal.Summary,
"status": int(proposal.Status),
"submit_time": proposal.SubmitTime.UnixNano(),
"deposit_end_time": proposal.DepositEndTime.UnixNano(),
"total_deposit": proposal.TotalDeposit.String(),
"total_deposit": proposal.TotalDeposit[len(proposal.TotalDeposit)-1].String(),
"voting_time": proposal.VotingStartTime.UnixNano(),
"voting_end_time": proposal.VotingEndTime.UnixNano(),
})
Expand All @@ -209,15 +207,25 @@ func (h *Hook) AfterInitChain(ctx sdk.Context, req abci.RequestInitChain, res ab
h.Write("SET_DEPOSIT", common.JsDict{
"proposal_id": deposit.ProposalId,
"depositor": deposit.Depositor,
"amount": deposit.Amount.String(),
"amount": deposit.Amount[len(deposit.Amount)-1].String(),
"tx_hash": nil,
})
}

for _, vote := range govState.Votes {

var answers []common.JsDict
for _, voteOption := range vote.Options {
answers = append(answers, common.JsDict{
"answer": voteOption.Option,
"weight": voteOption.Weight,
})
}

h.Write("SET_VOTE", common.JsDict{
"proposal_id": vote.ProposalId,
"voter": vote.Voter,
"answer": int(vote.Option),
"answers": answers,
"tx_hash": nil,
})
}
Expand Down
56 changes: 29 additions & 27 deletions hooks/emitter/gov.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package emitter

import (
"github.com/ODIN-PROTOCOL/odin-core/hooks/common"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/ODIN-PROTOCOL/odin-core/hooks/common"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)

var (
Expand All @@ -14,43 +14,48 @@ var (
)

func (h *Hook) emitGovModule(ctx sdk.Context) {
h.govKeeper.IterateProposals(ctx, func(proposal types.Proposal) (stop bool) {
h.govKeeper.IterateProposals(ctx, func(proposal govv1.Proposal) (stop bool) {
h.emitNewProposal(proposal, nil)
return false
})
h.govKeeper.IterateAllDeposits(ctx, func(deposit types.Deposit) (stop bool) {
h.govKeeper.IterateAllDeposits(ctx, func(deposit govv1.Deposit) (stop bool) {
h.Write("SET_DEPOSIT", common.JsDict{
"proposal_id": deposit.ProposalId,
"depositor": deposit.Depositor,
"amount": deposit.Amount.String(),
"amount": deposit.Amount,
"tx_hash": nil,
})
return false
})
h.govKeeper.IterateAllVotes(ctx, func(vote types.Vote) (stop bool) {
h.govKeeper.IterateAllVotes(ctx, func(vote govv1.Vote) (stop bool) {
var answers []common.JsDict
for _, voteOption := range vote.Options {
answers = append(answers, common.JsDict{
"answer": voteOption.Option,
"weight": voteOption.Weight,
})
}

h.Write("SET_VOTE", common.JsDict{
"proposal_id": vote.ProposalId,
"voter": vote.Voter,
"answer": int(vote.Option),
"answers": answers,
"tx_hash": nil,
})
return false
})
}

func (h *Hook) emitNewProposal(proposal types.Proposal, proposer sdk.AccAddress) {
content := proposal.GetContent()
func (h *Hook) emitNewProposal(proposal govv1.Proposal, proposer sdk.AccAddress) {
h.Write("NEW_PROPOSAL", common.JsDict{
"id": proposal.ProposalId,
"id": proposal.Id,
"proposer": proposer,
"type": content.ProposalType(),
"title": content.GetTitle(),
"description": content.GetDescription(),
"proposal_route": content.ProposalRoute(),
"title": proposal.Title,
"description": proposal.Summary,
"status": int(proposal.Status),
"submit_time": proposal.SubmitTime.UnixNano(),
"deposit_end_time": proposal.DepositEndTime.UnixNano(),
"total_deposit": proposal.TotalDeposit.String(),
"total_deposit": proposal.TotalDeposit[len(proposal.TotalDeposit)-1].String(),
"voting_time": proposal.VotingStartTime.UnixNano(),
"voting_end_time": proposal.VotingEndTime.UnixNano(),
})
Expand All @@ -61,7 +66,7 @@ func (h *Hook) emitSetDeposit(ctx sdk.Context, txHash []byte, id uint64, deposit
h.Write("SET_DEPOSIT", common.JsDict{
"proposal_id": id,
"depositor": depositor,
"amount": deposit.Amount.String(),
"amount": deposit.Amount[len(deposit.Amount)-1].String(),
"tx_hash": txHash,
})
}
Expand All @@ -71,30 +76,27 @@ func (h *Hook) emitUpdateProposalAfterDeposit(ctx sdk.Context, id uint64) {
h.Write("UPDATE_PROPOSAL", common.JsDict{
"id": id,
"status": int(proposal.Status),
"total_deposit": proposal.TotalDeposit.String(),
"total_deposit": proposal.TotalDeposit[len(proposal.TotalDeposit)-1].String(),
"voting_time": proposal.VotingStartTime.UnixNano(),
"voting_end_time": proposal.VotingEndTime.UnixNano(),
})
}

// handleMsgSubmitProposal implements emitter handler for MsgSubmitProposal.
func (app *Hook) handleMsgSubmitProposal(
ctx sdk.Context, txHash []byte, msg *types.MsgSubmitProposal, evMap common.EvMap,
ctx sdk.Context, txHash []byte, msg *govv1.MsgSubmitProposal, evMap common.EvMap,
) {
proposalId := uint64(common.Atoi(evMap[types.EventTypeSubmitProposal+"."+types.AttributeKeyProposalID][0]))
proposal, _ := app.govKeeper.GetProposal(ctx, proposalId)
content := msg.GetContent()
app.Write("NEW_PROPOSAL", common.JsDict{
"id": proposalId,
"id": proposal.Id,
"proposer": msg.Proposer,
"type": content.ProposalType(),
"title": content.GetTitle(),
"description": content.GetDescription(),
"proposal_route": content.ProposalRoute(),
"title": proposal.Title,
"description": proposal.Summary,
"status": int(proposal.Status),
"submit_time": proposal.SubmitTime.UnixNano(),
"deposit_end_time": proposal.DepositEndTime.UnixNano(),
"total_deposit": proposal.TotalDeposit.String(),
"total_deposit": proposal.TotalDeposit[len(proposal.TotalDeposit)-1].String(),
"voting_time": proposal.VotingStartTime.UnixNano(),
"voting_end_time": proposal.VotingEndTime.UnixNano(),
})
Expand All @@ -104,7 +106,7 @@ func (app *Hook) handleMsgSubmitProposal(

// handleMsgDeposit implements emitter handler for MsgDeposit.
func (h *Hook) handleMsgDeposit(
ctx sdk.Context, txHash []byte, msg *types.MsgDeposit,
ctx sdk.Context, txHash []byte, msg *govv1.MsgDeposit,
) {
depositor, _ := sdk.AccAddressFromBech32(msg.Depositor)
h.emitSetDeposit(ctx, txHash, msg.ProposalId, depositor)
Expand All @@ -113,7 +115,7 @@ func (h *Hook) handleMsgDeposit(

// handleMsgVote implements emitter handler for MsgVote.
func (h *Hook) handleMsgVote(
txHash []byte, msg *types.MsgVote,
txHash []byte, msg *govv1.MsgVote,
) {
h.Write("SET_VOTE", common.JsDict{
"proposal_id": msg.ProposalId,
Expand Down
12 changes: 6 additions & 6 deletions hooks/emitter/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (

distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/ODIN-PROTOCOL/odin-core/hooks/common"
oracletypes "github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/ODIN-PROTOCOL/odin-core/hooks/common"
oracletypes "github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
)

func parseEvents(events sdk.StringEvents) common.EvMap {
Expand Down Expand Up @@ -72,11 +72,11 @@ func (h *Hook) handleMsg(ctx sdk.Context, txHash []byte, msg sdk.Msg, log sdk.AB
h.handleMsgWithdrawValidatorCommission(ctx, msg, evMap, extra)
case *slashingtypes.MsgUnjail:
h.handleMsgUnjail(ctx, msg)
case *govtypes.MsgSubmitProposal:
case *govv1.MsgSubmitProposal:
h.handleMsgSubmitProposal(ctx, txHash, msg, evMap)
case *govtypes.MsgVote:
case *govv1.MsgVote:
h.handleMsgVote(txHash, msg)
case *govtypes.MsgDeposit:
case *govv1.MsgDeposit:
h.handleMsgDeposit(ctx, txHash, msg)
case *channeltypes.MsgRecvPacket:
h.handleMsgRecvPacket(ctx, txHash, msg, evMap, extra)
Expand Down

0 comments on commit 7532895

Please sign in to comment.