Web3py Infura – Fix ‘eth_newFilter Does Not Exist’ Error When Getting Events

infuraweb3.py

I'm trying to get all events from contract, but get error:
"ValueError: {'code': -32601, 'message': 'The method eth_newFilter does not exist/is not available'}"

I'm using Web3py and Infura. My code:

myfilter = <myContract>.events.<MyEvent>.createFilter(fromBlock=0)

or

myFilter = web3.eth.filter({"address": <myContractAddress>})

Why is this error? How do I get events?
Help me please.

Best Answer

The issue is that Infura only exposes the eth_newFilter endpoint via websockets, but you are connecting via https.

Try using a WebsocketProvider when declaring your web3 instance, as shown in the following example:

from web3 import Web3

PROVIDER = "wss://ropsten.infura.io/ws/v3/YOUR-PROJECT-ID"
web3 = Web3(Web3.WebsocketProvider(PROVIDER))
Related Topic