Change handling of fetching events to reduce RPC calls (especially on L2's) #112
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
The current usage of a
.onlistener can hammer the RPC on L2 chains witheth_getLogsrequests, as it seems to make a request per block, and commonly cause429: Too Many Requesterrors. The polling logic is hosted inside the ethers dependency, and is harder to modify as it would require patching.Why
This enhances the fetching of new events to only make a set amount of requests every X interval, with X being modifiable per chain as different chains have different block creation times. RPC calls, especially to
eth_getLogs, should be greatly reduced for all chains, but especially for chains that produce multiple blocks per second.How
queryFilterand a spaced out polling mechanism to request all new blocks created after X amount of time (polling function).scanAllEventsto take either 1) a unique event param for requesting specific new events (which does not require further decoding), or 2) no unique event param passed and fetch all new events in a specified block range (which then requires decoding)scanAllEvents*requestEDIT: The PR now simply uses JsonRpcApiProvider type, which allows for using a WebSocketProvider that uses eth_subscribe to listen for new events. This is better than custom polling using queryFilter, as events are only reported once they exist, instead of queryFilter calls on interval.