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{}
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// MsgSubmitProposal
|
|
|
|
type MsgSubmitProposal struct {
|
2019-04-30 09:31:38 -07:00
|
|
|
Content Content `json:"content"`
|
|
|
|
InitialDeposit sdk.Coins `json:"initial_deposit"` // Initial deposit paid by sender. Must be strictly positive
|
2018-09-02 11:20:14 -07:00
|
|
|
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-23 12:23:55 -07:00
|
|
|
//nolint
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgSubmitProposal) Route() string { return RouterKey }
|
2018-12-14 11:13:43 -08:00
|
|
|
func (msg MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// MsgDeposit
|
|
|
|
type MsgDeposit struct {
|
2018-11-06 23:33:18 -08:00
|
|
|
ProposalID uint64 `json:"proposal_id"` // ID of the proposal
|
2018-11-21 15:53:33 -08:00
|
|
|
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
|
2018-10-17 05:26:25 -07:00
|
|
|
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
2018-09-17 07:34:06 -07:00
|
|
|
// nolint
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgDeposit) Route() string { return RouterKey }
|
2018-12-14 11:13:43 -08:00
|
|
|
func (msg MsgDeposit) Type() string { return TypeMsgDeposit }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// MsgVote
|
|
|
|
type MsgVote struct {
|
2018-11-06 23:33:18 -08:00
|
|
|
ProposalID uint64 `json:"proposal_id"` // ID of the proposal
|
2018-10-17 05:26:25 -07:00
|
|
|
Voter sdk.AccAddress `json:"voter"` // address of the voter
|
|
|
|
Option VoteOption `json:"option"` // option from OptionSet chosen by the voter
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
2018-09-17 07:34:06 -07:00
|
|
|
// nolint
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgVote) Route() string { return RouterKey }
|
2018-12-14 11:13:43 -08:00
|
|
|
func (msg MsgVote) Type() string { return TypeMsgVote }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|