Both HTTP batch requests and multicall contracts can bundle multiple calls to reduce client-server overhead and improve response times.
In the tests, HTTP batch requests often slightly outperformed multicall contracts, but both outperformed sending multiple single requests by a significant margin.
Batch requests still count each call against your RPC usage, whereas multicall typically counts as a single request. However, multicall requires extra contract deployment and can lead to “request timeout” or “response size too big” errors for more complex calls.
The best choice depends on your specific use cases, desired simplicity, and performance requirements.
HTTP batch request is a feature most Ethereum clients support, for example, Geth. With batch requests enabled, multiple HTTP requests can be packaged into one single request and sent to the server. Server process this bulk request and returns a bulk result. All of these are done in a single round trip.
This feature can be useful for reducing the load on the server and improving the performance to a certain extent.
Popular Web3 libraries like web3.js and ethers.js support this feature too. Below is an example of getting ether’s balance from multiple accounts using web3.js.
Run npm i web3 to install web3.js. The code in this guide is compatible with web3.js V4.
Copy
const { Web3 } = require("web3");const NODE_URL = "YOUR_CHAINSTACK_ENDPOINT";const web3 = new Web3(NODE_URL);const addresses = [ "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326", "0x2bB42C655DdDe64EE508a0Bf29f9D1c6150Bee5F",];async function getBalances() { const startTime = Date.now(); // Create a batch request object const batch = new web3.BatchRequest(); // Array to hold promises for each request const promises = []; // Loop through each address and add a getBalance request to the batch addresses.forEach((address, index) => { const request = { jsonrpc: "2.0", id: index + 1, method: "eth_getBalance", params: [address, "latest"], }; // Add request to the batch and store the promise const requestPromise = batch.add(request); promises.push(requestPromise); }); // Send the batch request and wait for all responses const responses = await batch.execute(); // Process responses responses.forEach((response, index) => { if (response.error) { console.error(response.error); } else { const balance = response.result; const timeFromStart = Date.now() - startTime; console.log( `${addresses[index]} has a balance of ${Number( web3.utils.fromWei(balance, "ether") ).toFixed(3)} ETH retrieved in: ${timeFromStart / 1000} seconds.` ); } });}getBalances();
The getBalances function creates a new BatchRequest object using web3.BatchRequest().
The function then loops through each address in the addresses array and creates a new request to get the balance of that address using web3.eth.getBalance.request(). It adds each request to the batch using batch.add().
Finally, the function executes the batch request using batch.execute(). When executed, the requests in the batch are sent to the Ethereum network simultaneously, and the callback functions are executed when the responses are received.
A multicall contract is a smart contract that takes in the function call objects as parameters and executes them together. A developer can use the multicall contract as a proxy to call other contracts on Ethereum.
The implementation of a multicall contract is, in fact, very simple: it leverages Solidity’s call function to broadcast contract calls. This is a sample implementation of multicall’s aggregate function:
Copy
function aggregate(Call[] memory calls) public returns (uint256 blockNumber, bytes[] memory returnData) { blockNumber = block.number; returnData = new bytes[](calls.length); for(uint256 i = 0; i < calls.length; i++) { (bool success, bytes memory ret) = calls[i].target.call(calls[i].callData); require(success); returnData[i] = ret; } }
In summary, this function takes an array of Call, calls each one, and returns an array of the results along with the block number in which the function was called. It is designed to be used as a general-purpose aggregator for calling other contracts on the Ethereum blockchain.
Below is an example calling the smart contract with MakerDAO’s helper library multicall.js; it essentially does the same thing as the previous example:
Copy
const multicall = require("@makerdao/multicall")const config = { rpcUrl: "YOUR_CHAINSTACK_ENDPOINT", multicallAddress: "0xeefba1e63905ef1d7acba5a8513c70307c1ce441"};const addressArr = [ "0x2B6ee955a98cE90114BeaF8762819d94C107aCc7", "0x2bB42C655DdDe64EE508a0Bf29f9D1c6150Bee5F"];async function main() { const startTime = Date.now(); console.log("Started..."); const calls = []; // Retrieve the Ether balance of each Ethereum address in addressArr using the multicall library. for (let i = 0; i < addressArr.length; i++) { const callObj = { call: [ 'getEthBalance(address)(uint256)', addressArr[i] ], returns: [ [`ETH_BALANCE ${addressArr[i]}`, val => val / 10 ** 18] ] }; calls.push(callObj); } const result = await multicall.aggregate(calls, config); console.log(result); const timeFromStart = Date.now() - startTime; console.log(`Result received in ${timeFromStart / 1000} seconds`);}main();
The main function iterates through each address in the addressArr array and creates a call object for each address. These call objects use the multicall library to retrieve the ether balance for each address.
Once all of the call objects have been created and pushed to the calls array, the multicall library’s aggregate function is called with the array of call objects and the configuration object. This function aggregates the results of all of the calls into a single object, which is stored in the result variable.
Finally, the code logs the “result” to the console and calculates the time it took to receive the “result”, which is also logged to the console.
The testing script for batch requests and multicall contract is already included in the previous sections. Below is the code for sending multiple HTTP requests in parallel:
Copy
const Web3 = require('web3');const web3 = new Web3(new Web3.providers.HttpProvider('YOUR_CHAINSTACK_ENDPOINT'));var addressArr = [ "0x2B6ee955a98cE90114BeaF8762819d94C107aCc7", "0x2bB42C655DdDe64EE508a0Bf29f9D1c6150Bee5F"]async function main() { var startTime = Date.now() console.log("started") for (i = 0; i < addressArr.length; i++) { web3.eth.getBalance(addressArr[i]).then(function(result) { var timeFromStart = Date.now() - startTime console.log("Result received in:" + timeFromStart / 1000 + " seconds") }) }}main();
The test was conducted between a server in Europe and a client in Singapore. A total of 15 measurements were averaged, which shows the performance of batch request > Multicall > normal request. Compared with sending single requests in parallel, batch request reduces 38% of the total request time, and multicall reduces 18% of the total request time.
Below are the testing scripts using web3.js for making smart contract calls. The tests are based on an ERC-721 standard method ownerOf from BAYC’s smart contract.
Sending multiple HTTP requests in parallel:
Copy
const Web3 = require('web3');const web3 = new Web3(new Web3.providers.HttpProvider('YOUR_CHAINSTACK_ENDPOINT'));const abi = [{ "inputs": [{ "internalType": "uint256", "name": "tokenId", "type": "uint256" }], "name": "ownerOf", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }]const contract = new web3.eth.Contract(abi, "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D");async function main() { const startTime = Date.now() console.log("started") for (i = 0; i < 30; i++) { contract.methods.ownerOf(i).call().then(function(result) { console.log(result) var timeFromStart = Date.now() - startTime console.log("result received in: " + timeFromStart / 1000 + " seconds") }) }}main();
Sending batch request:
Copy
const { Web3 } = require('web3');const web3 = new Web3('YOUR_CHAINSTACK_ENDPOINT);const abi = [ { inputs: [{ internalType: "uint256", name: "tokenId", type: "uint256" }], name: "ownerOf", outputs: [{ internalType: "address", name: "", type: "address" }], stateMutability: "view", type: "function", },];const contract = new web3.eth.Contract( abi, "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D");async function main() { const startTime = Date.now(); const batch = new web3.BatchRequest(); console.log("started"); // Array to hold promises for each request const promises = []; for (let i = 0; i < 30; i++) { const request = { jsonrpc: "2.0", id: i + 1, method: "eth_call", params: [ { to: contract.options.address, data: contract.methods.ownerOf(i).encodeABI(), }, "latest", ], }; // Add request to the batch and store the promise const requestPromise = batch.add(request); promises.push(requestPromise); } // Send the batch request and wait for all responses const responses = await batch.execute(); // Process responses responses.forEach((response, index) => { if (response.error) { console.error(response.error); } else { const ownerAddress = web3.eth.abi.decodeParameter( "address", response.result ); const timeFromStart = Date.now() - startTime; console.log( `${index} token owner is ${ownerAddress} received in: ${ timeFromStart / 1000 } seconds` ); } });}main();
Multicall contract:
Copy
const multicall = require("@makerdao/multicall")const config = { rpcUrl: "YOUR_CHAINSTACK_ENDPOINT", multicallAddress: "0xeefba1e63905ef1d7acba5a8513c70307c1ce441"};async function main() { var startTime = Date.now() console.log("started") var calls = [] for (i = 0; i < 30; i++) { var callObj = { target: "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D", call: ['ownerOf(uint256)(address)', i], returns: [ ['OWNER_ADDR ' + i] ] } calls.push(callObj) } const result = await multicall.aggregate(calls, config); console.log(result.results); var timeFromStart = Date.now() - startTime console.log("Result received in: " + timeFromStart / 1000 + " seconds")}main();
The same test was conducted for read contract calls. The result shows that both batch requests and multicall contracts save around 20% of total request time compared with sending single requests.
If I package 100 requests into a single batch request, does that count as 1 request or 100 requests on Chainstack?
As an RPC provider, Chainstack counts “request” as RPC calls. After a server receives an HTTP batch request, it “unpacks” the request and processes the calls separately. So from the server’s point of view, 1 batch request of 100 calls consumes 100 requests instead of 1.
Even though tests show that batch request and multicall contract improves performance significantly, they do have their own limitations.
Requests in a batch request are executed in order, which means if a new block is received during execution, the subsequent results are likely to be different.
If you want to use a multicall contract, you should probably deploy your own contract for production just to ensure its availability.
Both batch request and multicall contract return multiple results in a single response. Both of them require much more computational resources. They can easily trigger “request timeout” errors and “response size too big” errors, which makes them not suitable for complex calls.