1

The following includes a null Tx id:

web3.eth.getTransaction('0x2fc36f9c6593bc1232b3466832c6ee38b5b2b3b4a54be3f6c68fb06e8d82e452', function(error, result){ 
  if (!error) {
    console.log(result.blockNumber)
  }
})

You would expect this to log null or undefined, but it throws in Web3.js 1.2.1. This makes it impossible to use getTransaction with polling to see if a transaction is confirmed.

checkTx()
function checkTx() {
    web3.eth.getTransaction('0x2fc36f9c6593bc1232b3466832c6ee38b5b2b3b4a54be3f6c68fb06e8d82e452')
        .then((result) => {
            if (result.blockNumber != null) {
                console.log(result.value)
            } else {
                console.log('Not confirmed.')
                setTimeout(() => { checkTx() }, 1000)
            }
        })
} 

In both case you get this error when the Tx blockNumber is null: TypeError: Cannot read property 'blockNumber' of null

How can I check that a transaction id is confirmed by polling if I can't evaluate the property of the blockNumber?

1

In your first example, you are trying to print result.blockNumber without verifying first that the result object is valid.

In your second example, you are referring to the first input parameter of the callback function as if it was the result object, when it is in fact the error object.

In short, try this:

web3.eth.getTransaction('0x2fc3...', function(error, result) { 
    if (result && result.blockNumber) {
        console.log(result.value);
    }
    else {
        console.log('Not confirmed');
        setTimeout(() => {checkTx()}, 1000);
    }
});
| improve this answer | |
  • If I try to verify it is valid it results in the same error though. Is this something related to promises in Web3? Your callback works as expected. All of my code is callbacks, but I was trying to do this with promises as if I use callbacks it's going to be 12 callbacks nested at least in this function and it's going to need to be worked on by others, and that's no fun for them. :-) – TrumpPaiPence Jan 5 at 18:46
  • 1
    @TrumpPaiPence: No, it has nothing to do with promises. Did you read my explanation regarding each one of your coding examples? – goodvibration Jan 5 at 18:47
  • Ahh, see your edit now. I evaluated what didn't exist. I think I went wrong because was first using getTransactionReceipt and that can only be two states, null and confirmed. getTransaction can be null, pending, and confirmed. I omitted the possibility of null for some reason in my mind and then ran into this. – TrumpPaiPence Jan 5 at 18:52

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.