From 61a33d3041abc3455a447b6b4d373103e2b2e94d Mon Sep 17 00:00:00 2001 From: Chef Omelette Date: Tue, 16 Apr 2024 15:00:10 +0800 Subject: [PATCH 1/2] feat: Add events and methods for off-chain farming --- .../CLPoolManagerTest#addLiquidity_fromEmpty.snap | 2 +- ...CLPoolManagerTest#addLiquidity_fromNonEmpty.snap | 2 +- .../CLPoolManagerTest#addLiquidity_nativeToken.snap | 2 +- ...LPoolManagerTest#removeLiquidity_toNonEmpty.snap | 2 +- .../CLPoolManagerTest#swap_againstLiquidity.snap | 2 +- ...olManagerTest#swap_leaveSurplusTokenInVault.snap | 2 +- .../CLPoolManagerTest#swap_runOutOfLiquidity.snap | 2 +- .forge-snapshots/CLPoolManagerTest#swap_simple.snap | 2 +- ...PoolManagerTest#swap_useSurplusTokenAsInput.snap | 2 +- .../CLPoolManagerTest#swap_withHooks.snap | 2 +- .../CLPoolManagerTest#swap_withNative.snap | 2 +- .../CLPoolManagerTest#testNoOp_gas_Swap.snap | 2 +- .forge-snapshots/ExtsloadTest#extsloadInBatch.snap | 2 +- src/pool-cl/CLPoolManager.sol | 8 ++++++++ src/pool-cl/libraries/CLPool.sol | 13 +++++++++++++ 15 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap index 579fbe3b..fac27e07 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap @@ -1 +1 @@ -348804 \ No newline at end of file +350133 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap index 86e18869..84069280 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap @@ -1 +1 @@ -59352 \ No newline at end of file +60681 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap index 628d837b..9edb74df 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap @@ -1 +1 @@ -242397 \ No newline at end of file +243726 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index a84eee00..4d04a3f6 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -42330 \ No newline at end of file +43659 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap index c7be6999..a326f1a4 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap @@ -1 +1 @@ -54822 \ No newline at end of file +54779 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap b/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap index 7f99925b..61f06764 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap @@ -1 +1 @@ -102898 \ No newline at end of file +102855 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap index 86c92b12..af5891cb 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap @@ -1 +1 @@ -25042646 \ No newline at end of file +25042603 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_simple.snap b/.forge-snapshots/CLPoolManagerTest#swap_simple.snap index 26b24138..f578697f 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_simple.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_simple.snap @@ -1 +1 @@ -36022 \ No newline at end of file +35979 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap b/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap index 5426c4ef..836742f3 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap @@ -1 +1 @@ -101660 \ No newline at end of file +101617 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_withHooks.snap b/.forge-snapshots/CLPoolManagerTest#swap_withHooks.snap index 16ad56ad..d48f70e7 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_withHooks.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_withHooks.snap @@ -1 +1 @@ -41672 \ No newline at end of file +41629 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_withNative.snap b/.forge-snapshots/CLPoolManagerTest#swap_withNative.snap index 6db723bb..47d65c10 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_withNative.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_withNative.snap @@ -1 +1 @@ -36025 \ No newline at end of file +35982 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap b/.forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap index c6b439c0..2f3b0384 100644 --- a/.forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap +++ b/.forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap @@ -1 +1 @@ -21667 \ No newline at end of file +21624 \ No newline at end of file diff --git a/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap b/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap index 6c07f02b..937832ea 100644 --- a/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap +++ b/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap @@ -1 +1 @@ -11352 \ No newline at end of file +11397 \ No newline at end of file diff --git a/src/pool-cl/CLPoolManager.sol b/src/pool-cl/CLPoolManager.sol index dbed1c4e..28f59671 100644 --- a/src/pool-cl/CLPoolManager.sol +++ b/src/pool-cl/CLPoolManager.sol @@ -302,6 +302,14 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload { return pools[id].tickBitmap[word]; } + function getPoolFeeGrowthInside(PoolId id, int24 tickLower, int24 tickUpper) + external + view + returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) + { + return pools[id].getFeeGrowthInside(tickLower, tickUpper); + } + /// @inheritdoc ICLPoolManager function setMasterChef(address _masterChef) external override onlyOwner { masterChef = _masterChef; diff --git a/src/pool-cl/libraries/CLPool.sol b/src/pool-cl/libraries/CLPool.sol index b1aeec7c..628bde88 100644 --- a/src/pool-cl/libraries/CLPool.sol +++ b/src/pool-cl/libraries/CLPool.sol @@ -42,6 +42,9 @@ library CLPool { /// @notice Thrown by donate if there is currently 0 liquidity, since the fees will not go to any liquidity providers error NoLiquidityToReceiveFees(); + /// @dev for off-chain farming + event SwapFees(uint256 feesOwed0, uint256 feesOwed1); + struct Slot0 { // the current price uint160 sqrtPriceX96; @@ -110,6 +113,8 @@ library CLPool { Tick.checkTicks(params.tickLower, params.tickUpper); (uint256 feesOwed0, uint256 feesOwed1) = _updatePosition(self, params, _slot0.tick); + // @dev track swap fees for off-chain farming + emit SwapFees(feesOwed0, feesOwed1); ///@dev calculate the tokens delta needed if (params.liquidityDelta != 0) { @@ -490,4 +495,12 @@ library CLPool { function isNotInitialized(State storage self) internal view returns (bool) { return self.slot0.sqrtPriceX96 == 0; } + + function getFeeGrowthInside(State storage self, int24 tickLower, int24 tickUpper) + internal + view + returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) + { + return self.ticks.getFeeGrowthInside(tickLower, tickUpper, self.slot0.tick, self.feeGrowthGlobal0X128, self.feeGrowthGlobal1X128); + } } From fc9bed28889ded5148bedcd93397e6ccc33b93ba Mon Sep 17 00:00:00 2001 From: Chef Omelette Date: Wed, 17 Apr 2024 15:30:23 +0800 Subject: [PATCH 2/2] feat: Remove SwapFees event --- .../CLPoolManagerTest#addLiquidity_fromEmpty.snap | 2 +- .../CLPoolManagerTest#addLiquidity_fromNonEmpty.snap | 2 +- .../CLPoolManagerTest#addLiquidity_nativeToken.snap | 2 +- .../CLPoolManagerTest#removeLiquidity_toNonEmpty.snap | 2 +- src/pool-cl/libraries/CLPool.sol | 9 +++------ 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap index fac27e07..579fbe3b 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap @@ -1 +1 @@ -350133 \ No newline at end of file +348804 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap index 84069280..86e18869 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap @@ -1 +1 @@ -60681 \ No newline at end of file +59352 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap index 9edb74df..628d837b 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap @@ -1 +1 @@ -243726 \ No newline at end of file +242397 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index 4d04a3f6..a84eee00 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -43659 \ No newline at end of file +42330 \ No newline at end of file diff --git a/src/pool-cl/libraries/CLPool.sol b/src/pool-cl/libraries/CLPool.sol index 628bde88..136b46f5 100644 --- a/src/pool-cl/libraries/CLPool.sol +++ b/src/pool-cl/libraries/CLPool.sol @@ -42,9 +42,6 @@ library CLPool { /// @notice Thrown by donate if there is currently 0 liquidity, since the fees will not go to any liquidity providers error NoLiquidityToReceiveFees(); - /// @dev for off-chain farming - event SwapFees(uint256 feesOwed0, uint256 feesOwed1); - struct Slot0 { // the current price uint160 sqrtPriceX96; @@ -113,8 +110,6 @@ library CLPool { Tick.checkTicks(params.tickLower, params.tickUpper); (uint256 feesOwed0, uint256 feesOwed1) = _updatePosition(self, params, _slot0.tick); - // @dev track swap fees for off-chain farming - emit SwapFees(feesOwed0, feesOwed1); ///@dev calculate the tokens delta needed if (params.liquidityDelta != 0) { @@ -501,6 +496,8 @@ library CLPool { view returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) { - return self.ticks.getFeeGrowthInside(tickLower, tickUpper, self.slot0.tick, self.feeGrowthGlobal0X128, self.feeGrowthGlobal1X128); + return self.ticks.getFeeGrowthInside( + tickLower, tickUpper, self.slot0.tick, self.feeGrowthGlobal0X128, self.feeGrowthGlobal1X128 + ); } }