How can i get latest 10k logs from the blockchain using filter in ethersjs
- Smart Contract
- Solidity
- Optimizer
- Yul
- Solidity Compiler Solidity
i want to get logs for my contract address from the blockchain by .provider.getLogs() method . i am applying an filter to it with block range 0 to latest. it gives me error that it can only get logs up to 2k blocks or you can filter latest 10k logs . now I won't want to use the first case of 2k blocks . how can I configure my filter to get latest 10k logs . as there is no property in filter to cap logs amount.
const filter = {
address: ethers.utils.getAddress(address),
fromBlock: 0, // Start from the earliest block
toBlock: "latest", // Fetch logs until the latest block
};
Answers 1
These limits are not one or another. Both limits are applied at the same time.
There is no filter configuration for that (client side). 2k blocks and 10k log limits are configured by the RPC provider (server side) that you are using. Other providers may have different limits.
10k logs limit means that if you are getting logs for a range of blocks (less than 2k blocks) and a number of events exceeds 10k then RPC will return to you only the first 10k events.
The general approach is to retrieve events with multiple calls.
- 1st call - fromBlock 0, toBlock 2000
- 2nd call - fromBlock 2001, toBlock 4000
and so on.
If you received exactly 10k logs for a call then it means the RPC provider excluded some of the events from the response and you need to re-run the call for a smaller range. However, that check is needed only if you expect such density of events for you filter.