4

Just started looking at Web3 and I'm currently trying to listen for an event emitted by a smart contract on the Polygon network by using

event_filter = contract.events.<MyEvent>.createFilter(fromBlock='latest')

I've tried three different providers, Infura (https), maticvigil and Alchemy (both https and wss), but when calling the method createFilter I get the response

{'code': -32601, 'message': 'Unsupported method: eth_newFilter.'}

When using the method in the Ethereum mainnet everything goes smoothly, but as soon as I change to a Polygon provider I get a similar error for all providers.

If I understand what's happening, that means that in order for me to listen to an event using that Web3 method the provider has to implement eth_newFilter

So my question is, does anyone happen to know of a provider for the Polygon network that implements the eth_newFilter method on their API?

Alternatively I would appreciate some guidance/sources on how to listen for a particular event triggered by a smart contract on the Polygon network without having to rely on a provider, thanks.

1 Answer 1

9

For future readers, apparently Polygon does not support eth_newFilter so a way to get events thru Web3 is subscribing to logs (currently available thru Web3.js) or using get_logs (if u wanna do it in python), I found an example in a reddit post, goes like this:

from web3 import Web3
from web3._utils.events import get_event_data

w3 = Web3(Web3.HTTPProvider("<Infura host>"))
contract = w3.eth.contract(address="0x33..", abi=abi['abi'])
event_template = contract.events.<EVENT_NAME>
events = w3.eth.get_logs({'fromBlock':from_block, 'toBlock': from_block+10000, 'address':"0x33.."})


def handle_event(event, event_template):
    try:
        result = get_event_data(event_template.web3.codec, event_template._get_event_abi(), event)
        return True, result
    except:
        return False, None

for event in events: 
    suc, res = self.handle_event(event=event, event_template=event_template)   
    if suc:
        print("Event found", res)
1
  • This totally worked! Waiting for the WS version once Infura decide to publish it.
    – max246
    Feb 20, 2022 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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