/
msgs.go
218 lines (180 loc) · 5.65 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package types
import (
"fmt"
yaml "gopkg.in/yaml.v2"
"github.com/gogo/protobuf/proto"
"github.com/creatachain/creata-sdk/codec/types"
sdk "github.com/creatachain/creata-sdk/types"
sdkerrors "github.com/creatachain/creata-sdk/types/errors"
)
// Governance message types and routes
const (
TypeMsgDeposit = "deposit"
TypeMsgVote = "vote"
TypeMsgSubmitProposal = "submit_proposal"
)
var (
_, _, _ sdk.Msg = &MsgSubmitProposal{}, &MsgDeposit{}, &MsgVote{}
_ types.UnpackInterfacesMessage = &MsgSubmitProposal{}
)
// NewMsgSubmitProposal creates a new MsgSubmitProposal.
//nolint:interfacer
func NewMsgSubmitProposal(content Content, initialDeposit sdk.Coins, proposer sdk.AccAddress) (*MsgSubmitProposal, error) {
m := &MsgSubmitProposal{
InitialDeposit: initialDeposit,
Proposer: proposer.String(),
}
err := m.SetContent(content)
if err != nil {
return nil, err
}
return m, nil
}
func (m *MsgSubmitProposal) GetInitialDeposit() sdk.Coins { return m.InitialDeposit }
func (m *MsgSubmitProposal) GetProposer() sdk.AccAddress {
proposer, _ := sdk.AccAddressFromBech32(m.Proposer)
return proposer
}
func (m *MsgSubmitProposal) GetContent() Content {
content, ok := m.Content.GetCachedValue().(Content)
if !ok {
return nil
}
return content
}
func (m *MsgSubmitProposal) SetInitialDeposit(coins sdk.Coins) {
m.InitialDeposit = coins
}
func (m *MsgSubmitProposal) SetProposer(address fmt.Stringer) {
m.Proposer = address.String()
}
func (m *MsgSubmitProposal) SetContent(content Content) error {
msg, ok := content.(proto.Message)
if !ok {
return fmt.Errorf("can't proto marshal %T", msg)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return err
}
m.Content = any
return nil
}
// Route implements Msg
func (m MsgSubmitProposal) Route() string { return RouterKey }
// Type implements Msg
func (m MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal }
// ValidateBasic implements Msg
func (m MsgSubmitProposal) ValidateBasic() error {
if m.Proposer == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Proposer)
}
if !m.InitialDeposit.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.InitialDeposit.String())
}
if m.InitialDeposit.IsAnyNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.InitialDeposit.String())
}
content := m.GetContent()
if content == nil {
return sdkerrors.Wrap(ErrInvalidProposalContent, "missing content")
}
if !IsValidProposalType(content.ProposalType()) {
return sdkerrors.Wrap(ErrInvalidProposalType, content.ProposalType())
}
if err := content.ValidateBasic(); err != nil {
return err
}
return nil
}
// GetSignBytes implements Msg
func (m MsgSubmitProposal) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&m)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (m MsgSubmitProposal) GetSigners() []sdk.AccAddress {
proposer, _ := sdk.AccAddressFromBech32(m.Proposer)
return []sdk.AccAddress{proposer}
}
// String implements the Stringer interface
func (m MsgSubmitProposal) String() string {
out, _ := yaml.Marshal(m)
return string(out)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (m MsgSubmitProposal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var content Content
return unpacker.UnpackAny(m.Content, &content)
}
// NewMsgDeposit creates a new MsgDeposit instance
//nolint:interfacer
func NewMsgDeposit(depositor sdk.AccAddress, proposalID uint64, amount sdk.Coins) *MsgDeposit {
return &MsgDeposit{proposalID, depositor.String(), 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 == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Depositor)
}
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
if msg.Amount.IsAnyNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
return nil
}
// String implements the Stringer interface
func (msg MsgDeposit) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// 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 {
depositor, _ := sdk.AccAddressFromBech32(msg.Depositor)
return []sdk.AccAddress{depositor}
}
// NewMsgVote creates a message to cast a vote on an active proposal
//nolint:interfacer
func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) *MsgVote {
return &MsgVote{proposalID, voter.String(), 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 == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Voter)
}
if !ValidVoteOption(msg.Option) {
return sdkerrors.Wrap(ErrInvalidVote, msg.Option.String())
}
return nil
}
// String implements the Stringer interface
func (msg MsgVote) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// 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 {
voter, _ := sdk.AccAddressFromBech32(msg.Voter)
return []sdk.AccAddress{voter}
}