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';
9
10export function SendVersionedTransaction() {
11 const { connection } = useConnection();
12 const { publicKey, sendTransaction } = useWallet();
13
14 const sendSol = async () => {
15 if (!publicKey) {
16 throw new Error('Wallet not connected!');
17 }
18
19 // Get the latest blockhash
20 const { blockhash } = await connection.getLatestBlockhash();
21
22 // Create a transaction message
23 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 SOL
31 })
32 ],
33 });
34
35 // Compile the message to V0
36 const compiledMessage = transactionMessage.compileToV0Message();
37
38 // Create versioned transaction
39 const versionedTransaction = new VersionedTransaction(compiledMessage);
40
41 try {
42 // Send the versioned transaction
43 const signature = await sendTransaction(versionedTransaction, connection);
44 console.log('Versioned transaction signature:', signature);
45
46 // Confirm the transaction
47 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 };
53
54 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 source
2export 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();
11
12 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';
9
10export async function sendMultiInstructionVersionedTransaction() {
11 const { connection } = useConnection();
12 const { publicKey, sendTransaction } = useWallet();
13
14 if (!publicKey) {
15 throw new Error('Wallet not connected!');
16 }
17
18 // Get the latest blockhash
19 const { blockhash } = await connection.getLatestBlockhash();
20
21 // Create transaction with multiple instructions
22 const transactionMessage = new TransactionMessage({
23 payerKey: publicKey,
24 recentBlockhash: blockhash,
25 instructions: [
26 // First transfer
27 SystemProgram.transfer({
28 fromPubkey: publicKey,
29 toPubkey: new PublicKey('RecipientPublicKey1'),
30 lamports: 0.05 * LAMPORTS_PER_SOL,
31 }),
32 // Second transfer
33 SystemProgram.transfer({
34 fromPubkey: publicKey,
35 toPubkey: new PublicKey('RecipientPublicKey2'),
36 lamports: 0.03 * LAMPORTS_PER_SOL,
37 })
38 ],
39 });
40
41 // Compile to V0 message and create versioned transaction
42 const compiledMessage = transactionMessage.compileToV0Message();
43 const versionedTransaction = new VersionedTransaction(compiledMessage);
44
45 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';
10
11export async function sendVersionedWithPriorityFee() {
12 const { connection } = useConnection();
13 const { publicKey, sendTransaction } = useWallet();
14
15 if (!publicKey) {
16 throw new Error('Wallet not connected!');
17 }
18
19 // Get the latest blockhash
20 const { blockhash } = await connection.getLatestBlockhash();
21
22 // Create a compute budget instruction to set priority fee
23 const priorityFeeInstruction = ComputeBudgetProgram.setComputeUnitPrice({
24 microLamports: 1000, // Set price per compute unit in micro-lamports
25 });
26
27 // Create transaction message with priority fee instruction
28 const transactionMessage = new TransactionMessage({
29 payerKey: publicKey,
30 recentBlockhash: blockhash,
31 instructions: [
32 priorityFeeInstruction, // Add priority fee instruction first
33 SystemProgram.transfer({
34 fromPubkey: publicKey,
35 toPubkey: new PublicKey('RecipientPublicKeyHere'),
36 lamports: 0.1 * LAMPORTS_PER_SOL,
37 })
38 ],
39 });
40
41 // Compile to V0 message and create versioned transaction
42 const compiledMessage = transactionMessage.compileToV0Message();
43 const versionedTransaction = new VersionedTransaction(compiledMessage);
44
45 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}