0

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 we try after sometime, they get proper response.

So wanted to ask if we can have any other method where we can listen all new incoming transactions and getTransaction for them?

1

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.

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.

  • 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 Mar 30 '20 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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