Web3.js Guide – How to Get Pending and Failed Transactions

web3js

I want my front end to show the status of my solidity functions. When my functions are successful I fire a "success" event. I can watch for that event and respond to it like so:

const events = myContract.allEvents({ fromBlock: 0, toBlock: 'latest' })
      events.watch( (err, evt) => {}

However, I would also like to display when these functions are pending and when they have failed, but I cant figure out how.

I notice that when I do this:

await web3.eth.filter("pending").watch(
      function(error,result){
          if (!error) {
              console.log("pending" + result);
          }
      }
    ) 

I can see various transactions as they are pending, but I'm not sure how to tell if one of them corresponds to one of the functions I care about.

I'm not even sure how I would figure out if a transaction has failed.

Any help would be great thanks!

Best Answer

I can see various transactions as they are pending, but I'm not sure how to tell if one of them corresponds to one of the functions I care about.

After you make a transaction, web3 gives you a transaction hash. This uniquely identifies the transaction.

I'm not even sure how I would figure out if a transaction has failed.

You use eth.getTransactionReceipt(transactionHash) (after transaction is mined) and check the status field. A status of 1 denotes success, whereas 0 denotes failure.

Related Topic