[Ethereum] Detecting a Reverted Transaction with Web3

contract-developmentmetamaskweb3js

I am using Web3 to submit transactions and it seems as though whenever a transaction is successfully mined but reverted I don't get that error on my frontend. Is there anyway to detect when the transaction is reverted with Web3 so I can display an appropriate message on the front end?

The code I am using is below.

Thanks!

 web3.contractInstance().methods.create().send({gas: 90000, value: 10, from: address})
                        .on('transactionHash', hash => {
                            console.log('TX Hash', hash)
                        })
                        .then(receipt => {
                            console.log('Mined', receipt)
                        })
                        .catch( err => {
                            console.log('Error', err)
                        })
                        .finally(() => {
                            console.log('Extra Code After Everything')
                        })

I receive the receipt but no error on revert. Thanks!

Best Answer

You can get this in status field of receipt. If status is 1, it means the transaction was successful, if 0, it means transaction failed.

web3.contractInstance().methods.create().send({gas: 90000, value: 10, from: address})
                        .on('transactionHash', hash => {
                            console.log('TX Hash', hash)
                        })
                        .then(receipt => {
                            console.log('Mined', receipt)
                            if(receipt.status == '0x1' || receipt.status == 1){
                                console.log('Transaction Success')
                            }
                            else
                                console.log('Transaction Failed')
                        })
                        .catch( err => {
                            console.log('Error', err)
                        })
                        .finally(() => {
                            console.log('Extra Code After Everything')
                        })

This ability was introduced by EIP-658

Related Topic