0

I want to sign a transaction of a user from phantom wallet and then send the transaction through web3.js but after successfully signing the transaction the web3js library function sendRawTransaction() is giving error message in console

const signedTransaction = await window.solana.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
await connection.confirmTransaction(signature);

1 Answers1

-1

If you look at the implementation of sendTransaction, you'll see that it's adding a blockhash to the transaction before signing, serializing, and sending. Without a blockhash, you'll get that error Blockhash not found. So instead, you need to do something like:

const latestBlockhash = await connection.getLatestBlockhash();
transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
transaction.recentBlockhash = latestBlockhash.blockhash;
const signedTransaction = await window.solana.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
await connection.confirmTransaction(signature);

Full implementation of sendTransaction at https://github.com/solana-labs/solana/blob/3fcdc45092b969baeb7273de6596399d98277366/web3.js/src/connection.ts#L4389

Jon C
  • 7,019
  • 10
  • 17