github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/types/v1beta1/msgs.go (about) 1 package v1beta1 2 3 import ( 4 "fmt" 5 6 "github.com/cosmos/gogoproto/proto" 7 8 codectypes "github.com/cosmos/cosmos-sdk/codec/types" 9 sdk "github.com/cosmos/cosmos-sdk/types" 10 ) 11 12 // Governance message types and routes 13 const ( 14 TypeMsgDeposit = "deposit" 15 TypeMsgVote = "vote" 16 TypeMsgVoteWeighted = "weighted_vote" 17 TypeMsgSubmitProposal = "submit_proposal" 18 ) 19 20 var ( 21 _, _, _, _ sdk.Msg = &MsgSubmitProposal{}, &MsgDeposit{}, &MsgVote{}, &MsgVoteWeighted{} 22 23 _ codectypes.UnpackInterfacesMessage = &MsgSubmitProposal{} 24 ) 25 26 // NewMsgSubmitProposal creates a new MsgSubmitProposal. 27 func NewMsgSubmitProposal(content Content, initialDeposit sdk.Coins, proposer sdk.AccAddress) (*MsgSubmitProposal, error) { 28 m := &MsgSubmitProposal{ 29 InitialDeposit: initialDeposit, 30 Proposer: proposer.String(), 31 } 32 err := m.SetContent(content) 33 if err != nil { 34 return nil, err 35 } 36 return m, nil 37 } 38 39 // GetInitialDeposit returns the initial deposit of MsgSubmitProposal. 40 func (m *MsgSubmitProposal) GetInitialDeposit() sdk.Coins { return m.InitialDeposit } 41 42 // GetContent returns the content of MsgSubmitProposal. 43 func (m *MsgSubmitProposal) GetContent() Content { 44 content, ok := m.Content.GetCachedValue().(Content) 45 if !ok { 46 return nil 47 } 48 return content 49 } 50 51 // SetInitialDeposit sets the given initial deposit for MsgSubmitProposal. 52 func (m *MsgSubmitProposal) SetInitialDeposit(coins sdk.Coins) { 53 m.InitialDeposit = coins 54 } 55 56 // SetProposer sets the given proposer address for MsgSubmitProposal. 57 func (m *MsgSubmitProposal) SetProposer(address fmt.Stringer) { 58 m.Proposer = address.String() 59 } 60 61 // SetContent sets the content for MsgSubmitProposal. 62 func (m *MsgSubmitProposal) SetContent(content Content) error { 63 msg, ok := content.(proto.Message) 64 if !ok { 65 return fmt.Errorf("can't proto marshal %T", msg) 66 } 67 any, err := codectypes.NewAnyWithValue(msg) 68 if err != nil { 69 return err 70 } 71 m.Content = any 72 return nil 73 } 74 75 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 76 func (m MsgSubmitProposal) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { 77 var content Content 78 return unpacker.UnpackAny(m.Content, &content) 79 } 80 81 // NewMsgDeposit creates a new MsgDeposit instance 82 func NewMsgDeposit(depositor sdk.AccAddress, proposalID uint64, amount sdk.Coins) *MsgDeposit { 83 return &MsgDeposit{proposalID, depositor.String(), amount} 84 } 85 86 // NewMsgVote creates a message to cast a vote on an active proposal 87 func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) *MsgVote { 88 return &MsgVote{proposalID, voter.String(), option} 89 } 90 91 // NewMsgVoteWeighted creates a message to cast a vote on an active proposal. 92 func NewMsgVoteWeighted(voter sdk.AccAddress, proposalID uint64, options WeightedVoteOptions) *MsgVoteWeighted { 93 return &MsgVoteWeighted{proposalID, voter.String(), options} 94 }