From 9eaffcd87f05828075e2e9e2cd795ae7d51e841c Mon Sep 17 00:00:00 2001 From: Sara Reynolds Date: Thu, 27 Jun 2024 20:10:45 -0400 Subject: [PATCH] update burn --- contracts/NonfungiblePositionManager.sol | 7 +++++-- contracts/base/BaseLiquidityManagement.sol | 20 +++++++++++--------- test/position-managers/FeeCollection.t.sol | 2 ++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/contracts/NonfungiblePositionManager.sol b/contracts/NonfungiblePositionManager.sol index 696f315d..ab1670cd 100644 --- a/contracts/NonfungiblePositionManager.sol +++ b/contracts/NonfungiblePositionManager.sol @@ -97,14 +97,17 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit isAuthorizedForToken(tokenId) returns (BalanceDelta delta) { + // TODO: Burn currently decreases and collects. However its done under different locks. + // Replace once we have the execute multicall. // remove liquidity TokenPosition storage tokenPosition = tokenPositions[tokenId]; LiquidityRangeId rangeId = tokenPosition.range.toId(); Position storage position = positions[msg.sender][rangeId]; - if (0 < position.liquidity) { + if (position.liquidity > 0) { (delta,) = decreaseLiquidity(tokenId, position.liquidity, hookData, claims); - (delta) = collect(tokenId, recipient, hookData, claims); } + + collect(tokenId, recipient, hookData, claims); require(position.tokensOwed0 == 0 && position.tokensOwed1 == 0, "NOT_EMPTY"); delete positions[msg.sender][rangeId]; delete tokenPositions[tokenId]; diff --git a/contracts/base/BaseLiquidityManagement.sol b/contracts/base/BaseLiquidityManagement.sol index 2bc939f6..df54345a 100644 --- a/contracts/base/BaseLiquidityManagement.sol +++ b/contracts/base/BaseLiquidityManagement.sol @@ -268,18 +268,20 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback { internal returns (BalanceDelta callerDelta, BalanceDelta thisDelta) { - // Do not add or decrease liquidity, just trigger fee updates. - // TODO: Fails when position is empty - (BalanceDelta liquidityDelta, BalanceDelta totalFeesAccrued) = _modifyLiquidity(owner, range, 0, hookData); - Position storage position = positions[owner][range.toId()]; - // Also updates the position's the feeGrowthInsideLast variables in storage. - (BalanceDelta callerFeesAccrued) = _updateFeeGrowth(range, position); + // Only call modify if there is still liquidty in this position. + if (position.liquidity != 0) { + // Do not add or decrease liquidity, just trigger fee updates. + (BalanceDelta liquidityDelta, BalanceDelta totalFeesAccrued) = _modifyLiquidity(owner, range, 0, hookData); - // Account for fees accrued to other users on the same range. - // TODO: Opt when liquidityDelta == 0 - (callerDelta, thisDelta) = liquidityDelta.split(callerFeesAccrued, totalFeesAccrued); + // Also updates the position's the feeGrowthInsideLast variables in storage. + (BalanceDelta callerFeesAccrued) = _updateFeeGrowth(range, position); + + // Account for fees accrued to other users on the same range. + // TODO: Opt when liquidityDelta == 0 + (callerDelta, thisDelta) = liquidityDelta.split(callerFeesAccrued, totalFeesAccrued); + } // Allow the caller to collect the tokens owed. // Tokens owed that the caller collects is paid for by this contract. diff --git a/test/position-managers/FeeCollection.t.sol b/test/position-managers/FeeCollection.t.sol index e2753f72..e89ff68a 100644 --- a/test/position-managers/FeeCollection.t.sol +++ b/test/position-managers/FeeCollection.t.sol @@ -218,6 +218,8 @@ contract FeeCollectionTest is Test, Deployers, GasSnapshot, LiquidityFuzzers { /// @dev Alice and bob create liquidity on the same range /// when alice decreases liquidity, she should only collect her fees + /// TODO Add back fuzz test on liquidityDeltaBob + /// TODO Assert state changes for lpm balance, position state, and return values function test_decreaseLiquidity_sameRange_exact() public { // alice and bob create liquidity on the same range [-120, 120] LiquidityRange memory range = LiquidityRange({poolKey: key, tickLower: -120, tickUpper: 120});