1

I have subscribed to ETH pendingTransactions in mainnet. I am doing getTransaction for each received pendingTransaction like below.

web3.eth.subscribe('pendingTransactions', function(error, txnid) {
    if (txnid) {
        web3.eth.getTransaction(txnid, function(err, txn) {
            if (!err) {
                console.log(txn);
            }
            else
                console.log(err);
        });
    }
    else
        console.log(error);
});

My issue is, I am getting txn=null for some of the txnid. And if I try after some time, I get proper response.

Is there any other method to listen all new incoming transactions and getTransaction for them?

2 Answers 2

5

This question is two years old by now. But I wanted to answer, because the original answer is not the correct explanation. As the comment says, Geth does not require transactions be mined before making them available on the getTransaction() API.

Rather, the issue is that Geth's txPool consistency model is not full read-after-write. When a tx hash is published to the subscriber, it is not guaranteed to be processed for serving on the API. If you try to fetch getTransaction() immediately after, you may hit a race condition and get a null (i.e. not found) result.

The solution isn't to wait for the next block to be mined, it's to retry in a short interval. Geth does support eventual consistency, typically on the scale of milliseconds, so you will see it well before the next block.

1
  • 1 year later;) Treating the race condition fix some of these, others are "zombies" (nonce double/replacement, tx failing, low gas, etc) waiting to get purged (accounting for a lot of those null's nowadays) Commented Sep 28, 2021 at 12:46
2

when transaction is just submitted into network, its still pending, you need to wait for it to be mined then only the web3.eth.getTransaction return correct value, put in in the loop with settimeout.

1
  • This is not correct, getTransaction returns transaction's data even when they are pending, some fields like block will be missing in that case.
    – Ismael
    Commented Mar 30, 2020 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.