ข้ามไปยังเนื้อหาหลัก

Ethers.js: How to send batch requests with Infura

Hello Infura frens 🙌

In this post, we will write a simple script using Web3.js and Infura for sending batch requests.

We will take advantage of Web3.js using the batch request method, you may find all the relevant details in the Web3.js documentation.

Please don’t forget to use your Infura API Key.

Our script will send 2 requests using eth_getBalance and eth_getTransactionCount and afterward print the results.

const { providers, JsonRpcBatchProvider } = require('ethers')

const infuraProvider = new providers.InfuraProvider('goerli', 'YOUR_INFURA_API_KEY')
const batchProvider = new JsonRpcBatchProvider(infuraProvider)

const requests = [
{
method: 'eth_getBalance',
params: ['0x742d35Cc6634C0532925a3b844Bc454e4438f44e', 'latest'],
},
{
method: 'eth_getTransactionCount',
params: ['0x742d35Cc6634C0532925a3b844Bc454e4438f44e', 'latest'],
},
]

batchProvider.send(requests).then(results => {
console.log(results)
})

I hope that this helps you, see you next time!

Was this helpful?
Connect MetaMask to provide feedback
What is this?
This is a trial feedback system that uses Verax to record your feedback as onchain attestations on Linea Mainnet. When you vote, submit a transaction in your wallet.