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
Substrate and Polkadot Stack Exchange is a question and answer site for developers building blockchains with the Substrate SDK. It only takes a minute to sign up.
Sign up to join this communityIs 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
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.