I am developing a simple Dapp in which the line charts will be shown and the data will be obtained for each day. However, when fetching the data I realized that it is really slow to get the historic data from different blocks mined at different times.
My first naive solution was just to make read function call for each block mined in the given day, but I quickly realized this is very slow. I am using RPC provider.
const blocksPerDay = 20 * 60 * 24;
const startingBlock = 1446905;
const lastBlock = await provider.getBlockNumber();
let day = (await provider.getBlock(startingBlock)).timestamp;
const secondsPerDay = 60 * 60 * 24;
const result: LineChartData = [];
for (
let currentBlock = startingBlock;
currentBlock <= lastBlock;
currentBlock += blocksPerDay
) {
const valueAtBlock = await contract.getData({ blockTag: currentBlock });
result.push({x: day, y: fromWeiNumber(valueAtBlock)});
day += secondsPerDay;
}
return result;
Then I did a little research and found Multicall.js. Unfortunately, I did not found a way to batch read function calls for different blocks using Multicall(as it's probably not possible).
Is there any way to send multiple read calls for different blocks in one function call or make it faster? How do the web apps showing charts (like poocoin) do it? Do I need to get my own archive node?