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:@jup-ag/apifor Jupiter API integration@swig-wallet/classicfor Swig wallet functionality@solana/web3.jsfor Solana interactions@solana/spl-tokenfor token account managementchalkfor colorful console output
Understanding the Code
The example demonstrates several key concepts:- Setting up Swig with proper compute budget handling
- Managing token accounts (ATAs)
- Using Jupiter’s API for quotes and swap instructions
- Properly separating and signing instructions
- Using utility functions for common operations
Utility Functions
The example includes several helper functions to simplify common operations: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: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.
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
-
Compute Budget Management:
- Set appropriate compute unit limit and price
- These instructions must be outside of Swig-signed instructions
-
Account Management:
- Create ATAs in separate transactions before swapping
- Account for Swig’s overhead when setting
maxAccounts
-
Transaction Structure:
- Properly separate outer instructions from Swig-signed instructions
- Use
getSignInstructionsto sign all Swig instructions at once - Use versioned transactions with address lookup tables
-
Error Handling:
- Verify ATA existence and creation
- Check transaction confirmation status
- Verify token balances after swap
Running the Example
- Prepare your keypair file
- Run the example:
- 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
-
Security:
- Securely manage keypairs and private keys
- Implement proper error recovery mechanisms
- Add transaction timeout handling
-
Performance:
- Adjust compute budget based on swap complexity
- Consider using RPC endpoints with higher rate limits
- Implement proper retry mechanisms
-
User Experience:
- Add progress indicators for multi-step operations
- Implement proper balance checking and updates
- Add slippage protection options

