0

Is it possible to get either block_number or block_hash of the nearest block for a given timestamp from python substrate interface?

For example if i pass timestamp of 2022-11-28 10:00:00 (+UTC) I would get block 13121267

1 Answer 1

1

You can use a binary search for this. There is no API which will give this to you without some kind of search on your side as blocks are not indexed by timestamp.

You can also estimate the start of your search knowing that each block is approximately 6 seconds. So if you get the timestamp of any starting block, you can guess approximately which block will be at a certain time.

For example, looking at the latest block:

Block 13,167,731: 12/1/2022, 3:30:06 PM (+UTC)

And you want: 2022-11-28 10:00:00 (+UTC)

The difference in time is 279,006 seconds, which is approximately 46,501 blocks.

13,167,731 - 46,501 = 13,121,230

Which is off by 37 blocks. This should be quick to search for using any alogrithm at this point.

1
  • This was the approach I was thinking about but wanted to check if there was a more straight forward way to achieve this, thanks @shawn
    – Pranay
    Dec 5, 2022 at 8:40

Your Answer

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

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