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?

5
  • Why don't you get the average block time interval from an API and then just calculate it by yourself ? What's the problem with this solution ?
    – Laxmana
    May 25, 2019 at 17:36
  • @Laxmana yeah I already did something like that, but my implementation's buggy and just thought to ask if the community knows any library. May 25, 2019 at 17:53
  • I see. I don't know any library providing that functionality. Maybe we can help with the code :).
    – Laxmana
    May 25, 2019 at 17:57
  • Haha thanks, but drafting the question helped me fix the bugs! Maybe I'll find time to make a PR to web3 or ethers.js to add this estimation feature. May 25, 2019 at 18:00
  • That's good! Good luck!
    – Laxmana
    May 25, 2019 at 18:08

4 Answers 4

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 :)

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

0

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

4
  • 1
    Yes, for past blocks, but what about future blocks? You need to estimate based on the most recent information regarding block times. May 25, 2019 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, 2019 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. May 25, 2019 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 6, 2020 at 21:50
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.