3

For web3.py==v5.0.1:

web3.geth.shh.newMessageFilter used to return the filter on v4.x.x but now it only returns the filter_id, so I received the filter object using w3.eth.filter(filter_id='0x').

The approach on the following code was working on web3==v4.x.x, but now I receive following error: shh: ValueError: {'code': -32000, 'message': 'filter not found'}for Listening For Events.


Example code:

from web3.auto import w3
import time

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))

def handle_event(event):
    print(event)

def log_loop(event_filter, poll_interval):
    while True:
        for event in event_filter.get_new_entries():
            handle_event(event)
        time.sleep(poll_interval)

def main():
    filter_id = web3.geth.shh.newMessageFilter({'topic': '0x12340000', 'privateKeyID': recipient_private})
    myFilter = web3.eth.filter(filter_id=filter_id)
    log_loop(myFilter, 2)

if __name__ == '__main__':
    main()

The error I am getting:

Traceback (most recent call last):
  File "example.py", line 22, in <module>
    main()
  File "example.py", line 19, in main
    log_loop(myFilter, 2)
  File "example.py", line 12, in log_loop
    for event in event_filter.get_new_entries():
  File "/home/netlab/venv/lib/python3.6/site-packages/web3/_utils/filters.py", line 124, in get_new_entries
    log_entries = self._filter_valid_entries(self.web3.eth.getFilterChanges(self.filter_id))
  File "/home/netlab/venv/lib/python3.6/site-packages/web3/eth.py", line 402, in getFilterChanges
    "eth_getFilterChanges", [filter_id],
  File "/home/netlab/venv/lib/python3.6/site-packages/web3/manager.py", line 97, in request_blocking
    raise ValueError(response["error"])
ValueError: {'code': -32000, 'message': 'filter not found'}
1

Shh.newMessageFilter(self, criteria)

Create a new filter id. This filter id can be used with ShhFilter to poll for new messages that match the set of criteria.

I was able to fix the problem by changing:

for event in event_filter.get_new_entries(): => as for event in w3.geth.shh.getMessages(filter_id):


from web3.auto import w3
import time

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))

def handle_event(event):
    print(event)

def log_loop(filter_id, poll_interval):
    while True:
        for event in w3.geth.shh.getMessages(filter_id):
            handle_event(event)
        time.sleep(poll_interval)

def main():
    filter_id = web3.geth.shh.newMessageFilter({'topic': '0x12340000', 'privateKeyID': recipient_private})  
    log_loop(filter_id, 2)

if __name__ == '__main__':
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.