A Solana program that enables Cross-Program Invocation (CPI) to Jupiter's swap aggregator, allowing other programs to perform token swaps through Jupiter programmatically.
This program provides a simple interface for integrating Jupiter swaps into your Solana programs. It handles the complexity of CPI calls to Jupiter while maintaining security and proper account management.
- Cross-Program Invocation: Seamlessly call Jupiter swap from your programs
- Token Interface Support: Compatible with both Token Program and Token-2022
- Flexible Account Management: Handles dynamic account requirements for Jupiter swaps
- Program ID:
AfHs25o5LMehuRKM3gE8Q4npQT9PXn9Lx2Br7ZdQCqfG - Network: Devnet/Mainnet (update as needed)
- Framework: Anchor
- Rust 1.86.0
- Solana CLI 1.16+
- Anchor CLI 0.31.1
- Node.js 16+
- Clone the repository:
git clone https://github.com/aneeshrem/jupiter-swap-cpi.git
cd jupiter-swap-cpi- Install dependencies:
npm install- Build the program:
anchor buildanchor buildsolana-keygen new -o /path/to/your/buffer/jup-swap.jsonsolana program deploy --buffer /path/to/your/buffer/jup-swap.json /path/to/your/project/target/deploy/jupiter_swap_cpi.soNote: Replace /path/to/your/ with your actual file paths.
The program exposes a single instruction:
pub fn swap(ctx: Context<SwapAccount>, data: Vec<u8>) -> Result<()>pub struct SwapAccount<'info> {
pub user_wallet: Signer<'info>, // User's wallet (signer)
pub input_mint: InterfaceAccount<'info, Mint>, // Input token mint
pub output_mint: InterfaceAccount<'info, Mint>, // Output token mint
pub user_input_token_account: InterfaceAccount<'info, TokenAccount>, // User's input token account
pub user_output_token_account: InterfaceAccount<'info, TokenAccount>, // User's output token account
pub token_program: Interface<'info, TokenInterface>, // Token program
pub output_token_program: Interface<'info, TokenInterface>, // Output token program
pub system_program: Program<'info, System>, // System program
pub jupiter_program: Program<'info, Jupiter>, // Jupiter program
}import { Program, AnchorProvider, web3 } from '@coral-xyz/anchor';
import { JupiterSwapCpi } from './target/types/jupiter_swap_cpi';
// Initialize program
const provider = AnchorProvider.env();
const program = new Program<JupiterSwapCpi>(idl, programId, provider);
// Perform swap
const tx = await program.methods
.swap(jupiterSwapData)
.accounts({
userWallet: userWallet.publicKey,
inputMint: inputMint,
outputMint: outputMint,
userInputTokenAccount: userInputTokenAccount,
userOutputTokenAccount: userOutputTokenAccount,
tokenProgram: TOKEN_PROGRAM_ID,
outputTokenProgram: TOKEN_PROGRAM_ID,
systemProgram: web3.SystemProgram.programId,
jupiterProgram: JUPITER_PROGRAM_ID,
})
.remainingAccounts(jupiterAccounts) // Additional accounts required by Jupiter
.rpc();The process_jupiter_swap function handles the cross-program invocation to Jupiter:
- Maps remaining accounts with proper signer/writable flags
- Constructs the instruction for Jupiter
- Executes the CPI call
The program ensures:
- User wallet is a valid signer
- Token accounts belong to the user
- Proper mint associations
- Correct token program references
- Signer validation for user wallet
- Associated token account constraints
- Proper account ownership verification
This program is designed to work with Jupiter's swap aggregator. When calling the swap instruction:
- Obtain swap route data from Jupiter API
- Prepare the required accounts list
- Call this program with the swap data and accounts
- The program will CPI to Jupiter to execute the swap
anchor run swap- Always validate token account ownership
- Verify mint associations
- Be cautious with remaining accounts
- Test thoroughly on devnet before mainnet deployment
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support:
- email : aneeshm.rem@gmail.com
- linkedin : https://www.linkedin.com/in/aneeshmaniyappan/
- Jupiter team for the swap aggregator
- Anchor framework for Solana development
- Solana community for continuous support
Disclaimer: This program is provided as-is. Always audit and test thoroughly before using in production environments.