2019-04-30 09:31:38 -07:00
|
|
|
package types
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
import (
|
2020-10-27 04:50:11 -07:00
|
|
|
"encoding/json"
|
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
|
|
|
|
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
|
2020-09-25 03:25:37 -07:00
|
|
|
//nolint:interfacer
|
2020-10-27 04:50:11 -07:00
|
|
|
func NewVote(proposalID uint64, voter sdk.AccAddress, subvotes []SubVote) Vote {
|
|
|
|
return Vote{proposalID, voter.String(), subvotes}
|
2019-06-04 11:38:11 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
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-27 04:50:11 -07:00
|
|
|
out += fmt.Sprintf("\n %s: %s", vot.Voter, vot.SubVotes)
|
2019-01-22 09:28:39 -08:00
|
|
|
}
|
|
|
|
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-09-25 03:25:37 -07:00
|
|
|
return v.String() == Vote{}.String()
|
2018-08-04 22:56:48 -07:00
|
|
|
}
|
|
|
|
|
2020-10-27 04:50:11 -07:00
|
|
|
// NewSubVote creates a new Vote instance
|
|
|
|
//nolint:interfacer
|
|
|
|
func NewSubVote(option VoteOption, rate int64) SubVote {
|
|
|
|
return SubVote{option, sdk.NewDec(rate)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v SubVote) String() string {
|
|
|
|
out, _ := json.Marshal(v)
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2020-10-28 05:05:35 -07:00
|
|
|
// SubVotes describes array of SubVote
|
|
|
|
type SubVotes []SubVote
|
|
|
|
|
|
|
|
func (v SubVotes) String() string {
|
|
|
|
out, _ := json.Marshal(v)
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2020-10-27 04:50:11 -07:00
|
|
|
// ValidSubVote returns true if the sub vote is valid and false otherwise.
|
|
|
|
func ValidSubVote(subvote SubVote) bool {
|
|
|
|
if !subvote.Rate.IsPositive() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return ValidVoteOption(subvote.Option)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-29 02:26:51 -07:00
|
|
|
// SubVotesFromString returns a SubVotes from a string. It returns an error
|
|
|
|
// if the string is invalid.
|
|
|
|
func SubVotesFromString(str string) (SubVotes, error) {
|
2020-10-29 05:30:42 -07:00
|
|
|
subvotes := SubVotes{}
|
2020-10-29 02:26:51 -07:00
|
|
|
for _, subvote := range strings.Split(str, ",") {
|
|
|
|
fields := strings.Split(subvote, "=")
|
|
|
|
option, err := VoteOptionFromString(fields[0])
|
|
|
|
if err != nil {
|
|
|
|
return subvotes, err
|
|
|
|
}
|
|
|
|
if len(fields) < 2 {
|
|
|
|
return subvotes, fmt.Errorf("rate field does not exist for %s opion", fields[0])
|
|
|
|
}
|
|
|
|
rate, err := sdk.NewDecFromStr(fields[1])
|
|
|
|
if err != nil {
|
|
|
|
return subvotes, err
|
|
|
|
}
|
|
|
|
subvotes = append(subvotes, SubVote{
|
|
|
|
option,
|
|
|
|
rate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return subvotes, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|