Skip to content

Repository files navigation

Jupiter Swap CPI

A Solana program that enables Cross-Program Invocation (CPI) to Jupiter's swap aggregator, allowing other programs to perform token swaps through Jupiter programmatically.

Overview

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.

Features

  • 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 Details

  • Program ID: AfHs25o5LMehuRKM3gE8Q4npQT9PXn9Lx2Br7ZdQCqfG
  • Network: Devnet/Mainnet (update as needed)
  • Framework: Anchor

⚠️ Important: Jupiter swap functionality is only available on Mainnet-beta. This program will not work on Devnet or Localnet as Jupiter's swap aggregator operates exclusively on Mainnet.

Installation

Prerequisites

  • Rust 1.86.0
  • Solana CLI 1.16+
  • Anchor CLI 0.31.1
  • Node.js 16+

Setup

  1. Clone the repository:
git clone https://github.com/aneeshrem/jupiter-swap-cpi.git
cd jupiter-swap-cpi
  1. Install dependencies:
npm install
  1. Build the program:
anchor build

Deployment

Step 1: Build the Program

anchor build

Step 2: Generate a Keypair for Buffer Account

solana-keygen new -o /path/to/your/buffer/jup-swap.json

Step 3: Deploy to Solana

solana program deploy --buffer /path/to/your/buffer/jup-swap.json /path/to/your/project/target/deploy/jupiter_swap_cpi.so

Note: Replace /path/to/your/ with your actual file paths.

Usage

Program Structure

The program exposes a single instruction:

pub fn swap(ctx: Context<SwapAccount>, data: Vec<u8>) -> Result<()>

Account Structure

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
}

Client Integration Example

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();

Key Components

1. CPI Handler

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

2. Account Validation

The program ensures:

  • User wallet is a valid signer
  • Token accounts belong to the user
  • Proper mint associations
  • Correct token program references

3. Security Features

  • Signer validation for user wallet
  • Associated token account constraints
  • Proper account ownership verification

Integration with Jupiter

This program is designed to work with Jupiter's swap aggregator. When calling the swap instruction:

  1. Obtain swap route data from Jupiter API
  2. Prepare the required accounts list
  3. Call this program with the swap data and accounts
  4. The program will CPI to Jupiter to execute the swap

Development

Running Tests

anchor run swap

Security Considerations

  • Always validate token account ownership
  • Verify mint associations
  • Be cautious with remaining accounts
  • Test thoroughly on devnet before mainnet deployment

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For questions and support:

Acknowledgments

  • 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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages