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:@jup-ag/apifor Jupiter API integration@swig-wallet/classicfor Swig wallet functionality@solana/web3.jsfor Solana interactionschalkfor 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
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.
2. Getting Jupiter Quote
3. Handling Compute Budget and Instructions
The most critical part is properly separating and handling instructions:4. 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
- Sign each Swig instruction individually
- 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

