3

I am in need of a library that estimates a date or some sort of timestamp when fed with a future block number (on mainnet or any major testnet).

For instance, https://etherscan.io/block/countdown/9950000 shows up like this:

Etherscan Countdown

Unfortunately, I emailed Etherscan and the countdown feature is not available in their API (I probably could reverse engineer it by parsing the HTML response, but seems overkill).

Is there any library that does this?

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143

4 Answers4

2

You don't need an API to do this.

The standard way to calculate future blocks is to take the the current block timestamp, the average block time and the difference between the current block and the future block.

blockDiff*avgTime+currentTimestamp

This is how Etherscan does it as well.

As a side note, it's usually slightly overestimated for some networks as historically hashrate grows. IE in Bitcoin the target time is 10mins the real time is closer to 9mins

1

We built a little tool for this last year called BlockBucket:

https://blockbucket.io/block-calculator

It works by using the average block times and figuring out how many blocks it will take to get to that time. We've built it to work for most EVM chains.

Let me know if you have any questions :)

Ismael
  • 30,570
  • 21
  • 53
  • 96
0

you can try this api of Etherscan: https://docs.etherscan.io/api-endpoints/blocks#get-estimated-block-countdown-time-by-blockno

0

You can do this with web3.js - web3.eth.getBlock will return the block info containing the timestamp of the block

KNK
  • 679
  • 4
  • 8
  • 1
    Yes, for past blocks, but what about future blocks? You need to estimate based on the most recent information regarding block times. – Paul Razvan Berg May 25 '19 at 16:40
  • get the last block and 1000 or 10000 blocks before the last one, divide the timedifference to the number of blocks and you will get the average block time for future estimations – KNK May 25 '19 at 16:57
  • I already have a back-of-the-napkin implementation for that, but a library or an API would be far more accurate and faster than I could be. – Paul Razvan Berg May 25 '19 at 16:59
  • Nothing can be more accurate that use the last mean of the interval and calculate the number you are asking for. Nothing in the world and beyond. Why should I create a library for a y=mX+C calculation? – Rick Park May 06 '20 at 21:50