forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Wojtek
committed
Nov 17, 2023
1 parent
62a1b7f
commit 3ede61f
Showing
5 changed files
with
131 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package group | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/group" | ||
"github.com/forbole/bdjuno/v4/utils" | ||
juno "github.com/forbole/juno/v5/types" | ||
) | ||
|
||
// HandleMsg implements MessageModule | ||
func (m *Module) HandleMsg(index int, msg sdk.Msg, tx *juno.Tx) error { | ||
if len(tx.Events) == 0 { | ||
return nil | ||
} | ||
|
||
switch cosmosMsg := msg.(type) { | ||
case *group.MsgExec, | ||
*group.MsgCreateGroup, | ||
*group.MsgUpdateGroupMembers, | ||
*group.MsgSubmitProposal, | ||
*group.MsgUpdateGroupPolicyDecisionPolicy, | ||
*group.MsgUpdateGroupPolicyAdmin, | ||
*group.MsgLeaveGroup, | ||
*group.MsgCreateGroupWithPolicy, | ||
*group.MsgVote, | ||
*group.MsgUpdateGroupMetadata, | ||
*group.MsgUpdateGroupAdmin, | ||
*group.MsgCreateGroupPolicy, | ||
*group.MsgWithdrawProposal, | ||
*group.MsgUpdateGroupPolicyMetadata: | ||
return utils.HandleMessageWithAddresses(index, cosmosMsg, tx, m.messageParser, m.cdc, m.db) | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package utils | ||
|
||
import ( | ||
"strings" | ||
|
||
tmtypes "github.com/cometbft/cometbft/abci/types" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/forbole/bdjuno/v4/database" | ||
junomessages "github.com/forbole/juno/v5/modules/messages" | ||
juno "github.com/forbole/juno/v5/types" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/samber/lo" | ||
) | ||
|
||
// HandleMessageWithAddresses handle messages and takes addresses from events. | ||
func HandleMessageWithAddresses( | ||
index int, | ||
msg sdk.Msg, | ||
tx *juno.Tx, | ||
messageParser junomessages.MessageAddressesParser, | ||
cdc codec.Codec, db *database.Db, | ||
) error { | ||
addresses, err := collectAddresses(cdc, messageParser, msg, tx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// marshal the value properly | ||
bz, err := cdc.MarshalJSON(msg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return db.SaveMessage(juno.NewMessage( | ||
tx.TxHash, | ||
index, | ||
proto.MessageName(msg), | ||
string(bz), | ||
addresses, | ||
tx.Height, | ||
)) | ||
} | ||
|
||
func collectAddresses( | ||
cdc codec.Codec, | ||
messageParser junomessages.MessageAddressesParser, | ||
msg sdk.Msg, tx *juno.Tx, | ||
) ([]string, error) { | ||
// get the involved addresses with general parser first | ||
messageAddresses, err := messageParser(cdc, msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
addresses := make(map[string]struct{}) | ||
for _, address := range messageAddresses { | ||
addresses[address] = struct{}{} | ||
} | ||
// add address from event values | ||
addBech32EventValues(addresses, tx.Events) | ||
|
||
return lo.Keys(addresses), nil | ||
} | ||
|
||
func addBech32EventValues(addressSet map[string]struct{}, events []tmtypes.Event) { | ||
for _, ev := range sdk.StringifyEvents(events) { | ||
for _, attrItem := range ev.Attributes { | ||
address := strings.Trim(strings.TrimSpace(attrItem.Value), `"`) | ||
if !isBech32Address(address) { | ||
continue | ||
} | ||
addressSet[address] = struct{}{} | ||
} | ||
} | ||
} | ||
|
||
func isBech32Address(address string) bool { | ||
_, err := sdk.AccAddressFromBech32(address) | ||
return err == nil | ||
} |