Reputation: 263
I'm trying to stream new pending transactions from the ethereum chain, for that i'm using web3py. The problem with my code is that on every new pending transaction it gives a Transaction with hash xx not found
.
Here is my code:
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()
Here i'm listening for new pending transaction using a filter, whenever there is a new one i need to get data from it (from, to, data and so on), so for that i use getTransaction
. What else can i use for this?
Upvotes: 0
Views: 2059
Reputation: 293
For pending transactions use:
txn = w3.eth.get_transaction(event)
txHash = w3.toJSON(txn['hash'])
Upvotes: 0
Reputation: 43591
TLDR: Receipt is not available for pending transactions by design.
Your code is executing getTransactionReceipt(txHash)
with txHash
that hasn't been mined yet (i.e. is pending).
Tx receipt (containing for example execution status [success/fail] and emitted event logs) becomes available when the transaction is mined, because that's when the execution status and event logs are generated.
from
, to
, and other fields of the unexecuted transaction are available in the "regular" getTransaction()
function.
Upvotes: 1