2019-04-30 09:31:38 -07:00
|
|
|
package types
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2018-12-14 11:13:43 -08:00
|
|
|
// Governance message types and routes
|
|
|
|
const (
|
|
|
|
TypeMsgDeposit = "deposit"
|
|
|
|
TypeMsgVote = "vote"
|
|
|
|
TypeMsgSubmitProposal = "submit_proposal"
|
|
|
|
)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2018-09-17 07:34:06 -07:00
|
|
|
var _, _, _ sdk.Msg = MsgSubmitProposal{}, MsgDeposit{}, MsgVote{}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// MsgSubmitProposal defines a message to create a governance proposal with a
|
|
|
|
// given content and initial deposit
|
2018-06-21 17:19:14 -07:00
|
|
|
type MsgSubmitProposal struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
Content Content `json:"content" yaml:"content"`
|
|
|
|
InitialDeposit sdk.Coins `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
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// NewMsgSubmitProposal creates a new MsgSubmitProposal instance
|
2019-04-30 09:31:38 -07:00
|
|
|
func NewMsgSubmitProposal(content Content, initialDeposit sdk.Coins, proposer sdk.AccAddress) MsgSubmitProposal {
|
|
|
|
return MsgSubmitProposal{content, initialDeposit, proposer}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// Route implements Msg
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgSubmitProposal) Route() string { return RouterKey }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// Type implements Msg
|
|
|
|
func (msg MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal }
|
|
|
|
|
|
|
|
// ValidateBasic implements Msg
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgSubmitProposal) ValidateBasic() sdk.Error {
|
2019-04-30 09:31:38 -07:00
|
|
|
if msg.Content == nil {
|
|
|
|
return ErrInvalidProposalContent(DefaultCodespace, "missing content")
|
2019-01-29 14:23:25 -08:00
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
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(DefaultCodespace, msg.Content.ProposalType())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2019-02-08 15:54:40 -08:00
|
|
|
if msg.Proposer.Empty() {
|
2018-06-21 17:19:14 -07:00
|
|
|
return sdk.ErrInvalidAddress(msg.Proposer.String())
|
|
|
|
}
|
|
|
|
if !msg.InitialDeposit.IsValid() {
|
|
|
|
return sdk.ErrInvalidCoins(msg.InitialDeposit.String())
|
|
|
|
}
|
2019-02-06 14:45:15 -08:00
|
|
|
if msg.InitialDeposit.IsAnyNegative() {
|
2018-06-21 17:19:14 -07:00
|
|
|
return sdk.ErrInvalidCoins(msg.InitialDeposit.String())
|
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
if !IsValidProposalType(msg.Content.ProposalType()) {
|
|
|
|
return ErrInvalidProposalType(DefaultCodespace, msg.Content.ProposalType())
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg.Content.ValidateBasic()
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// String implements the Stringer interface
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgSubmitProposal) String() string {
|
2019-04-30 09:31:38 -07:00
|
|
|
return fmt.Sprintf(`Submit Proposal Message:
|
|
|
|
Content: %s
|
|
|
|
Initial Deposit: %s
|
|
|
|
`, msg.Content.String(), msg.InitialDeposit)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// GetSignBytes implements Msg
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgSubmitProposal) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// GetSigners implements Msg
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgSubmitProposal) GetSigners() []sdk.AccAddress {
|
|
|
|
return []sdk.AccAddress{msg.Proposer}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// MsgDeposit defines a message to submit a deposit to an existing proposal
|
2018-06-21 17:19:14 -07:00
|
|
|
type MsgDeposit struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
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.Coins `json:"amount" yaml:"amount"` // Coins to add to the proposal's deposit
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// NewMsgDeposit creates a new MsgDeposit instance
|
2018-11-21 15:53:33 -08:00
|
|
|
func NewMsgDeposit(depositor sdk.AccAddress, proposalID uint64, amount sdk.Coins) MsgDeposit {
|
2019-04-30 09:31:38 -07:00
|
|
|
return MsgDeposit{proposalID, depositor, amount}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// Route implements Msg
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgDeposit) Route() string { return RouterKey }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// Type implements Msg
|
|
|
|
func (msg MsgDeposit) Type() string { return TypeMsgDeposit }
|
|
|
|
|
|
|
|
// ValidateBasic implements Msg
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgDeposit) ValidateBasic() sdk.Error {
|
2019-02-08 18:33:06 -08:00
|
|
|
if msg.Depositor.Empty() {
|
2018-11-21 15:53:33 -08:00
|
|
|
return sdk.ErrInvalidAddress(msg.Depositor.String())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
if !msg.Amount.IsValid() {
|
|
|
|
return sdk.ErrInvalidCoins(msg.Amount.String())
|
|
|
|
}
|
2019-02-06 14:45:15 -08:00
|
|
|
if msg.Amount.IsAnyNegative() {
|
2018-06-21 17:19:14 -07:00
|
|
|
return sdk.ErrInvalidCoins(msg.Amount.String())
|
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// String implements the Stringer interface
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgDeposit) String() string {
|
2019-04-30 09:31:38 -07:00
|
|
|
return fmt.Sprintf(`Deposit Message:
|
|
|
|
Depositer: %s
|
|
|
|
Proposal ID: %d
|
|
|
|
Amount: %s
|
|
|
|
`, msg.Depositor, msg.ProposalID, msg.Amount)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// GetSignBytes implements Msg
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgDeposit) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// GetSigners implements Msg
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
|
2018-11-21 15:53:33 -08:00
|
|
|
return []sdk.AccAddress{msg.Depositor}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// MsgVote defines a message to cast a vote
|
2018-06-21 17:19:14 -07:00
|
|
|
type MsgVote struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
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
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// NewMsgVote creates a message to cast a vote on an active proposal
|
2018-11-06 23:33:18 -08:00
|
|
|
func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) MsgVote {
|
2019-04-30 09:31:38 -07:00
|
|
|
return MsgVote{proposalID, voter, option}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// Route implements Msg
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgVote) Route() string { return RouterKey }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// Type implements Msg
|
|
|
|
func (msg MsgVote) Type() string { return TypeMsgVote }
|
|
|
|
|
|
|
|
// ValidateBasic implements Msg
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgVote) ValidateBasic() sdk.Error {
|
2019-02-08 15:54:40 -08:00
|
|
|
if msg.Voter.Empty() {
|
2018-06-21 17:19:14 -07:00
|
|
|
return sdk.ErrInvalidAddress(msg.Voter.String())
|
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
if !ValidVoteOption(msg.Option) {
|
2018-07-10 17:59:07 -07:00
|
|
|
return ErrInvalidVote(DefaultCodespace, msg.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// String implements the Stringer interface
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgVote) String() string {
|
2019-04-30 09:31:38 -07:00
|
|
|
return fmt.Sprintf(`Vote Message:
|
|
|
|
Proposal ID: %d
|
|
|
|
Option: %s
|
|
|
|
`, msg.ProposalID, msg.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// GetSignBytes implements Msg
|
2018-06-21 17:19:14 -07:00
|
|
|
func (msg MsgVote) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// GetSigners implements Msg
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgVote) GetSigners() []sdk.AccAddress {
|
|
|
|
return []sdk.AccAddress{msg.Voter}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|