Add Fee To Swap

The Referral Program is an open-source initiative by Jupiter to provide referral fees for integrators who are using Jupiter Swap and Jupiter Limit Order. You can check out the code here to better understand how it works.

By default, there are no protocol fees on Jupiter. However, integrators have the option to introduce a platform fee on swaps. The platform fee is specified in basis points, e.g., 20 bps for 0.2% of the token input or output. If a platform fee is set by an integrator, Jupiter will take 2.5% of the platform fee charged. Note that Token2022 tokens are not supported.

Adding Your Own Fee to Jupiter Swap

  1. Obtain a referral account

*Referral key is your referral account public key, you'll need this key to gather platform fees

  1. Set your referral fee

Setting your referral fee with the Jupiter API is simple. You just add in the platformFeeBps parameter to the [GET /quote](https://docs.jup.ag/reference/get_swap-v6-quote-1) endpoint:

See this for a guide on how to get the route for a swap!

// Function to swap SOL to USDC with input 0.1 SOL and 0.5% slippage
async function getQuote() {
  try {
    // Create a new URL object for the quote API endpoint
    const url = new URL("https://api.jup.ag/swap/v6/quote");

    // Append query parameters to the URL
    // inputMint: The mint address of the input token (SOL)
    url.searchParams.append(
      "inputMint",
      "So11111111111111111111111111111111111111112"
    );

    // outputMint: The mint address of the output token (USDC)
    url.searchParams.append(
      "outputMint",
      "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
    );

    // amount: The amount of input tokens to be swapped (0.1 SOL in lamports, where 1 SOL = 1,000,000,000 lamports)
    url.searchParams.append("amount", 100000000);

    // slippageBps: The maximum allowable slippage for the swap (0.5% expressed in basis points)
    url.searchParams.append("slippageBps", 50);

    // platformFeeBps: The platform fee to be added (20 basis points)
    url.searchParams.append("platformFeeBps", 20);

    // Perform the fetch request to the constructed URL
    const response = await fetch(url.toString());

    // Check if the response is not OK (status code is not in the range 200-299)
    if (!response.ok) {
      // Throw an error with the status text from the response
      throw new Error(`Error fetching quote: ${response.statusText}`);
    }

    // Parse the response body as JSON
    const quoteResponse = await response.json();

    // Log the parsed response to the console
    console.log({ quoteResponse });
  } catch (error) {
    // Catch any errors that occur during the fetch request or JSON parsing
    // Log the error to the console
    console.error("Failed to get quote:", error);
  }
}

// Call the function to get the quote
getQuote();
  1. Set your fee token account
  • Add your feeAccount parameter to your [POST /swap]endpoint
// Function to find the fee account and get serialized transactions for the swap
async function getFeeAccountAndSwapTransaction(
  referralAccountPubkey,
  mint,
  quoteResponse,
  wallet
) {
  try {
    // Find the fee account program address synchronously
    // Parameters:
    // - Buffer.from("referral_ata"): A buffer containing the string "referral_ata"
    // - referralAccountPubkey.toBuffer(): The buffer representation of the referral account public key
    // - mint.toBuffer(): The buffer representation of the token mint
    // - new PublicKey("REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3"): The public key of the Referral Program
    const [feeAccount] = await PublicKey.findProgramAddressSync(
      [
        Buffer.from("referral_ata"),
        referralAccountPubkey.toBuffer(),
        mint.toBuffer(),
      ],
      new PublicKey("REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3")
    );

    // Construct the request body for the swap API
    const requestBody = {
      quoteResponse, // The quote response from the /quote API
      userPublicKey: wallet.publicKey.toString(), // The user's public key
      wrapAndUnwrapSol: true, // Auto wrap and unwrap SOL (default is true)
      feeAccount, // The fee account obtained from findProgramAddressSync
    };

    // Perform the fetch request to the swap API
    const response = await fetch("https://api.jup.ag/swap/v6/transaction", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(requestBody), // Convert the request body to a JSON string
    });

    // Check if the response is not OK (status code is not in the range 200-299)
    if (!response.ok) {
      // Throw an error with the status text from the response
      throw new Error(`Error performing swap: ${response.statusText}`);
    }

    // Parse the response body as JSON to get the swap transaction
    const { swapTransaction } = await response.json();

    // Log the swap transaction to the console
    console.log({ swapTransaction });

    return swapTransaction; // Return the swap transaction
  } catch (error) {
    // Catch any errors that occur during the fetch request or JSON parsing
    // Log the error to the console
    console.error("Failed to get fee account and swap transaction:", error);
  }
}

// Example usage of the function
// Assuming you have defined referralAccountPubkey, mint, quoteResponse, and wallet elsewhere
getFeeAccountAndSwapTransaction(
  referralAccountPubkey,
  mint,
  quoteResponse,
  wallet
);

👍

NOTE

Do ensure your fee token account is created. The fee token account can be created on the referral dashboard. For ExactIn, the fee token account can be either the input mint or the output mint of the swap. For ExactOut, the fee is taken from the same mint as the input mint.

*Token2022 tokens are not supported.

Referral Javascript SDK

Check out the Referral Javascript SDK here. For a list of methods that you can use, check out the source code here.

There are also examples on how to use the SDK here.

👍

NOTE

The Jupiter Swap's project account for the Referral Program is 45ruCyfdRkWpRNGEqWzjCiXRHkZs8WXCLQ67Pnpye7Hp.