Web3.py ” filter not found “


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 ?


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:

The filter is created on the blockchain node so this can cause situations.

  1. Filter Expiration: Filters are not permanent. They can expire after a certain period of inactivity, usually around 5 minutes. Once a filter expires, querying it will result in the "Filter Not Found" error.
  2. Node Restarts: If the blockchain node is restarted, filters created before the restart might be lost.
  3. State Pruning: Some nodes might prune or remove older states, including filters, to save space.
  4. Manual Deletion: Filters can be manually removed using methods like eth_uninstallFilter.

Given your scenario and the details provided, here are some solutions you can implement to handle the "Filter Not Found" error effectively:

  1. Immediate Change Retrieval with Error Handling:

    • After setting up a filter, fetch the changes immediately.
    • Implement error handling to handle the potential "Filter Not Found" error.
    from web3 import Web3
    import time
    # Initialize web3 connection
    node_url = "YOUR_BSC_ENDPOINT"
    web3 = Web3(Web3.HTTPProvider(node_url))
    def get_new_blocks():
            blocks_filter = web3.eth.filter('latest')
            return blocks_filter
        except Exception as e:
    blocks = get_new_blocks()
    filter_id = blocks.filter_id
        changes = web3.eth.get_filter_changes(filter_id)
        # Process the changes accordingly
    except ValueError as error:
        if "filter not found" in str(error).lower():
            print("The filter seems to have expired or is non-existent. Consider recreating it.")
            print(f"Encountered an error: {error}")
  2. Automated Filter Recreation: If a filter expires or is not found, consider automating its recreation within your error-handling logic.

  3. 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.

  4. 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.

  5. 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.

  6. Regularly Refresh Filters: Proactively refresh or recreate filters to ensure they remain active and reduce the chances of encountering the error.

  7. 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!

Related Topic