I am working on a project where I need to retrieve all transactions associated with a specific method within a given block or set of blocks. As an example, Etherscan provides this functionality through their advanced filter feature, allowing users to filter transactions based on a particular method such as the "Approve" method in ERC20 contracts.

Could anyone guide me on how to implement a similar filter to Etherscan's, to find transactions in Ethereum? While I am coding in JavaScript, I am open to solutions in other languages and can translate them myself.

Thank you for your assistance!

1 Answer 1


You can filter Ethereum transactions by method ID(i.e. the function signature hash) using ethers.js to fetch and then iterate through transactions in specific blocks. Below is a simplified example:

const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('YOUR_PROVIDER_URL');

const methodId = '0x095ea7b3'; // Example for ERC20 approve method

async function getTransactionsByMethodId(blockNumber) {
  const block = await provider.getBlockWithTransactions(blockNumber);

  const filteredTransactions = block.transactions.filter((tx) => {
    return tx.data.startsWith(methodId);

  return filteredTransactions;

getTransactionsByMethodId(YOUR_BLOCK_NUMBER).then((transactions) => {

Replace YOUR_PROVIDER_URL and YOUR_BLOCK_NUMBER with the appropriate values. Please note that this code will only detect transactions where the specified method ID is at the start of the transaction data. It will NOT be able to detect nested contract calls or instances where the method ID is part of other data fields(e.g. in a multicall).

Alternative Approach: Filtering by Event Logs:

If you are certain that a particular function/method emits a specific event log, you can use the eth_getLogs JSON-RPC method to filter for those events.

Here's a simplified example using ethers.js to filter logs:

const filter = {
  topics: [ethers.utils.id('EventName(paramType1,paramType2,...)')],
  fromBlock: YOUR_START_BLOCK,

provider.getLogs(filter).then((logs) => {
  logs.forEach((log) => {

This approach detects all calls(including nested calls) to a specific function/method provided it emits a particular event.

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.