Skip to content

Commit

Permalink
Parse addresses in events
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtek committed Nov 17, 2023
1 parent 62a1b7f commit 3ede61f
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 59 deletions.
35 changes: 35 additions & 0 deletions modules/group/handle_msg.go
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
}
17 changes: 12 additions & 5 deletions modules/group/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/forbole/bdjuno/v4/database"
"github.com/forbole/juno/v5/modules"
junomessages "github.com/forbole/juno/v5/modules/messages"
)

var _ modules.Module = &Module{}
var (
_ modules.Module = &Module{}
_ modules.MessageModule = &Module{}
)

// Module represents the x/asset/ft module
type Module struct {
cdc codec.Codec
db *database.Db
cdc codec.Codec
db *database.Db
messageParser junomessages.MessageAddressesParser
}

// NewModule returns a new Module instance
func NewModule(
messageParser junomessages.MessageAddressesParser,
cdc codec.Codec, db *database.Db,
) *Module {
return &Module{
cdc: cdc,
db: db,
cdc: cdc,
db: db,
messageParser: messageParser,
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (r *Registrar) BuildModules(ctx registrar.Context) jmodules.Modules {
pricefeed.NewModule(ctx.JunoConfig, cdc, db),
slashingModule,
stakingModule,
group.NewModule(cdc, db),
group.NewModule(r.parser, cdc, db),
upgradeModule,
feeModelModule,
customParamsModule,
Expand Down
55 changes: 2 additions & 53 deletions modules/wasm/handle_msg.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package wasm

import (
"strings"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
tmtypes "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/bdjuno/v4/utils"
juno "github.com/forbole/juno/v5/types"
"github.com/gogo/protobuf/proto"
"github.com/samber/lo"
)

// HandleMsg implements MessageModule
Expand All @@ -22,55 +18,8 @@ func (m *Module) HandleMsg(index int, msg sdk.Msg, tx *juno.Tx) error {
*wasmtypes.MsgInstantiateContract2,
*wasmtypes.MsgMigrateContract,
*wasmtypes.MsgExecuteContract:
return m.handleWasmRelatedAddress(index, cosmosMsg, tx)
return utils.HandleMessageWithAddresses(index, cosmosMsg, tx, m.messageParser, m.cdc, m.db)
}

return nil
}

func (m *Module) handleWasmRelatedAddress(index int, msg sdk.Msg, tx *juno.Tx) error {
// get the involved addresses with general parser first
messageAddresses, err := m.messageParser(m.cdc, msg)
if err != nil {
return err
}

addresses := make(map[string]struct{})
for _, address := range messageAddresses {
addresses[address] = struct{}{}
}
// add address from event values
m.addBech32EventValues(addresses, tx.Events)

// marshal the value properly
bz, err := m.cdc.MarshalJSON(msg)
if err != nil {
return err
}

return m.db.SaveMessage(juno.NewMessage(
tx.TxHash,
index,
proto.MessageName(msg),
string(bz),
lo.Keys(addresses),
tx.Height,
))
}

func (m *Module) 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 !m.isBech32Address(address) {
continue
}
addressSet[address] = struct{}{}
}
}
}

func (m *Module) isBech32Address(address string) bool {
_, err := sdk.AccAddressFromBech32(address)
return err == nil
}
81 changes: 81 additions & 0 deletions utils/addresses.go
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
}

0 comments on commit 3ede61f

Please sign in to comment.