[Ethereum] Web3py – how to get pending transaction data

pythonweb3.pyweb3js

I'm using web3py to receive new pending transactions. The problem is that i'm only receiving the hash of the transaction, while i would like to get that transaction's data too, such as input, sender and so on. If i use getTransaction and getTransactionReceipt i always get a Transaction with hash xx not found, what else can i use for this?

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

Best Answer

By definition, transaction receipts can be only available after the transaction is mined and the state transition is complete. Because until the transaction is mined, you do not know if it is rejected or not.

So what you are asking is impossible.

Related Topic