Get Coin Amounts or Liquidity for Position

1. Calculates token amounts from a given liquidity delta.

cetusdlmm/sources/dlmm_path.move

/// This function calculates how much of each token corresponds to a given
/// liquidity delta, proportional to the current bin composition.
/// The result is floor-rounded to ensure the bin has sufficient tokens.
///
/// ## Mathematical Formula
/// ```
/// amount_a_out = floor(amount_a * delta_liquidity / liquidity_share)
/// amount_b_out = floor(amount_b * delta_liquidity / liquidity_share)
/// ```
///
/// ## Parameters
/// - `amount_a`: Current amount of token A in the bin
/// - `amount_b`: Current amount of token B in the bin
/// - `delta_liquidity`: The liquidity delta to calculate amounts for
/// - `liquidity_share`: Total liquidity share in the bin
///
/// ## Returns
/// - `(u64, u64)`: Tuple of (amount_a, amount_b) corresponding to the liquidity delta
///
/// ## Errors
/// - `ELiquiditySupplyIsZero`: If liquidity supply is zero
/// - `EInvalidDeltaLiquidity`: If delta liquidity exceeds supply
public fun calculate_amounts_by_liquidity(
    amount_a: u64,
    amount_b: u64,
    delta_liquidity: u128,
    liquidity_share: u128,
): (u64, u64) {
    assert!(liquidity_share > 0, ELiquiditySupplyIsZero);
    assert!(delta_liquidity <= liquidity_share, EInvalidDeltaLiquidity);
    if (delta_liquidity == 0) {
        return (0, 0)
    };

    let out_amount_a = if (amount_a == 0) { 0 } else {
        full_math_u128::mul_div_floor(
            amount_a as u128,
            delta_liquidity,
            liquidity_share,
        )
    };
    let out_amount_b = if (amount_b == 0) { 0 } else {
        full_math_u128::mul_div_floor(
            amount_b as u128,
            delta_liquidity,
            liquidity_share,
        )
    };
    (out_amount_a as u64, out_amount_b as u64)
}

2. Calculates liquidity using the constant sum formula.

/// This function calculates the total liquidity value based on the amounts
/// of both tokens and the bin's price using the constant sum formula.
///
/// ## Mathematical Formula
/// `L = price * amount_a + amount_b`
///
/// Where:
/// - `L` = Total liquidity
/// - `price` = Bin price in Q64x64 format
/// - `amount_a` = Amount of token A
/// - `amount_b` = Amount of token B
///
/// ## Parameters
/// - `amount_a`: Amount of token A
/// - `amount_b`: Amount of token B
/// - `price`: Bin price in Q64x64 format
///
/// ## Returns
/// - `u128`: Total liquidity value
///
/// ## Errors
/// - `EPriceIsZero`: If the price is zero
/// - `ELiquidityOverflow`: If the calculation results in overflow
public fun calculate_liquidity_by_amounts(amount_a: u64, amount_b: u64, price: u128): u128 {
    assert!(price > 0, EPriceIsZero);
    if (amount_a == 0 && amount_b == 0) {
        return 0
    };
    let liquidity =
        full_math_u128::full_mul(amount_a as u128, price) + (amount_b as u256 << scale_offset!());
    assert!(liquidity < std::u128::max_value!() as u256, ELiquidityOverflow);
    liquidity as u128
}

Last updated