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"
|
2019-12-27 09:57:54 -08:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
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
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgSubmitProposal) ValidateBasic() error {
|
2019-04-30 09:31:38 -07:00
|
|
|
if msg.Content == nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(ErrInvalidProposalContent, "missing content")
|
2019-01-29 14:23:25 -08:00
|
|
|
}
|
2019-02-08 15:54:40 -08:00
|
|
|
if msg.Proposer.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Proposer.String())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
if !msg.InitialDeposit.IsValid() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.InitialDeposit.String())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2019-02-06 14:45:15 -08:00
|
|
|
if msg.InitialDeposit.IsAnyNegative() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.InitialDeposit.String())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
if !IsValidProposalType(msg.Content.ProposalType()) {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(ErrInvalidProposalType, msg.Content.ProposalType())
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
// 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
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgDeposit) ValidateBasic() error {
|
2019-02-08 18:33:06 -08:00
|
|
|
if msg.Depositor.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Depositor.String())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
if !msg.Amount.IsValid() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2019-02-06 14:45:15 -08:00
|
|
|
if msg.Amount.IsAnyNegative() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
|
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 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
|
|
|
// 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
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgVote) ValidateBasic() error {
|
2019-02-08 15:54:40 -08:00
|
|
|
if msg.Voter.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Voter.String())
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
if !ValidVoteOption(msg.Option) {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(ErrInvalidVote, msg.Option.String())
|
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
|
|
|
}
|