繁体   English   中英

以太坊 - 未找到交易

[英]Ethereum - transaction not found

我正在尝试 stream 来自以太坊链的新待处理交易,因为我正在使用 web3py。 我的代码的问题是,在每个新的未决交易中,它都会给出一个Transaction with hash xx not found

这是我的代码:

from web3 import Web3
import asyncio, time
from hexbytes import HexBytes # the read hexabyte data
import web3 as web3
import logging
import requests
import json

Infura_HTTP = 'MY-PROVIDER'
Infura_WS = 'PROVIDER'

w3_ws = Web3(Web3.WebsocketProvider(Infura_WS))
w3 = Web3(Web3.HTTPProvider(Infura_HTTP))


async def handle_event(event):
    txHash = HexBytes.hex(event)

    try:
        print(txHash, w3.eth.getTransactionReceipt(txHash))

    except Exception as e:
        print('Error in handle_event', e)


async def log_loop(event_filter, poll_interval):
    while True:
        try:
            for event in event_filter.get_new_entries():
                await handle_event(event)
            await asyncio.sleep(poll_interval)
        
        except Exception as e:
            print(e)

def main():
    global loop
    
    block_filter = w3_ws.eth.filter('pending')
    loop = asyncio.get_event_loop()

    try:
        loop.run_until_complete(
            asyncio.gather(
                log_loop(block_filter, 2)))
    finally:
        loop.close()

if __name__ == '__main__':
    main()

在这里,我正在使用过滤器监听新的待处理事务,每当有新事务时我需要从中获取数据(从、到、数据等等),因此我使用getTransaction 我还能用什么做这个?

TLDR:根据设计,收据不适用于待处理的交易。


您的代码正在执行getTransactionReceipt(txHash)与尚未被挖掘的txHash (即待处理)。

Tx 收据(例如包含执行状态 [成功/失败] 和发出的事件日志)在交易被挖掘时变得可用,因为那是生成执行状态和事件日志的时候。

未执行事务的fromto和其他字段在“常规” getTransaction() function 中可用。

对于未决交易,请使用:

txn = w3.eth.get_transaction(event)
txHash = w3.toJSON(txn['hash'])

暂无
暂无

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

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