-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
handler.go
97 lines (83 loc) · 2.45 KB
/
handler.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
package gov
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/tags"
)
// Handle all "gov" type messages.
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgDeposit:
return handleMsgDeposit(ctx, keeper, msg)
case MsgSubmitProposal:
return handleMsgSubmitProposal(ctx, keeper, msg)
case MsgVote:
return handleMsgVote(ctx, keeper, msg)
default:
errMsg := fmt.Sprintf("Unrecognized gov msg type: %T", msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgSubmitProposal(ctx sdk.Context, keeper Keeper, msg MsgSubmitProposal) sdk.Result {
var content ProposalContent
switch msg.ProposalType {
case ProposalTypeText:
content = NewTextProposal(msg.Title, msg.Description)
case ProposalTypeSoftwareUpgrade:
content = NewSoftwareUpgradeProposal(msg.Title, msg.Description)
default:
return ErrInvalidProposalType(keeper.codespace, msg.ProposalType).Result()
}
proposal, err := keeper.SubmitProposal(ctx, content)
if err != nil {
return err.Result()
}
proposalID := proposal.ProposalID
proposalIDStr := fmt.Sprintf("%d", proposalID)
err, votingStarted := keeper.AddDeposit(ctx, proposalID, msg.Proposer, msg.InitialDeposit)
if err != nil {
return err.Result()
}
resTags := sdk.NewTags(
tags.Proposer, []byte(msg.Proposer.String()),
tags.ProposalID, proposalIDStr,
)
if votingStarted {
resTags = resTags.AppendTag(tags.VotingPeriodStart, proposalIDStr)
}
return sdk.Result{
Data: keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID),
Tags: resTags,
}
}
func handleMsgDeposit(ctx sdk.Context, keeper Keeper, msg MsgDeposit) sdk.Result {
err, votingStarted := keeper.AddDeposit(ctx, msg.ProposalID, msg.Depositor, msg.Amount)
if err != nil {
return err.Result()
}
proposalIDStr := fmt.Sprintf("%d", msg.ProposalID)
resTags := sdk.NewTags(
tags.Depositor, []byte(msg.Depositor.String()),
tags.ProposalID, proposalIDStr,
)
if votingStarted {
resTags = resTags.AppendTag(tags.VotingPeriodStart, proposalIDStr)
}
return sdk.Result{
Tags: resTags,
}
}
func handleMsgVote(ctx sdk.Context, keeper Keeper, msg MsgVote) sdk.Result {
err := keeper.AddVote(ctx, msg.ProposalID, msg.Voter, msg.Option)
if err != nil {
return err.Result()
}
return sdk.Result{
Tags: sdk.NewTags(
tags.Voter, msg.Voter.String(),
tags.ProposalID, fmt.Sprintf("%d", msg.ProposalID),
),
}
}