[Ethereum] How to get transaction failed reason with transaction hash with web3

transactionsweb3js

I'm trying to get transaction failed reason with transaction hash with web3?

I have checked getTransactionReceipt() method

{ blockHash: '0x29b253c6b69bc632535fd3ee20a5fd48a66635b9baea43bc3985d4048c40363e',
  blockNumber: 3345585,
  contractAddress: null,
  cumulativeGasUsed: 738250,
  from: '0x01312d9393560dd371fc6ceb0858377ea4f5f96b',
  gasUsed: 93263,
  logs: [],
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  status: false,
  to: '0x71c46ed333c35e4e6c62d32dc7c8f00d125b4fee',
  transactionHash: '0x9867604300bb4ea4c0dda68e6ad3a400e7df55e7695aadd80058595815cdae78',
  transactionIndex: 3 }

and getTransaction() method

{ blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  blockNumber: null,
  from: '0x3e823606431003B42878267faD2B8B520327CC01',
  gas: 7000000,
  gasPrice: '20000000000',
  hash: '0xbe863134a0192ea9ba42f619a98db343ccbae84e3caded7d09ee4db2dab31de0',
  input: '0xd377234b00000000000000000000000000000000000000000000000000000000000004df000000000000000000000000bd5dbffe75274258bc9d0d907f957a2d0774d60f',
  nonce: 2844,
  to: '0x2b3b6353e8E9897069E40b15d615AC3bBeEa1cFD',
  transactionIndex: 0,
  value: '0',
  v: '0x1c',
  r: '0x3244a729240b0c9da3578177d906bb9c3db2db4a8e9844983d8b235e4137d34e',
  s: '0x30a6ca26f46baa0eff3abcd49f7a581e8fde962afa5adb9b03e16c0bc4db0e97' }

Both methods do not have any reason field but I have checked in etherscan it's showing the failed reason in the transaction as below image.

enter image description here
enter image description here
is there any way to get reason like etherscan using web3?
I know we can get status with a status key but my concern is I need reason only how transaction got failed.

Best Answer

The answer is

status: false

This indicates what you see on etherscan. If you try getting this field through web3, the response is usually 0x0 or 0x1.

So here you go:

web3.eth.getTransactionReceipt(txID, function (e, data) {
            if (e !== null) {
                console.log("Could not find a transaction for your id! ID you provided was " + txID);
            } else {
                console.log(data);
                if(data.status == '0x0') {
                    console.log("The contract execution was not successful, check your transaction !");
                } else {
                    console.log("Execution worked fine!");
                }
Related Topic