read_events

Documentation for eth_defi.event_reader.reader.read_events function.

read_events(web3, start_block, end_block, events, notify, chunk_size=100, context=None, extract_timestamps=<function extract_timestamps_json_rpc>, filter=None)[source]

Reads multiple events from the blockchain.

Optimized to read multiple events fast.

  • Scans chains block by block

  • Returns events as a dict for optimal performance

  • Supports interactive progress bar

  • Reads all the events matching signature - any filtering must be done by the reader

See scripts/read-uniswap-v2-pairs-and-swaps.py for a full example.

Example:

# HTTP 1.1 keep-alive
session = requests.Session()

json_rpc_url = os.environ["JSON_RPC_URL"]
web3 = Web3(HTTPProvider(json_rpc_url, session=session))

# Enable faster ujson reads
patch_web3(web3)

web3.middleware_onion.clear()

# Get contracts
Factory = get_contract(web3, "UniswapV2Factory.json")

events = [
    Factory.events.PairCreated, # https://etherscan.io/txs?ea=0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f&topic0=0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9
]

token_cache = TokenCache()

start_block = 10_000_835  # Uni deployed
end_block = 10_009_000  # The first pair created before this block

# Read through the blog ran
out = []
for log_result in read_events(
    web3,
    start_block,
    end_block,
    events,
    None,
    chunk_size=1000,
    context=token_cache,
    extract_timestamps=None,
):
    out.append(decode_pair_created(log_result))
Parameters
  • web3 (web3.main.Web3) – Web3 instance

  • events (List[web3.contract.ContractEvent]) – List of Web3.py contract event classes to scan for

  • notify (Optional[eth_defi.event_reader.reader.ProgressUpdate]) – Optional callback to be called before starting to scan each chunk

  • start_block (int) – First block to process (inclusive)

  • end_block (int) – Last block to process (inclusive)

  • extract_timestamps (Optional[Callable]) – Override for different block timestamp extraction methods

  • chunk_size (int) – How many blocks to scan in one eth_getLogs call

  • context (Optional[eth_defi.event_reader.logresult.LogContext]) – Passed to the all generated logs

  • filter (Optional[eth_defi.event_reader.filter.Filter]) – Pass a custom event filter for the readers

Return type

Iterable[eth_defi.event_reader.logresult.LogResult]