0xKitsune/uniswap-v3-math

Sqrt Price Math get_amount_0_delta & get_amount_1_delta return type wrong

0xOsiris opened this issue · 1 comments

In sqrt_price_math.rs the following needs to be changed.

pub fn get_amount_0_delta(
    sqrt_ratio_a_x_96: U256,
    sqrt_ratio_b_x_96: U256,
    liquidity: i128,
) -> I256 {
    if liquidity < 0 {
        return I256::from_raw(_get_amount_0_delta(
            sqrt_ratio_b_x_96,
            sqrt_ratio_a_x_96,
            -liquidity as i128,
            false,
        ));
    } else {
        return I256::from_raw(_get_amount_0_delta(
            sqrt_ratio_a_x_96,
            sqrt_ratio_b_x_96,
            liquidity,
            true,
        ));
    }
}

Should be

pub fn get_amount_0_delta(
    sqrt_ratio_a_x_96: U256,
    sqrt_ratio_b_x_96: U256,
    liquidity: i128,
) -> I256 {
    if liquidity < 0 {
       //Return the negation
        return -I256::from_raw(_get_amount_0_delta(
            sqrt_ratio_b_x_96,
            sqrt_ratio_a_x_96,
            -liquidity as i128,
            false,
        ));
    } else {
        return I256::from_raw(_get_amount_0_delta(
            sqrt_ratio_a_x_96,
            sqrt_ratio_b_x_96,
            liquidity,
            true,
        ));
    }
}

Further,

pub fn get_amount_1_delta(
    sqrt_ratio_a_x_96: U256,
    sqrt_ratio_b_x_96: U256,
    liquidity: i128,
) -> I256 {
    if liquidity < 0 {
        return I256::from_raw(_get_amount_1_delta(
            sqrt_ratio_b_x_96,
            sqrt_ratio_a_x_96,
            -liquidity as i128,
            false,
        ));
    } else {
        return I256::from_raw(_get_amount_1_delta(
            sqrt_ratio_a_x_96,
            sqrt_ratio_b_x_96,
            liquidity,
            true,
        ));
    }
}

Should be

pub fn get_amount_1_delta(
    sqrt_ratio_a_x_96: U256,
    sqrt_ratio_b_x_96: U256,
    liquidity: i128,
) -> I256 {
    if liquidity < 0 {
       //Return the negation
        return -I256::from_raw(_get_amount_1_delta(
            sqrt_ratio_b_x_96,
            sqrt_ratio_a_x_96,
            -liquidity as i128,
            false,
        ));
    } else {
        return I256::from_raw(_get_amount_1_delta(
            sqrt_ratio_a_x_96,
            sqrt_ratio_b_x_96,
            liquidity,
            true,
        ));
    }
}

Reference:

/// @notice Helper that gets signed token0 delta
    /// @param sqrtRatioAX96 A sqrt price
    /// @param sqrtRatioBX96 Another sqrt price
    /// @param liquidity The change in liquidity for which to compute the amount0 delta
    /// @return amount0 Amount of token0 corresponding to the passed liquidityDelta between the two prices
    function getAmount0Delta(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        int128 liquidity
    ) internal pure returns (int256 amount0) {
        unchecked {
            return
                liquidity < 0
                    ? -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
                    : getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
        }
    }

    /// @notice Helper that gets signed token1 delta
    /// @param sqrtRatioAX96 A sqrt price
    /// @param sqrtRatioBX96 Another sqrt price
    /// @param liquidity The change in liquidity for which to compute the amount1 delta
    /// @return amount1 Amount of token1 corresponding to the passed liquidityDelta between the two prices
    function getAmount1Delta(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        int128 liquidity
    ) internal pure returns (int256 amount1) {
        unchecked {
            return
                liquidity < 0
                    ? -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
                    : getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
        }
    }

Thanks for the issue, just merged your PR #4