event_reader.lazy_timestamp_reader

Documentation for eth_defi.event_reader.lazy_timestamp_reader Python module.

Lazily load block timestamps and headers.

See extract_timestamps_json_rpc_lazy()

Functions

extract_timestamps_json_rpc_lazy(web3, ...)

Create a cache container that instead of reading block timestamps upfront for the given range, only calls JSON-RPC API when requested

Classes

LazyTimestampContainer

Dictionary-like object to get block timestamps on-demand.

TrackedLazyTimestampReader

Track block header fetching across multiple chunks.

Exceptions

OutOfSpecifiedRangeRead

We tried to read a block outside out original given range.

exception OutOfSpecifiedRangeRead

Bases: Exception

We tried to read a block outside out original given range.

__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 LazyTimestampContainer

Bases: object

Dictionary-like object to get block timestamps on-demand.

Lazily load any block timestamp over JSON-RPC API if we have not cached it yet.

See extract_timestamps_json_rpc_lazy().

TODO: This is not using middleware and fails to retry any failed JSON-RPC requests.

Parameters
  • web3 – Connection

  • start_block – Start block range, inclusive

  • end_block – End block range, inclusive

__init__(web3, start_block, end_block, callback=None)
Parameters
  • web3 (web3.main.Web3) – Connection

  • start_block (int) – Start block range, inclusive

  • end_block (int) – End block range, inclusive

  • callback (Callable) –

api_call_counter

How many API requets we have made

update_block_hash(block_identifier)

Internal function to get block timestamp from JSON-RPC and store it in the cache.

Parameters

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]) –

Return type

int

extract_timestamps_json_rpc_lazy(web3, start_block, end_block, fetch_boundaries=True)

Create a cache container that instead of reading block timestamps upfront for the given range, only calls JSON-RPC API when requested

  • Works on the cases where sparse event data is read over long block range Use slow JSON-RPC block headers call to get this information.

  • The reader is hash based. It is mainly meant to resolve eth_getLogs resulting block hashes to corresponding event timestamps.

  • This is a drop-in replacement for the dict returned by eager eth_defi.reader.extract_timestamps_json_rpc()

Example:

# Allocate timestamp reader for blocks 1...100
timestamps = extract_timestamps_json_rpc_lazy(web3, 1, 100)

# Get a hash of some block
block_hash = web3.eth.get_block(5)["hash"]

# Read timestamp for block 5
unix_time = timestamps[block_hash]

For more information see

  • eth_defi.reader.extract_timestamps_json_rpc()

  • eth_defi.reorganisation_monitor.ReorganisationMonitor

Returns

Wrapper object for block hash based timestamp access.

Parameters
  • web3 (web3.main.Web3) –

  • start_block (int) –

  • end_block (int) –

Return type

eth_defi.event_reader.lazy_timestamp_reader.LazyTimestampContainer

class TrackedLazyTimestampReader

Bases: object

Track block header fetching across multiple chunks.

Monitor expensive eth_getBlock JSON-RPC process via :py:method:`get_count`.

__init__()