timestamp

Documentation for eth_defi.timestamp Python module.

Block timestamp related utilities

Functions

estimate_block_number_for_timestamp_by_findblock(...)

Estimate block number for a given timestamp.

get_block_timestamp(web3, block_identifier)

Get a block timestamp.

get_latest_block_timestamp(web3)

Get the latest block timestamp.

Classes

FindBlockReply

For block number estimation by time

Exceptions

FindBlockError

Something wrong with FindBlock AI call

exception FindBlockError

Bases: Exception

Something 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: object

For block number estimation by time

__init__(hash, block_number, block_timestamp, searched_timestamp)
Parameters
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

datetime.datetime

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
  • raw – Don’t do slow Python datetime conversion, return raw integer timestamp

  • web3 (web3.main.Web3) –

  • block_identifier (Union[Literal['latest', 'earliest', 'pending', 'safe', 'finalized'], eth_typing.evm.BlockNumber, eth_typing.evm.Hash32, eth_typing.encoding.HexStr, hexbytes.main.HexBytes, int]) –

Returns

Timezone naive UTC datetime

Return type

datetime.datetime | int

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/before API

  • Gets 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
Returns

Estimated block number

Return type

eth_defi.timestamp.FindBlockReply