Fix guide
Fix "Filter Not Found" & Block Range Limits on Polygon
The filter not found error and block range limits occur when polling stateful filters on load-balanced RPC nodes or requesting log ranges that exceed server memory limits.
Why Filters Expire and Range Errors Occur
Calling eth_getFilterChanges or eth_getFilterLogs after the RPC node evicted the filter ID due to inactivity timeout (typically 5–15 minutes).
Querying broad block ranges (e.g. from block 0 to latest) triggering hard limits like 'query returned more than 10,000 results'.
Multi-tenant cloud providers load-balancing filter calls across different backend nodes that do not share local filter memory.
EVM node memory pruning discarding ephemeral state filters to prevent out-of-memory (OOM) crashes under concurrent load.
Recommended Architectural Fix: Stateless Chunking
Replace stateful polling (`eth_newFilter` / `eth_getFilterChanges`) with stateless, idempotent `eth_getLogs` queries.
Implement dynamic binary block-chunking: divide historical scans into 2,000-block ranges (`fromBlock`, `toBlock`).
If the provider returns 'query returned more than X results', recursively halve the block interval until the batch succeeds.
Connect to a dedicated bare-metal Polygon node that does not arbitrarily evict state filters or artificially limit log ranges.
// Recommended Viem Stateless Filter Pattern:
const logs = await client.getLogs({
address: '0x...',
fromBlock: currentBlock > 2000n ? currentBlock - 2000n : 0n,
toBlock: currentBlock,
});
Implementation Strategy
Stateful filters are acceptable when:
- ✓ running single-tenant scripts on a dedicated local node
- ✓ polling interval is under 30 seconds
- ✓ monitoring a small, focused contract address list
- ✓ occasional filter recreation on error is handled in code
Stateless block chunking is required when:
- ✓ running production dapp backends, indexing subgraphs, or scrapers
- ✓ relying on cloud or third-party multi-tenant RPC endpoints
- ✓ backfilling large historical transaction event histories
- ✓ preventing unhandled promise rejections from crashing backend workers

