-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.go
178 lines (148 loc) · 5.02 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
package types
import (
"fmt"
sdk "github.com/brc20-collab/brczero/libs/cosmos-sdk/types"
)
// Governance message types and routes
const (
TypeMsgDeposit = "deposit"
TypeMsgVote = "vote"
TypeMsgSubmitProposal = "submit_proposal"
)
var _, _, _ sdk.Msg = MsgSubmitProposal{}, MsgDeposit{}, MsgVote{}
// MsgSubmitProposal
type MsgSubmitProposal struct {
Content Content `json:"content" yaml:"content"`
InitialDeposit sdk.SysCoins `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
}
func NewMsgSubmitProposal(content Content, initialDeposit sdk.SysCoins, proposer sdk.AccAddress) MsgSubmitProposal {
return MsgSubmitProposal{content, initialDeposit, proposer}
}
//nolint
func (msg MsgSubmitProposal) Route() string { return RouterKey }
func (msg MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal }
// Implements Msg.
func (msg MsgSubmitProposal) ValidateBasic() sdk.Error {
if msg.Content == nil {
return ErrInvalidProposalContent("content is required")
}
if msg.Content.ProposalType() == ProposalTypeSoftwareUpgrade {
// Disable software upgrade proposals as they are currently equivalent
// to text proposals. Re-enable once a valid software upgrade proposal
// handler is implemented.
return ErrInvalidProposalType(msg.Content.ProposalType())
}
if msg.Proposer.Empty() {
return ErrInvalidAddress(msg.Proposer.String())
}
if !msg.InitialDeposit.IsValid() {
return ErrInvalidCoins()
}
if msg.InitialDeposit.IsAnyNegative() {
return ErrInvalidCoins()
}
if len(msg.InitialDeposit) != 1 || msg.InitialDeposit[0].Denom != sdk.DefaultBondDenom || !msg.InitialDeposit.IsValid() {
return sdk.ErrInvalidCoins(fmt.Sprintf("must deposit %s but got %s", sdk.DefaultBondDenom,
msg.InitialDeposit.String()))
}
if !IsValidProposalType(msg.Content.ProposalType()) {
return ErrInvalidProposalType(msg.Content.ProposalType())
}
return msg.Content.ValidateBasic()
}
func (msg MsgSubmitProposal) String() string {
return fmt.Sprintf(`Submit Proposal Message:
Content: %s
Initial Deposit: %s
`, msg.Content.String(), msg.InitialDeposit)
}
// Implements Msg.
func (msg MsgSubmitProposal) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// Implements Msg.
func (msg MsgSubmitProposal) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Proposer}
}
// MsgDeposit
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.SysCoins `json:"amount" yaml:"amount"` // Coins to add to the proposal's deposit
}
func NewMsgDeposit(depositor sdk.AccAddress, proposalID uint64, amount sdk.SysCoins) MsgDeposit {
return MsgDeposit{proposalID, depositor, amount}
}
// Implements Msg.
// nolint
func (msg MsgDeposit) Route() string { return RouterKey }
func (msg MsgDeposit) Type() string { return TypeMsgDeposit }
// Implements Msg.
func (msg MsgDeposit) ValidateBasic() sdk.Error {
if msg.Depositor.Empty() {
return ErrInvalidAddress(msg.Depositor.String())
}
if !msg.Amount.IsValid() {
return ErrInvalidCoins()
}
if msg.Amount.IsAnyNegative() {
return ErrInvalidCoins()
}
return nil
}
func (msg MsgDeposit) String() string {
return fmt.Sprintf(`Deposit Message:
Depositer: %s
Proposal ID: %d
Amount: %s
`, msg.Depositor, msg.ProposalID, msg.Amount)
}
// Implements Msg.
func (msg MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// Implements Msg.
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Depositor}
}
// MsgVote
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
}
func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) MsgVote {
return MsgVote{proposalID, voter, option}
}
// Implements Msg.
// nolint
func (msg MsgVote) Route() string { return RouterKey }
func (msg MsgVote) Type() string { return TypeMsgVote }
// Implements Msg.
func (msg MsgVote) ValidateBasic() sdk.Error {
if msg.Voter.Empty() {
return ErrInvalidAddress(msg.Voter.String())
}
if !ValidVoteOption(msg.Option) {
return ErrInvalidVote(msg.Option)
}
return nil
}
func (msg MsgVote) String() string {
return fmt.Sprintf(`Vote Message:
Proposal ID: %d
Option: %s
`, msg.ProposalID, msg.Option)
}
// Implements Msg.
func (msg MsgVote) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// Implements Msg.
func (msg MsgVote) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Voter}
}