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 feeGrowthInsideLastX128 #74

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 BNBPartyFee {
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
) = manager == BNBPositionManager ? _getPartyFeeGrowthInsideLastX128(pool) : _getFeeGrowthInsideLastX128(pool);
) = manager == BNBPositionManager ? _getFeeGrowthInsideLastX128(
BNBPositionManager,
pool,
party.partyTicks
) :
_getFeeGrowthInsideLastX128(
positionManager,
pool,
party.lpTicks
);
}
}