Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created September 10, 2023 07:01
Show Gist options
  • Save miguelmota/8d85432528ba5c616665bab2bd3d45ae to your computer and use it in GitHub Desktop.
Save miguelmota/8d85432528ba5c616665bab2bd3d45ae to your computer and use it in GitHub Desktop.
ethers filters events logs with batching
async function batchFetch (contract: Contract, filter: any, startBlockNumber: number, endBlockNumber: number, batchSize = 10000) {
const logs: any[] = []
let start = startBlockNumber
let end = Math.min(start + batchSize, endBlockNumber)
while (end <= endBlockNumber) {
const _logs = await contract.queryFilter(
filter,
start,
end
)
console.log(start, end, _logs.length)
logs.push(..._logs)
// Add 1 so that boundary blocks are not double counted
start = end + 1
// If the batch is less than the batchSize, use the endBlockNumber
const newEnd = start + batchSize
end = Math.min(endBlockNumber, newEnd)
// For the last batch, start will be greater than end because end is capped at endBlockNumber
if (start > end) {
break
}
}
return logs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment