1

Blockchain explorers show "transfer" instead of the method id for some of the transactions on their list, so there should be a way to filter transactions based on the method they call. How can I filter transaction based on being transfer/non-transfer ones? I guess I can use

web3.eth.abi.encodeFunctionSignature 

Not sure how it can be done though.

I am aware that debug.traceTransaction is a possibility, however I'm looking for a less costly way to achieve this.

1 Answer 1

4

The function identifier is what will allow you to define transfer vs non transfer function.

Basically, it is the first 4 bytes of the call data.

Take this transaction which did a USDC transfer.

The first 4 bytes of the call data are : 0xa9059cbb. Those are matching the first 4 bytes of keccak256("transfer(address, uint256)") : 0xa9059cbb....

So you can identify every transfer transaction if the first 4 bytes of call data match that value.

The following code does exactly this on the tx that I linked previously :

import web3 from "web3";

const Web3 = new web3("https://mainnet.infura.io/v3/PROJECT-ID");

const transferIdentifier = Web3.eth.abi.encodeFunctionSignature("transfer(address,uint256)");

const tx = await Web3.eth.getTransaction("0x87f86d88cc3461b0ed120979bc7c1c47fd3dd8638cc3a4632f02ace629920678");

if (tx.input.slice(0, transferIdentifier.length) === transferIdentifier) {
  console.log("This is a transfer");
}

Hope that helps !

EDIT: This might be usefull for you if you want to identify a wider range of calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.