web3.eth.getTransaction when pending then returns null

i’m in mainnet.infura.io
my web3 code :

web3.eth.getTransaction(txid, function(err,receipt){
console.log(receipt);
});

if txid is finish ,all ok
if txid is pending , the receipt is null
help me…

I am new to this matter and cant give you a reliable answer, but in my research I stumbled upon the following information on the web3.eth documentation: https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#gettransactionreceipt

The receipt is not available for pending transactions and returns null

I think you can not obtain the receipt for a pending transaction, however you can retrieve other information such as FROM/TO/VALUE/GAS/GASFEE . So if you are interested in retrieving information about pending transaction I would not use the callback function with ‘receipt’ but rewrite such that you analyze the tx.

Hi @1823944494 - welcome to the Infura community!

@sdfg_jajaja is correct with the docs they have linked. Until the tx has been completed, there is no receipt to be had, so that’s why it’s returning null.

If you want to get transaction receipts from completed transactions, check out getTransactionReceipt!

i used getTransaction but not getTransactionReceipt ,
i’m interested in retrieving information about pending transaction ,
but i dont konw how to do this , how to rewrite ? rewrite such that you analyze the tx ?
Give me a little detail please, thks

i’m interested in retrieving information about pending transaction only , so i cant use getTransactionReceipt, what should I do?

you can try something like this:

tx = web3.eth.getTransaction(txid);
console.log({address: tx.from, hash: txid });

Using the getTransaction function you can retrieve informaiton about every transaction even pending

1 Like

I think this tutorial gives you exactly what you need: https://www.youtube.com/watch?v=GSLEz-XxGY8
Check out the second part of the video starting from 13:23 the code is also linked in the description so you can copy it and try it out yourself

1 Like