wp-solana-amm-math 0.1.1

Protocol-agnostic AMM math for Solana DEX — tick pricing, bin pricing, liquidity math, swap simulation
Documentation
  • Coverage
  • 95.73%
    112 out of 117 items documented0 out of 67 items with examples
  • Size
  • Source code size: 165.79 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.68 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • daiwanwei

waterpump-solana-amm-math

crates.io

Protocol-agnostic AMM math library for Solana concentrated-liquidity pools (CLMM and DLMM).

Modules

Module Description
tick_math Tick-to-sqrt-price conversion, tick array helpers, bounds checks
price_math Human-readable price conversion (Q64.64 to decimal/f64)
swap_math Single-step swap computation (next price, amounts in/out, fees)
liquidity_math Liquidity deposit/withdraw quotes for CLMM positions
fee_math Fee growth and fee-owed calculations
fixed_point Q64.64 fixed-point arithmetic (mul, div, pow, rounding)
bin_price Meteora DLMM bin-to-price conversion
slippage Slippage tolerances and transfer fee helpers
full_math U256-backed mul_div_floor / mul_div_ceil

Quick start

use waterpump_solana_amm_math::{compute_swap_step, increase_liquidity_quote};
use waterpump_solana_amm_math::tick_math::tick_to_sqrt_price_x64;

// Compute a single swap step
let sqrt_price = tick_to_sqrt_price_x64(0).unwrap();
let sqrt_target = tick_to_sqrt_price_x64(100).unwrap();
let step = compute_swap_step(
    sqrt_price,   // current sqrt price (Q64.64)
    sqrt_target,  // target sqrt price
    1_000_000,    // available liquidity
    100_000,      // remaining amount
    300,          // fee rate (bps)
    true,         // a_to_b
    true,         // amount is specified input
);

// Quote a liquidity deposit
let lower = tick_to_sqrt_price_x64(-1000).unwrap();
let upper = tick_to_sqrt_price_x64(1000).unwrap();
let quote = increase_liquidity_quote(
    500_000,       // token A amount
    1_000_000,     // token B amount
    sqrt_price,    // current sqrt price
    lower,         // position lower sqrt price
    upper,         // position upper sqrt price
    Default::default(), // transfer fee A
    Default::default(), // transfer fee B
);

Meteora DLMM support

The bin_price and fixed_point modules provide bin-to-price conversion for Meteora DLMM pools. Bin pricing uses a different model (geometric bins with a configurable base factor) rather than the tick-based 1.0001^tick formula used by CLMM protocols.

Design notes

This is a pure math library with no Solana SDK dependency. It can be used in off-chain tooling, bots, and simulations without pulling in the full Solana runtime. The only dependencies are ethnum (U256 arithmetic), rust_decimal (precise decimal conversion), and thiserror (error types).