Created
September 10, 2023 07:01
-
-
Save miguelmota/8d85432528ba5c616665bab2bd3d45ae to your computer and use it in GitHub Desktop.
ethers filters events logs with batching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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