Overview
Theank-math module provides fixed-point math helpers for WAD (1e18) and RAY (1e27) arithmetic, along with Uniswap V3-style tick and liquidity math utilities.
Constants
WAD
RAY
Other Constants
WAD Math Functions
wad_mul
(a * b) / WAD (truncating).
First WAD-scaled operand
Second WAD-scaled operand
Result in WAD scale
wad_div
(a * WAD) / b (truncating; 0 if b == 0).
Numerator in WAD scale
Denominator in WAD scale
Result in WAD scale, or 0 if b is 0
wad_mul_sat
floor(a*b/WAD) using a hi/lo split.
First WAD-scaled operand
Second WAD-scaled operand
Result in WAD scale (saturates on overflow)
wad_div_sat
floor(a*WAD/b) via quotient/remainder splitting.
Numerator in WAD scale
Denominator in WAD scale
Result in WAD scale (saturates on overflow)
RAY Math Functions
ray_mul
(a * b) / RAY (truncating).
First RAY-scaled operand
Second RAY-scaled operand
Result in RAY scale
ray_div
(a * RAY) / b (truncating; 0 if b == 0).
Numerator in RAY scale
Denominator in RAY scale
Result in RAY scale, or 0 if b is 0
ray_mul_sat
First RAY-scaled operand
Second RAY-scaled operand
Result in RAY scale (saturates on overflow)
ray_div_sat
Numerator in RAY scale
Denominator in RAY scale
Result in RAY scale (saturates on overflow)
ray_pow
base^exp in RAY. Uses repeated squaring with saturating mul/div.
Base value in RAY scale
Exponent (not scaled)
Result in RAY scale (base^exp)
ray_mul_checked
+ RAY/2.
First RAY-scaled operand
Second RAY-scaled operand
Result in RAY scale, or None on overflow
Utility Functions
mul_div_wad
wad_div_sat(num, den). Computes num * WAD / den with saturation.
mul_bps_sat
x * bps / 10_000 with saturation (basis points multiply).
Value to multiply
Basis points (1 bps = 0.01%)
Result after applying basis points
mul_div_small
floor(num * mul / den) splitting to avoid overflow; 0 if den == 0.
Uniswap V3 Math
The module includes extensive Uniswap V3-style math utilities in submodules:tick_math
Converts between ticks and sqrt-price (Q96 format).liquidity_math
Calculates liquidity from token amounts and vice versa.sqrt_price_math
Calculates next sqrt-price after swaps.Usage Examples
Basic WAD arithmetic
RAY exponentiation
Notes
- ”*_sat” variants use saturating arithmetic to avoid panics
- Liquidity/price formulas follow the standard V3 equations, using
BigUintinternally to avoid intermediate overflow and then clamping tou128 - The tick conversions use
f64forln/powand are accurate for typical ranges; extreme ticks may incur tiny rounding drift - Big-int →
u128conversion uses a saturating clamp (u128::MAX) on overflow