2018-06-21 17:19:14 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// name to idetify transaction types
|
2018-10-23 12:23:55 -07:00
|
|
|
const MsgRoute = "gov"
|
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 {
|
2018-09-02 11:20:14 -07:00
|
|
|
Title string `json:"title"` // Title of the proposal
|
|
|
|
Description string `json:"description"` // Description of the proposal
|
|
|
|
ProposalType ProposalKind `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
|
|
|
|
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
|
|
|
|
InitialDeposit sdk.Coins `json:"initial_deposit"` // Initial deposit paid by sender. Must be strictly positive.
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
func NewMsgSubmitProposal(title string, description string, proposalType ProposalKind, proposer sdk.AccAddress, initialDeposit sdk.Coins) MsgSubmitProposal {
|
2018-06-21 17:19:14 -07:00
|
|
|
return MsgSubmitProposal{
|
|
|
|
Title: title,
|
|
|
|
Description: description,
|
|
|
|
ProposalType: proposalType,
|
|
|
|
Proposer: proposer,
|
|
|
|
InitialDeposit: initialDeposit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:23:55 -07:00
|
|
|
//nolint
|
|
|
|
func (msg MsgSubmitProposal) Route() string { return MsgRoute }
|
|
|
|
func (msg MsgSubmitProposal) Type() string { return "submit_proposal" }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgSubmitProposal) ValidateBasic() sdk.Error {
|
|
|
|
if len(msg.Title) == 0 {
|
|
|
|
return ErrInvalidTitle(DefaultCodespace, msg.Title) // TODO: Proper Error
|
|
|
|
}
|
|
|
|
if len(msg.Description) == 0 {
|
|
|
|
return ErrInvalidDescription(DefaultCodespace, msg.Description) // TODO: Proper Error
|
|
|
|
}
|
|
|
|
if !validProposalType(msg.ProposalType) {
|
2018-07-10 17:59:07 -07:00
|
|
|
return ErrInvalidProposalType(DefaultCodespace, msg.ProposalType)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
if len(msg.Proposer) == 0 {
|
|
|
|
return sdk.ErrInvalidAddress(msg.Proposer.String())
|
|
|
|
}
|
|
|
|
if !msg.InitialDeposit.IsValid() {
|
|
|
|
return sdk.ErrInvalidCoins(msg.InitialDeposit.String())
|
|
|
|
}
|
|
|
|
if !msg.InitialDeposit.IsNotNegative() {
|
|
|
|
return sdk.ErrInvalidCoins(msg.InitialDeposit.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg MsgSubmitProposal) String() string {
|
2018-07-10 17:59:07 -07:00
|
|
|
return fmt.Sprintf("MsgSubmitProposal{%s, %s, %s, %v}", msg.Title, msg.Description, msg.ProposalType, msg.InitialDeposit)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgSubmitProposal) Get(key interface{}) (value interface{}) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgSubmitProposal) GetSignBytes() []byte {
|
2018-07-10 17:59:07 -07:00
|
|
|
b, err := msgCdc.MarshalJSON(msg)
|
2018-06-21 17:19:14 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-05 17:09:23 -07:00
|
|
|
return sdk.MustSortJSON(b)
|
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-10-17 05:26:25 -07:00
|
|
|
ProposalID int64 `json:"proposal_id"` // ID of the proposal
|
|
|
|
Depositer sdk.AccAddress `json:"depositer"` // Address of the depositer
|
|
|
|
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
func NewMsgDeposit(depositer sdk.AccAddress, proposalID int64, amount sdk.Coins) MsgDeposit {
|
2018-06-21 17:19:14 -07:00
|
|
|
return MsgDeposit{
|
|
|
|
ProposalID: proposalID,
|
|
|
|
Depositer: depositer,
|
|
|
|
Amount: amount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
2018-09-17 07:34:06 -07:00
|
|
|
// nolint
|
2018-10-23 12:23:55 -07:00
|
|
|
func (msg MsgDeposit) Route() string { return MsgRoute }
|
|
|
|
func (msg MsgDeposit) Type() string { return "deposit" }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgDeposit) ValidateBasic() sdk.Error {
|
|
|
|
if len(msg.Depositer) == 0 {
|
|
|
|
return sdk.ErrInvalidAddress(msg.Depositer.String())
|
|
|
|
}
|
|
|
|
if !msg.Amount.IsValid() {
|
|
|
|
return sdk.ErrInvalidCoins(msg.Amount.String())
|
|
|
|
}
|
|
|
|
if !msg.Amount.IsNotNegative() {
|
|
|
|
return sdk.ErrInvalidCoins(msg.Amount.String())
|
|
|
|
}
|
|
|
|
if msg.ProposalID < 0 {
|
|
|
|
return ErrUnknownProposal(DefaultCodespace, msg.ProposalID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg MsgDeposit) String() string {
|
2018-07-10 17:59:07 -07:00
|
|
|
return fmt.Sprintf("MsgDeposit{%s=>%v: %v}", msg.Depositer, msg.ProposalID, msg.Amount)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgDeposit) Get(key interface{}) (value interface{}) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgDeposit) GetSignBytes() []byte {
|
2018-07-10 17:59:07 -07:00
|
|
|
b, err := msgCdc.MarshalJSON(msg)
|
2018-06-21 17:19:14 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-05 17:09:23 -07:00
|
|
|
return sdk.MustSortJSON(b)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
|
|
|
|
return []sdk.AccAddress{msg.Depositer}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
// MsgVote
|
|
|
|
type MsgVote struct {
|
2018-10-17 05:26:25 -07:00
|
|
|
ProposalID int64 `json:"proposal_id"` // ID of the proposal
|
|
|
|
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-07-06 00:06:53 -07:00
|
|
|
func NewMsgVote(voter sdk.AccAddress, proposalID int64, option VoteOption) MsgVote {
|
2018-06-21 17:19:14 -07:00
|
|
|
return MsgVote{
|
|
|
|
ProposalID: proposalID,
|
|
|
|
Voter: voter,
|
|
|
|
Option: option,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
2018-09-17 07:34:06 -07:00
|
|
|
// nolint
|
2018-10-23 12:23:55 -07:00
|
|
|
func (msg MsgVote) Route() string { return MsgRoute }
|
|
|
|
func (msg MsgVote) Type() string { return "vote" }
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgVote) ValidateBasic() sdk.Error {
|
|
|
|
if len(msg.Voter.Bytes()) == 0 {
|
|
|
|
return sdk.ErrInvalidAddress(msg.Voter.String())
|
|
|
|
}
|
|
|
|
if msg.ProposalID < 0 {
|
|
|
|
return ErrUnknownProposal(DefaultCodespace, msg.ProposalID)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg MsgVote) String() string {
|
2018-07-10 17:59:07 -07:00
|
|
|
return fmt.Sprintf("MsgVote{%v - %s}", msg.ProposalID, msg.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgVote) Get(key interface{}) (value interface{}) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Msg.
|
|
|
|
func (msg MsgVote) GetSignBytes() []byte {
|
2018-07-10 17:59:07 -07:00
|
|
|
b, err := msgCdc.MarshalJSON(msg)
|
2018-06-21 17:19:14 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-05 17:09:23 -07:00
|
|
|
return sdk.MustSortJSON(b)
|
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
|
|
|
}
|