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 lookup-table curve
Stable AMM replaces the formula x·y=k with a sparse lookup table of (x, y, price) tuples. When pricing a swap, the program:- Computes the pool’s current ratio from reserves.
- Binary searches the table to find the two entries that bracket that ratio.
- Linearly interpolates between them to get an intermediate price.
- Applies fees and returns the quote.
Table layout and binary search
TheModelDataInfo holds up to 50,000 DataElement entries, indexed by the admin. Only the first valid_data_count are live. Each entry:
- Compute the ratio:
target_ratio = (x_real * multiplier) / y_real. - Binary search for entries where
(element.x * multiplier) / element.ybracketstarget_ratio. - When a bracket
[min_idx, max_idx]is found, interpolate.
state.rs::ModelDataInfo::get_mininum_range_by_xy_real. The key invariant: entries must be sorted (x ascending, y descending, price ascending) for the search to work.
Linear interpolation
Once two table points bracket the ratio, interpolation computes an intermediate price and reserve pair:Scaling: the multiplier
Pool reserves and prices are stored at different scales. Themultiplier field on ModelDataInfo accounts for this. A common pattern:
- Coin has 6 decimals, PC has 18 decimals.
- Multiplier = 10^6 (or similar).
- Table entries are stored at a reduced scale to fit u64 bounds.
Swap pricing: SwapBaseIn and SwapBaseOut
SwapBaseIn (exact input)
Given input amountamount_in:
- Get current ratio from
(coin_vault, pc_vault). - Find bracketing table entries and interpolate to get the table-space ratio.
- Convert input to table space:
dx_table = amount_in * multiplier / ratio. - Query the table at the new X coordinate to find the new Y.
dy_table = y_old - y_new.- Convert back:
dy_real = dy_table * ratio / multiplier. - Apply trade fee:
dy_output = dy_real - (dy_real * trade_fee_numerator / trade_fee_denominator). - Return
dy_output.
SwapBaseOut (exact output)
Symmetric: given desiredamount_out, solve for the required amount_in.
Both paths settle filled OpenBook orders first (via internal MonitorStep-like logic), so the effective reserves reflect any fills from the previous block.
Fee application
Identical to AMM v4: seeproducts/amm-v4/math for the full derivation.
pnl_portion goes to need_take_pnl_* and is swept by the admin via WithdrawPnl. The lp_portion stays in the vault, inflating k and benefiting LP token holders.
MonitorStep and OpenBook
Like AMM v4,MonitorStep is a crank instruction that:
- Settles pending OpenBook order fills (moves tokens from vaults to the pool).
- Updates
AmmInfo.target_orderswith a new grid of limit-order slots. - Posts the new grid to OpenBook.
MonitorStep: ~150k–180k CU (similar to AMM v4).
Summary: why this works
The lookup table + interpolation design is efficient and flexible:- Efficiency: Binary search is O(log 50,000) ≈ 16 iterations, each ~ 300–500 CU. Interpolation is a few multiplies/divides. Total quoting cost is ~5k–15k CU, much cheaper than recomputing a formula on every swap.
- Flexibility: The admin can encode any piecewise-linear curve. Stablecoin pairs get high density around 1:1; collateralized pairs get custom curves.
- OpenBook composability: The same
MonitorStep/TargetOrderslogic from AMM v4 applies. Price discovery via the table feeds into order grid generation.
raydium-stable/program/src/state.rs, methods get_data_by_x, get_data_by_y, get_dy_by_dx_base_in, etc.
Where to go next
- Accounts —
ModelDataInfoandDataElementfield reference. - Instructions —
InitModelData,UpdateModelDatafor populating the table. - Fees — fee application and
WithdrawPnl. products/amm-v4/math— for the OpenBook fee-inclusive order pricing logic.
raydium-stable/program/src/state.rs(interpolation and binary search implementations)raydium-stable/program/src/math.rs(calculator utilities)


