timestamp
Documentation for eth_defi.timestamp Python module.
Block timestamp related utilities
Functions
Estimate block number for a given timestamp. |
|
|
Get a block timestamp. |
Get the latest block timestamp. |
Classes
For block number estimation by time |
Exceptions
Something wrong with FindBlock AI call |
- exception FindBlockError
Bases:
ExceptionSomething wrong with FindBlock AI call
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class FindBlockReply
Bases:
objectFor block number estimation by time
- __init__(hash, block_number, block_timestamp, searched_timestamp)
- Parameters
hash (eth_typing.encoding.HexStr) –
block_number (int) –
block_timestamp (datetime.datetime) –
searched_timestamp (datetime.datetime) –
- Return type
None
- get_latest_block_timestamp(web3)
Get the latest block timestamp.
- Returns
Timezone naive UTC datetime
- Parameters
web3 (web3.main.Web3) –
- Return type
- get_block_timestamp(web3, block_identifier, raw=False)
Get a block timestamp.
Slow method. Use only for individual queries.
By hand:
curl $JSON_RPC_MANTLE -X POST -H "Content-Type: application/json" --data '{"method":"eth_getBlockByNumber","params":["0x1",false],"id":1,"jsonrpc":"2.0"}'
- Parameters
- Returns
Timezone naive UTC datetime
- Return type
- estimate_block_number_for_timestamp_by_findblock(chain_id, timestamp, attempts=5, sleep=60)
Estimate block number for a given timestamp.
To convert timestamps to block numbers
Uses FindBlock API, using
block/beforeAPIGets the block that was finaliesd at the timestamp or before it.
FindBlock API is free
Note
FindBlock API is too throttled to get the timestamp only but few blocks. I think we get 5 request per minute.
- Parameters
timestamp (datetime.datetime) – Timestamp to estimate the block number for
chain_id (int) –
- Returns
Estimated block number
- Return type