Skip to content

Slippage Error When Adding Liquidity Involving SOL Using Raydium SDK v2 #121

Open
@Chirag-Jani

Description

@Chirag-Jani

🐛 Bug: Slippage Error When Adding Liquidity Involving SOL Using Raydium SDK v2


📝 Description

Using Raydium SDK v2, I’m able to successfully add liquidity to standard SPL token pairs such as:

  • USDT/USDC
  • USDT/RAY

However, when attempting to add liquidity to pairs involving SOL (e.g., SOL/USDT, SOL/USDC), I consistently encounter slippage-related errors, even when:

  • Using higher slippage tolerance (e.g., 8% or 20%)
  • Trying fallback logic to switch to AMM pools
  • Reducing transaction size

This issue seems specific to pools involving native SOL or WSOL.


✅ Expected Behavior

Liquidity should be added successfully using the same logic and slippage configuration as non-SOL pairs.


❌ Actual Behavior

  • Slippage errors (e.g., "Price slippage check failed" or custom program error: 0x1785) are thrown.
  • Issue persists despite retries, higher slippage, or alternative pools.

🔁 Reproduction Steps

  1. Use Raydium SDK v2 to call addLiquidityToPool for a SOL/USDC or SOL/USDT pair.
  2. Set slippage to 8% or more.
  3. Observe the slippage-related failure despite fallback attempts.

💻 Environment

  • SDK: Raydium SDK v2
  • Network: Solana Mainnet
  • Working Pairs: USDT/USDC, USDT/RAY
  • Failing Pairs: SOL/USDT, SOL/USDC

🔧 Code Snippet

export const addLiquidityToPool = async (
  poolId,
  amountA,
  amountB,
  tokenAMint,
  tokenBMint,
  walletProvider,
  slippageTolerance = 2.0,
  recursionDepth = 0,
  triedPools = new Set()
) => {
  const isSolPool = tokenAMint === WSOL_MINT || tokenBMint === WSOL_MINT;

  try {
    if (recursionDepth > 2) throw new Error("Maximum recursion depth reached.");
    if (triedPools.has(poolId)) throw new Error(`Pool ${poolId} already tried.`);
    triedPools.add(poolId);

    const publicKey = walletProvider.publicKey;
    const effectiveSlippageTolerance = isSolPool ? Math.max(slippageTolerance, 8.0) : slippageTolerance;

    await checkSolBalance(publicKey, isSolPool);

    const raydium = await Raydium.load({ connection, disableLoadToken: true, owner: publicKey });
    const pool = await findPoolById(raydium, poolId, tokenAMint, tokenBMint);

    return await handleLiquidityByPoolType(
      raydium,
      pool,
      poolId,
      amountA,
      amountB,
      tokenAMint,
      tokenBMint,
      walletProvider,
      effectiveSlippageTolerance
    );
  } catch (error) {
    console.error("Error in addLiquidityToPool:", error);

    if (
      (error.message.includes("Price slippage check") || error.message.includes("0x1785")) &&
      recursionDepth < 1
    ) {
      try {
        const raydium = await Raydium.load({ connection, disableLoadToken: true });
        const alternativePools = await raydium.api.fetchPoolByMints({ mint1: tokenAMint, mint2: tokenBMint });

        const availablePools = alternativePools?.data?.filter((p) => !triedPools.has(p.id));
        if (availablePools?.length > 0) {
          const nextPool = availablePools[0];
          return await addLiquidityToPool(
            nextPool.id,
            amountA,
            amountB,
            tokenAMint,
            tokenBMint,
            walletProvider,
            slippageTolerance + 10.0,
            recursionDepth + 1,
            triedPools
          );
        }
      } catch (fallbackError) {
        console.log("Fallback error:", fallbackError.message);
      }
    }

    if (
      isSolPool &&
      (error.message.includes("Price slippage check") || error.message.includes("0x1785")) &&
      recursionDepth >= 1
    ) {
      try {
        const raydium = await Raydium.load({ connection, disableLoadToken: true });
        const allPools = await raydium.api.fetchPoolByMints({ mint1: tokenAMint, mint2: tokenBMint });
        const ammPools = allPools?.data?.filter(
          (p) =>
            !triedPools.has(p.id) &&
            p.type !== "Concentrated" &&
            !p.programId?.includes("CAMMCzo5Y...")
        );
        if (ammPools?.length > 0) {
          const ammPool = ammPools[0];
          return await addLiquidityToPool(
            ammPool.id,
            amountA,
            amountB,
            tokenAMint,
            tokenBMint,
            walletProvider,
            slippageTolerance + 20.0,
            recursionDepth + 1,
            triedPools
          );
        }
      } catch (ammFallbackError) {
        console.log("AMM fallback failed:", ammFallbackError.message);
      }
    }

    if (error.message.includes("Price slippage check") || error.message.includes("0x1785")) {
      const suggestedSlippage = isSolPool ? "20% or higher" : "10% or higher";
      throw new Error(
        `Slippage failed. Try increasing slippage tolerance (e.g., ${suggestedSlippage}), reduce amount, or try AMM pools.`
      );
    }

    throw error;
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions