I expected the same swap quote and slippage setting to produce the same amount limits when I built a transaction twice. While reviewing Raydium SDK V2, I found that its CPMM swap builder changed the quote object passed into it. Reusing that object applied slippage again.

The second transaction could therefore accept less output or allow more input, even though I had supplied the same parameters. I reported the behavior in issue #222 and submitted PR #224, which merged on October 26, 2026, UTC. The fix keeps the quote intact and calculates the transaction's limits separately.

Raydium quotes and slippage limits

Raydium is a decentralized exchange and liquidity protocol on Solana. Its CPMM, or constant product market maker, prices swaps using a pool's token reserves and a constant-product curve. An application can use Raydium's SDK to calculate a quote and construct the instructions for a swap. Raydium overview

A quote describes expected amounts for the pool state used in the calculation. Slippage tolerance allows execution to differ within a chosen range. The two swap modes put that limit on different sides of the trade:

ModeFixed amountLimit derived from the quote
Exact-inAmount to spendMinimum output to accept
Exact-outAmount to receiveMaximum input to spend

For example, if an exact-in quote returns 10,000 raw output units, a 1% tolerance gives a minimum output of 9,900. Raw units are the integer amounts used by token instructions, before applying the token's decimal scale.

The quote and the minimum output serve different purposes. The builder needs both, but calculating the second should preserve the first.

Reusing a quote changed the limits

The reproduction called the real SDK builder twice with the same quote object. This is the relevant part, after preparing the pool and quote:

ts
const params = {
  poolInfo,
  poolKeys,
  baseIn: true,
  fixedOut: false,
  inputAmount: swapResult.inputAmount,
  swapResult,
  slippage: 0.01,
  txVersion,
};

const first = await raydium.cpmm.swap(params);
const second = await raydium.cpmm.swap(params);

I decoded the generated swap instructions and compared their amount fields. Before the fix, the second call produced a different limit. Both mint directions and both legacy and v0 transaction formats exhibited the problem. Reproduction and observed amounts

The following illustrative amounts make the repeated adjustment easier to see. Each row starts with its own quote of 10,000 raw units and uses a 1% tolerance:

ModeQuoted amountFirst build before the fixSecond build before the fix
Exact-in10,000 output9,900 minimum output9,801 minimum output
Exact-out10,000 input10,100 maximum input10,201 maximum input

For exact-in, the builder applied 1% to an output amount that had already been reduced. For exact-out, it applied 1% to an input amount that had already been increased. In both cases, the second transaction permitted a worse outcome than the first.

When this affects an application

The compounded limits occur in the affected builder when an application passes the same swapResult object into cpmm.swap() more than once with nonzero slippage. The first call changes the quote, and subsequent calls calculate their limits from the changed values.

An application might build once for a preview and again before submission, or call the builder again after a failed attempt. Reusing the quote in those flows triggers the repeated adjustment. Resending an already-built transaction does not call the builder again, so that action alone does not compound the limit.

The practical problem is weaker protection against an unfavorable execution price. In the illustrative exact-in example, the second build accepts 9,801 units from an original quote of 10,000: 1.99% below the quote despite a 1% setting. The exact-out example permits spending 10,201 instead of the quoted 10,000: 2.01% more. A swap could therefore settle beyond the intended tolerance while still satisfying the widened instruction limit. Actual amounts depend on execution; the local reproduction demonstrated the wider limits encoded in the instructions.

The builder wrote the limits into the quote

The old implementation assigned the adjusted amounts directly to swapResult. The exact-in branch included this assignment:

ts
swapResult.outputAmount = swapResult.outputAmount
  .mul(new BN((1 - slippage) * 10000))
  .div(new BN(10000));

The exact-out branch similarly overwrote swapResult.inputAmount. Those assignments changed the caller's object. After the first build, one field that had held a quoted amount instead held a slippage-adjusted limit. The next build treated that limit as a fresh quoted amount. Implementation before the fix

Freezing the quote with Object.freeze exposed the same behavior another way: the first build threw when it tried to assign to the object. A caller trying to protect its quote could not use that builder successfully.

Keep the quote and derive the limits

My patch calculates two local values, inputAmountMax and outputAmountMin. For exact-in, the input stays fixed and the minimum output is derived from the quoted output. For exact-out, the output stays fixed and the maximum input is derived from the quoted input. The appropriate values then go into the swap instruction. Updated builder

An unchanged quote of 10,000 raw output units feeds two independent transaction builds. Each applies 1 percent slippage and produces a minimum output of 9,900.

An illustrative exact-in example after the fix. Each build derives its limit from the original quote.

This also required following the input amount through native SOL preparation. For an exact-out swap funded with SOL, the SDK creates a temporary wrapped SOL account. That account must be funded for the maximum input, plus rent, so its prepared balance agrees with the swap instruction's allowance. Once the quoted input stopped being overwritten, this path needed to use inputAmountMax explicitly. Funding and instruction changes

The patch retains the existing slippage arithmetic. Its change is where the result is stored and how it reaches the instruction and funding paths.

Test the quote and the generated instruction

I added 13 regression cases around the built SDK. The tests check the caller's quote after construction and inspect the generated instruction data:

CoverageCasesWhat the tests check
Exact-in and exact-out, both mint directions, legacy and v08The quote stays unchanged, repeated builds have identical swap instruction data, and the amount fields contain the expected limits
Exact-out with native SOL input, both directions and formats4The temporary account receives the maximum input plus rent, matching the instruction's input limit
A frozen quote1The builder accepts a quote whose fields cannot be reassigned

All 13 cases failed against the implementation before the fix and passed with the patch. The SDK build, including its type declarations, also passed. These were local tests of transaction construction with controlled account and RPC responses. They checked the instructions and funding amounts without submitting a swap on chain. Regression suite, validation results

A successful build alone would have missed the central problem: both calls could return transactions while permitting different amounts. Comparing the swap instructions made the change visible. The test targets that instruction data rather than requiring the complete transactions to be byte-for-byte identical.

What I now check in a swap builder

This contribution gave me a concrete question to add to SDK reviews: after a builder returns, can the caller still trust the meaning of the object it passed in?

For a swap quote, I now check whether its fields survive construction unchanged, whether rebuilding from that quote preserves the amount limits, and whether temporary funding agrees with those limits. Reading the generated instructions connects the SDK's calculations to the constraints the transaction will carry.

The fix is small, but the boundary matters. A quote should remain a quote while the builder derives the limits for a transaction.

✳

Thanks for reading. Keep following your curiosity.

NEXT NOTE ↗

Testing Token-2022 Accounts and Swap Quotes