Skip to main content

curve_math/
lib.rs

1//! Pure Rust implementation of [Curve Finance](https://curve.fi/) AMM math.
2//!
3//! Exact on-chain match — no tolerances, no approximations, wei-level precision.
4//! Differentially fuzz-tested against on-chain `get_dy` for 200+ pools.
5//!
6//! # Architecture
7//!
8//! - **`core`** — stateless math functions (Newton solvers, Cardano cubic, fee).
9//!   Always available, zero dependencies beyond `alloy-primitives`.
10//! - **`swap`** + **`Pool`** — pool simulation with normalization, fees,
11//!   and denormalization. Requires the `swap` feature.
12//!
13//! # Quick start
14//!
15//! ```rust,ignore
16//! use curve_math::Pool; // requires feature "swap"
17//! use alloy_primitives::U256;
18//!
19//! let pool = Pool::StableSwapV2 {
20//!     balances: vec![U256::from(1_000_000_000_000_000_000_000u128),
21//!                    U256::from(1_000_000_000_000_000_000_000u128)],
22//!     rates: vec![U256::from(1_000_000_000_000_000_000u128),
23//!                 U256::from(1_000_000_000_000_000_000u128)],
24//!     amp: U256::from(40_000u64),    // A * A_PRECISION (400 * 100)
25//!     fee: U256::from(4_000_000u64), // 0.04%
26//! };
27//!
28//! let dx = U256::from(1_000_000_000_000_000_000u128); // 1 token
29//! let dy = pool.get_amount_out(0, 1, dx).expect("swap should succeed");
30//! assert!(dy > U256::ZERO);
31//! ```
32//!
33//! # Supported variants
34//!
35//! All 11 Curve pool types: `StableSwapV0`, `StableSwapV1`, `StableSwapV2`,
36//! `StableSwapALend`, `StableSwapNG`, `StableSwapMeta`, `TwoCryptoV1`,
37//! `TwoCryptoNG`, `TwoCryptoStable`, `TriCryptoV1`, `TriCryptoNG`.
38
39#![allow(clippy::too_many_arguments)]
40
41pub mod core;
42
43#[cfg(feature = "swap")]
44pub mod swap;
45
46#[cfg(feature = "swap")]
47mod pool;
48#[cfg(feature = "swap")]
49pub use pool::{Pool, PoolError};