Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect addresses fromt transfer topic #61

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading