0

I have a big array of emitted events that I retrieved using an archive node with Ethers queryFilter. I want to get the transaction parameters for each of those transactions.

I am using Ethers.js await provider.getTransaction(txHash); and passing a transaction hash which I have from my event data. Getting these one at a time from the archive node (Alchemy provider) is super slow and feels wrong.

Is there a way to batch fetch by passing say 1000s of transaction hashes at a time? Thinking it would be great to do this in the same way I batch fetch the events using an Ethers queryFilter.

1 Answer 1

0

Could not find a way to do it with Ethers.js.

According to Alchemy docs you can do up to 1000 calls in a batch. https://docs.alchemy.com/reference/batch-requests

Used node-fetch to build the Alchemy docs HTTP example in JS. This sped things up!

import fetch from 'node-fetch';

async function alchemyProviderBatch(method, param2dArray, alchemyProviderUrl) {
  if (param2dArray.length > 1000) {
    throw Error('Alchemy provider can only do batches of 1000 or fewer.');
  }

  const postBody = [];
  param2dArray.forEach((params, i) => {
    postBody.push({
      jsonrpc: "2.0",
      id: i,
      method, // "eth_getTransactionByHash"
      params, // [ "0xtransaction_hash" ]
    });
  });

  const response = await fetch(alchemyProviderUrl, {
    method: 'post',
    body: JSON.stringify(postBody),
    headers: { 'Content-Type': 'application/json' }
  });
  
  const data = await response.json();
  return data;
}

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.