From e28ed92f0805adf29b81473aa627e1b71eb857d3 Mon Sep 17 00:00:00 2001 From: Andrew Dmytrenko Date: Thu, 15 Aug 2024 09:34:59 +0300 Subject: [PATCH] add _calcSqrtPriceX96 --- contracts/BNBPartyLiquidity.sol | 20 ++++++++++++++++++-- contracts/BNBPartyView.sol | 8 ++++---- test/BNBPartyFactory.ts | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/contracts/BNBPartyLiquidity.sol b/contracts/BNBPartyLiquidity.sol index 17c9435..39a92fe 100644 --- a/contracts/BNBPartyLiquidity.sol +++ b/contracts/BNBPartyLiquidity.sol @@ -86,7 +86,6 @@ abstract contract BNBPartyLiquidity is BNBPartySwaps { /// @dev Decreases liquidity, collects tokens, creates a new pool, and sends bonuses function _handleLiquidity(address recipient) internal { IUniswapV3Pool pool = IUniswapV3Pool(msg.sender); - (uint160 sqrtPriceX96, , , , , , ) = pool.slot0(); address token0 = pool.token0(); address token1 = pool.token1(); @@ -121,11 +120,28 @@ abstract contract BNBPartyLiquidity is BNBPartySwaps { IERC20(token0).approve(address(positionManager), amount0); IERC20(token1).approve(address(positionManager), amount1); - + uint160 sqrtPriceX96 = _calcSqrtPriceX96(amount0, amount1); // Create new Liquidity Pool _createLP(positionManager, token0, token1, amount0, amount1, sqrtPriceX96, party.lpFee); // Send bonuses _unwrapAndSendBNB(recipient, unwrapAmount); } + + function _calcSqrtPriceX96( + uint256 amount0, + uint256 amount1 + ) internal pure returns (uint160 sqrtPriceX96) { + uint256 ratioX192 = (amount1 << 192) / amount0; // Shift left by 192 to maintain precision + sqrtPriceX96 = uint160(_sqrt(ratioX192)); + } + + function _sqrt(uint256 x) private pure returns (uint256 y) { + uint256 z = (x + 1) / 2; + y = x; + while (z < y) { + y = z; + z = (x / z + z) / 2; + } + } } diff --git a/contracts/BNBPartyView.sol b/contracts/BNBPartyView.sol index de2a9d5..5430398 100644 --- a/contracts/BNBPartyView.sol +++ b/contracts/BNBPartyView.sol @@ -7,16 +7,16 @@ import "./BNBPartyModifiers.sol"; /// @notice This abstract contract provides view functions for the BNB Party system, including fee calculations and token checks. abstract contract BNBPartyView is BNBPartyModifiers { /// @notice Checks if WBNB is the token0 in the provided Uniswap V3 pool - /// @param liquidtyPool Address of the Uniswap V3 pool to check + /// @param liquidityPool Address of the Uniswap V3 pool to check /// @return True if WBNB is token0, false otherwise /// @dev Reverts if the provided pool address is zero function isToken0WBNB( - IUniswapV3Pool liquidtyPool + IUniswapV3Pool liquidityPool ) external view returns (bool) { - if (liquidtyPool == IUniswapV3Pool(address(0))) { + if (liquidityPool == IUniswapV3Pool(address(0))) { revert ZeroAddress(); } - return liquidtyPool.token0() == address(WBNB); // Checks if WBNB is token0 + return liquidityPool.token0() == address(WBNB); // Checks if WBNB is token0 } /// @notice Calculates the fees earned based on liquidity and global fee growth diff --git a/test/BNBPartyFactory.ts b/test/BNBPartyFactory.ts index e399513..c9457b9 100644 --- a/test/BNBPartyFactory.ts +++ b/test/BNBPartyFactory.ts @@ -154,7 +154,7 @@ describe("BNBPartyFactory", function () { const lpAddress = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH) const balance = await weth9.balanceOf(lpAddress) const percentFee = ethers.parseEther("0.91") - expect(balance).to.be.equal(BNBToTarget - returnFeeAmount - bonusFee - targetReachFee - percentFee - 1n) + expect(balance).to.be.equal(BNBToTarget - returnFeeAmount - bonusFee - targetReachFee - percentFee - 3n) }) }) })