From eb85edee1928c469a8151df4d8b61eb51af027f0 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Wed, 26 Jun 2024 11:13:51 -0400 Subject: [PATCH] additional cleaning --- .forge-snapshots/increaseLiquidity_erc20.snap | 2 +- .../increaseLiquidity_erc6909.snap | 2 +- .forge-snapshots/mintWithLiquidity.snap | 2 +- contracts/base/BaseLiquidityManagement.sol | 64 +++++++++++-------- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/.forge-snapshots/increaseLiquidity_erc20.snap b/.forge-snapshots/increaseLiquidity_erc20.snap index 6221e0be..4ea517e8 100644 --- a/.forge-snapshots/increaseLiquidity_erc20.snap +++ b/.forge-snapshots/increaseLiquidity_erc20.snap @@ -1 +1 @@ -172027 \ No newline at end of file +171241 \ No newline at end of file diff --git a/.forge-snapshots/increaseLiquidity_erc6909.snap b/.forge-snapshots/increaseLiquidity_erc6909.snap index 78ff3eaf..c2e421fa 100644 --- a/.forge-snapshots/increaseLiquidity_erc6909.snap +++ b/.forge-snapshots/increaseLiquidity_erc6909.snap @@ -1 +1 @@ -147609 \ No newline at end of file +146823 \ No newline at end of file diff --git a/.forge-snapshots/mintWithLiquidity.snap b/.forge-snapshots/mintWithLiquidity.snap index c1b3ba3f..d2591995 100644 --- a/.forge-snapshots/mintWithLiquidity.snap +++ b/.forge-snapshots/mintWithLiquidity.snap @@ -1 +1 @@ -467316 \ No newline at end of file +466530 \ No newline at end of file diff --git a/contracts/base/BaseLiquidityManagement.sol b/contracts/base/BaseLiquidityManagement.sol index a55cebbd..dda04d7a 100644 --- a/contracts/base/BaseLiquidityManagement.sol +++ b/contracts/base/BaseLiquidityManagement.sol @@ -106,7 +106,7 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback { LiquidityRange memory range, uint256 liquidityToAdd, bytes memory hookData - ) internal returns (BalanceDelta, BalanceDelta) { + ) internal returns (BalanceDelta callerDelta, BalanceDelta thisDelta) { // Note that the liquidityDelta includes totalFeesAccrued. The totalFeesAccrued is returned separately for accounting purposes. (BalanceDelta liquidityDelta, BalanceDelta totalFeesAccrued) = _modifyLiquidity(owner, range, liquidityToAdd.toInt256(), hookData); @@ -126,45 +126,37 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback { position.liquidity ); - // the delta for increase liquidity assuming that totalFeesAccrued was not applied - BalanceDelta principalDelta = liquidityDelta - totalFeesAccrued; + if (totalFeesAccrued == callerFeesAccrued) { + // when totalFeesAccrued == callerFeesAccrued, the caller is not sharing the range + // therefore, the caller is responsible for the entire liquidityDelta + callerDelta = liquidityDelta; + } else { + // the delta for increasing liquidity assuming that totalFeesAccrued was not applied + BalanceDelta principalDelta = liquidityDelta - totalFeesAccrued; - // outstanding deltas the caller is responsible for, after their fees are credited to the principal delta - BalanceDelta callerDelta = principalDelta + callerFeesAccrued; + // outstanding deltas the caller is responsible for, after their fees are credited to the principal delta + callerDelta = principalDelta + callerFeesAccrued; - // outstanding deltas this contract is responsible for, intuitively the contract is responsible for taking fees external to the caller's accrued fees - BalanceDelta thisDelta = totalFeesAccrued - callerFeesAccrued; + // outstanding deltas this contract is responsible for, intuitively the contract is responsible for taking fees external to the caller's accrued fees + thisDelta = totalFeesAccrued - callerFeesAccrued; + } // Update position storage, flushing the callerDelta value to tokensOwed first if necessary. // If callerDelta > 0, then even after investing callerFeesAccrued, the caller still has some amount to collect that were not added into the position so they are accounted to tokensOwed and removed from the final callerDelta returned. BalanceDelta tokensOwed; if (callerDelta.amount0() > 0) { - // credit the excess tokens to the position's tokensOwed - tokensOwed = toBalanceDelta(callerDelta.amount0(), 0); - - // this contract is responsible for custodying the excess tokens - thisDelta = thisDelta + toBalanceDelta(callerDelta.amount0(), 0); - - // the caller is not expected to collect the excess tokens - callerDelta = toBalanceDelta(0, callerDelta.amount1()); + (tokensOwed, callerDelta, thisDelta) = + _moveCallerDeltaToTokensOwed(true, tokensOwed, callerDelta, thisDelta); } if (callerDelta.amount1() > 0) { - // credit the excess tokens to the position's tokensOwed - tokensOwed = toBalanceDelta(tokensOwed.amount0(), callerDelta.amount1()); - - // this contract is responsible for custodying the excess tokens - thisDelta = thisDelta + toBalanceDelta(0, callerDelta.amount1()); - - // the caller is not expected to collect the excess tokens - callerDelta = toBalanceDelta(callerDelta.amount0(), 0); + (tokensOwed, callerDelta, thisDelta) = + _moveCallerDeltaToTokensOwed(false, tokensOwed, callerDelta, thisDelta); } position.addTokensOwed(tokensOwed); position.addLiquidity(liquidityToAdd); position.updateFeeGrowthInside(feeGrowthInside0X128, feeGrowthInside1X128); - - return (callerDelta, thisDelta); } function _increaseLiquidityAndZeroOut( @@ -209,6 +201,28 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback { if (delta1 < 0) currency1.settle(manager, address(this), uint256(int256(-delta1)), true); } + function _moveCallerDeltaToTokensOwed( + bool useAmount0, + BalanceDelta tokensOwed, + BalanceDelta callerDelta, + BalanceDelta thisDelta + ) private returns (BalanceDelta, BalanceDelta, BalanceDelta) { + // credit the excess tokens to the position's tokensOwed + tokensOwed = useAmount0 + ? toBalanceDelta(callerDelta.amount0(), tokensOwed.amount1()) + : toBalanceDelta(tokensOwed.amount0(), callerDelta.amount1()); + + // this contract is responsible for custodying the excess tokens + thisDelta = useAmount0 + ? thisDelta + toBalanceDelta(callerDelta.amount0(), 0) + : thisDelta + toBalanceDelta(0, callerDelta.amount1()); + + // the caller is not expected to collect the excess tokens + callerDelta = useAmount0 ? toBalanceDelta(0, callerDelta.amount1()) : toBalanceDelta(callerDelta.amount0(), 0); + + return (tokensOwed, callerDelta, thisDelta); + } + function _lockAndIncreaseLiquidity( address owner, LiquidityRange memory range,