Crate clmm_swap_math

Crate clmm_swap_math 

Source
Expand description

Uniswap V3–style math and swap simulation in pure Rust.

This crate exposes:

  • Low‑level math primitives (math::*) for ticks, prices and bitmaps.
  • A lightweight in‑memory V3Pool that can execute Uniswap V3‑style swaps.
  • Optional onchain helpers to hydrate the pool from a real on‑chain pool.

§Examples

§Pure math

use clmm_swap_math::{math::tick_math, RESOLUTION, U256};

let sqrt_price = tick_math::get_sqrt_ratio_at_tick(0).unwrap();
assert!(sqrt_price > U256::ZERO);
assert_eq!(RESOLUTION, 96);

§Simulating a swap in an in‑memory pool

use clmm_swap_math::{
    math::tick_math::get_sqrt_ratio_at_tick,
    pool::swap::{SwapParams, calculate_sqrt_price_limit},
    V3Pool, FastMap, I256, U256,
};

// Construct a simple in‑memory pool (no on‑chain provider).
let mut pool: V3Pool<()> = V3Pool::new(pool_address, token0, token1, 3000);
pool.slot0.sqrt_price_x96 = get_sqrt_ratio_at_tick(0).unwrap();
pool.slot0.tick = 0;
pool.liquidity = 1_000_000_000_000_000_000u128;
pool.tick_spacing = 1;
pool.bitmap = FastMap::default();

let zero_for_one = true;
let amount_specified = I256::from_raw(U256::from(1_000_000_000_000_000_000u128)); // 1e18
let sqrt_price_limit_x96 =
    calculate_sqrt_price_limit(pool.slot0.sqrt_price_x96, zero_for_one, 1.0); // 1% slippage

let params = SwapParams::new(zero_for_one, amount_specified, sqrt_price_limit_x96);
let result = pool.swap(params).unwrap();
println!("amount0: {}, amount1: {}", result.amount0_delta, result.amount1_delta);

Re-exports§

pub use pool::v3_pool::V3Pool;

Modules§

error
math
pool

Structs§

Address
An Ethereum address, 20 bytes in length.

Constants§

Q96
RESOLUTION

Type Aliases§

FastMap
I256
256-bit signed integer type, consisting of 4, 64-bit limbs.
U256
256-bit unsigned integer type, consisting of 4, 64-bit limbs.