This Recipe shows how to send batch requests to your Chainstack node instead of sending multiple loop-based requests. Adopting this approach can substantially boost the performance of your DApp.
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.
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
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 does the following:
ethers.toQuantity()
method. All these request objects are stored in pastBlocksPromises._send()
method on the provider sends all these requests in a batch.pastBlocks
.Get your Chainstack endpoint
You will need a Chainstack account and an Ethereum node to run this 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
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.
This Recipe shows how to send batch requests to your Chainstack node instead of sending multiple loop-based requests. Adopting this approach can substantially boost the performance of your DApp.
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.
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
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 does the following:
ethers.toQuantity()
method. All these request objects are stored in pastBlocksPromises._send()
method on the provider sends all these requests in a batch.pastBlocks
.Get your Chainstack endpoint
You will need a Chainstack account and an Ethereum node to run this 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
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.