Error when calling web3.sendRawTransaction(signature.serialize())

438 Views Asked by At

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

There are 1 best solutions below

0
On

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