Open sebastiantf opened 1 year ago
There is no API that I know of that provides this sort of service. How do they do that? By guessing based on block time and extrapolating/interpolating?
What networks does it work with?
There's also Etherscan's API: https://docs.etherscan.io/api-endpoints/blocks#get-block-number-by-timestamp
Any API added to the Provider Interface must be present in all (or at least almost all) backends.
It is possible to add a method to just the EtherscanProvider though, similar to getHistory
.
cast find-block
seem to be doing a binary search, I think.
ethereum-block-by-date
seem to be doing some form of extrapolation / heuristics.
Haven't really dug into either of them
For now, I'll prolly leave it as an option for someone to provide a utility library for then, some function that looks like getBlockByTimestamp(provider: Provider)
. But if it becomes popular, useful and (generally) efficient, I can add it to the internal API.
I'll likely include it in the EtherscanProvider though, since it is just coming from an index.
Describe the Feature
I was looking for an API that fetches you the block thats closest to a given timestamp. This feature is available in Foundry's cast CLI:
cast find-block
. There's also theethereum-block-by-date
packageEdit: There's also Etherscan's API: https://docs.etherscan.io/api-endpoints/blocks#get-block-number-by-timestamp
Code Example