Transaction Recent Blockhash Required - Phantom Wallet Solana

Viewed 2442
Uncaught (in promise) WalletSignTransactionError: Transaction recentBlockhash required
at PhantomWalletAdapter.signTransaction (adapter.ts:215:1)
at async WalletProvider.tsx:247:1
at async mintNFT (luckoo.tsx:100:1)

I got this when im trying to send transaction using solana wallet, what should i do?

2 Answers

You need to provide a recentBlockhash in your transaction in order for it to be signed. Most likely, you need to fetch a blockhash and add it before signing the transaction using getLatestBlockhash, ie:

let blockhash = await connection.getLatestBlockhash('finalized').blockhash;
transaction.recentBlockhash = blockhash;

Previously, you would use the similar deprecated function getRecentBlockhash:

let blockhash = await connection.getRecentBlockhash('finalized').blockhash;
transaction.recentBlockhash = blockhash;

Your better bet is to directly use sendTransaction, which does all of that work for you. As you can see in the source code, sendTransaction is filling in the recent blockhash if it isn't already present: https://github.com/solana-labs/wallet-adapter/blob/a5b1ebd70ae9753d188fec60e95e252402f3f371/packages/core/base/src/signer.ts#L13

Use this

let blockhash = (await connection.getLatestBlockhash("finalized")).blockhash;
console.log("recentBlockhash: ", blockhash);
Related