Send Versioned Transaction
Learn how to create and send versioned transactions using the Gorbag wallet on Gorbagana.
Basic Versioned Transaction
Send a SOL transfer using a versioned transaction (V0):
JAVASCRIPT
1import { useConnection, useWallet } from '@solana/wallet-adapter-react';2import { 3 LAMPORTS_PER_SOL, 4 PublicKey, 5 TransactionMessage,6 VersionedTransaction,7 SystemProgram 8} from '@solana/web3.js';910export function SendVersionedTransaction() {11 const { connection } = useConnection();12 const { publicKey, sendTransaction } = useWallet();1314 const sendSol = async () => {15 if (!publicKey) {16 throw new Error('Wallet not connected!');17 }1819 // Get the latest blockhash20 const { blockhash } = await connection.getLatestBlockhash();2122 // Create a transaction message23 const transactionMessage = new TransactionMessage({24 payerKey: publicKey,25 recentBlockhash: blockhash,26 instructions: [27 SystemProgram.transfer({28 fromPubkey: publicKey,29 toPubkey: new PublicKey('RecipientPublicKeyHere'),30 lamports: 0.1 * LAMPORTS_PER_SOL, // 0.1 SOL31 })32 ],33 });3435 // Compile the message to V036 const compiledMessage = transactionMessage.compileToV0Message();37 38 // Create versioned transaction39 const versionedTransaction = new VersionedTransaction(compiledMessage);4041 try {42 // Send the versioned transaction43 const signature = await sendTransaction(versionedTransaction, connection);44 console.log('Versioned transaction signature:', signature);45 46 // Confirm the transaction47 const confirmation = await connection.confirmTransaction(signature);48 console.log('Versioned transaction confirmed:', confirmation);49 } catch (error) {50 console.error('Versioned transaction failed:', error);51 }52 };5354 return (55 <button onClick={sendSol} disabled={!publicKey}>56 Send 0.1 SOL (Versioned)57 </button>58 );59}Gorbag Adapter Transaction Support
The Gorbag wallet adapter supports both legacy and versioned transactions:
TYPESCRIPT
1// From the GorbagWalletAdapter source2export class GorbagWalletAdapter extends BaseMessageSignerWalletAdapter<'Gorbag'> {3 supportedTransactionVersions = new Set<'legacy' | 0>(['legacy', 0]) as Set<TransactionVersion>;4 5 // The adapter supports both legacy transactions and versioned transactions (0)6 7 async signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T> {8 try {9 const wallet = this._wallet;10 if (!wallet) throw new WalletNotConnectedError();1112 try {13 return (await wallet.signTransaction(transaction)) || transaction;14 } catch (error: any) {15 throw new WalletSignTransactionError(error?.message, error);16 }17 } catch (error: any) {18 this.emit('error', error);19 throw error;20 }21 }22}Versioned Transaction with Multiple Instructions
Include multiple instructions in a versioned transaction:
JAVASCRIPT
1import { useConnection, useWallet } from '@solana/wallet-adapter-react';2import { 3 LAMPORTS_PER_SOL, 4 PublicKey, 5 TransactionMessage,6 VersionedTransaction,7 SystemProgram 8} from '@solana/web3.js';910export async function sendMultiInstructionVersionedTransaction() {11 const { connection } = useConnection();12 const { publicKey, sendTransaction } = useWallet();1314 if (!publicKey) {15 throw new Error('Wallet not connected!');16 }1718 // Get the latest blockhash19 const { blockhash } = await connection.getLatestBlockhash();2021 // Create transaction with multiple instructions22 const transactionMessage = new TransactionMessage({23 payerKey: publicKey,24 recentBlockhash: blockhash,25 instructions: [26 // First transfer27 SystemProgram.transfer({28 fromPubkey: publicKey,29 toPubkey: new PublicKey('RecipientPublicKey1'),30 lamports: 0.05 * LAMPORTS_PER_SOL,31 }),32 // Second transfer33 SystemProgram.transfer({34 fromPubkey: publicKey,35 toPubkey: new PublicKey('RecipientPublicKey2'),36 lamports: 0.03 * LAMPORTS_PER_SOL,37 })38 ],39 });4041 // Compile to V0 message and create versioned transaction42 const compiledMessage = transactionMessage.compileToV0Message();43 const versionedTransaction = new VersionedTransaction(compiledMessage);4445 try {46 const signature = await sendTransaction(versionedTransaction, connection);47 console.log('Multi-instruction versioned transaction signature:', signature);48 return signature;49 } catch (error) {50 console.error('Multi-instruction versioned transaction failed:', error);51 throw error;52 }53}Versioned Transaction with Priority Fees
Include priority fees in your versioned transaction to increase processing priority:
JAVASCRIPT
1import { useConnection, useWallet } from '@solana/wallet-adapter-react';2import { 3 LAMPORTS_PER_SOL, 4 PublicKey, 5 TransactionMessage,6 VersionedTransaction,7 SystemProgram,8 ComputeBudgetProgram 9} from '@solana/web3.js';1011export async function sendVersionedWithPriorityFee() {12 const { connection } = useConnection();13 const { publicKey, sendTransaction } = useWallet();1415 if (!publicKey) {16 throw new Error('Wallet not connected!');17 }1819 // Get the latest blockhash20 const { blockhash } = await connection.getLatestBlockhash();2122 // Create a compute budget instruction to set priority fee23 const priorityFeeInstruction = ComputeBudgetProgram.setComputeUnitPrice({24 microLamports: 1000, // Set price per compute unit in micro-lamports25 });2627 // Create transaction message with priority fee instruction28 const transactionMessage = new TransactionMessage({29 payerKey: publicKey,30 recentBlockhash: blockhash,31 instructions: [32 priorityFeeInstruction, // Add priority fee instruction first33 SystemProgram.transfer({34 fromPubkey: publicKey,35 toPubkey: new PublicKey('RecipientPublicKeyHere'),36 lamports: 0.1 * LAMPORTS_PER_SOL,37 })38 ],39 });4041 // Compile to V0 message and create versioned transaction42 const compiledMessage = transactionMessage.compileToV0Message();43 const versionedTransaction = new VersionedTransaction(compiledMessage);4445 try {46 const signature = await sendTransaction(versionedTransaction, connection);47 console.log('Versioned transaction with priority fee signature:', signature);48 return signature;49 } catch (error) {50 console.error('Versioned transaction with priority fee failed:', error);51 throw error;52 }53}