使用Web3和Python在多边形区块链上检测新的成对创建事件

2024-10-05 13:13:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个程序,一旦添加了流动性(PairCreated事件),就可以在多边形区块链上持续检测新代币。下面是代码的主要部分

我使用的是quickSwap工厂地址(0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32),因为这似乎是多边形网络的pancakeswap等价物(pancakeswap代码中有参考,我指的是quickSwap)。还使用与BSC相同的ABI,这似乎很好

我已经设法使它在binance智能链上运行良好,但在运行多边形时从未检测到任何东西。然而,我在某个地方读到,多边形显然不支持当前的方法

谁能解释一下需要做些什么才能使这项工作正常进行? 谢谢

#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()

Tags: 代码loopeventasynciodeffilter多边形web3

热门问题