-
Notifications
You must be signed in to change notification settings - Fork 43
/
gov.go
137 lines (125 loc) · 4.66 KB
/
gov.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package emitter
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/bandprotocol/chain/hooks/common"
)
var (
EventTypeInactiveProposal = types.EventTypeInactiveProposal
EventTypeActiveProposal = types.EventTypeActiveProposal
StatusInactive = 6
)
func (h *Hook) emitGovModule(ctx sdk.Context) {
h.govKeeper.IterateProposals(ctx, func(proposal types.Proposal) (stop bool) {
h.emitNewProposal(proposal, nil)
return false
})
h.govKeeper.IterateAllDeposits(ctx, func(deposit types.Deposit) (stop bool) {
h.Write("SET_DEPOSIT", common.JsDict{
"proposal_id": deposit.ProposalID,
"depositor": deposit.Depositor,
"amount": deposit.Amount.String(),
"tx_hash": nil,
})
return false
})
h.govKeeper.IterateAllVotes(ctx, func(vote types.Vote) (stop bool) {
h.Write("SET_VOTE", common.JsDict{
"proposal_id": vote.ProposalID,
"voter": vote.Voter,
"answer": int(vote.Option),
"tx_hash": nil,
})
return false
})
}
func (h *Hook) emitNewProposal(proposal gov.Proposal, proposer sdk.AccAddress) {
h.Write("NEW_PROPOSAL", common.JsDict{
"id": proposal.ProposalID,
"proposer": proposer,
"type": proposal.Content.ProposalType(),
"title": proposal.Content.GetTitle(),
"description": proposal.Content.GetDescription(),
"proposal_route": proposal.Content.ProposalRoute(),
"status": int(proposal.Status),
"submit_time": proposal.SubmitTime.UnixNano(),
"deposit_end_time": proposal.DepositEndTime.UnixNano(),
"total_deposit": proposal.TotalDeposit.String(),
"voting_time": proposal.VotingStartTime.UnixNano(),
"voting_end_time": proposal.VotingEndTime.UnixNano(),
})
}
func (h *Hook) emitSetDeposit(ctx sdk.Context, txHash []byte, id uint64, depositor sdk.AccAddress) {
deposit, _ := h.govKeeper.GetDeposit(ctx, id, depositor)
h.Write("SET_DEPOSIT", common.JsDict{
"proposal_id": id,
"depositor": depositor,
"amount": deposit.Amount.String(),
"tx_hash": txHash,
})
}
func (h *Hook) emitUpdateProposalAfterDeposit(ctx sdk.Context, id uint64) {
proposal, _ := h.govKeeper.GetProposal(ctx, id)
h.Write("UPDATE_PROPOSAL", common.JsDict{
"id": id,
"status": int(proposal.Status),
"total_deposit": proposal.TotalDeposit.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 gov.MsgSubmitProposal, evMap common.EvMap,
) {
proposalId := uint64(common.Atoi(evMap[types.EventTypeSubmitProposal+"."+types.AttributeKeyProposalID][0]))
proposal, _ := app.govKeeper.GetProposal(ctx, proposalId)
app.Write("NEW_PROPOSAL", common.JsDict{
"id": proposalId,
"proposer": msg.Proposer,
"type": msg.Content.ProposalType(),
"title": msg.Content.GetTitle(),
"description": msg.Content.GetDescription(),
"proposal_route": msg.Content.ProposalRoute(),
"status": int(proposal.Status),
"submit_time": proposal.SubmitTime.UnixNano(),
"deposit_end_time": proposal.DepositEndTime.UnixNano(),
"total_deposit": proposal.TotalDeposit.String(),
"voting_time": proposal.VotingStartTime.UnixNano(),
"voting_end_time": proposal.VotingEndTime.UnixNano(),
})
app.emitSetDeposit(ctx, txHash, proposalId, msg.Proposer)
}
// handleMsgDeposit implements emitter handler for MsgDeposit.
func (h *Hook) handleMsgDeposit(
ctx sdk.Context, txHash []byte, msg gov.MsgDeposit,
) {
h.emitSetDeposit(ctx, txHash, msg.ProposalID, msg.Depositor)
h.emitUpdateProposalAfterDeposit(ctx, msg.ProposalID)
}
// handleMsgVote implements emitter handler for MsgVote.
func (h *Hook) handleMsgVote(
txHash []byte, msg gov.MsgVote,
) {
h.Write("SET_VOTE", common.JsDict{
"proposal_id": msg.ProposalID,
"voter": msg.Voter,
"answer": int(msg.Option),
"tx_hash": txHash,
})
}
func (h *Hook) handleEventInactiveProposal(evMap common.EvMap) {
h.Write("UPDATE_PROPOSAL", common.JsDict{
"id": common.Atoi(evMap[types.EventTypeInactiveProposal+"."+types.AttributeKeyProposalID][0]),
"status": StatusInactive,
})
}
func (h *Hook) handleEventTypeActiveProposal(ctx sdk.Context, evMap common.EvMap) {
id := uint64(common.Atoi(evMap[types.EventTypeActiveProposal+"."+types.AttributeKeyProposalID][0]))
proposal, _ := h.govKeeper.GetProposal(ctx, id)
h.Write("UPDATE_PROPOSAL", common.JsDict{
"id": id,
"status": int(proposal.Status),
})
}