I'm looking for a reliable way to determine when the block was produced by given any block number in Polygon Mumbai Testnet. I can't use Api for this task . I'm just looking for a convenient way of computing. Do you have any suggestion for this?
Best Answer
I definitely recommend invoking APIs, however, given your heavy requirement on NoAPIs
, we'd have to go with estimates.
In this case -
- First, we need to find the average time per block. Polygon takes approximately -
2.5s
to generate a new block - Data - Then you'd need to identify the number of blocks that have already been generated (manual input in this case)
- At the time of writing this answer -
36996889
blocks have been generated.
- At the time of writing this answer -
- Finally, you need the block number you want to get the time estimate for.
- let's take
36996800
as an example in this case.
- let's take
So, to identify the estimates (again, not accurate at all) you can use the following algorithm.
const currentBlockNumber = 36996889; const targetBlockNumber = 36996800; const averageTimePerBlock = 2.5;// Get the numenr of blocks that have been produced sinve the target block you're trying to estimate forconst numberOfBlocks = currentBlockNumber - targetBlockNumber;// Calculate the approximate time that the target block was produced atconst approximateTimeInSeconds = numberOfBlocks * averageTimePerBlock;// Calculate the approximate date and time that the target block was producedconst dateAndTimeCurrentBlockWasProducedAt = new Date(1671405095000); // This was the epoch currentBlock was produced atconst approximateTimeInMilliseconds = dateAndTimeCurrentBlockWasProducedAt.getTime() - (approximateTimeInSeconds * 1000); // get approx epoch for targetBlockconst approximateDate = new Date(approximateTimeInMilliseconds); // convert approxTime to human readable dateconsole.log(`The target block was produced approximately on ${approximateDate}.`);