-
Notifications
You must be signed in to change notification settings - Fork 11
/
msgs.go
172 lines (141 loc) · 5.27 KB
/
msgs.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Governance message types and routes
const (
TypeMsgDeposit = "deposit"
TypeMsgVote = "vote"
TypeMsgSubmitProposal = "submit_proposal"
)
var _, _, _ sdk.Msg = MsgSubmitProposal{}, MsgDeposit{}, MsgVote{}
// MsgSubmitProposal defines a message to create a governance proposal with a
// given content and initial deposit
type MsgSubmitProposal struct {
Content Content `json:"content" yaml:"content"`
InitialDeposit sdk.Int `json:"initial_deposit" yaml:"initial_deposit"` // Initial deposit paid by sender. Must be strictly positive
Proposer sdk.AccAddress `json:"proposer" yaml:"proposer"` // Address of the proposer
}
// NewMsgSubmitProposal creates a new MsgSubmitProposal instance
func NewMsgSubmitProposal(content Content, initialDeposit sdk.Int, proposer sdk.AccAddress) MsgSubmitProposal {
return MsgSubmitProposal{content, initialDeposit, proposer}
}
// Route implements Msg
func (msg MsgSubmitProposal) Route() string { return RouterKey }
// Type implements Msg
func (msg MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal }
// ValidateBasic implements Msg
func (msg MsgSubmitProposal) ValidateBasic() error {
if msg.Content == nil {
return sdkerrors.Wrap(ErrInvalidProposalContent, "missing content")
}
if msg.Proposer.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Proposer.String())
}
if msg.InitialDeposit.IsNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deposit cannot be negative")
}
if !IsValidProposalType(msg.Content.ProposalType()) {
return sdkerrors.Wrap(ErrInvalidProposalType, msg.Content.ProposalType())
}
return msg.Content.ValidateBasic()
}
// String implements the Stringer interface
func (msg MsgSubmitProposal) String() string {
return fmt.Sprintf(`Submit Proposal Message:
Content: %s
Initial Deposit: %s
`, msg.Content.String(), msg.InitialDeposit)
}
// GetSignBytes implements Msg
func (msg MsgSubmitProposal) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgSubmitProposal) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Proposer}
}
// MsgDeposit defines a message to submit a deposit to an existing proposal
type MsgDeposit struct {
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"` // ID of the proposal
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` // Address of the depositor
Amount sdk.Int `json:"amount" yaml:"amount"` // Int to add to the proposal's deposit
}
// NewMsgDeposit creates a new MsgDeposit instance
func NewMsgDeposit(depositor sdk.AccAddress, proposalID uint64, amount sdk.Int) MsgDeposit {
return MsgDeposit{proposalID, depositor, amount}
}
// Route implements Msg
func (msg MsgDeposit) Route() string { return RouterKey }
// Type implements Msg
func (msg MsgDeposit) Type() string { return TypeMsgDeposit }
// ValidateBasic implements Msg
func (msg MsgDeposit) ValidateBasic() error {
if msg.Depositor.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Depositor.String())
}
if msg.Amount.IsNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deposit cannot be negative")
}
return nil
}
// String implements the Stringer interface
func (msg MsgDeposit) String() string {
return fmt.Sprintf(`Deposit Message:
Depositer: %s
Proposal ID: %d
Amount: %s
`, msg.Depositor, msg.ProposalID, msg.Amount)
}
// GetSignBytes implements Msg
func (msg MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Depositor}
}
// MsgVote defines a message to cast a vote
type MsgVote struct {
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"` // ID of the proposal
Voter sdk.AccAddress `json:"voter" yaml:"voter"` // address of the voter
Option VoteOption `json:"option" yaml:"option"` // option from OptionSet chosen by the voter
}
// NewMsgVote creates a message to cast a vote on an active proposal
func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) MsgVote {
return MsgVote{proposalID, voter, option}
}
// Route implements Msg
func (msg MsgVote) Route() string { return RouterKey }
// Type implements Msg
func (msg MsgVote) Type() string { return TypeMsgVote }
// ValidateBasic implements Msg
func (msg MsgVote) ValidateBasic() error {
if msg.Voter.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Voter.String())
}
if !ValidVoteOption(msg.Option) {
return sdkerrors.Wrap(ErrInvalidVote, msg.Option.String())
}
return nil
}
// String implements the Stringer interface
func (msg MsgVote) String() string {
return fmt.Sprintf(`Vote Message:
Proposal ID: %d
Option: %s
`, msg.ProposalID, msg.Option)
}
// GetSignBytes implements Msg
func (msg MsgVote) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgVote) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Voter}
}