首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Web3和Python在多边形区块链上检测新的PairCreated事件

使用Web3和Python在多边形区块链上检测新的PairCreated事件
EN

Stack Overflow用户
提问于 2021-09-24 16:49:22
回答 1查看 1.1K关注 0票数 1

我正在尝试编写一个程序,一旦添加了流动性(PairCreated事件),它就会不断检测多边形区块链上的新令牌。下面是代码的主要部分。

我使用quickSwap工厂地址(0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32),因为这似乎是多边形网络的pancakeswap等价物(在pancakeswap的代码中有引用,我指的是快速交换)。也使用与BSC相同的ABI,这看起来很好。

我已经设法让它在binance智能链上工作得很好,但在运行多边形时没有检测到任何东西。然而,我在某处读到,显然Polygon不支持当前方法。

有没有人能解释一下需要做些什么才能让这件事起作用?谢谢!

代码语言:javascript
运行
复制
#import web3 and all other modules

web3 = Web3(Web3.WebsocketProvider(bscNode))

contract = web3.eth.contract(address=pancakeSwapFactoryAddress, abi=listeningABI)

def foundToken(event):
    jsonEventContents = json.loads(Web3.toJSON(event))
    #process token data etc
        

        
async def tokenLoop(event_filter, poll_interval):
    while True:
        try:
            for PairCreated in event_filter.get_new_entries():
                foundToken(PairCreated)
            await asyncio.sleep(poll_interval)
        except:
            pass
            
            
def listenForTokens():
    event_filter = contract.events.PairCreated.createFilter(fromBlock='latest')
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(
            asyncio.gather(
                tokenLoop(event_filter, 2)))       
                 
    finally:
        listenForTokens()


listenForTokens()
EN

回答 1

Stack Overflow用户

发布于 2021-11-20 10:48:40

下面的方法对我很有效:

代码语言:javascript
运行
复制
pair_created_event = contract.events.PairCreated()
event_filter = pair_created_event.createFilter(fromBlock='latest')
entries = event_filter.get_new_entries()
print(entries)

您需要调用PairCreated事件,而不仅仅是使用引用。

另外,请检查您正在使用的合同地址。对于PancakeSwap,您可以在其文档中找到官方文档:https://docs.pancakeswap.finance/code/smart-contracts#main-contracts

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69318614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档