Skip to content

Commit

Permalink
Revert "checkpointing"
Browse files Browse the repository at this point in the history
This reverts commit 8ce4ed6.
  • Loading branch information
saucepoint committed Jun 28, 2024
1 parent 35a9d3a commit 3039b7b
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/autocompound_exactUnclaimedFees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
264058
262501
Original file line number Diff line number Diff line change
@@ -1 +1 @@
196431
194874
2 changes: 1 addition & 1 deletion .forge-snapshots/autocompound_excessFeesCredit.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
284597
283040
2 changes: 1 addition & 1 deletion .forge-snapshots/increaseLiquidity_erc20.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
176815
175258
2 changes: 1 addition & 1 deletion .forge-snapshots/increaseLiquidity_erc6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152404
150847
2 changes: 1 addition & 1 deletion .forge-snapshots/mintWithLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
475856
472846
8 changes: 2 additions & 6 deletions contracts/NonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 0 additions & 5 deletions contracts/base/BaseLiquidityManagement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 3039b7b

Please sign in to comment.