Expand description
Pure-Rust, no-RPC swap math for the Raydium concentrated-liquidity AMM (CLMM) on Solana.
This crate contains the deterministic integer arithmetic that the on-chain
Raydium CLMM program executes — extracted unchanged into a library that has
no dependency on anchor-lang, solana-program, the Solana runtime, or
the Anchor account model. Given pre-decoded pool state and tick-array data,
every function here is a pure function of its inputs.
§Scope
- Tick ↔ sqrt-price conversions (
get_sqrt_price_at_tick,get_tick_at_sqrt_price) - Liquidity ↔ token-amount conversions
(
get_liquidity_from_amounts,get_delta_amounts_signed, …) - Single-tick swap step (
compute_swap_step) - Multi-tick swap orchestration
(
compute_swap_full) - Tick-array bitmap navigation
(
next_initialized_tick_array_start_index) - Token-2022 transfer-fee math (
calculate_fee,apply_transfer_fee,reverse_apply_transfer_fee) — mirrorsspl_token_2022_interface::extension::transfer_feebyte-exactly
§Out of scope
- Pool / tick-array account decoding (this crate takes pre-decoded state;
caller flattens decoded
TickArrayStates into theInitializedTickslice thatcompute_swap_fullconsumes) - Token-2022 mint-extension TLV decoding and transfer-hook execution
(caller resolves the active
TransferFeeand CPIs hook programs) - Position fee and reward accumulation beyond
liquidity_from_amounts
§Provenance
Math is extracted from
raydium-io/raydium-clmm/programs/amm/src/libraries/. The arithmetic
itself is byte-for-byte identical to the on-chain implementation; the only
changes are import paths, an internal ErrorCode enum that replaces
anchor_lang::error::Error, and free-function rehosting of three static
methods that used no struct fields.
Re-exports§
pub use error::ErrorCode;pub use tick_math::get_sqrt_price_at_tick;pub use tick_math::get_tick_at_sqrt_price;pub use tick_math::MAX_SQRT_PRICE_X64;pub use tick_math::MAX_TICK;pub use tick_math::MIN_SQRT_PRICE_X64;pub use tick_math::MIN_TICK;pub use liquidity_math::add_delta;pub use liquidity_math::cross;pub use liquidity_math::get_delta_amount_0_signed;pub use liquidity_math::get_delta_amount_0_unsigned;pub use liquidity_math::get_delta_amount_1_signed;pub use liquidity_math::get_delta_amount_1_unsigned;pub use liquidity_math::get_delta_amounts_signed;pub use liquidity_math::get_liquidity_from_amount_0;pub use liquidity_math::get_liquidity_from_amount_1;pub use liquidity_math::get_liquidity_from_amounts;pub use liquidity_math::get_liquidity_from_single_amount_0;pub use liquidity_math::get_liquidity_from_single_amount_1;pub use sqrt_price_math::get_next_sqrt_price_from_amount_0_rounding_up;pub use sqrt_price_math::get_next_sqrt_price_from_amount_1_rounding_down;pub use sqrt_price_math::get_next_sqrt_price_from_input;pub use sqrt_price_math::get_next_sqrt_price_from_output;pub use swap_math::compute_swap_step;pub use swap_math::SwapStep;pub use swap_full::compute_swap_full;pub use swap_full::InitializedTick;pub use swap_full::SwapPool;pub use swap_full::SwapResult;pub use tick_array_bit_map::check_current_tick_array_is_initialized;pub use tick_array_bit_map::next_initialized_tick_array_start_index;pub use tick_array_bit_map::PoolTickBitmap;pub use tick_array_bit_map::TICK_ARRAY_BITMAP_SIZE;pub use state_helpers::array_start_index_for_tick;pub use state_helpers::is_tick_out_of_boundary;pub use state_helpers::is_valid_tick_array_start_index;pub use state_helpers::tick_count_in_array;pub use state_helpers::FEE_RATE_DENOMINATOR_VALUE;pub use state_helpers::TICK_ARRAY_SIZE;pub use transfer_fee::apply_transfer_fee;pub use transfer_fee::calculate_fee;pub use transfer_fee::reverse_apply_transfer_fee;pub use transfer_fee::TransferFee;pub use transfer_fee::MAX_FEE_BASIS_POINTS;pub use core as core_;
Modules§
- error
- liquidity_
math - sqrt_
price_ math - state_
helpers - Pure-arithmetic helpers that the upstream Raydium program defines as
static methods on
TickState/TickArrayState. The methods use no struct fields, so we re-host them as free functions here. - swap_
full - Multi-tick swap orchestrator.
- swap_
math - tick_
array_ bit_ map - tick_
math - transfer_
fee - Token-2022 transfer-fee math.