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"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2020-07-28 05:53:35 -07:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2020-03-02 11:13:42 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
// NewVote creates a new Vote instance
|
|
|
|
func NewVote(proposalID uint64, voter sdk.AccAddress, option VoteOption) Vote {
|
|
|
|
return Vote{proposalID, voter, option}
|
|
|
|
}
|
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
func (v Vote) String() string {
|
2020-03-02 11:13:42 -08:00
|
|
|
out, _ := yaml.Marshal(v)
|
|
|
|
return string(out)
|
2019-01-22 09:28:39 -08:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Votes is a collection of Vote objects
|
2019-01-22 09:28:39 -08:00
|
|
|
type Votes []Vote
|
|
|
|
|
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 {
|
|
|
|
if !vote.Equal(other[i]) {
|
|
|
|
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 "[]"
|
|
|
|
}
|
2019-01-22 09:28:39 -08:00
|
|
|
out := fmt.Sprintf("Votes for Proposal %d:", v[0].ProposalID)
|
|
|
|
for _, vot := range v {
|
|
|
|
out += fmt.Sprintf("\n %s: %s", vot.Voter, vot.Option)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Empty returns whether a vote is empty.
|
2019-01-22 09:28:39 -08:00
|
|
|
func (v Vote) Empty() bool {
|
2020-03-02 12:50:20 -08:00
|
|
|
return v.Equal(Vote{})
|
2018-08-04 22:56:48 -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 {
|
|
|
|
return OptionEmpty, fmt.Errorf("'%s' is not a valid vote option", 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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|