Documentation Index
Fetch the complete documentation index at: https://docs.raydium.io/llms.txt
Use this file to discover all available pages before exploring further.
The invariant
A constant-product market maker (CPMM) holds two reservesx and y and enforces:
k is the product of the reserves before the trade. For a fee-free market, x · y = k exactly. With fees, k strictly grows (the LP share of the fee is retained in the reserves).
The invariant is deliberately geometric: it guarantees that no matter how small one reserve becomes, the other grows unboundedly to match — i.e. the pool can never be drained to zero on either side.
Pricing
Spot price
The marginal price ofy denominated in x at any instant is the tangent of the curve:
x · y = k gives dy/dx = −y/x; ignoring the sign, |dy/dx| = y/x).
This is the price that the pool quotes for an infinitesimally small trade. For any finite trade, the realized price is worse due to slippage along the curve.
Exact-input swap (give Δx, receive Δy)
With fees, let f be the fee rate (e.g. f = 0.0025 for 25 bps). Apply the fee to the input, then use the invariant to solve for the output:
Δx enters the reserves. The LP portion of the fee stays in x'; the protocol portion is excluded from the curve via a separate accounting step (see Fee accounting variants below).
Exact-output swap (receive Δy, pay the minimal Δx)
Δx is rounded up to ensure the pool does not undercharge.
Slippage and price impact
Price impact measures how much the pool’s spot price moves as a result of the trade:Δx / x, a first-order expansion gives:
p_before and effective is slippage. On-chain slippage UI is usually expressed as (effective − p_before) / p_before; the SDK’s computeAmountOut returns both amountOut and priceImpact for this reason.
Invariant check in code
After a swap, protocols re-verify:Fee accounting variants
The invariant check assumes the LP fee stays in the reserves. Different Raydium products handle the protocol / fund / creator components differently:CPMM convention
Fees areu64 basis-point-like rates on a 1_000_000 denominator. The trade fee is split into trade_fee_rate (total) and then subdivided via protocol_fee_rate, fund_fee_rate, creator_fee_rate. On each swap:
protocol_fees_*, fund_fees_*, creator_fees_*) that are excluded from the reserves used in the invariant. This is how fees can be swept without moving the curve. See products/cpmm/fees.
AMM v4 convention
Fees arenumerator / denominator ratios on a 10_000 denominator. The split is fixed at pool creation and stored on AmmInfo.fees:
pnl_share accrues into state_data.need_take_pnl_* and is excluded from reserves; lp_share stays in the vault. See products/amm-v4/fees.
Both conventions preserve the invariant the same way — the difference is cosmetic (denominator + number of sub-categories).
Rounding rules
- Fee calculation rounds up. Ensures the pool never undercharges on the fee.
- Output amount rounds down. Ensures the invariant holds strictly (
k' > keven before the fee is added). - Exact-output input amount rounds up. Ensures the user does not underpay.
u128 for the intermediate x · Δx products to avoid overflow on large reserves. Final results are cast back to u64 with a saturation check.
Edge cases
Empty pool
Before the firstDeposit, x = y = 0. Swap instructions reject pre-deposit.
Zero output
IfΔx is small enough that the rounded-down Δy is 0, the instruction reverts with ZeroTradingTokens. This prevents extraction of value without payment; also means dust swaps on highly imbalanced pools fail.
Dust LP
The firstDeposit has special handling: it computes the initial LP supply as sqrt(x · y) and burns a small “init burn” amount (usually 100 LP units) to prevent the “first-depositor inflation attack” (where an attacker donates to the vault and inflates the LP token value). Subsequent deposits use pro-rata math.
Relationship to arbitrage
A CPMM pool’s price only changes via:- Trades through the pool itself (users walking the curve).
- Donations (sending tokens to the vault without a swap).
Worked examples
Example 1 — small trade, negligible slippage
Pool:x = 1_000_000, y = 2_000_000, k = 2·10^12. Fee f = 0.0025.
Trade Δx = 1_000:
1000 / 1993.01 ≈ 0.5018. Spot before: 0.5. Impact: ~0.36%.
Example 2 — mid-size trade, visible slippage
Same pool,Δx = 100_000 (10% of x):
100_000 / 181_405 ≈ 0.5513. Impact: ~10.3% — roughly half the 2 · 10% = 20% rule of thumb (the rule is a worst-case ceiling for a no-fee constant-product curve; the trade fee plus the inversion in the formula brings it down).
Pointers
products/cpmm/math— CPMM’s specific rounding + fee-denominator choices.products/amm-v4/math— how AMM v4’s OpenBook-integrated reserves extend this model.algorithms/slippage-and-price-impact— dedicated page on slippage tolerance sizing for UIs.
- Uniswap v2 whitepaper — the canonical statement of
x · y = k. - Raydium CPMM program source.
- Raydium AMM v4 program source.


