Overview
Overview
HTTP batch request is a feature most Ethereum clients support. With batch requests enabled, multiple HTTP requests can be packaged into one single request and sent to the server. The server then processes this bulk and returns a bulk result.All of these requests are done in a single round trip.This feature can be useful for reducing the load on the server and improving the performance. Here you will learn how to do it using ether.js V6.
Batch response ordering: The JSON-RPC specification does not guarantee that batch responses will be returned in the same order as requests. Always use the
id
field to match responses with their corresponding requests. This recipe uses ethers.js v6, which correctly handles response ordering. If you’re using ethers.js v5, be aware of this ordering issue.Environment setup
Environment setup
Install node.js in case it is not installed yet.Create a new directory for your project, then install the web3.js and ethers.js libraries:
npm install ethers
Batch requests with ethers
Batch requests with ethers
The
JsonRpcProvider
in ethers
can accept a single request or an array of requests using the _send
method.In this example, the script gets the details of the last 20 blocks using a batch.The code
The code
The code does the following:
- An ethers provider is initialized using your Chainstack endpoint.
- The latest block number on the blockchain is fetched using the getBlockNumber method.
- The current time is recorded in milliseconds.
- An array of size 20 is created, and each entry corresponds to a block number, starting from the latest block and going back by 20 blocks.
- A JSON-RPC request object is created for each block number to fetch block details. These request objects are made using the eth_getBlockByNumber method, and the block numbers are converted to hexadecimal strings using the
ethers.toQuantity()
method. All these request objects are stored in pastBlocksPromises. - The
_send()
method on the provider sends all these requests in a batch. - The responses are stored in
pastBlocks
. - The current time is re-recorded, and the execution time for these operations is logged to the console.
- The pastBlocks array containing the block details is returned.
Get your Chainstack endpoint
Get your Chainstack endpoint
You will need a Chainstack account and an Ethereum node to run this code.
Run the code
Run the code
Now you can get your endpoint and paste it into the
endpoint
const. Then you can run it with node YOUR_SCRIPT_NAME
The console will log the time needed to execute the batch, meaning sending the requests and receiving the responses; it then logs block numbers and hashes of the last 20 blocks.Understanding the response
Understanding the response
As you can see in the console, it took about 800ms to get the responses for all 20 blocks, which is a significant improvement compared to sending each request using a
for
loop.
For context, during the development of this Recipe, a regular for
loop would take about 10/12 seconds.