3

I'm attempting to create a website that allows a user to send a payment for a service. In order to send a transaction on behalf of the user, I have them sign the transaction,which is then serialized and sent to the backend for further processing.

Quite occasionally, I receive this preflight error message when using the sendTransaction rpc method: Transaction simulation failed: Blockhash not found.

This makes using our service quite unreliable, as maybe even as much as 1/4 of the requests results in this error. How does one avoid this?

Below is how I ask the user to sign the transaction, and serialize the transaction.

const transaction = new Transaction()
transaction.add(instruction) // Assume this is a transfer instruction
const { blockhash } = await connection.getLatestBlockhash("finalized")
transaction.recentBlockhash = blockhash
transaction.feePayer = feePayer // Assume this public key is defined
const signedTransaction = await signTransaction(transaction) // From @solana/wallet-adapter-react
const signature = signedTransaction.serialize()

1 Answer 1

5

Most likely, the user is taking too long to sign the transaction, so by the time you send the transaction, the blockhash has expired. There's a few options to make this better:

The easiest is to get a slightly newer blockhash that's been "confirmed" rather than "finalized", so you have more time to sign the transaction. With this code, if your user spends too long to sign the transaction on their wallet, the blockhash could expire, ie:

const { blockhash } = await connection.getLatestBlockhash("confirmed")

Otherwise, you can rely on the wallet to add the blockhash before signing the transaction, and that way your user can take as long as needed to examine the transaction before pressing "sign" in their wallet. I'm not sure if the API you're using supports that though.

1
  • Hi. For me, it worked the opposite way. I was using "confirmed". I changed to "finalized" and it worked. So I suggest testing both if you are getting "Blockhash not found" Commented May 3 at 17:12

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.