I have a payment transaction that might take a while to finalize, and I am trying to write a function that will take a transaction id as an input, and return either "success", "failure" or "pending". I will call at regular intervals until the result resolves to either "success" or "failure".

It seems like its fairly easy to establish the "success" case with:

 let tx = await provider.getTransaction(txId);
 if(tx.blockNumber !==null) return success;

I'm less certain if I am missing the obvious way to distinguish between the three possible cases if tx.blocknumber comes back null namely,

  • The transaction is pending being mined
  • The transaction failed and was reverted
  • The transaction was replaced by another transaction in an attempt to speed up.

It seems like the one documented way might be to call

 await tx.wait(0);

which hopefully will return immediately, returning null if the transaction hash't been mined, or reverting with an error if the transaction either failed or was reverted. In that case I still need to discriminate between the two cases, which I gather I have to do by checking whether the error type was CALL_EXCEPTION or TRANSACTION_REPLACED. If TRANSACTION_REPLACED, I guess I need to get the new transaction ID and start from the top.

So my question is, isn't there an easier way to find out if transaction failed or succeeded (or is still pending)? The above seems quite labor intensive.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.