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.

Last updated