1

How can I determine whether a transaction completed successfully?

Basically I'm going to have a Cron task that will perform an instruction from my Smart Contract. This instruction, once performed, will send me back a txid. Since it's a Cron Task, I want to be 100% sure that the transaction went through. How can I make sure that my transaction is good (after having performed program.instruction().rpc()) and when in case it has failed, restart it, and those until it is good

2
  • Have you tried fetching the transaction using its signature and checking for commitment status and whether there are any errors?
    – sohrab
    Commented Jul 14, 2022 at 23:29
  • I've improved objectivity and precision of the main question. Can you try to distill the remainder to only what's pertinent to what you'd like to check and how you'd like to respond to failures? All of the "Cron Task" stuff is superfluous, likely to distract from the core and prevent getting to a high quality answer quickly
    – trent.sol
    Commented Jul 14, 2022 at 23:38

1 Answer 1

3

There are two things you need to check:

  1. Did it land on chain?
  2. Did it execute successfully?

For 1) call getSignatureStatuses until you see a finalized commitment (guarantees its landed on chain).

For 2) call getTransaction with a 'finalized' commitment and check to ensure there wasn't an error.

These are the RPC calls, but there are relevant similar calls on the web3 library.

Generally this is the only robust mechanism for knowing a transaction succeeded.

2
  • 1
    So if I follow your advice, I do my instruction, then I do an await of getSignatureStatuses to make sure that the tx has landed on the chain (I guess the function waits to have a result ?) If fail, I start again, if success, I check then by recovering the transaction itself, and if in the meta I have no error then it's good, if not I also start again right ?
    – Swifty
    Commented Jul 15, 2022 at 0:03
  • Yep, you'd want to loop and poll getSignatureStatuses until its done. However, once the chain moves forward 150 slots from your initial submission (technically the blockhash associated with your initial submission), if your transaction hasn't been found it will have never made it into the chain. The real logic to test for this is a bit complex, the simplest workaround is to only loop and check for ~2 minutes, and then timeout.
    – yamen
    Commented Jul 15, 2022 at 6:28

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.