Skip to content

Commit

Permalink
add _calcSqrtPriceX96
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Aug 15, 2024
1 parent 396aae3 commit e28ed92
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
20 changes: 18 additions & 2 deletions contracts/BNBPartyLiquidity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
}
}
8 changes: 4 additions & 4 deletions contracts/BNBPartyView.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/BNBPartyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit e28ed92

Please sign in to comment.