diff --git a/submodules/evm-tx/collect.go b/submodules/evm-tx/collect.go index fb27a6a..f87d149 100644 --- a/submodules/evm-tx/collect.go +++ b/submodules/evm-tx/collect.go @@ -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) @@ -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 { @@ -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 { diff --git a/submodules/evm-tx/types/keys.go b/submodules/evm-tx/types/keys.go index 692c0a0..d0aa95d 100644 --- a/submodules/evm-tx/types/keys.go +++ b/submodules/evm-tx/types/keys.go @@ -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