diff --git a/contracts/NonfungiblePositionManager.sol b/contracts/NonfungiblePositionManager.sol index b6318f31..e6be1e49 100644 --- a/contracts/NonfungiblePositionManager.sol +++ b/contracts/NonfungiblePositionManager.sol @@ -72,8 +72,8 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit // close the deltas int128[] memory returnData = new int128[](currencies.length); for (uint256 i; i < currencies.length; i++) { - returnData[i] = currencies[i].close(manager, msgSender); - currencies[i].close(manager, address(this)); + returnData[i] = currencies[i].close(manager, msgSender, false); // TODO: support claims + currencies[i].close(manager, address(this), true); // position manager always takes 6909 } // Should just be returning the netted amount that was settled on behalf of the caller (msgSender) diff --git a/contracts/base/BaseLiquidityManagement.sol b/contracts/base/BaseLiquidityManagement.sol index b6f5d0e2..79862ded 100644 --- a/contracts/base/BaseLiquidityManagement.sol +++ b/contracts/base/BaseLiquidityManagement.sol @@ -51,27 +51,6 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb function _msgSenderInternal() internal virtual returns (address); - function _closeCallerDeltas( - BalanceDelta callerDeltas, - Currency currency0, - Currency currency1, - address owner, - bool claims - ) internal { - int128 callerDelta0 = callerDeltas.amount0(); - int128 callerDelta1 = callerDeltas.amount1(); - // On liquidity increase, the deltas should never be > 0. - // We always 0 out a caller positive delta because it is instead accounted for in position.tokensOwed. - - if (callerDelta0 < 0) currency0.settle(manager, owner, uint256(int256(-callerDelta0)), claims); - else if (callerDelta0 > 0) currency0.take(manager, owner, uint128(callerDelta0), claims); - - if (callerDelta1 < 0) currency1.settle(manager, owner, uint256(int256(-callerDelta1)), claims); - else if (callerDelta1 > 0) currency1.take(manager, owner, uint128(callerDelta1), claims); - - owner.close(currency0, currency1); - } - function _modifyLiquidity(address owner, LiquidityRange memory range, int256 liquidityChange, bytes memory hookData) internal returns (BalanceDelta liquidityDelta, BalanceDelta totalFeesAccrued) @@ -131,22 +110,6 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb position.addLiquidity(liquidityToAdd); } - // When chaining many actions, this should be called at the very end to close out any open deltas owed to or by this contract for other users on the same range. - // This is safe because any amounts the caller should not pay or take have already been accounted for in closeCallerDeltas. - function _closeThisDeltas(BalanceDelta delta, Currency currency0, Currency currency1) internal { - int128 delta0 = delta.amount0(); - int128 delta1 = delta.amount1(); - - // Mint a receipt for the tokens owed to this address. - if (delta0 > 0) currency0.take(manager, address(this), uint128(delta0), true); - if (delta1 > 0) currency1.take(manager, address(this), uint128(delta1), true); - // Burn the receipt for tokens owed to this address. - if (delta0 < 0) currency0.settle(manager, address(this), uint256(int256(-delta0)), true); - if (delta1 < 0) currency1.settle(manager, address(this), uint256(int256(-delta1)), true); - - address(this).close(currency0, currency1); - } - function _moveCallerDeltaToTokensOwed( bool useAmount0, BalanceDelta tokensOwed, @@ -236,7 +199,7 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb if (recipient == _msgSenderInternal()) { callerDelta.flush(recipient, range.poolKey.currency0, range.poolKey.currency1); } else { - callerDelta.closeDelta(manager, recipient, range.poolKey.currency0, range.poolKey.currency1); + callerDelta.closeDelta(manager, recipient, range.poolKey.currency0, range.poolKey.currency1, false); // TODO: allow recipient to receive claims, and add test! } thisDelta.flush(address(this), range.poolKey.currency0, range.poolKey.currency1); diff --git a/contracts/libraries/TransientLiquidityDelta.sol b/contracts/libraries/TransientLiquidityDelta.sol index ccf520d4..fdc1e614 100644 --- a/contracts/libraries/TransientLiquidityDelta.sol +++ b/contracts/libraries/TransientLiquidityDelta.sol @@ -25,11 +25,6 @@ library TransientLiquidityDelta { } } - function close(address holder, Currency currency0, Currency currency1) internal { - setDelta(currency0, holder, 0); - setDelta(currency1, holder, 0); - } - /// @notice Flush a BalanceDelta into transient storage for a given holder function flush(BalanceDelta delta, address holder, Currency currency0, Currency currency1) internal { addDelta(currency0, holder, delta.amount0()); @@ -54,18 +49,20 @@ library TransientLiquidityDelta { } } - function close(Currency currency, IPoolManager manager, address holder) internal returns (int128 delta) { + function close(Currency currency, IPoolManager manager, address holder, bool claims) + internal + returns (int128 delta) + { // getDelta(currency, holder); bytes32 hashSlot = _computeSlot(holder, currency); assembly { delta := tload(hashSlot) } - // TODO support claims field if (delta < 0) { - currency.settle(manager, holder, uint256(-int256(delta)), false); + currency.settle(manager, holder, uint256(-int256(delta)), claims); } else { - currency.take(manager, holder, uint256(int256(delta)), false); + currency.take(manager, holder, uint256(int256(delta)), claims); } // setDelta(0); @@ -79,10 +76,11 @@ library TransientLiquidityDelta { IPoolManager manager, address holder, Currency currency0, - Currency currency1 + Currency currency1, + bool claims ) internal { - close(currency0, manager, holder); - close(currency1, manager, holder); + close(currency0, manager, holder, claims); + close(currency1, manager, holder, claims); } function getBalanceDelta(address holder, Currency currency0, Currency currency1)