I am trying to build a simple database table containing block.number
and block.timestamp
starting at a given block number.
This could be easily done with the following JS code using ethers.js library:
const loadEthBlocks = async (fromBlock, toBlock) => {
for (let i = fromBlock; i <= toBlock; i++) {
const block = await provider()
.getBlock(i)
// store record into database
}
}
However, this will generate a request per block (when using Alchemy or Infura, this is a handicap). After some tests, it takes 1 second to store 2 blocks into a PostgreSQL DB, so it's not feasible for a few millions of records.
I am wondering if there is a way to retrieve a range of blocks in bulk mode containing at least the block number and block timestamp and then doing a bulk data load (instead of one record per request).
It would be great if we could define a starting block in the below function and retrieve also the timestamp, but this listener only returns the block number starting from the latest block:
provider.on("block", (blockNumber) => {
// Emitted on every block change
})
Anyone facing the same issue with a scalable solution?