Listening to Uniswap contract events using web3.py and infura.io
Asked Answered
S

1

5

I want to use web3.py and infura.io to listen for Uniswap factory events however I am not too sure where to go about doing so.

Here is a link: https://uniswap.org/docs/v2/smart-contracts/factory/

More specifically I want to listen for the PairCreated event.

Sassan answered 14/6, 2020 at 19:23 Comment(0)
N
11

Here is rough guide

  1. Get ABI for Uniswap contract

  2. Create a web3.py contract object

  3. You can use web3.eth.getLogs() to query events over past block range

  4. You cannot query all events once, because there are so many events and Infura would time out. Instead you need to query events carefully over a block range slices.

Here is some old code which may or may not work with the latest web3.py versions

https://github.com/TokenMarketNet/sto/blob/master/sto/ethereum/scanner.py#L153

If you want a real time scanner you can listed to events over a WebSocket connection as they happen:

https://web3py.readthedocs.io/en/stable/filters.html#asynchronous-filter-polling

Nearly answered 25/6, 2020 at 8:22 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.