nautilus-model 0.58.0

Domain model for the Nautilus trading engine
Documentation
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

use crate::defi::{pool_analysis::error::LiquidityMathError, tick_map::tick::PoolTick};

/// Adds a signed liquidity delta to liquidity, returning a structured error on
/// overflow or underflow.
///
/// # Errors
///
/// Returns [`LiquidityMathError::Overflow`] when adding a positive delta wraps past
/// `u128::MAX`, or [`LiquidityMathError::Underflow`] when subtracting wraps below zero.
pub fn try_liquidity_math_add(x: u128, y: i128) -> Result<u128, LiquidityMathError> {
    if y < 0 {
        let delta = y.unsigned_abs();
        let z = x.wrapping_sub(delta);
        if z >= x {
            return Err(LiquidityMathError::Underflow { current: x, delta });
        }
        Ok(z)
    } else {
        let delta = y as u128;
        let z = x.wrapping_add(delta);
        if z < x {
            return Err(LiquidityMathError::Overflow { current: x, delta });
        }
        Ok(z)
    }
}

/// Adds a signed liquidity delta to liquidity, panicking on overflow or underflow.
///
/// Prefer [`try_liquidity_math_add`] in event-replay paths where a structured error
/// with surrounding context is preferred. This panic-style variant is kept for
/// in-pool invariants where overflow is treated as a contract bug rather than an
/// expected runtime error.
///
/// # Returns
///
/// The resulting liquidity after applying the delta.
///
/// # Panics
///
/// This function panics if:
/// - Adding positive delta causes overflow.
/// - Subtracting causes underflow.
#[must_use]
pub fn liquidity_math_add(x: u128, y: i128) -> u128 {
    match try_liquidity_math_add(x, y) {
        Ok(value) => value,
        Err(LiquidityMathError::Overflow { current, delta }) => {
            panic!("Liquidity addition overflow: x={current}, y={y}, delta={delta}")
        }
        Err(LiquidityMathError::Underflow { current, delta }) => {
            panic!("Liquidity subtraction underflow: x={current}, y={y}, delta={delta}")
        }
    }
}

/// Derives max liquidity per tick from a given tick spacing.
///
/// # Panics
///
/// Panics if `tick_spacing` is zero.
#[must_use]
pub fn tick_spacing_to_max_liquidity_per_tick(tick_spacing: i32) -> u128 {
    assert!(tick_spacing != 0, "Tick spacing must be non-zero");

    // Calculate min and max tick aligned to tick spacing
    let min_tick = (PoolTick::MIN_TICK / tick_spacing) * tick_spacing;
    let max_tick = (PoolTick::MAX_TICK / tick_spacing) * tick_spacing;

    // Calculate total number of ticks, cast to i64 to avoid potential overflow in subtraction
    let num_ticks = ((i64::from(max_tick) - i64::from(min_tick)) / i64::from(tick_spacing)) + 1;

    u128::MAX / num_ticks as u128
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;

    #[rstest]
    fn test_add() {
        assert_eq!(liquidity_math_add(1, 0), 1);
        assert_eq!(liquidity_math_add(1, 1), 2);
    }

    #[rstest]
    fn test_subtract_one() {
        assert_eq!(liquidity_math_add(1, -1), 0);
        assert_eq!(liquidity_math_add(3, -2), 1);
    }

    #[rstest]
    #[should_panic(expected = "Liquidity addition overflow")]
    fn test_addition_overflow() {
        let x = u128::MAX - 14; // Close to max so adding 15 will overflow
        let _ = liquidity_math_add(x, 15);
    }

    #[rstest]
    #[should_panic(expected = "Liquidity subtraction underflow")]
    fn test_subtraction_underflow_zero() {
        let _ = liquidity_math_add(0, -1);
    }

    #[rstest]
    #[should_panic(expected = "Liquidity subtraction underflow")]
    fn test_subtraction_underflow() {
        let _ = liquidity_math_add(3, -4);
    }

    #[rstest]
    fn test_try_add_returns_overflow_error() {
        let x = u128::MAX - 14;
        let err = try_liquidity_math_add(x, 15).unwrap_err();
        assert_eq!(
            err,
            LiquidityMathError::Overflow {
                current: x,
                delta: 15
            }
        );
    }

    #[rstest]
    fn test_try_add_returns_underflow_error() {
        let err = try_liquidity_math_add(3, -4).unwrap_err();
        assert_eq!(
            err,
            LiquidityMathError::Underflow {
                current: 3,
                delta: 4
            }
        );
    }

    #[rstest]
    fn test_tick_spacing_to_max_liquidity() {
        // 0.01 tier ot 1 tick spacing
        assert_eq!(
            tick_spacing_to_max_liquidity_per_tick(1),
            191_757_530_477_355_301_479_181_766_273_477
        );
        // 0.05 % tier or 10 tick spacing
        assert_eq!(
            tick_spacing_to_max_liquidity_per_tick(10),
            1_917_569_901_783_203_986_719_870_431_555_990
        );
        // 0.3 % tier or 60 tick spacing
        assert_eq!(
            tick_spacing_to_max_liquidity_per_tick(60),
            11_505_743_598_341_114_571_880_798_222_544_994
        );
        // 1.00% tier or 200 tick spacing
        assert_eq!(
            tick_spacing_to_max_liquidity_per_tick(200),
            38_350_317_471_085_141_830_651_933_667_504_588
        );
    }
}