5

What I have tried

I've previously used the @web3/solana npm package, but ended up having to create my own (private) library due to using a niche language.

Here is what I'm trying to do:

  • Serialize and sign a transaction (via a 3rd party)
  • Send the signed transaction
  • Wait for the transaction to be processed, and confirm its validity

Here is what I tried:

  • Serialize and sign the transaction
  • Send the transaction via sendTransaction rpc method
  • Wait for the transaction to be committed via signatureSubscribe
  • Use getTransaction to confirm the transaction details, assuming the call to signatureSubscribe did not close its connection early, and received a signatureNotification payload

What the problem is

I've noticed that signatureSubscribe can sometimes abruptly close my connection. This results in my program thinking the transaction is invalid, when the transaction is actually valid.

Is this "transaction confirmation" strategy flawed? If a connection to signatureSubscribe errors, should it be retried?
How can I know for certain whether or not a transaction is fully valid/invalid after sending it via the rpc method?

1 Answer 1

7

Transaction confirmation is a hard problem, which is why the current web3.js code can be very confusing!

The way it currently works is:

  • use signatureSubscribe in one promise, wait to get a confirmation at the level that you care about (processed / confirmed / finalized)
  • in parallel, use getSignatureStatuses to see if it's already been confirmed https://docs.solana.com/api/http#getsignaturestatuses

An older transaction won't create new notifications, so you could get a false negative if you just do signatureSubscribe, so you should also do getSignatureStatuses at first.

And finally, you can be sure that a transaction is fully invalid when the cluster has reached the block height for its blockhash, meaning that the blockhash is no longer valid. This is provided as lastValidBlockHeight when you fetch the blockhash. You can do this by polling getBlockHeight in parallel.

You can see the full web3.js code at https://github.com/solana-labs/solana-web3.js/blob/e76bea942df606a292e845d634db87d6dbf438ce/packages/library-legacy/src/connection.ts#L3840

Not the answer you're looking for? Browse other questions tagged or ask your own question.