4

When using sendAndConfirmTransaction with a commitment, or pre-flight commitment level of finalized and it fails, is it guaranteed to be safe to retry the function call again?

The solana cookbook explains a method of avoiding duplicate signed transactions by comparing the last valid blockheight used.
I'd prefer not having to do this. Would the method I explained above be enough?

2 Answers 2

2

First you have to define "fails":

  • If it returns an error on the transaction with a finalized commitment, it is safe to retry given the commitment guarantees
  • Another failure scenario is the awaiter timing out, older @solana/web3.js package version are not safe on that regard as it awaits a set amount of time regardless of the blockhash validity.

Use instead a recent @solana/web3.js package version and make sure sendAndConfirmRawTransaction function uses the BlockheightBasedTransactionConfirmationStrategy stategy which does what the cookbook recommends to do, for you, for free.

Link to the source code of the function:

https://github.com/solana-labs/solana/blob/f57f228126b01eec2a48cc39711252dfacf04462/web3.js/src/util/send-and-confirm-raw-transaction.ts#L40-L48

2

What makes rebroadcasting a transaction "safe" or not is whether or not the same instructions are being resigned with a new recent blockhash. This should never be done so long as a previously broadcast transaction's recent blockhash is extant. Instead it can be simply rebroadcast until it expires via sendAndConfirmRawTransaction, as suggested by @arowana. You will likely need to query and set the recent blockhash by hand as well as sign and serialize. The "convenience" function sendAndConfirmTransacion does so much behind the scenes as to be considered harmful in all but the most trivial example code.

1
  • So... sendAndConfirmRawTransaction is better than sendAndConfirmTransacion because sendAndConfirmRawTransaction has some type of retry functionality built-in ? Commented Sep 29, 2022 at 21:55

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.