diff --git a/.forge-snapshots/increaseLiquidity_erc20.snap b/.forge-snapshots/increaseLiquidity_erc20.snap index 872ccdcc..6221e0be 100644 --- a/.forge-snapshots/increaseLiquidity_erc20.snap +++ b/.forge-snapshots/increaseLiquidity_erc20.snap @@ -1 +1 @@ -172030 \ No newline at end of file +172027 \ No newline at end of file diff --git a/.forge-snapshots/increaseLiquidity_erc6909.snap b/.forge-snapshots/increaseLiquidity_erc6909.snap index 86733333..78ff3eaf 100644 --- a/.forge-snapshots/increaseLiquidity_erc6909.snap +++ b/.forge-snapshots/increaseLiquidity_erc6909.snap @@ -1 +1 @@ -147612 \ No newline at end of file +147609 \ No newline at end of file diff --git a/.forge-snapshots/mintWithLiquidity.snap b/.forge-snapshots/mintWithLiquidity.snap index 6608ff54..c1b3ba3f 100644 --- a/.forge-snapshots/mintWithLiquidity.snap +++ b/.forge-snapshots/mintWithLiquidity.snap @@ -1 +1 @@ -467319 \ No newline at end of file +467316 \ No newline at end of file diff --git a/contracts/base/BaseLiquidityManagement.sol b/contracts/base/BaseLiquidityManagement.sol index a5c20499..a55cebbd 100644 --- a/contracts/base/BaseLiquidityManagement.sol +++ b/contracts/base/BaseLiquidityManagement.sol @@ -132,16 +132,31 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback { // outstanding deltas the caller is responsible for, after their fees are credited to the principal delta BalanceDelta 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; + // 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()); } 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); } @@ -149,12 +164,6 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback { position.addLiquidity(liquidityToAdd); position.updateFeeGrowthInside(feeGrowthInside0X128, feeGrowthInside1X128); - // The delta owed or credited by this contract. - // TODO @sauce check that if callerDelta == 0 (zerod out from above), then this line just credits the posm to takes on behalf of the caller - // outstanding deltas this contract is responsible for - // TODO: why can't we do `thisDelta = totalFeesAccrued - callerFeesAccrued`? - BalanceDelta thisDelta = liquidityDelta - callerDelta; - return (callerDelta, thisDelta); }