2019-04-30 09:31:38 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
2019-04-30 09:31:38 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-12-13 10:48:44 -08:00
|
|
|
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
|
2019-04-30 09:31:38 -07:00
|
|
|
)
|
|
|
|
|
2021-12-16 10:23:47 -08:00
|
|
|
const (
|
|
|
|
// DefaultStartingProposalID is 1
|
|
|
|
DefaultStartingProposalID uint64 = 1
|
|
|
|
|
|
|
|
StatusNil = ProposalStatus_PROPOSAL_STATUS_UNSPECIFIED
|
|
|
|
StatusDepositPeriod = ProposalStatus_PROPOSAL_STATUS_DEPOSIT_PERIOD
|
|
|
|
StatusVotingPeriod = ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD
|
|
|
|
StatusPassed = ProposalStatus_PROPOSAL_STATUS_PASSED
|
|
|
|
StatusRejected = ProposalStatus_PROPOSAL_STATUS_REJECTED
|
|
|
|
StatusFailed = ProposalStatus_PROPOSAL_STATUS_FAILED
|
|
|
|
)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// NewProposal creates a new Proposal instance
|
2021-12-13 10:48:44 -08:00
|
|
|
func NewProposal(messages []sdk.Msg, id uint64, submitTime, depositEndTime time.Time) (Proposal, error) {
|
2020-05-19 13:17:29 -07:00
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
msgs, err := sdktx.SetMsgs(messages)
|
2020-05-19 13:17:29 -07:00
|
|
|
if err != nil {
|
|
|
|
return Proposal{}, err
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
2021-02-15 07:43:09 -08:00
|
|
|
p := Proposal{
|
|
|
|
ProposalId: id,
|
2021-12-13 10:48:44 -08:00
|
|
|
Messages: msgs,
|
2021-02-15 07:43:09 -08:00
|
|
|
Status: StatusDepositPeriod,
|
|
|
|
FinalTallyResult: EmptyTallyResult(),
|
|
|
|
TotalDeposit: sdk.NewCoins(),
|
2021-12-16 10:23:47 -08:00
|
|
|
SubmitTime: &submitTime,
|
|
|
|
DepositEndTime: &depositEndTime,
|
2021-02-15 07:43:09 -08:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
return p, nil
|
2020-03-02 14:21:00 -08:00
|
|
|
}
|
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
// GetMessages returns the proposal messages
|
2021-12-16 10:23:47 -08:00
|
|
|
func (p Proposal) GetMsgs() ([]sdk.Msg, error) {
|
2021-12-13 10:48:44 -08:00
|
|
|
return sdktx.GetMsgs(p.Messages, "sdk.MsgProposal")
|
2020-05-19 13:17:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
|
|
|
func (p Proposal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
|
2021-12-13 10:48:44 -08:00
|
|
|
return sdktx.UnpackInterfaces(unpacker, p.Messages)
|
2020-05-19 13:17:29 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Proposals is an array of proposal
|
|
|
|
type Proposals []Proposal
|
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
var _ types.UnpackInterfacesMessage = Proposals{}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// String implements stringer interface
|
2019-04-30 09:31:38 -07:00
|
|
|
func (p Proposals) String() string {
|
|
|
|
out := "ID - (Status) [Type] Title\n"
|
|
|
|
for _, prop := range p {
|
2021-12-13 10:48:44 -08:00
|
|
|
out += fmt.Sprintf("%d - %s\n",
|
|
|
|
prop.ProposalId, prop.Status)
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
return strings.TrimSpace(out)
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
|
|
|
func (p Proposals) UnpackInterfaces(unpacker types.AnyUnpacker) error {
|
|
|
|
for _, x := range p {
|
|
|
|
err := x.UnpackInterfaces(unpacker)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
type (
|
2019-08-08 12:51:18 -07:00
|
|
|
// ProposalQueue defines a queue for proposal ids
|
2019-04-30 09:31:38 -07:00
|
|
|
ProposalQueue []uint64
|
|
|
|
)
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// ProposalStatusFromString turns a string into a ProposalStatus
|
2019-04-30 09:31:38 -07:00
|
|
|
func ProposalStatusFromString(str string) (ProposalStatus, error) {
|
2020-08-11 07:22:30 -07:00
|
|
|
num, ok := ProposalStatus_value[str]
|
|
|
|
if !ok {
|
|
|
|
return StatusNil, fmt.Errorf("'%s' is not a valid proposal status", str)
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
2020-08-11 07:22:30 -07:00
|
|
|
return ProposalStatus(num), nil
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal needed for protobuf compatibility
|
|
|
|
func (status ProposalStatus) Marshal() ([]byte, error) {
|
|
|
|
return []byte{byte(status)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal needed for protobuf compatibility
|
|
|
|
func (status *ProposalStatus) Unmarshal(data []byte) error {
|
|
|
|
*status = ProposalStatus(data[0])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format implements the fmt.Formatter interface.
|
|
|
|
// nolint: errcheck
|
|
|
|
func (status ProposalStatus) Format(s fmt.State, verb rune) {
|
|
|
|
switch verb {
|
|
|
|
case 's':
|
|
|
|
s.Write([]byte(status.String()))
|
|
|
|
default:
|
|
|
|
// TODO: Do this conversion more directly
|
|
|
|
s.Write([]byte(fmt.Sprintf("%v", byte(status))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
// ValidProposalStatus returns true if the proposal status is valid and false
|
|
|
|
// otherwise.
|
|
|
|
func ValidProposalStatus(status ProposalStatus) bool {
|
|
|
|
if status == StatusDepositPeriod ||
|
|
|
|
status == StatusVotingPeriod ||
|
|
|
|
status == StatusPassed ||
|
|
|
|
status == StatusRejected ||
|
|
|
|
status == StatusFailed {
|
|
|
|
return true
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
2021-12-13 10:48:44 -08:00
|
|
|
return false
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|