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
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Vote
|
|
|
|
type Vote struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"` // proposalID of the proposal
|
|
|
|
Voter sdk.AccAddress `json:"voter" yaml:"voter"` // address of the voter
|
|
|
|
Option VoteOption `json:"option" yaml:"option"` // option from OptionSet chosen by the voter
|
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 {
|
2019-04-30 09:31:38 -07:00
|
|
|
return fmt.Sprintf("voter %s voted with option %s on proposal %d", v.Voter, v.Option, v.ProposalID)
|
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
|
|
|
|
|
|
|
|
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
|
|
|
// Equals returns whether two votes are equal.
|
2019-01-22 09:28:39 -08:00
|
|
|
func (v Vote) Equals(comp Vote) bool {
|
2019-04-30 09:31:38 -07:00
|
|
|
return v.Voter.Equals(comp.Voter) &&
|
|
|
|
v.ProposalID == comp.ProposalID &&
|
|
|
|
v.Option == comp.Option
|
2018-08-04 22:56:48 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return v.Equals(Vote{})
|
2018-08-04 22:56:48 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// VoteOption defines a vote option
|
2018-07-10 17:59:07 -07:00
|
|
|
type VoteOption byte
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// Vote options
|
2018-07-10 17:59:07 -07:00
|
|
|
const (
|
|
|
|
OptionEmpty VoteOption = 0x00
|
|
|
|
OptionYes VoteOption = 0x01
|
|
|
|
OptionAbstain VoteOption = 0x02
|
|
|
|
OptionNo VoteOption = 0x03
|
|
|
|
OptionNoWithVeto VoteOption = 0x04
|
|
|
|
)
|
2018-06-21 17:19:14 -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) {
|
2018-06-21 17:19:14 -07:00
|
|
|
switch str {
|
|
|
|
case "Yes":
|
|
|
|
return OptionYes, nil
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
case "Abstain":
|
|
|
|
return OptionAbstain, nil
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
case "No":
|
|
|
|
return OptionNo, nil
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
case "NoWithVeto":
|
|
|
|
return OptionNoWithVeto, nil
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
default:
|
2019-02-08 11:37:46 -08:00
|
|
|
return VoteOption(0xff), fmt.Errorf("'%s' is not a valid vote option", str)
|
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
|
|
|
// Marshals to JSON using string.
|
2018-07-10 17:59:07 -07:00
|
|
|
func (vo VoteOption) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(vo.String())
|
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// UnmarshalJSON decodes from JSON assuming Bech32 encoding.
|
2018-07-10 17:59:07 -07:00
|
|
|
func (vo *VoteOption) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(data, &s)
|
|
|
|
if err != nil {
|
2019-03-06 03:38:18 -08:00
|
|
|
return err
|
2018-06-27 16:49:59 -07:00
|
|
|
}
|
2018-07-10 17:59:07 -07:00
|
|
|
|
|
|
|
bz2, err := VoteOptionFromString(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2018-07-10 17:59:07 -07:00
|
|
|
*vo = bz2
|
|
|
|
return nil
|
2018-06-27 16:49:59 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
// String implements the Stringer interface.
|
2018-07-10 17:59:07 -07:00
|
|
|
func (vo VoteOption) String() string {
|
|
|
|
switch vo {
|
|
|
|
case OptionYes:
|
|
|
|
return "Yes"
|
|
|
|
case OptionAbstain:
|
|
|
|
return "Abstain"
|
|
|
|
case OptionNo:
|
|
|
|
return "No"
|
|
|
|
case OptionNoWithVeto:
|
|
|
|
return "NoWithVeto"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
2018-06-21 17:19:14 -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
|
|
|
}
|
|
|
|
}
|