cosmos-sdk/x/gov/depositsvotes.go

175 lines
4.0 KiB
Go
Raw Normal View History

2018-06-21 17:19:14 -07:00
package gov
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 {
2018-07-06 00:06:53 -07:00
Voter sdk.AccAddress `json:"voter"` // address of the voter
ProposalID uint64 `json:"proposal_id"` // proposalID of the proposal
2018-07-06 00:06:53 -07:00
Option VoteOption `json:"option"` // option from OptionSet chosen by the voter
2018-06-21 17:19:14 -07:00
}
func (v Vote) String() string {
return fmt.Sprintf("Voter %s voted with option %s on proposal %d", v.Voter, v.Option, v.ProposalID)
}
// Votes is a collection of Vote
type Votes []Vote
func (v Votes) String() string {
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
}
2018-08-04 22:56:48 -07:00
// Returns whether 2 votes are equal
func (v Vote) Equals(comp Vote) bool {
return v.Voter.Equals(comp.Voter) && v.ProposalID == comp.ProposalID && v.Option == comp.Option
2018-08-04 22:56:48 -07:00
}
// Returns whether a vote is empty
func (v Vote) Empty() bool {
return v.Equals(Vote{})
2018-08-04 22:56:48 -07:00
}
2018-06-21 17:19:14 -07:00
// Deposit
type Deposit struct {
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
ProposalID uint64 `json:"proposal_id"` // proposalID of the proposal
2018-07-06 00:06:53 -07:00
Amount sdk.Coins `json:"amount"` // Deposit amount
2018-06-21 17:19:14 -07:00
}
func (d Deposit) String() string {
return fmt.Sprintf("Deposit by %s on Proposal %d is for the amount %s",
d.Depositor, d.ProposalID, d.Amount)
}
// Deposits is a collection of depoist
type Deposits []Deposit
func (d Deposits) String() string {
2019-02-08 18:33:06 -08:00
if len(d) == 0 {
return "[]"
}
out := fmt.Sprintf("Deposits for Proposal %d:", d[0].ProposalID)
for _, dep := range d {
out += fmt.Sprintf("\n %s: %s", dep.Depositor, dep.Amount)
}
return out
}
2018-08-04 22:56:48 -07:00
// Returns whether 2 deposits are equal
func (d Deposit) Equals(comp Deposit) bool {
return d.Depositor.Equals(comp.Depositor) && d.ProposalID == comp.ProposalID && d.Amount.IsEqual(comp.Amount)
2018-08-04 22:56:48 -07:00
}
// Returns whether a deposit is empty
func (d Deposit) Empty() bool {
return d.Equals(Deposit{})
2018-08-04 22:56:48 -07:00
}
2018-07-10 17:59:07 -07:00
// Type that represents VoteOption as a byte
type VoteOption byte
2018-06-21 17:19:14 -07:00
2018-07-10 17:59:07 -07:00
//nolint
const (
OptionEmpty VoteOption = 0x00
OptionYes VoteOption = 0x01
OptionAbstain VoteOption = 0x02
OptionNo VoteOption = 0x03
OptionNoWithVeto VoteOption = 0x04
)
2018-06-21 17:19:14 -07:00
// String to proposalType byte. Returns ff if 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
case "Abstain":
return OptionAbstain, nil
case "No":
return OptionNo, nil
case "NoWithVeto":
return OptionNoWithVeto, nil
default:
return VoteOption(0xff), fmt.Errorf("'%s' is not a valid vote option", str)
2018-07-10 17:59:07 -07:00
}
}
// Is defined VoteOption
func validVoteOption(option VoteOption) bool {
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
}
2018-07-10 17:59:07 -07:00
// Marshal needed for protobuf compatibility
func (vo VoteOption) Marshal() ([]byte, error) {
return []byte{byte(vo)}, nil
}
2018-07-10 17:59:07 -07:00
// Unmarshal needed for protobuf compatibility
func (vo *VoteOption) Unmarshal(data []byte) error {
*vo = VoteOption(data[0])
return nil
}
2018-07-10 17:59:07 -07:00
// Marshals to JSON using string
func (vo VoteOption) MarshalJSON() ([]byte, error) {
return json.Marshal(vo.String())
}
// Unmarshals from JSON assuming Bech32 encoding
func (vo *VoteOption) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
2018-07-10 17:59:07 -07:00
bz2, err := VoteOptionFromString(s)
if err != nil {
return err
}
*vo = bz2
return nil
}
2018-07-10 17:59:07 -07:00
// Turns VoteOption byte to String
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
}
2018-07-10 17:59:07 -07:00
// For Printf / Sprintf, returns bech32 when using %s
2018-08-31 15:22:37 -07:00
// nolint: errcheck
2018-07-10 17:59:07 -07:00
func (vo VoteOption) Format(s fmt.State, verb rune) {
switch verb {
case 's':
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
}
}