5

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.

1
  • Hi, did you found a solution? Oct 8, 2021 at 7:47

3 Answers 3

2

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!");
                }
4
  • 1
    Absolutely. The reason why your transaction failed (which it technically didn't) was that the execution of your code lead to a revert(). If you would like to find out, why your contract code could not be executed, then this is what you should probably ask.
    – n1cK
    Jun 26, 2018 at 13:16
  • 1
    Have seen the etherscan Image? In the image, they are showing the exact reason why this transaction got failed. Need this reason with web3js. Jun 26, 2018 at 13:24
  • 1
    The only information Etherscan gives you is that the transaction was reverted. This is indicated by the status field in the transaction receipt. @NikitaFuchs 's code is correct: it translates that status: false to a human readable message.
    – Henk
    Jun 26, 2018 at 13:42
  • 4
    It's easy to know if the tx success or fail but the question is "get reason like etherscan". So this answer isn't useful. Oct 8, 2021 at 7:45
0

Using Parity, you can do eth_tracetranssction.

The reason for the reversion will appear in the error field (if not empty — empty means success means receipt.status == 0x1).

0

You can use Tenderly API to do it, here is a sample:

GET https://api.tenderly.co/api/v1/public-contract/{chainId}/tx/{txHash}

Set the chain id and transaction hash in the request path.

By calling this API, you could check the failure reason in the error_message field or in the stack_trace[i].error

Here is a sample failed transaction:

https://polygonscan.com/tx/0x7473494c4d6ac6976193f67e624dbd0963a321dc883ea8d90e115f5246a3b6d2

As it is polygon and polygon chain id is 137, you could call this API like this:

curl 'https://api.tenderly.co/api/v1/public-contract/137/tx/0x7473494c4d6ac6976193f67e624dbd0963a321dc883ea8d90e115f5246a3b6d2' \
      -H 'authority: api.tenderly.co' \
      -H 'accept: application/json, text/plain, */*' \
      -H 'accept-language: fa,en-US;q=0.9,en;q=0.8,es;q=0.7' \
      -H 'origin: https://dashboard.tenderly.co' \
      -H 'referer: https://dashboard.tenderly.co/' \
       --compressed

This is the API response:

{
  "hash": "0x7473494c4d6ac6976193f67e624dbd0963a321dc883ea8d90e115f5246a3b6d2",
  "error_message": "ERC20: transfer amount exceeds balance",
  ...
}
1
  • This answer was flagged as a duplicate. May have been better to use the transaction hash in the question.
    – eth
    Dec 4, 2022 at 3:24

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.