You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
TheMultiCall contract is designed to execute multiple view calls at one time.For example, you have a list of tokens, and you need to get balances for all the items on that list.
The downside to this solution is that you make as many requests for a contract as you have tokens on the list.And if the list is large enough, you will create a significant load on the provider.
Simple MultiCall
AmultiCallService.callByChunks() contract takes a list of requests, splits them into chunks and calls the provider in batches.
Default params
maxChunkSize:100
retriesLimit:3
blockNumber:'latest'
Example:
constencodeAbi= ...constprovider=newWeb3ProviderConnector(newWeb3('...'));constwalletAddress='0x1111111111111111111111111111111111111111';constcontractAddress='0x8d035edd8e09c3283463dade67cc0d49d6868063';constmultiCallService=newMultiCallService(provider,contractAddress);consttokens=['0x6b175474e89094c44da98b954eedeac495271d0f','0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48','0xdac17f958d2ee523a2206206994597c13d831ec7'];// The parameters are optional, if not specified, the default will be usedconstparams:MultiCallParams={chunkSize:100,retriesLimit:3,blockNumber:'latest'};constcallDatas=tokens.map((tokenAddress)=>{return{to:tokenAddress,data:encodeAbi(ERC20ABI,tokenAddress,'balanceOf',[walletAddress])};});constbalances=awaitmultiCallService.callByChunks(callDatas,params);
Got better! Instead of making a separate request to the provider for each item, we group them into butches and make much fewer requests.
Note:If the call to this method exceeds the gas limit, then the entire request will be reverted.
MultiCall by gas limit
Problem:The point is that the node has a limit for gas per call of the contract.And it may happen that by making a simple MultiCall we will not meet this limit.If the gas limit on the node is large enough, we may face a time limit on the execution of the contract method.
In total,there are 2 restrictions on a node:
by gas
by time
To avoid these limitations, there is a more advanced method:multiCallService.callByGasLimit()
Default params
maxChunkSize:500
retriesLimit:3
blockNumber:'latest'
gasBuffer:3000000
maxGasLimit:150000000
Example:
constcontractAddress='0x8d035edd8e09c3283463dade67cc0d49d6868063';constprovider=newWeb3ProviderConnector(newWeb3('...'));constgasLimitService=newGasLimitService(provider,contractAddress);constmultiCallService=newMultiCallService(provider,contractAddress);constbalanceOfGasUsage=30_000;consttokens=['0x6b175474e89094c44da98b954eedeac495271d0f','0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48','0xdac17f958d2ee523a2206206994597c13d831ec7'];constrequests:MultiCallRequest[]=tokens.map((tokenAddress)=>{return{to:tokenAddress,data:provider.contractEncodeABI(ERC20ABI,tokenAddress,'balanceOf',[walletAddress]),gas:balanceOfGasUsage};});constgasLimit:number=awaitgasLimitService.calculateGasLimit();// The parameters are optional, if not specified, the default will be usedconstparams:MultiCallParams={maxChunkSize:500,retriesLimit:3,blockNumber:'latest',gasBuffer:100_000};constresponse=awaitmultiCallService.callByGasLimit(requests,gasLimit,params);
The idea is that we request the gas limit from the node and split the requests into chunks regarding this limit.Accordingly, we must set the gas limit for each request.
It is noteworthy that if suddenly the request does not fit into the gas limit, the entire request will not be reverted, and the request will return the results of those calls that fit into the gas limit.
If the call to the contract all the same does not fit into the gas limit, then the callByGasLimit() will automatically re-request those elements that have not been fulfilled.
You can see a more detailed description of the library's work in the diagrams below.
GasLimitService
This service is used to correctly calculate the gas limit for calling a MultiCall.The basic formula for calculating the limit is as follows:
Where:gasLimitFromNode - is the gas limit taken from the nodemaxGasLimit - limiter on top, in case the gas limit from the node is too large (may cause timeout)gasBuffer - is some safe buffer that allows you to avoid crossing the limit in case of unforeseen situations
We believe that the multicall call should fit into 11_990_000 gas.
Default params:
gasBuffer:3000000
maxGasLimit:150000000
Params forGasLimitService.calculateGasLimit() are optional, if not specified, then gas limit will be requested from the node and the default params will be used.