Skip to content

Commit 487bce9

Browse files
author
Your Name
committed
Refactor paginated event query
1 parent a389d70 commit 487bce9

1 file changed

Lines changed: 45 additions & 28 deletions

File tree

lib/web3.rs

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,36 +1173,53 @@ pub fn get_eth_events_paginated(
11731173
to_block: u64,
11741174
topics: &Vec<B256>,
11751175
) -> Result<Vec<Log>, ValidationError> {
1176-
if to_block - from_block > config.max_blocks_per_event_query {
1177-
let pb = ProgressBar::new(to_block - from_block);
1178-
if to_block - from_block > LARGE_BLOCK_RANGE {
1179-
let mut num_events = String::new();
1180-
if topics.is_empty() {
1181-
num_events.push_str("all");
1182-
} else {
1183-
num_events.push_str(&topics.len().to_string());
1184-
}
1185-
info!(
1186-
"You are querying {} event(s) for a range of {} blocks. This will take some time.",
1187-
num_events,
1188-
to_block - from_block + 1
1189-
);
1190-
}
1191-
let mut last_block = from_block + config.max_blocks_per_event_query;
1192-
let mut events = get_eth_events_paginated(config, address, from_block, last_block, topics)?;
1193-
while last_block < to_block {
1194-
let next_last_block =
1195-
std::cmp::min(last_block + config.max_blocks_per_event_query - 1, to_block);
1196-
let mut next_events =
1197-
get_eth_events_paginated(config, address, last_block + 1, next_last_block, topics)?;
1198-
last_block = next_last_block;
1199-
pb.set_position(next_last_block - from_block);
1200-
events.append(&mut next_events);
1176+
let mut events = Vec::new();
1177+
1178+
// If the range is small enough, just make a single request
1179+
if to_block - from_block <= config.max_blocks_per_event_query {
1180+
return Ok(get_eth_events(
1181+
config, address, from_block, to_block, topics,
1182+
)?);
1183+
}
1184+
1185+
// Otherwise, if the range is larger than the maximum allowed for one request, paginate
1186+
let pb = ProgressBar::new(to_block - from_block);
1187+
1188+
// Inform user if the block range is large
1189+
if to_block - from_block > LARGE_BLOCK_RANGE {
1190+
let mut num_events = String::new();
1191+
if topics.is_empty() {
1192+
num_events.push_str("all");
1193+
} else {
1194+
num_events.push_str(&topics.len().to_string());
12011195
}
1202-
pb.finish_and_clear();
1203-
return Ok(events);
1196+
info!(
1197+
"You are querying {} event(s) for a range of {} blocks. This will take some time.",
1198+
num_events,
1199+
to_block - from_block + 1
1200+
);
12041201
}
1205-
return get_eth_events(config, address, from_block, to_block, topics);
1202+
1203+
let mut current_from = from_block;
1204+
while current_from <= to_block {
1205+
// Calculate the end of this chunk (either the max allowed or the to_block)
1206+
let current_to = std::cmp::min(
1207+
current_from + config.max_blocks_per_event_query - 1,
1208+
to_block,
1209+
);
1210+
1211+
let mut chunk_events = get_eth_events(config, address, current_from, current_to, topics)?;
1212+
events.append(&mut chunk_events);
1213+
1214+
pb.set_position(current_to - from_block + 1);
1215+
1216+
// Move the starting point to the next block after the current range
1217+
current_from = current_to + 1;
1218+
}
1219+
1220+
pb.finish_and_clear();
1221+
1222+
Ok(events)
12061223
}
12071224

12081225
// Fetches events, in a single call, does NOT paginate the block range.

0 commit comments

Comments
 (0)