Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor party #56

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add BNBPartyFee file
YouStillAlive committed Aug 23, 2024
commit 8db23a4c48f5ffbab8d4f67035f5cc4fd8f83868
11 changes: 2 additions & 9 deletions contracts/BNBPartyFactory.sol
Original file line number Diff line number Diff line change
@@ -58,17 +58,10 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab
IUniswapV3Pool pool = IUniswapV3Pool(msg.sender);

uint256 WBNBBalance = WBNB.balanceOf(msg.sender);
uint256 feeGrowthGlobal = 0;
if (pool.token0() == address(WBNB)) {
(uint256 feeGrowthInside0LastX128, ) = _getPartyFeeGrowthInsideLastX128(pool);
feeGrowthGlobal = pool.feeGrowthGlobal0X128() - feeGrowthInside0LastX128;
} else {
(, uint256 feeGrowthInside1LastX128) = _getPartyFeeGrowthInsideLastX128(pool);
feeGrowthGlobal = pool.feeGrowthGlobal1X128() - feeGrowthInside1LastX128;
}

uint256 feeGrowthGlobal = _calculateFeeGrowthGlobal(pool);
uint256 liquidity = pool.liquidity();
uint256 feesEarned = calculateFees(liquidity, feeGrowthGlobal);

if (WBNBBalance - feesEarned < party.partyTarget) return;
// Handle liquidity
_handleLiquidity(recipient);
92 changes: 92 additions & 0 deletions contracts/BNBPartyFee.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./BNBPartyModifiers.sol";

/// @title BNBPartyFee
/// @notice This abstract contract provides internal functions for calculating fees in the BNB Party system.
abstract contract BNBPartyFee is BNBPartyModifiers {
/// @notice Internal function to retrieve the fee growth inside the position from the last observation
/// @param pool Address of the Uniswap V3 pool
/// @return feeGrowthInside0LastX128 Fee growth inside for token0 from the last observation
/// @return feeGrowthInside1LastX128 Fee growth inside for token1 from the last observation
function _getFeeGrowthInsideLastX128(
IUniswapV3Pool pool
)
internal
view
returns (
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
{
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = _getFeeGrowthInsideLastX128(
pool,
keccak256(
abi.encodePacked(
address(positionManager),
party.lpTicks.tickLower,
party.lpTicks.tickUpper
)
)
);
}

/// @notice Internal function to retrieve the fee growth inside the position from the last observation
/// @param pool Address of the Uniswap V3 pool
function _getPartyFeeGrowthInsideLastX128(
IUniswapV3Pool pool
)
internal
view
returns (
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
{
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = _getFeeGrowthInsideLastX128(
pool,
keccak256(
abi.encodePacked(
address(BNBPositionManager),
party.partyTicks.tickLower,
party.partyTicks.tickUpper
)
)
);
}

/// @notice Internal function to retrieve the fee growth inside the position from the last observation
/// @param pool Address of the Uniswap V3 pool
function _getFeeGrowthInsideLastX128(
IUniswapV3Pool pool,
bytes32 key
)
internal
view
returns (
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
{
(, feeGrowthInside0LastX128, feeGrowthInside1LastX128, , ) = pool.positions(key);
}

/// @notice Internal function to calculate the global fee growth
/// @param pool Address of the Uniswap V3 pool
function _calculateFeeGrowthGlobal(IUniswapV3Pool pool) internal view returns (uint256 feeGrowthGlobal) {
if (pool.token0() == address(WBNB)) {
(uint256 feeGrowthInside0LastX128,) = _getPartyFeeGrowthInsideLastX128(pool);
feeGrowthGlobal = pool.feeGrowthGlobal0X128() - feeGrowthInside0LastX128;
} else {
(,uint256 feeGrowthInside1LastX128) = _getPartyFeeGrowthInsideLastX128(pool);
feeGrowthGlobal = pool.feeGrowthGlobal1X128() - feeGrowthInside1LastX128;
}
}
}
72 changes: 2 additions & 70 deletions contracts/BNBPartyView.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./BNBPartyModifiers.sol";
import "./BNBPartyFee.sol";

/// @title BNBPartyView
/// @notice This abstract contract provides view functions for the BNB Party system, including fee calculations and token checks.
abstract contract BNBPartyView is BNBPartyModifiers {
abstract contract BNBPartyView is BNBPartyFee {
/// @notice Checks if WBNB is the token0 in the provided Uniswap V3 pool
/// @param liquidityPool Address of the Uniswap V3 pool to check
/// @return True if WBNB is token0, false otherwise
@@ -55,72 +55,4 @@ abstract contract BNBPartyView is BNBPartyModifiers {
feeGrowthInside1LastX128
) = manager == BNBPositionManager ? _getPartyFeeGrowthInsideLastX128(pool) : _getFeeGrowthInsideLastX128(pool);
}

/// @notice Internal function to retrieve the fee growth inside the position from the last observation
/// @param pool Address of the Uniswap V3 pool
/// @return feeGrowthInside0LastX128 Fee growth inside for token0 from the last observation
/// @return feeGrowthInside1LastX128 Fee growth inside for token1 from the last observation
function _getFeeGrowthInsideLastX128(
IUniswapV3Pool pool
)
internal
view
returns (
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
{
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = _getFeeGrowthInsideLastX128(
pool,
keccak256(
abi.encodePacked(
address(positionManager),
party.lpTicks.tickLower,
party.lpTicks.tickUpper
)
)
);
}

function _getPartyFeeGrowthInsideLastX128(
IUniswapV3Pool pool
)
internal
view
returns (
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
{
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = _getFeeGrowthInsideLastX128(
pool,
keccak256(
abi.encodePacked(
address(BNBPositionManager),
party.partyTicks.tickLower,
party.partyTicks.tickUpper
)
)
);
}

function _getFeeGrowthInsideLastX128(
IUniswapV3Pool pool,
bytes32 key
)
internal
view
returns (
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
{
(, feeGrowthInside0LastX128, feeGrowthInside1LastX128, , ) = pool.positions(key);
}
}