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
signAndSendTransaction
method from the useSignAndSendTransaction
hook:Report incorrect code
Copy
Ask AI
signAndSendTransaction: (input: {
transaction: Uint8Array<ArrayBufferLike>;
wallet: ConnectedStandardSolanaWallet;
chain?: SolanaChain;
options?: (SolanaSignTransactionOptions & {
mode?: SolanaSignAndSendTransactionMode;
commitment?: SolanaTransactionCommitment;
skipPreflight?: boolean;
maxRetries?: number;
} & {
uiOptions?: SendTransactionModalUIOptions;
})
}) => Promise<SolanaTransactionReceipt>
Usage
Report incorrect code
Copy
Ask AI
import {useSignAndSendTransaction, useWallets} from '@privy-io/react-auth/solana';
import {
pipe,
createSolanaRpc,
getTransactionEncoder,
createTransactionMessage,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash,
appendTransactionMessageInstructions,
compileTransaction,
address,
createNoopSigner
} from '@solana/kit';
import {getTransferSolInstruction} from '@solana-program/system';
// Inside your component
const {signAndSendTransaction} = useSignAndSendTransaction();
const {wallets} = useWallets();
const selectedWallet = wallets[0];
const amount = 1;
const transferInstruction = getTransferSolInstruction({
amount: BigInt(parseFloat(amount) * 1_000_000_000), // Convert SOL to lamports
destination: address('RecipientAddressHere'),
source: createNoopSigner(address(selectedWallet.address))
});
// Configure your RPC connection to point to the correct Solana network
const {getLatestBlockhash} = createSolanaRpc('https://api.mainnet-beta.solana.com'); // Replace with your Solana RPC endpoint
const {value: latestBlockhash} = await getLatestBlockhash().send();
// Create a transaction using @solana/kit
const transaction = pipe(
createTransactionMessage({version: 0}),
(tx) => setTransactionMessageFeePayer(address(selectedWallet.address), tx), // Set the message fee payer
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), // Set recent blockhash
(tx) => appendTransactionMessageInstructions([transferInstruction], tx), // Add your instructions to the transaction
(tx) => compileTransaction(tx), // Compile the transaction
(tx) => new Uint8Array(getTransactionEncoder().encode(tx)) // Finally encode the transaction
);
// Send the transaction
const receipt = await signAndSendTransaction({
transaction: transaction,
wallet: selectedWallet
});
console.log('Transaction sent with signature:', receipt.signature);
Parameters
Hide parameters
Hide parameters
The encoded transaction to be sent.
The Solana wallet to use for sending the transaction.
Type of all Solana chains supported by Privy.
Additional options for sending the transaction.
Show child attributes
Show child attributes
Available options:
serial
and parallel
.Available options:
processed
, confirmed
, finalized
. Default is finalized
.If true, skips the preflight transaction checks. Default is false.
The maximum number of times to retry sending the transaction in case of failure. Default is
3.
The minimum slot that the client is willing to accept for the transaction. Default is
undefined.
Commitment level used for preflight checks. Default is
finalized
.Returns
The signature of the transaction.
Sign and send all transactions
To sign and send multiple transactions in a single call, use thesignAndSendAllTransactions
method from the ConnectedStandardSolanaWallet
object:Report incorrect code
Copy
Ask AI
signAndSendAllTransactions(inputs: SolanaSignAndSendTransactionInput[]): Promise<SolanaSignAndSendTransactionOutput[]>
Usage
Report incorrect code
Copy
Ask AI
import {useWallets} from '@privy-io/react-auth/solana';
import {
pipe,
createSolanaRpc,
getTransactionEncoder,
createTransactionMessage,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash,
appendTransactionMessageInstructions,
compileTransaction,
address,
createNoopSigner
} from '@solana/kit';
import {getTransferSolInstruction} from '@solana-program/system';
// Inside your component
const {wallets} = useWallets();
const selectedWallet = wallets[0];
const amount = 1;
const transferInstruction = getTransferSolInstruction({
amount: BigInt(parseFloat(amount) * 1_000_000_000), // Convert SOL to lamports
destination: address('RecipientAddressHere'),
source: createNoopSigner(address(selectedWallet.address))
});
// Configure your RPC connection to point to the correct Solana network
const {getLatestBlockhash} = createSolanaRpc('https://api.mainnet-beta.solana.com'); // Replace with your Solana RPC endpoint
const {value: latestBlockhash} = await getLatestBlockhash().send();
// Create transactions using @solana/kit
const transactions = [
pipe(
createTransactionMessage({version: 0}),
(tx) => setTransactionMessageFeePayer(address(selectedWallet.address), tx), // Set the message fee payer
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), // Set recent blockhash
(tx) => appendTransactionMessageInstructions([transferInstruction], tx), // Add your instructions to the transaction
(tx) => compileTransaction(tx), // Compile the transaction
(tx) => new Uint8Array(getTransactionEncoder().encode(tx)) // Finally encode the transaction
),
pipe(
createTransactionMessage({version: 0}),
(tx) => setTransactionMessageFeePayer(address(selectedWallet.address), tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
(tx) => appendTransactionMessageInstructions([transferInstruction], tx),
(tx) => compileTransaction(tx),
(tx) => new Uint8Array(getTransactionEncoder().encode(tx))
)
];
// Send the transaction
const receipts = await wallet.signAndSendAllTransactions(
transactions.map((transaction) => ({
chain: 'solana:mainnet',
transaction
}))
);
console.log(
'Transaction sent with signature:',
receipts.map((receipt) => receipt.signature.toString()).join(',')
);
Parameters
Hide parameters
Hide parameters
The encoded transaction to be sent.
Type of all Solana chains supported by Privy.
Additional options for sending the transaction.
Show child attributes
Show child attributes
Available options:
serial
and parallel
.Available options:
processed
, confirmed
, finalized
. Default is finalized
.If true, skips the preflight transaction checks. Default is false.
The maximum number of times to retry sending the transaction in case of failure. Default
is 3.
The minimum slot that the client is willing to accept for the transaction. Default is
undefined.
Commitment level used for preflight checks. Default is
finalized
.For complete examples of sending SOL and SPL tokens using Privy’s SDKs, check out the sending a
SOL transaction recipe and the sending SPL tokens
recipe.