im using web3.py v5.19 in my project and we are on BSC mainnet
also i use bsc urls recommended in site to connect to nodes
this is my code to get events :
block_filter1 = contract.events.validatorSuggested.createFilter(fromBlock='latest')
for event in filter.get_new_entries():
if event.event == 'validatorSuggested' :
...
but after a while i got this error :
File "/usr/local/lib/python3.8/dist-packages/web3/_utils/filters.py", line 160, in get_new_entries
log_entries = self._filter_valid_entries(self.eth_module.get_filter_changes(self.filter_id))
File "/usr/local/lib/python3.8/dist-packages/web3/module.py", line 57, in caller
result = w3.manager.request_blocking(method_str, params, error_formatters)
File "/usr/local/lib/python3.8/dist-packages/web3/manager.py", line 158, in request_blocking
raise ValueError(response["error"])
ValueError: {'code': -32000, 'message': 'filter not found'}
as i searched , the problem comes from '' stateless '' nodes which clear filters .
what should i do exactly in order to solve this problem and receive events ?
Thanks
Best Answer
Developer advocate at Chainstack here!
The error you're seeing,
ValueError: {'code': -32000, 'message': 'filter not found'}
, is a common issue developers encounter when working with filters on EVM-compatible blockchains like BNB Smart Chain. This error arises when the system cannot locate the specified filter ID during a query.There are several reasons why you might encounter this error:
Given your scenario and the details provided, here are some solutions you can implement to handle the "Filter Not Found" error effectively:
Immediate Change Retrieval with Error Handling:
Automated Filter Recreation: If a filter expires or is not found, consider automating its recreation within your error-handling logic.
Persistent Filter Monitoring: Set up a loop continuously monitoring the filter for changes. If the filter expires or is removed, you can detect it immediately and take corrective action.
Use Event Logs Instead of Filters: Instead of relying solely on filters, you can use the eth_getLogs method to fetch historical data. This is especially useful for past events or transactions.
Backup Filters on Multiple Nodes: Create the same filter on multiple nodes to ensure redundancy. If one node fails or the filter is removed, you can still retrieve data from another node.
Regularly Refresh Filters: Proactively refresh or recreate filters to ensure they remain active and reduce the chances of encountering the error.
Implement Comprehensive Logging: Incorporate detailed logging into your application to diagnose issues quickly and understand their root causes.
Hope this helps! There is an entire guide on the Chainstack dev portal!
Feel free to reach out as well!