-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: re-add support for gov v1beta1 messages (#725)
## Description Closes: #XXXX Currently, gov module is updated to gov v1, however, gov v1beta1 is still alive to chains, Cosmos-SDK doesn't completely remove v1beta1 support. As a result, we should re-add gov v1beta1 support to make sure our proposals data are also updated when gov v1beta1 messages are performed. Say, some of users on Cheqd is using gov.v1beta1.MsgVote to vote on the proposal. https://bigdipper.live/cheqd/transactions/19E4F068FBFBC46DA9BEF3E0F7CA908317D5582CAB2A17683138A5248ED34E3C <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch - [ ] provided a link to the relevant issue or specification - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
Showing
6 changed files
with
264 additions
and
90 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
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,66 @@ | ||
package gov | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" | ||
eventsutil "github.com/forbole/callisto/v4/utils/events" | ||
) | ||
|
||
// ProposalIDFromEvent returns the proposal id from the given events | ||
func ProposalIDFromEvents(events sdk.StringEvents) (uint64, error) { | ||
for _, event := range events { | ||
attribute, ok := eventsutil.FindAttributeByKey(event, govtypes.AttributeKeyProposalID) | ||
if ok { | ||
return strconv.ParseUint(attribute.Value, 10, 64) | ||
} | ||
} | ||
|
||
return 0, fmt.Errorf("no proposal id found") | ||
} | ||
|
||
// WeightVoteOptionFromEvents returns the vote option from the given events | ||
func WeightVoteOptionFromEvents(events sdk.StringEvents) (govtypesv1.WeightedVoteOption, error) { | ||
for _, event := range events { | ||
attribute, ok := eventsutil.FindAttributeByKey(event, govtypes.AttributeKeyOption) | ||
if ok { | ||
return parseWeightVoteOption(attribute.Value) | ||
} | ||
} | ||
|
||
return govtypesv1.WeightedVoteOption{}, fmt.Errorf("no vote option found") | ||
} | ||
|
||
// parseWeightVoteOption returns the vote option from the given string | ||
// option value in string has 2 cases, for example: | ||
// 1. "{\"option\":1,\"weight\":\"1.000000000000000000\"}" | ||
// 2. "option:VOTE_OPTION_NO weight:\"1.000000000000000000\"" | ||
func parseWeightVoteOption(optionValue string) (govtypesv1.WeightedVoteOption, error) { | ||
// try parse json option value | ||
var weightedVoteOption govtypesv1.WeightedVoteOption | ||
err := json.Unmarshal([]byte(optionValue), &weightedVoteOption) | ||
if err == nil { | ||
return weightedVoteOption, nil | ||
} | ||
|
||
// try parse string option value | ||
// option:VOTE_OPTION_NO weight:"1.000000000000000000" | ||
voteOptionParsed := strings.Split(optionValue, " ") | ||
if len(voteOptionParsed) != 2 { | ||
return govtypesv1.WeightedVoteOption{}, fmt.Errorf("failed to parse vote option %s", optionValue) | ||
} | ||
|
||
voteOption, err := govtypesv1.VoteOptionFromString(strings.ReplaceAll(voteOptionParsed[0], "option:", "")) | ||
if err != nil { | ||
return govtypesv1.WeightedVoteOption{}, fmt.Errorf("failed to parse vote option %s: %s", optionValue, err) | ||
} | ||
weight := strings.ReplaceAll(voteOptionParsed[1], "weight:", "") | ||
weight = strings.ReplaceAll(weight, "\"", "") | ||
|
||
return govtypesv1.WeightedVoteOption{Option: voteOption, Weight: weight}, nil | ||
} |
Oops, something went wrong.