'Batch together multiple read function calls for different blocks of ethereum blockchain

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?



Solution 1:[1]

You will need access to a node that allows these functionalities, but to answer your batch question, with geth as your node you can batch read requests with graphql endpoint, batch the requests and responses, or use the json-rpc-batch-provider

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 omencat