Skip to main content

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 (at least 0.02 SOL)
  • A keypair file for the root user
  • RPC_URL environment variable set to your Solana RPC endpoint

Setup

First, clone the swig-ts repository and navigate to the swap example:
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:
Let’s break down the main components:

1. Creating and Getting the Swig Wallet Address

After creating your Swig account, you need to get the wallet address which is used for all transfers and token operations:
Important: Always use swigWalletAddress (not swigAccountAddress) for all transfer operations, token accounts, and Jupiter swaps.

2. Setting Up Token Accounts

Jupiter’s returned setup instructions create any missing token accounts. The explicit setup below is useful when you want to create and inspect them first.
Before swapping, we need to ensure the Swig wallet has the necessary token accounts (both USDC and Wrapped SOL):

3. Getting Jupiter Quote

4. Handling Compute Budget and Instructions

The most critical part is properly separating and handling instructions:

5. Creating and Sending the Transaction

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:
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: