From 3039b7b8ef5288065cb786510e227b242dea524f Mon Sep 17 00:00:00 2001 From: saucepoint Date: Fri, 28 Jun 2024 12:01:31 -0400 Subject: [PATCH] Revert "checkpointing" This reverts commit 8ce4ed6f250ee8a64fd2a15c2f2fc49a705f7278. --- .../autocompound_exactUnclaimedFees.snap | 2 +- ...exactUnclaimedFees_exactCustodiedFees.snap | 2 +- .../autocompound_excessFeesCredit.snap | 2 +- .forge-snapshots/increaseLiquidity_erc20.snap | 2 +- .../increaseLiquidity_erc6909.snap | 2 +- .forge-snapshots/mintWithLiquidity.snap | 2 +- contracts/NonfungiblePositionManager.sol | 8 ++------ contracts/base/BaseLiquidityManagement.sol | 5 ----- ...ntLiquidityDelta.sol => TransientDemo.sol} | 19 ++++++------------- 9 files changed, 14 insertions(+), 30 deletions(-) rename contracts/libraries/{TransientLiquidityDelta.sol => TransientDemo.sol} (73%) diff --git a/.forge-snapshots/autocompound_exactUnclaimedFees.snap b/.forge-snapshots/autocompound_exactUnclaimedFees.snap index 668407442..2e536b9f2 100644 --- a/.forge-snapshots/autocompound_exactUnclaimedFees.snap +++ b/.forge-snapshots/autocompound_exactUnclaimedFees.snap @@ -1 +1 @@ -264058 \ No newline at end of file +262501 \ No newline at end of file diff --git a/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap b/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap index e109a0c77..553b43e9e 100644 --- a/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap +++ b/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap @@ -1 +1 @@ -196431 \ No newline at end of file +194874 \ No newline at end of file diff --git a/.forge-snapshots/autocompound_excessFeesCredit.snap b/.forge-snapshots/autocompound_excessFeesCredit.snap index 24fdaa942..ac41e55d0 100644 --- a/.forge-snapshots/autocompound_excessFeesCredit.snap +++ b/.forge-snapshots/autocompound_excessFeesCredit.snap @@ -1 +1 @@ -284597 \ No newline at end of file +283040 \ No newline at end of file diff --git a/.forge-snapshots/increaseLiquidity_erc20.snap b/.forge-snapshots/increaseLiquidity_erc20.snap index 8fedeb730..6691256e4 100644 --- a/.forge-snapshots/increaseLiquidity_erc20.snap +++ b/.forge-snapshots/increaseLiquidity_erc20.snap @@ -1 +1 @@ -176815 \ No newline at end of file +175258 \ No newline at end of file diff --git a/.forge-snapshots/increaseLiquidity_erc6909.snap b/.forge-snapshots/increaseLiquidity_erc6909.snap index 79c05ace6..babfbeebd 100644 --- a/.forge-snapshots/increaseLiquidity_erc6909.snap +++ b/.forge-snapshots/increaseLiquidity_erc6909.snap @@ -1 +1 @@ -152404 \ No newline at end of file +150847 \ No newline at end of file diff --git a/.forge-snapshots/mintWithLiquidity.snap b/.forge-snapshots/mintWithLiquidity.snap index 618059cd4..33617d434 100644 --- a/.forge-snapshots/mintWithLiquidity.snap +++ b/.forge-snapshots/mintWithLiquidity.snap @@ -1 +1 @@ -475856 \ No newline at end of file +472846 \ No newline at end of file diff --git a/contracts/NonfungiblePositionManager.sol b/contracts/NonfungiblePositionManager.sol index f3b1940d7..4f4056f1a 100644 --- a/contracts/NonfungiblePositionManager.sol +++ b/contracts/NonfungiblePositionManager.sol @@ -18,7 +18,6 @@ import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {TransientStateLibrary} from "@uniswap/v4-core/src/libraries/TransientStateLibrary.sol"; import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol"; -import {TransientLiquidityDelta} from "./libraries/TransientLiquidityDelta.sol"; contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidityManagement, ERC721Permit { using CurrencyLibrary for Currency; @@ -28,7 +27,6 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit using StateLibrary for IPoolManager; using TransientStateLibrary for IPoolManager; using SafeCast for uint256; - using TransientLiquidityDelta for address; /// @dev The ID of the next token that will be minted. Skips 0 uint256 private _nextId = 1; @@ -71,12 +69,10 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit ) public payable returns (uint256 tokenId, BalanceDelta delta) { // TODO: optimization, read/write manager.isUnlocked to avoid repeated external calls for batched execution if (manager.isUnlocked()) { - _increaseLiquidity(recipient, range, liquidity, hookData); + BalanceDelta thisDelta; + (delta, thisDelta) = _increaseLiquidity(recipient, range, liquidity, hookData); // TODO: should be triggered by zeroOut in _execute... - delta = recipient.getBalanceDelta(range.poolKey.currency0, range.poolKey.currency1); - BalanceDelta thisDelta = address(this).getBalanceDelta(range.poolKey.currency0, range.poolKey.currency1); - _closeCallerDeltas(delta, range.poolKey.currency0, range.poolKey.currency1, recipient, false); _closeThisDeltas(thisDelta, range.poolKey.currency0, range.poolKey.currency1); diff --git a/contracts/base/BaseLiquidityManagement.sol b/contracts/base/BaseLiquidityManagement.sol index 0c4f44718..63d325de5 100644 --- a/contracts/base/BaseLiquidityManagement.sol +++ b/contracts/base/BaseLiquidityManagement.sol @@ -24,7 +24,6 @@ import {IBaseLiquidityManagement} from "../interfaces/IBaseLiquidityManagement.s import {PositionLibrary} from "../libraries/Position.sol"; import {BalanceDeltaExtensionLibrary} from "../libraries/BalanceDeltaExtensionLibrary.sol"; import {LiquidityDeltaAccounting} from "../libraries/LiquidityDeltaAccounting.sol"; -import {TransientLiquidityDelta} from "../libraries/TransientLiquidityDelta.sol"; import "forge-std/console2.sol"; @@ -41,8 +40,6 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb using PositionLibrary for IBaseLiquidityManagement.Position; using BalanceDeltaExtensionLibrary for BalanceDelta; using LiquidityDeltaAccounting for BalanceDelta; - using TransientLiquidityDelta for BalanceDelta; - using TransientLiquidityDelta for Currency; mapping(address owner => mapping(LiquidityRangeId rangeId => Position)) public positions; @@ -104,8 +101,6 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb // Calculate the portion of the liquidityDelta that is attributable to the caller. // We must account for fees that might be owed to other users on the same range. (callerDelta, thisDelta) = liquidityDelta.split(callerFeesAccrued, totalFeesAccrued); - callerDelta.flush(owner, range.poolKey.currency0, range.poolKey.currency1); - thisDelta.flush(address(this), range.poolKey.currency0, range.poolKey.currency1); // 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. diff --git a/contracts/libraries/TransientLiquidityDelta.sol b/contracts/libraries/TransientDemo.sol similarity index 73% rename from contracts/libraries/TransientLiquidityDelta.sol rename to contracts/libraries/TransientDemo.sol index 8f4739270..2845878dd 100644 --- a/contracts/libraries/TransientLiquidityDelta.sol +++ b/contracts/libraries/TransientDemo.sol @@ -1,13 +1,12 @@ // SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8.24; -import {BalanceDelta, toBalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; +import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; import {Currency} from "@uniswap/v4-core/src/types/Currency.sol"; /// @title a library to store callers' currency deltas in transient storage /// @dev this library implements the equivalent of a mapping, as transient storage can only be accessed in assembly library TransientLiquidityDelta { - /// @notice calculates which storage slot a delta should be stored in for a given caller and currency function _computeSlot(address caller_, Currency currency) internal pure returns (bytes32 hashSlot) { assembly { @@ -19,8 +18,8 @@ library TransientLiquidityDelta { /// @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()); - addDelta(currency1, holder, delta.amount1()); + setDelta(currency0, holder, delta.amount0()); + setDelta(currency1, holder, delta.amount1()); } function addDelta(Currency currency, address caller, int128 delta) internal { @@ -32,7 +31,7 @@ library TransientLiquidityDelta { } } - function subtractDelta(Currency currency, address caller, int128 delta) internal { + function subDelta(Currency currency, address caller, int128 delta) internal { bytes32 hashSlot = _computeSlot(caller, currency); assembly { let oldValue := tload(hashSlot) @@ -41,13 +40,8 @@ library TransientLiquidityDelta { } } - function getBalanceDelta(address holder, Currency currency0, Currency currency1) internal view returns (BalanceDelta delta) { - delta = toBalanceDelta(getDelta(currency0, holder), getDelta(currency1, holder)); - } - - /// Copied from v4-core/src/libraries/CurrencyDelta.sol: /// @notice sets a new currency delta for a given caller and currency - function setDelta(Currency currency, address caller, int128 delta) internal { + function setDelta(Currency currency, address caller, int256 delta) internal { bytes32 hashSlot = _computeSlot(caller, currency); assembly { @@ -56,8 +50,7 @@ library TransientLiquidityDelta { } /// @notice gets a new currency delta for a given caller and currency - // TODO: is returning 128 bits safe? - function getDelta(Currency currency, address caller) internal view returns (int128 delta) { + function getDelta(Currency currency, address caller) internal view returns (int256 delta) { bytes32 hashSlot = _computeSlot(caller, currency); assembly {