wp-solana-amm-math 0.1.1

Protocol-agnostic AMM math for Solana DEX — tick pricing, bin pricing, liquidity math, swap simulation
Documentation
//! Solana AMM math primitives.
//!
//! Pure math library for concentrated liquidity AMMs (Uniswap V3-style).
//! Covers tick/price conversions, swap step computation, liquidity math,
//! fee calculations, slippage utilities, and Meteora DLMM bin-price math.
//!
//! All functions are deterministic and RPC-free.

pub mod bin_price;
pub mod fee_math;
pub mod fixed_point;
pub mod full_math;
pub mod liquidity_math;
pub mod price_math;
pub mod slippage;
pub mod swap_math;
pub mod tick_math;

// Re-export commonly used fixed-point primitives
pub use fixed_point::{
    mul_div, mul_shr, pow, safe_mul_div_cast, safe_mul_shr_cast, safe_shl_div_cast, shl_div,
    Rounding, MAX_EXPONENTIAL, ONE, PRECISION, SCALE_OFFSET,
};
// Re-export commonly used types from liquidity_math for convenience
pub use liquidity_math::{
    decrease_liquidity_quote, decrease_liquidity_quote_a, decrease_liquidity_quote_b,
    increase_liquidity_quote, increase_liquidity_quote_a, increase_liquidity_quote_b,
    order_tick_indexes, DecreaseLiquidityQuote, IncreaseLiquidityQuote, TickRange, TransferFee,
};
// Re-export price math utilities
pub use price_math::invert_price;
// Re-export slippage and transfer fee utilities
pub use slippage::{
    apply_transfer_fee, max_amount_with_slippage, min_amount_with_slippage,
    reverse_apply_transfer_fee, sqrt_price_slippage_bounds,
};
// Re-export swap math primitives
pub use swap_math::{
    compute_swap_step, get_next_sqrt_price_from_input, get_next_sqrt_price_from_output,
    SwapStepResult,
};
use thiserror::Error;
// Re-export tick array helpers from tick_math
pub use tick_math::{
    get_full_range_tick_indexes, get_initializable_tick_index, get_next_initializable_tick_index,
    get_prev_initializable_tick_index, get_tick_array_start_index, get_tick_index_in_array,
    invert_tick_index, is_position_in_range, is_tick_in_bounds, is_tick_initializable, tick_count,
    RAYDIUM_TICK_ARRAY_SIZE, WHIRLPOOL_TICK_ARRAY_SIZE,
};

/// Errors returned by AMM math operations.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum AmmMathError {
    /// A division by zero was attempted.
    #[error("division by zero")]
    DivisionByZero,
    /// An intermediate or final result overflowed the target integer type.
    #[error("result overflow")]
    Overflow,
    /// The tick index is outside the valid range [`MIN_TICK`, `MAX_TICK`].
    #[error("tick out of range: {0}")]
    TickOutOfRange(i32),
    /// The sqrt price is outside the valid range.
    #[error("sqrt price out of range: {0}")]
    SqrtPriceOutOfRange(u128),
    /// The fee rate exceeds the maximum allowed (10 000 bps = 100%).
    #[error("invalid fee rate: {0} bps")]
    InvalidFeeRate(u16),
}