Swapping Tokens with Jupiter using Swig

This guide demonstrates how to perform token swaps on Solana using Jupiter Protocol with a Swig wallet. We’ll create a simple example that swaps SOL for USDC using Jupiter’s optimized routing.

Prerequisites

Before you begin, make sure you have the following installed:
  • Bun (latest version)
  • A Solana wallet with some SOL for transaction fees
  • A keypair file for the root user

Setup

First, clone the swig-ts repository and navigate to the swap example:
git clone https://github.com/anagrambuild/swig-ts.git
cd swig-ts/examples/swap
bun install
The example already includes all necessary dependencies:
  • @jup-ag/api for Jupiter API integration
  • @swig-wallet/classic for Swig wallet functionality
  • @solana/web3.js for Solana interactions
  • @solana/spl-token for token account management
  • chalk for colorful console output

Understanding the Code

The example demonstrates several key concepts:
  1. Setting up Swig with proper compute budget handling
  2. Managing token accounts (ATAs)
  3. Using Jupiter’s API for quotes and swap instructions
  4. Properly separating and signing instructions
  5. Using utility functions for common operations

Utility Functions

The example includes several helper functions to simplify common operations:
// Helper function to send transactions
async function sendTransaction(
  connection: Connection,
  instructions: TransactionInstruction[],
  payer: Keypair,
): Promise<string> {
  const tx = new Transaction().add(...instructions);
  tx.feePayer = payer.publicKey;
  tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
  tx.sign(payer);
  const sig = await connection.sendRawTransaction(tx.serialize());
  await connection.confirmTransaction(sig);
  return sig;
}

// Helper function to convert Jupiter instructions
function toTransactionInstruction(instruction: any): TransactionInstruction {
  return new TransactionInstruction({
    programId: new PublicKey(instruction.programId),
    keys: instruction.accounts.map((k: any) => ({
      pubkey: new PublicKey(k.pubkey),
      isSigner: k.isSigner,
      isWritable: k.isWritable,
    })),
    data: Buffer.from(instruction.data, 'base64'),
  });
}
Let’s break down the main components:

1. Setting Up Token Accounts

This isnt technically necessary, but we leave it here for your benefit. Jupiter will create the token accounts for you if they don’t exisst in the setup instructions that are returned from the Jupiter API.
Before swapping, we need to ensure the Swig account has the necessary token accounts:
// Check if Swig has USDC ATA
const swigUsdcAta = await getAssociatedTokenAddress(
  usdcMint,
  swigAddress,
  true,
);
try {
  await getAccount(connection, swigUsdcAta);
  console.log(
    chalk.green('✓ USDC ATA exists:'),
    chalk.cyan(swigUsdcAta.toBase58()),
  );
} catch {
  const createAtaIx = createAssociatedTokenAccountInstruction(
    rootUser.publicKey,
    swigUsdcAta,
    swigAddress,
    usdcMint,
  );
  await sendTransaction(connection, [createAtaIx], rootUser);
  console.log(
    chalk.green('✓ Created USDC ATA:'),
    chalk.cyan(swigUsdcAta.toBase58()),
  );
}

2. Getting Jupiter Quote

const jupiter = createJupiterApiClient();
const quote = await jupiter.quoteGet({
  inputMint: wrappedSolMint.toBase58(),
  outputMint: usdcMint.toBase58(),
  amount: Math.floor(transferAmount),
  slippageBps: 50,
  maxAccounts: 64,
});

3. Handling Compute Budget and Instructions

The most critical part is properly separating and handling instructions:
const swapInstructionsRes = await jupiter.swapInstructionsPost({
  swapRequest: {
    quoteResponse: quote,
    userPublicKey: swigAddress.toBase58(),
    wrapAndUnwrapSol: true,
    useSharedAccounts: true,
  },
});

const swapInstructions: TransactionInstruction[] = [
  ...(swapInstructionsRes.setupInstructions || []).map(
    toTransactionInstruction,
  ),
  toTransactionInstruction(swapInstructionsRes.swapInstruction),
];

// Sign instructions with Swig
const rootRole = swig.findRolesByEd25519SignerPk(rootUser.publicKey)[0];
const signIxs = await getSignInstructions(
  swig,
  rootRole.id,
  swapInstructions,
);

4. Creating and Sending the Transaction

const lookupTables = await Promise.all(
  swapInstructionsRes.addressLookupTableAddresses.map(async (addr) => {
    const res = await connection.getAddressLookupTable(new PublicKey(addr));
    return res.value!;
  }),
);

const { blockhash, lastValidBlockHeight } =
  await connection.getLatestBlockhash();
const outerIxs = [
  ComputeBudgetProgram.setComputeUnitLimit({ units: 150_000 }),
  ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 50 }),
];

const messageV0 = new TransactionMessage({
  payerKey: rootUser.publicKey,
  recentBlockhash: blockhash,
  instructions: [...outerIxs, ...signIxs],
}).compileToV0Message(lookupTables);

const tx = new VersionedTransaction(messageV0);
tx.sign([rootUser]);

const signature = await connection.sendTransaction(tx, {
  skipPreflight: true,
  preflightCommitment: 'confirmed',
});

const result = await connection.confirmTransaction({
  signature,
  blockhash,
  lastValidBlockHeight,
});

if (result.value.err) {
  throw new Error(`Transaction failed: ${JSON.stringify(result.value.err)}`);
}

Important Considerations

  1. Compute Budget Management:
    • Set appropriate compute unit limit and price
    • These instructions must be outside of Swig-signed instructions
  2. Account Management:
    • Create ATAs in separate transactions before swapping
    • Account for Swig’s overhead when setting maxAccounts
  3. Transaction Structure:
    • Properly separate outer instructions from Swig-signed instructions
    • Use getSignInstructions to sign all Swig instructions at once
    • Use versioned transactions with address lookup tables
  4. Error Handling:
    • Verify ATA existence and creation
    • Check transaction confirmation status
    • Verify token balances after swap

Running the Example

  1. Prepare your keypair file
  2. Run the example:
bun run index.ts path/to/keypair.json [optional-swig-address]
The example will:
  • Create or use an existing Swig wallet
  • Set up necessary token accounts
  • Get the best swap route from Jupiter
  • Execute the swap with proper instruction handling

Production Considerations

  1. Security:
    • Securely manage keypairs and private keys
    • Implement proper error recovery mechanisms
    • Add transaction timeout handling
  2. Performance:
    • Adjust compute budget based on swap complexity
    • Consider using RPC endpoints with higher rate limits
    • Implement proper retry mechanisms
  3. User Experience:
    • Add progress indicators for multi-step operations
    • Implement proper balance checking and updates
    • Add slippage protection options
For more information, check out: