2019-04-30 09:31:38 -07:00
|
|
|
package types
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
import (
|
2018-07-10 17:59:07 -07:00
|
|
|
"fmt"
|
2020-10-29 02:26:51 -07:00
|
|
|
"strings"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2021-02-12 09:26:24 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
2021-12-16 10:23:47 -08:00
|
|
|
const (
|
|
|
|
OptionEmpty = VoteOption_VOTE_OPTION_UNSPECIFIED
|
|
|
|
OptionYes = VoteOption_VOTE_OPTION_YES
|
|
|
|
OptionNo = VoteOption_VOTE_OPTION_NO
|
|
|
|
OptionNoWithVeto = VoteOption_VOTE_OPTION_NO_WITH_VETO
|
|
|
|
OptionAbstain = VoteOption_VOTE_OPTION_ABSTAIN
|
|
|
|
)
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
// NewVote creates a new Vote instance
|
2020-09-25 03:25:37 -07:00
|
|
|
//nolint:interfacer
|
2020-10-30 06:14:39 -07:00
|
|
|
func NewVote(proposalID uint64, voter sdk.AccAddress, options WeightedVoteOptions) Vote {
|
2021-06-23 06:03:33 -07:00
|
|
|
return Vote{ProposalId: proposalID, Voter: voter.String(), Options: options}
|
2019-06-04 11:38:11 -07:00
|
|
|
}
|
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
// Empty returns whether a vote is empty.
|
|
|
|
func (v Vote) Empty() bool {
|
2021-12-16 10:23:47 -08:00
|
|
|
return v.ProposalId == 0 || v.Voter == "" || len(v.Options) == 0
|
2021-12-13 10:48:44 -08:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Votes is a collection of Vote objects
|
2021-12-16 10:23:47 -08:00
|
|
|
type Votes []*Vote
|
2019-01-22 09:28:39 -08:00
|
|
|
|
2020-03-02 12:50:20 -08:00
|
|
|
// Equal returns true if two slices (order-dependant) of votes are equal.
|
|
|
|
func (v Votes) Equal(other Votes) bool {
|
|
|
|
if len(v) != len(other) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, vote := range v {
|
2020-09-25 03:25:37 -07:00
|
|
|
if vote.String() != other[i].String() {
|
2020-03-02 12:50:20 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
func (v Votes) String() string {
|
2019-06-28 13:11:27 -07:00
|
|
|
if len(v) == 0 {
|
|
|
|
return "[]"
|
|
|
|
}
|
2020-08-14 01:55:54 -07:00
|
|
|
out := fmt.Sprintf("Votes for Proposal %d:", v[0].ProposalId)
|
2019-01-22 09:28:39 -08:00
|
|
|
for _, vot := range v {
|
2020-10-30 06:14:39 -07:00
|
|
|
out += fmt.Sprintf("\n %s: %s", vot.Voter, vot.Options)
|
2019-01-22 09:28:39 -08:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:23:47 -08:00
|
|
|
func NewWeightedVoteOption(option VoteOption, weight sdk.Dec) *WeightedVoteOption {
|
|
|
|
return &WeightedVoteOption{Option: option, Weight: weight.String()}
|
2020-10-27 04:50:11 -07:00
|
|
|
}
|
|
|
|
|
2021-12-16 10:23:47 -08:00
|
|
|
// IsValid returns true if the sub vote is valid and false otherwise.
|
|
|
|
func (w *WeightedVoteOption) IsValid() bool {
|
|
|
|
weight, err := sdk.NewDecFromStr(w.Weight)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !weight.IsPositive() || weight.GT(sdk.NewDec(1)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidVoteOption(w.Option)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNonSplitVoteOption creates a single option vote with weight 1
|
|
|
|
func NewNonSplitVoteOption(option VoteOption) WeightedVoteOptions {
|
|
|
|
return WeightedVoteOptions{{option, sdk.NewDec(1).String()}}
|
2020-10-27 04:50:11 -07:00
|
|
|
}
|
|
|
|
|
2020-10-30 06:14:39 -07:00
|
|
|
// WeightedVoteOptions describes array of WeightedVoteOptions
|
2021-12-16 10:23:47 -08:00
|
|
|
type WeightedVoteOptions []*WeightedVoteOption
|
2020-10-28 05:05:35 -07:00
|
|
|
|
2021-02-05 06:57:12 -08:00
|
|
|
func (v WeightedVoteOptions) String() (out string) {
|
|
|
|
for _, opt := range v {
|
|
|
|
out += opt.String() + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(out)
|
2020-10-28 05:05:35 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// VoteOptionFromString returns a VoteOption from a string. It returns an error
|
|
|
|
// if the string is invalid.
|
2018-07-10 17:59:07 -07:00
|
|
|
func VoteOptionFromString(str string) (VoteOption, error) {
|
2020-08-13 06:20:02 -07:00
|
|
|
option, ok := VoteOption_value[str]
|
|
|
|
if !ok {
|
2021-04-28 03:19:08 -07:00
|
|
|
return OptionEmpty, fmt.Errorf("'%s' is not a valid vote option, available options: yes/no/no_with_veto/abstain", str)
|
2018-07-10 17:59:07 -07:00
|
|
|
}
|
2020-08-13 06:20:02 -07:00
|
|
|
return VoteOption(option), nil
|
2018-07-10 17:59:07 -07:00
|
|
|
}
|
|
|
|
|
2020-10-30 06:14:39 -07:00
|
|
|
// WeightedVoteOptionsFromString returns weighted vote options from string. It returns an error
|
2020-10-29 02:26:51 -07:00
|
|
|
// if the string is invalid.
|
2020-10-30 06:14:39 -07:00
|
|
|
func WeightedVoteOptionsFromString(str string) (WeightedVoteOptions, error) {
|
|
|
|
options := WeightedVoteOptions{}
|
|
|
|
for _, option := range strings.Split(str, ",") {
|
|
|
|
fields := strings.Split(option, "=")
|
2020-10-29 02:26:51 -07:00
|
|
|
option, err := VoteOptionFromString(fields[0])
|
|
|
|
if err != nil {
|
2020-10-30 06:14:39 -07:00
|
|
|
return options, err
|
2020-10-29 02:26:51 -07:00
|
|
|
}
|
|
|
|
if len(fields) < 2 {
|
2020-10-30 06:14:39 -07:00
|
|
|
return options, fmt.Errorf("weight field does not exist for %s option", fields[0])
|
2020-10-29 02:26:51 -07:00
|
|
|
}
|
2020-10-30 06:14:39 -07:00
|
|
|
weight, err := sdk.NewDecFromStr(fields[1])
|
2020-10-29 02:26:51 -07:00
|
|
|
if err != nil {
|
2020-10-30 06:14:39 -07:00
|
|
|
return options, err
|
2020-10-29 02:26:51 -07:00
|
|
|
}
|
2021-12-16 10:23:47 -08:00
|
|
|
options = append(options, NewWeightedVoteOption(option, weight))
|
2020-10-29 02:26:51 -07:00
|
|
|
}
|
2020-10-30 06:14:39 -07:00
|
|
|
return options, nil
|
2020-10-29 02:26:51 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// ValidVoteOption returns true if the vote option is valid and false otherwise.
|
|
|
|
func ValidVoteOption(option VoteOption) bool {
|
2018-07-10 17:59:07 -07:00
|
|
|
if option == OptionYes ||
|
|
|
|
option == OptionAbstain ||
|
|
|
|
option == OptionNo ||
|
|
|
|
option == OptionNoWithVeto {
|
|
|
|
return true
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2018-07-10 17:59:07 -07:00
|
|
|
return false
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Marshal needed for protobuf compatibility.
|
2018-07-10 17:59:07 -07:00
|
|
|
func (vo VoteOption) Marshal() ([]byte, error) {
|
|
|
|
return []byte{byte(vo)}, nil
|
|
|
|
}
|
2018-06-27 16:49:59 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Unmarshal needed for protobuf compatibility.
|
2018-07-10 17:59:07 -07:00
|
|
|
func (vo *VoteOption) Unmarshal(data []byte) error {
|
|
|
|
*vo = VoteOption(data[0])
|
|
|
|
return nil
|
2018-06-27 16:49:59 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Format implements the fmt.Formatter interface.
|
2018-07-10 17:59:07 -07:00
|
|
|
func (vo VoteOption) Format(s fmt.State, verb rune) {
|
|
|
|
switch verb {
|
|
|
|
case 's':
|
2019-02-08 12:45:23 -08:00
|
|
|
s.Write([]byte(vo.String()))
|
2018-07-10 17:59:07 -07:00
|
|
|
default:
|
|
|
|
s.Write([]byte(fmt.Sprintf("%v", byte(vo))))
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|