To sponsor gas fees for transactions on Solana, see our guide
here.
To rely on Privy’s API to fill in the recentBlockhash field of the Solana transaction, pass in the
dummy value 11111111111111111111111111111111 for the recentBlockhash field.
To send a transaction from a wallet using the React SDK, use the sendTransaction method from the useSendTransaction hook:
import {useSendTransaction, useSolanaWallets} from '@privy-io/react-auth/solana';import {Connection, Transaction, VersionedTransaction, PublicKey, SystemProgram} from '@solana/web3.js';// Inside your componentconst { sendTransaction } = useSendTransaction();const { wallets } = useSolanaWallets();// Configure your connection to point to the correct Solana networkconst connection = new Connection('https://api.mainnet-beta.solana.com');// Create your transaction (either legacy Transaction or VersionedTransaction)const transaction = new Transaction(); // or new VersionedTransaction()// Build and add your instructions to the transaction...const transferInstruction = SystemProgram.transfer({ fromPubkey: new PublicKey(wallets[0].address), // Replace with the sender's address toPubkey: new PublicKey('RecipientPublicKeyHere'), // Replace with the recipient's address lamports: 1000000 // Amount in lamports (1 SOL = 1,000,000,000 lamports)});transaction.add(transferInstruction);// Fetch and set the latest blockhashconst connection = new Connection('https://api.mainnet-beta.solana.com'); // Replace with your Solana RPC endpointconst latestBlockhash = await connection.getLatestBlockhash();transaction.recentBlockhash = latestBlockhash.blockhash; // Replace with the latest blockhash// Set your address as the fee payertransaction.feePayer = new PublicKey(wallets[0].address); // Set fee payer// Send the transactionconst receipt = await sendTransaction({ transaction: transaction, connection: connection, address: wallets[0].address, // Optional: Specify the wallet to use for signing. If not provided, the first wallet will be used.});console.log("Transaction sent with signature:", receipt.signature);
Address of the wallet to use for sending the transaction. Recommended when working with external wallets to ensure reliable functionality. If not provided, the first wallet will be used.