2018-06-21 17:19:14 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Type that represents VoteOption as a byte
|
|
|
|
type VoteOption = byte
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
const (
|
|
|
|
OptionEmpty VoteOption = 0x00
|
|
|
|
OptionYes VoteOption = 0x01
|
|
|
|
OptionAbstain VoteOption = 0x02
|
|
|
|
OptionNo VoteOption = 0x03
|
|
|
|
OptionNoWithVeto VoteOption = 0x04
|
|
|
|
)
|
|
|
|
|
|
|
|
// Vote
|
|
|
|
type Vote struct {
|
|
|
|
Voter sdk.Address `json:"voter"` // address of the voter
|
|
|
|
ProposalID int64 `json:"proposal_id"` // proposalID of the proposal
|
|
|
|
Option VoteOption `json:"option"` // option from OptionSet chosen by the voter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deposit
|
|
|
|
type Deposit struct {
|
2018-06-27 16:49:59 -07:00
|
|
|
Depositer sdk.Address `json:"depositer"` // Address of the depositer
|
|
|
|
ProposalID int64 `json:"proposal_id"` // proposalID of the proposal
|
|
|
|
Amount sdk.Coins `json:"amount"` // Deposit amount
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProposalTypeToString for pretty prints of ProposalType
|
|
|
|
func VoteOptionToString(option VoteOption) string {
|
|
|
|
switch option {
|
|
|
|
case OptionYes:
|
|
|
|
return "Yes"
|
|
|
|
case OptionAbstain:
|
|
|
|
return "Abstain"
|
|
|
|
case OptionNo:
|
|
|
|
return "No"
|
|
|
|
case OptionNoWithVeto:
|
|
|
|
return "NoWithVeto"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validVoteOption(option VoteOption) bool {
|
|
|
|
if option == OptionYes ||
|
|
|
|
option == OptionAbstain ||
|
|
|
|
option == OptionNo ||
|
|
|
|
option == OptionNoWithVeto {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// String to proposalType byte. Returns ff if invalid.
|
|
|
|
func StringToVoteOption(str string) (VoteOption, sdk.Error) {
|
|
|
|
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), ErrInvalidVote(DefaultCodespace, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
2018-06-27 16:49:59 -07:00
|
|
|
// REST
|
|
|
|
|
|
|
|
// Rest Deposits
|
|
|
|
type DepositRest struct {
|
2018-07-05 13:36:51 -07:00
|
|
|
Depositer sdk.Address `json:"depositer"` // address of the depositer
|
|
|
|
ProposalID int64 `json:"proposal_id"` // proposalID of the proposal
|
|
|
|
Amount sdk.Coins `json:"option"`
|
2018-06-27 16:49:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Turn any Deposit to a DepositRest
|
|
|
|
func DepositToRest(deposit Deposit) DepositRest {
|
|
|
|
return DepositRest{
|
2018-07-05 13:36:51 -07:00
|
|
|
Depositer: deposit.Depositer,
|
2018-06-27 16:49:59 -07:00
|
|
|
ProposalID: deposit.ProposalID,
|
|
|
|
Amount: deposit.Amount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// Rest Votes
|
|
|
|
type VoteRest struct {
|
2018-07-05 13:36:51 -07:00
|
|
|
Voter sdk.Address `json:"voter"` // address of the voter
|
|
|
|
ProposalID int64 `json:"proposal_id"` // proposalID of the proposal
|
|
|
|
Option string `json:"option"`
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2018-06-27 16:49:59 -07:00
|
|
|
// Turn any Vote to a VoteRest
|
2018-06-21 17:19:14 -07:00
|
|
|
func VoteToRest(vote Vote) VoteRest {
|
|
|
|
return VoteRest{
|
2018-07-05 13:36:51 -07:00
|
|
|
Voter: vote.Voter,
|
2018-06-21 17:19:14 -07:00
|
|
|
ProposalID: vote.ProposalID,
|
|
|
|
Option: VoteOptionToString(vote.Option),
|
|
|
|
}
|
|
|
|
}
|