[英]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 收據(例如包含執行狀態 [成功/失敗] 和發出的事件日志)在交易被挖掘時變得可用,因為那是生成執行狀態和事件日志的時候。
未執行事務的from
、 to
和其他字段在“常規” getTransaction()
function 中可用。
對於未決交易,請使用:
txn = w3.eth.get_transaction(event)
txHash = w3.toJSON(txn['hash'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.