Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/common/contracts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import itertools
import json
from functools import cached_property
from pathlib import Path
Expand Down Expand Up @@ -73,15 +74,28 @@ async def _get_last_event(
argument_filters: dict | None = None,
) -> EventData | None:
blocks_range = settings.events_blocks_range_interval
while to_block >= from_block:
events = await event.get_logs(
from_block=BlockNumber(max(to_block - blocks_range, from_block)),
to_block=to_block,

# Build all chunk ranges from newest to oldest
ranges: list[tuple[BlockNumber, BlockNumber]] = []
chunk_to = to_block
while chunk_to >= from_block:
chunk_from = BlockNumber(max(chunk_to - blocks_range, from_block))
ranges.append((chunk_from, chunk_to))
chunk_to = BlockNumber(chunk_to - blocks_range - 1)

async def fetch_chunk(chunk_from: BlockNumber, chunk_to: BlockNumber) -> list[EventData]:
return await event.get_logs(
from_block=chunk_from,
to_block=chunk_to,
argument_filters=argument_filters,
)
if events:
return events[-1]
to_block = BlockNumber(to_block - blocks_range - 1)

# Process chunks in batches (newest-first), abort on first hit
for batch in itertools.batched(ranges, EVENTS_CONCURRENCY_LIMIT):
batch_results = await asyncio.gather(*[fetch_chunk(f, t) for f, t in batch])
for chunk_events in batch_results:
if chunk_events:
return chunk_events[-1]
return None

async def _get_events(
Expand Down
Loading