Skip to content

Commit

Permalink
Merge pull request #61 from initia-labs/collect-account-from-evm-log
Browse files Browse the repository at this point in the history
collect addresses fromt transfer topic
  • Loading branch information
Vritra4 authored Sep 3, 2024
2 parents f3b0e30 + 541e16a commit 144ed45
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
37 changes: 31 additions & 6 deletions submodules/evm-tx/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"github.com/initia-labs/kvindexer/submodules/tx/types"
)

const (
lenTransferTopic = 4
transferTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
)

func (sm EvmTxSubmodule) finalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error {
sm.Logger(ctx).Debug("finalizeBlock", "submodule", types.SubmoduleName, "txs", len(req.Txs), "height", req.Height)

Expand Down Expand Up @@ -115,11 +120,11 @@ func grepAddressesFromTx(txr *sdk.TxResponse) ([]string, error) {

switch {
case event.Type == evmtypes.EventTypeEVM && attr.Key == evmtypes.AttributeKeyLog:
contractAddr, err := extractAddressesFromEVMLog(attr.Value)
contractAddrs, err := extractAddressesFromEVMLog(attr.Value)
if err != nil {
continue
}
addrs = append(addrs, contractAddr)
addrs = append(addrs, contractAddrs...)
case (event.Type == evmtypes.EventTypeCreate || event.Type == evmtypes.EventTypeCall) && attr.Key == evmtypes.AttributeKeyContract:
addr, err := convertContractAddressToBech32(attr.Value)
if err != nil {
Expand All @@ -144,12 +149,32 @@ func grepAddressesFromTx(txr *sdk.TxResponse) ([]string, error) {
return grepped, nil
}

func extractAddressesFromEVMLog(attrVal string) (string, error) {
func extractAddressesFromEVMLog(attrVal string) (addrs []string, err error) {
log := evmtypes.Log{}
if err := json.Unmarshal([]byte(attrVal), &log); err != nil {
return "", err
if err = json.Unmarshal([]byte(attrVal), &log); err != nil {
return
}
var addr string
addr, err = convertContractAddressToBech32(log.Address)
if err == nil {
addrs = append(addrs, addr)
}

// if the topic is about transfer, we need to extract the addresses from the topics.
// Topics[1] is the sender, Topics[2] is the receiver.
if log.Topics != nil && len(log.Topics) == lenTransferTopic {
if log.Topics[0] == transferTopic {
addr, err = convertContractAddressToBech32(log.Topics[1])
if err == nil {
addrs = append(addrs, addr)
}
addr, err = convertContractAddressToBech32(log.Topics[2])
if err == nil {
addrs = append(addrs, addr)
}
}
}
return convertContractAddressToBech32(log.Address)
return
}

func (sm EvmTxSubmodule) storeAccTxs(ctx context.Context, addr string, txHashes []string) error {
Expand Down
2 changes: 1 addition & 1 deletion submodules/evm-tx/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
SubmoduleName = "evm-tx"

// Version is the current version of the submodule
Version = "v0.1.0"
Version = "v0.1.1"
)

// store prefixes
Expand Down

0 comments on commit 144ed45

Please sign in to comment.