1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ethers } from 'ethers'
/**
* Helper function for querying all events for a given contract/filter. Improves on the standard
* event querying functionality by decreasing the block range by half when a query errors out. If
* the query succeeds, event range will return back to the default size, and so on. Also allows
* more advanced filtering during the querying process to avoid OOM issues.
*
* @param contract Contract to query events for.
* @param options Options for the query.
* @returns Array of events.
*/
export const advancedQueryFilter = async (
contract: ethers.Contract,
options: {
queryFilter: ethers.EventFilter
filter?: (event: ethers.Event) => boolean
startBlock?: number
endBlock?: number
}
): Promise<ethers.Event[]> => {
const defaultStep = 500000
const end = options.endBlock ?? (await contract.provider.getBlockNumber())
let step = defaultStep
let i = options.startBlock ?? 0
let events: ethers.Event[] = []
while (i < end) {
try {
const allEvents = await contract.queryFilter(
options.queryFilter,
i,
i + step
)
const matching = options.filter
? allEvents.filter(options.filter)
: allEvents
events = events.concat(matching)
i += step
step = step * 2
} catch (err) {
step = Math.floor(step / 2)
if (step < 1) {
throw err
}
}
}
return events
}