1

Let's say we have 1000 contracts with the same type of event 'Event'. I can subscribe to each one of them individually using contract.listen('Event'), but I cannot use this if I want to create a single handshake that sends me every 'Event' happening in each of them. For that I would need to create 1000 contracts and then call .listen('Event') a 1000 times.

Is there a way to do something like w3.listen([contracts],'Event')? Or some feasible way to subscribe to so many contracts at the same time?

Thanks

Hiperfly
  • 459
  • 4
  • 11

2 Answers2

1

The best way to is to listen for the event topic signature for all new events the signature in all new blocks. I believe you can do this with a single filter. Then you filter it out on the application side if the event is not part of a contract set you are interested in.

Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127
1

I made it following the suggestion of Mikko. Here is the piece of code I used (I'm using Infura so I cannot directly use filters):

async def event_filter(list_of_pools,list_of_topics):
    msg_events = {"id": 1, "method": "eth_subscribe", "params": ["logs", {"address":list_of_pools, "topics": list_of_topics}]}
    async with websockets.connect(API) as ws:
        await ws.send(json.dumps(msg_events))
        while ws.open:
            response = json.loads(await ws.recv())
            print(response)
Hiperfly
  • 459
  • 4
  • 11