-
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 msgs
- Loading branch information
Showing
5 changed files
with
168 additions
and
49 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,63 @@ | ||
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/bdjuno/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") | ||
} | ||
|
||
// VoteOptionFromEvents returns the vote option from the given events | ||
func VoteOptionFromEvents(events sdk.StringEvents) (govtypesv1.VoteOption, error) { | ||
for _, event := range events { | ||
attribute, ok := eventsutil.FindAttributeByKey(event, govtypes.AttributeKeyOption) | ||
if ok { | ||
return parseVoteOption(attribute.Value) | ||
} | ||
} | ||
|
||
return 0, fmt.Errorf("no vote option found") | ||
} | ||
|
||
// parseVoteOption returns the vote option from the given string | ||
// option value in string could be 2 cases, for example: | ||
// 1. "{\"option\":1,\"weight\":\"1.000000000000000000\"}" | ||
// 2. "option:VOTE_OPTION_NO weight:\"1.000000000000000000\"" | ||
func parseVoteOption(optionValue string) (govtypesv1.VoteOption, error) { | ||
// try parse option value as json | ||
type voteOptionJSON struct { | ||
Option govtypesv1.VoteOption `json:"option"` | ||
} | ||
var voteOptionParsedJSON voteOptionJSON | ||
err := json.Unmarshal([]byte(optionValue), &voteOptionParsedJSON) | ||
if err == nil { | ||
return voteOptionParsedJSON.Option, nil | ||
} | ||
|
||
// try parse option value as string | ||
// option:VOTE_OPTION_NO weight:"1.000000000000000000" | ||
voteOptionParsed := strings.Split(optionValue, " ") | ||
voteOption, err := govtypesv1.VoteOptionFromString(strings.ReplaceAll(voteOptionParsed[0], "option:", "")) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to parse vote option %s: %s", optionValue, err) | ||
} | ||
|
||
return voteOption, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package events | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// FindEventByType returns the event with the given type | ||
func FindEventByType(events sdk.StringEvents, eventType string) (sdk.StringEvent, bool) { | ||
for _, event := range events { | ||
if event.Type == eventType { | ||
return event, true | ||
} | ||
} | ||
return sdk.StringEvent{}, false | ||
} | ||
|
||
// FindAttributeByKey returns the attribute with the given key | ||
func FindAttributeByKey(event sdk.StringEvent, key string) (sdk.Attribute, bool) { | ||
for _, attribute := range event.Attributes { | ||
if attribute.Key == key { | ||
return attribute, true | ||
} | ||
} | ||
return sdk.Attribute{}, false | ||
} |