Skip to content

Commit

Permalink
Merge pull request #74 from bnb-party/issue-68
Browse files Browse the repository at this point in the history
refactor `feeGrowthInsideLastX128`
  • Loading branch information
YouStillAlive authored Sep 24, 2024
2 parents 83442bd + cce88a9 commit cf9ee84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 49 deletions.
65 changes: 17 additions & 48 deletions contracts/BNBPartyFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,17 @@ import "./interfaces/IUniswapV3Pool.sol";
/// @notice This abstract contract provides internal functions for calculating fees in the BNB Party system.
abstract contract BNBPartyFee is BNBPartyState {
/// @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
)
{
Ticks memory ticks = _getTicks(pool.token0(), party.lpTicks);
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = _getFeeGrowthInsideLastX128(
pool,
keccak256(
abi.encodePacked(
address(positionManager),
ticks.tickLower,
ticks.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
)
{
Ticks memory ticks = _getTicks(pool.token0(), party.partyTicks);
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = _getFeeGrowthInsideLastX128(
INonfungiblePositionManager manager,
IUniswapV3Pool pool,
Ticks memory lpTicks
) internal view returns (uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128) {
Ticks memory ticks = _getTicks(pool.token0(), lpTicks);
(feeGrowthInside0LastX128, feeGrowthInside1LastX128) = _getFeeGrowthInsideLastX128(
pool,
keccak256(
abi.encodePacked(
address(BNBPositionManager),
address(manager),
ticks.tickLower,
ticks.tickUpper
)
Expand Down Expand Up @@ -87,10 +48,18 @@ abstract contract BNBPartyFee is BNBPartyState {
IUniswapV3Pool pool
) internal view returns (uint256 feeGrowthGlobal) {
if (pool.token0() == address(WBNB)) {
(uint256 feeGrowthInside0LastX128 , ) = _getPartyFeeGrowthInsideLastX128(pool);
(uint256 feeGrowthInside0LastX128 , ) = _getFeeGrowthInsideLastX128(
BNBPositionManager,
pool,
party.partyTicks
);
feeGrowthGlobal = pool.feeGrowthGlobal0X128() -feeGrowthInside0LastX128;
} else {
( , uint256 feeGrowthInside1LastX128) = _getPartyFeeGrowthInsideLastX128(pool);
( , uint256 feeGrowthInside1LastX128) = _getFeeGrowthInsideLastX128(
BNBPositionManager,
pool,
party.partyTicks
);
feeGrowthGlobal = pool.feeGrowthGlobal1X128() - feeGrowthInside1LastX128;
}
}
Expand Down
11 changes: 10 additions & 1 deletion contracts/BNBPartyView.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ abstract contract BNBPartyView is BNBPartyManageable {
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = manager == BNBPositionManager ? _getPartyFeeGrowthInsideLastX128(pool) : _getFeeGrowthInsideLastX128(pool);
) = manager == BNBPositionManager ? _getFeeGrowthInsideLastX128(
BNBPositionManager,
pool,
party.partyTicks
) :
_getFeeGrowthInsideLastX128(
positionManager,
pool,
party.lpTicks
);
}
}

0 comments on commit cf9ee84

Please sign in to comment.