You could take a look at @0xsequence/multicall
. It consists of:
Smart contract deployments:
To make aggregated smart contract calls:
- Install the npm package:
npm i @0xsequence/multicall
- Import the library to your project:
const { providers } = require('@0xsequence/multicall');
- Create a Rootstock Testnet configuration:
const multicallConfig = {
// RSK Testnet
31: {
// maximum number of calls to batch into a single JSON-RPC call
batchSize: 50,
// defines the time each call is held on buffer waiting for subsequent calls before aggregation, ms
timeWindow: 50,
// MultiCallUtils smart contract
contract: '0xb39d1Dea1bF91Aef02484F677925904b9d6936B4',
},
};
- Wrap your ethers.js current provider with a Multicall provider:
const multicallProvider = new providers.MulticallProvider(ethersProvider, multicallConfig[31]);
- Connect your token smart contracts to the Multicall provider instead of ethers.js provider to be to able make aggregated calls:
const token1 = new ethers.Contract(
token1Address,
token1ABI,
multicallProvider,
);
const token2 = ...
- Now you are ready to create an aggregated call:
function makeAggregatedCall() {
const aggregatedCall = [
multicallProvider.getBalance(address),
token1.balanceOf(address),
token1.allowance(owner, spender),
token2.balanceOf(address),
token2.allowance(owner, spender),
];
[
rbtcBalance,
balance1,
allowance1,
balance2,
allowance2,
] = await Promise.all(aggregatedCall);
}
The library will attempt to send all these function calls within a single call to MultiCallUtils
smart contract multiCall()
function.
However if the aggregated call fails,
as a fallback,
its constituent functions calls will be called individually via the ethers.js provider.
- In order to subscribe to every newly minted block, attach the
makeAggregatedCall
function to the ethers provider block
event listener:
ethersProvider.on('block', makeAggregatedCall);