github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/types/v1/proposal.go (about) 1 package v1 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/cosmos/cosmos-sdk/codec/types" 9 sdk "github.com/cosmos/cosmos-sdk/types" 10 sdktx "github.com/cosmos/cosmos-sdk/types/tx" 11 ) 12 13 const ( 14 // DefaultStartingProposalID is 1 15 DefaultStartingProposalID uint64 = 1 16 17 StatusNil = ProposalStatus_PROPOSAL_STATUS_UNSPECIFIED 18 StatusDepositPeriod = ProposalStatus_PROPOSAL_STATUS_DEPOSIT_PERIOD 19 StatusVotingPeriod = ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD 20 StatusPassed = ProposalStatus_PROPOSAL_STATUS_PASSED 21 StatusRejected = ProposalStatus_PROPOSAL_STATUS_REJECTED 22 StatusFailed = ProposalStatus_PROPOSAL_STATUS_FAILED 23 ) 24 25 // NewProposal creates a new Proposal instance 26 func NewProposal(messages []sdk.Msg, id uint64, submitTime, depositEndTime time.Time, metadata, title, summary string, proposer sdk.AccAddress, expedited bool) (Proposal, error) { 27 msgs, err := sdktx.SetMsgs(messages) 28 if err != nil { 29 return Proposal{}, err 30 } 31 32 tally := EmptyTallyResult() 33 34 p := Proposal{ 35 Id: id, 36 Messages: msgs, 37 Metadata: metadata, 38 Status: StatusDepositPeriod, 39 FinalTallyResult: &tally, 40 SubmitTime: &submitTime, 41 DepositEndTime: &depositEndTime, 42 Title: title, 43 Summary: summary, 44 Proposer: proposer.String(), 45 Expedited: expedited, 46 } 47 48 return p, nil 49 } 50 51 // GetMessages returns the proposal messages 52 func (p Proposal) GetMsgs() ([]sdk.Msg, error) { 53 return sdktx.GetMsgs(p.Messages, "sdk.MsgProposal") 54 } 55 56 // GetMinDepositFromParams returns min expedited deposit from the gov params if 57 // the proposal is expedited. Otherwise, returns the regular min deposit from 58 // gov params. 59 func (p Proposal) GetMinDepositFromParams(params Params) sdk.Coins { 60 if p.Expedited { 61 return params.ExpeditedMinDeposit 62 } 63 return params.MinDeposit 64 } 65 66 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 67 func (p Proposal) UnpackInterfaces(unpacker types.AnyUnpacker) error { 68 return sdktx.UnpackInterfaces(unpacker, p.Messages) 69 } 70 71 // Proposals is an array of proposal 72 type Proposals []*Proposal 73 74 var _ types.UnpackInterfacesMessage = Proposals{} 75 76 // String implements stringer interface 77 func (p Proposals) String() string { 78 out := "ID - (Status) [Type] Title\n" 79 for _, prop := range p { 80 out += fmt.Sprintf("%d - %s\n", 81 prop.Id, prop.Status) 82 } 83 return strings.TrimSpace(out) 84 } 85 86 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 87 func (p Proposals) UnpackInterfaces(unpacker types.AnyUnpacker) error { 88 for _, x := range p { 89 err := x.UnpackInterfaces(unpacker) 90 if err != nil { 91 return err 92 } 93 } 94 return nil 95 } 96 97 type ( 98 // ProposalQueue defines a queue for proposal ids 99 ProposalQueue []uint64 100 ) 101 102 // ProposalStatusFromString turns a string into a ProposalStatus 103 func ProposalStatusFromString(str string) (ProposalStatus, error) { 104 num, ok := ProposalStatus_value[str] 105 if !ok { 106 return StatusNil, fmt.Errorf("'%s' is not a valid proposal status", str) 107 } 108 return ProposalStatus(num), nil 109 } 110 111 // Format implements the fmt.Formatter interface. 112 func (status ProposalStatus) Format(s fmt.State, verb rune) { 113 switch verb { 114 case 's': 115 s.Write([]byte(status.String())) 116 default: 117 // TODO: Do this conversion more directly 118 s.Write([]byte(fmt.Sprintf("%v", byte(status)))) 119 } 120 } 121 122 // ValidProposalStatus returns true if the proposal status is valid and false 123 // otherwise. 124 func ValidProposalStatus(status ProposalStatus) bool { 125 if status == StatusDepositPeriod || 126 status == StatusVotingPeriod || 127 status == StatusPassed || 128 status == StatusRejected || 129 status == StatusFailed { 130 return true 131 } 132 return false 133 }