github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/types/v1beta1/vote.go (about) 1 package v1beta1 2 3 import ( 4 "fmt" 5 "strings" 6 7 sdkmath "cosmossdk.io/math" 8 9 sdk "github.com/cosmos/cosmos-sdk/types" 10 ) 11 12 // NewVote creates a new Vote instance. 13 func NewVote(proposalID uint64, voter sdk.AccAddress, options WeightedVoteOptions) Vote { 14 return Vote{ProposalId: proposalID, Voter: voter.String(), Options: options} 15 } 16 17 // Empty returns whether a vote is empty. 18 func (v Vote) Empty() bool { 19 return v.String() == (&Vote{}).String() 20 } 21 22 // Votes is an array of vote 23 type Votes []Vote 24 25 // Equal returns true if two slices (order-dependant) of votes are equal. 26 func (v Votes) Equal(other Votes) bool { 27 if len(v) != len(other) { 28 return false 29 } 30 31 for i, vote := range v { 32 if vote.String() != other[i].String() { 33 return false 34 } 35 } 36 37 return true 38 } 39 40 // String implements stringer interface 41 func (v Votes) String() string { 42 if len(v) == 0 { 43 return "[]" 44 } 45 out := fmt.Sprintf("Votes for Proposal %d:", v[0].ProposalId) 46 for _, vot := range v { 47 out += fmt.Sprintf("\n %s: %s", vot.Voter, vot.Options) 48 } 49 return out 50 } 51 52 // NewNonSplitVoteOption creates a single option vote with weight 1 53 func NewNonSplitVoteOption(option VoteOption) WeightedVoteOptions { 54 return WeightedVoteOptions{{option, sdkmath.LegacyNewDec(1)}} 55 } 56 57 // WeightedVoteOptions describes array of WeightedVoteOptions 58 type WeightedVoteOptions []WeightedVoteOption 59 60 func (v WeightedVoteOptions) String() (out string) { 61 for _, opt := range v { 62 out += opt.String() + "\n" 63 } 64 65 return strings.TrimSpace(out) 66 } 67 68 // ValidWeightedVoteOption returns true if the sub vote is valid and false otherwise. 69 func ValidWeightedVoteOption(option WeightedVoteOption) bool { 70 if !option.Weight.IsPositive() || option.Weight.GT(sdkmath.LegacyNewDec(1)) { 71 return false 72 } 73 return ValidVoteOption(option.Option) 74 } 75 76 // VoteOptionFromString returns a VoteOption from a string. It returns an error 77 // if the string is invalid. 78 func VoteOptionFromString(str string) (VoteOption, error) { 79 option, ok := VoteOption_value[str] 80 if !ok { 81 return OptionEmpty, fmt.Errorf("'%s' is not a valid vote option, available options: yes/no/no_with_veto/abstain", str) 82 } 83 return VoteOption(option), nil 84 } 85 86 // WeightedVoteOptionsFromString returns weighted vote options from string. It returns an error 87 // if the string is invalid. 88 func WeightedVoteOptionsFromString(str string) (WeightedVoteOptions, error) { 89 options := WeightedVoteOptions{} 90 for _, option := range strings.Split(str, ",") { 91 fields := strings.Split(option, "=") 92 option, err := VoteOptionFromString(fields[0]) 93 if err != nil { 94 return options, err 95 } 96 if len(fields) < 2 { 97 return options, fmt.Errorf("weight field does not exist for %s option", fields[0]) 98 } 99 weight, err := sdkmath.LegacyNewDecFromStr(fields[1]) 100 if err != nil { 101 return options, err 102 } 103 options = append(options, WeightedVoteOption{option, weight}) 104 } 105 return options, nil 106 } 107 108 // ValidVoteOption returns true if the vote option is valid and false otherwise. 109 func ValidVoteOption(option VoteOption) bool { 110 if option == OptionYes || 111 option == OptionAbstain || 112 option == OptionNo || 113 option == OptionNoWithVeto { 114 return true 115 } 116 return false 117 } 118 119 // Format implements the fmt.Formatter interface. 120 func (vo VoteOption) Format(s fmt.State, verb rune) { 121 switch verb { 122 case 's': 123 s.Write([]byte(vo.String())) 124 default: 125 s.Write([]byte(fmt.Sprintf("%v", byte(vo)))) 126 } 127 }