I have multiple ERC20 tokens, deployed on Rootstock,
and I want to track their balances and allowances in a real time from my DApp.
In ethers.js,
I can track their balances by alternately calling the functions
balanceOf(address) and allowance(owner, spender).
However, across two tokens, that equates to about
4 JSON-RPC requests every ~30 seconds.
I would prefer to reduce the frequency of JSON-RPC requests made by my application, by aggregating these particular requests.
Is it possible to combine multiple smart contract data queries
into a single JSON-RPC request via ethers.js or any other library?
You could take a look at
@0xsequence/multicall. It consists of:multiCall()function intended for making aggregated callsSmart contract deployments:
MultiCallUtilsis deployed on the Rootstock Testnet:0xb39d1Dea1bF91Aef02484F677925904b9d6936B4To make aggregated smart contract calls:
The library will attempt to send all these function calls within a single call to
MultiCallUtilssmart contractmultiCall()function. However if the aggregated call fails, as a fallback, its constituent functions calls will be called individually via the ethers.js provider.makeAggregatedCallfunction to the ethers providerblockevent listener: