I want to calculate an average gas fee paid for all transactions on the RSK within the last three months. I tried to query the RSK blockchain using the Web3.js library. To get transaction data for just a single block height, I had to do:
const txsPerBlock = await web3.getBlockTransactionCount(block.height);
const txs = await Promise.all(
[...Array(txsPerBlock).keys()].map(
(i) => web3.eth.getTransactionFromBlock(block.height, i))
);
And then to calculate the average gas fee:
const averageGasFee = txs.reduce(
(p, c) => p + Number(c.gasPrice) * c.gas,
0
) / txs.length;
However, within 3 months there were about 230,000 blocks on RSK including about 3 transactions per block. So I had to poll about 600,000 transactions that would take forever to complete. I waited for an hour and then canceled the script. Is there a better to query the RSK blockchain and calculate the average transaction gas fee over such a large number of blocks and transactions?