简体   繁体   English

使用 web3.py 和 infura.io 监听 Uniswap 合约事件

[英]Listening to Uniswap contract events using web3.py and infura.io

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.我想使用 web3.py 和 infura.io 来监听 Uniswap 工厂事件,但是我不太确定 go 应该在哪里这样做。

Here is a link: https://uniswap.org/docs/v2/smart-contracts/factory/这是一个链接: https://uniswap.org/docs/v2/smart-contracts/factory/

More specifically I want to listen for the PairCreated event.更具体地说,我想监听PairCreated事件。

Here is rough guide这是粗略的指南

  1. Get ABI for Uniswap contract获取 Uniswap 合约的 ABI

  2. Create a web3.py contract object创建 web3.py合约 object

  3. You can use web3.eth.getLogs() to query events over past block range您可以使用web3.eth.getLogs()来查询过去区块范围内的事件

  4. You cannot query all events once, because there are so many events and Infura would time out.您不能一次查询所有事件,因为事件太多,Infura 会超时。 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这是一些旧代码,可能适用于也可能不适用于最新的 web3.py 版本

https://github.com/TokenMarketNet/sto/blob/master/sto/ethereum/scanner.py#L153 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:如果您想要一个实时扫描仪,您可以在事件发生时通过 WebSocket 连接列出事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM