0

If you look at this transaction in etherscan.io you can see that it notes that the transaction has an error Warning! Error encountered during contract execution [Bad instruction]. Same status can be found here on ethplorer.io

Does anybody know how I can detect this when processing a transaction via web3js?

1

This is typically solved by supplying a callback function when you send the transaction:

    web3.eth.sendTransaction(txParams, function (error, txhash) {
        if (error) {
            // error handling
        }
        else {
            // handling of successful transaction
        }

or

        web3.eth.sendRawTransaction(serializedTx, function (error, txhash) {
        if (error) {
            // error handling
        }
        else {
            // handling of successful transaction
        }

The callback function is invoked either when the transaction was processed successfully or after the transaction ran into an error. If there was an error, the error parameter of the callback function contains an error description, if not the error parameter is undefined. Callback functions have an error first callback style and are generally possible, see the web3.js documentation.

For transactions that are already mined, you can check the value of getTransactionReceipt(txhash).status, which is 0 if the transaction failed an 1 if the transaction was successful. For more information see questions Transaction Status and How do I detect a failed transaction after the Byzantium fork as the REVERT opcode does not consume all gas?. Note that this works only for transactions after the Byzantinum Fork, which was at block 4370000. For transactions made earlier, you can check if the gas spent is equal to max gas, if so you can assume that there was a transaction failure.

  • this is only valid for transactions that I send though. My primary goal is to analyze transactions that already exist and to see if those contain an error. How would I do this? – phoebus Nov 22 '17 at 6:15
  • Ok, understood. I extended the answer. – gisdev_p Nov 22 '17 at 8:06
  • Thank you for extending. Though, when executing, the receipt does not have a status attribute. I am using web3js version 1.0.0-beta.24 – phoebus Nov 22 '17 at 16:28
  • only newer transactions have it, never mind. – phoebus Nov 22 '17 at 17:56
  • Yes, the status field was introduced with the Byzantinum Fork, so you have it for transactions since block 4370000. Before, it was usually checked if the gas spent was equal to max gas, if so there was a transaction failure. – gisdev_p Nov 22 '17 at 18:51

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.