-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd8fd54
commit 70c0cae
Showing
10 changed files
with
163 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package responses | ||
|
||
import ( | ||
"main/pkg/types" | ||
"strconv" | ||
) | ||
|
||
// cosmos/gov/v1beta1/proposals/:id/votes/:wallet | ||
|
||
type Vote struct { | ||
ProposalID string `json:"proposal_id"` | ||
Voter string `json:"voter"` | ||
Option string `json:"option"` | ||
Options []VoteOption `json:"options"` | ||
} | ||
|
||
type VoteOption struct { | ||
Option string `json:"option"` | ||
Weight string `json:"weight"` | ||
} | ||
|
||
type VoteRPCResponse struct { | ||
Code int64 `json:"code"` | ||
Message string `json:"message"` | ||
Vote *Vote `json:"vote"` | ||
} | ||
|
||
func (v VoteRPCResponse) IsError() bool { | ||
return v.Code != 0 | ||
} | ||
|
||
func (v VoteRPCResponse) ToVote() (*types.Vote, error) { | ||
var options []types.VoteOption | ||
|
||
if len(v.Vote.Options) > 0 { | ||
options = make([]types.VoteOption, len(v.Vote.Options)) | ||
|
||
for index, option := range v.Vote.Options { | ||
weight, err := strconv.ParseFloat(option.Weight, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
options[index] = types.VoteOption{ | ||
Option: types.VoteType(option.Option), | ||
Weight: weight, | ||
} | ||
} | ||
} else { | ||
options = make([]types.VoteOption, 1) | ||
options[0] = types.VoteOption{ | ||
Option: types.VoteType(v.Vote.Option), | ||
Weight: 1, | ||
} | ||
} | ||
|
||
return &types.Vote{ | ||
ProposalID: v.Vote.ProposalID, | ||
Voter: v.Vote.Voter, | ||
Options: options, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package types | ||
|
||
import ( | ||
"main/pkg/utils" | ||
"strings" | ||
) | ||
|
||
// cosmos/gov/v1beta1/proposals/:id/votes/:wallet | ||
|
||
type VoteType string | ||
|
||
func (v VoteType) Resolve() string { | ||
votes := map[string]string{ | ||
"VOTE_OPTION_YES": "Yes", | ||
"VOTE_OPTION_ABSTAIN": "Abstain", | ||
"VOTE_OPTION_NO": "No", | ||
"VOTE_OPTION_NO_WITH_VETO": "No with veto", | ||
} | ||
|
||
if vote, ok := votes[string(v)]; ok && v != "" { | ||
return vote | ||
} | ||
|
||
return string(v) | ||
} | ||
|
||
type VoteOption struct { | ||
Option VoteType | ||
Weight float64 | ||
} | ||
type VoteOptions []VoteOption | ||
|
||
type Vote struct { | ||
ProposalID string | ||
Voter string | ||
Options VoteOptions | ||
} | ||
|
||
func (v Vote) ResolveVote() string { | ||
optionsStrings := utils.Map(v.Options, func(v VoteOption) string { | ||
return v.Option.Resolve() | ||
}) | ||
|
||
return strings.Join(optionsStrings, ", ") | ||
} | ||
|
||
func (v Vote) VotesEquals(other *Vote) bool { | ||
if len(v.Options) != len(other.Options) { | ||
return false | ||
} | ||
|
||
for index, option := range v.Options { | ||
if option.Weight != other.Options[index].Weight || option.Option != other.Options[index].Option { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
Oops, something went wrong.