From a2186d779bc0812e968768fbbda1bb8f51878ed3 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Sun, 30 Jun 2024 00:22:22 -0400 Subject: [PATCH] wip --- .../autocompound_exactUnclaimedFees.snap | 6 +-- ...exactUnclaimedFees_exactCustodiedFees.snap | 6 +-- .../autocompound_excessFeesCredit.snap | 6 +-- .forge-snapshots/decreaseLiquidity_erc20.snap | 6 +-- .../decreaseLiquidity_erc6909.snap | 6 +-- .forge-snapshots/increaseLiquidity_erc20.snap | 6 +-- .../increaseLiquidity_erc6909.snap | 6 +-- .forge-snapshots/mintWithLiquidity.snap | 6 +-- contracts/NonfungiblePositionManager.sol | 38 ++++++++++++++++--- .../libraries/TransientLiquidityDelta.sol | 4 +- test/position-managers/Execute.t.sol | 15 ++++++-- 11 files changed, 55 insertions(+), 50 deletions(-) diff --git a/.forge-snapshots/autocompound_exactUnclaimedFees.snap b/.forge-snapshots/autocompound_exactUnclaimedFees.snap index 37b86092..c931974c 100644 --- a/.forge-snapshots/autocompound_exactUnclaimedFees.snap +++ b/.forge-snapshots/autocompound_exactUnclaimedFees.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -265935 -======= -262456 ->>>>>>> sauce/posm-batch-execute +299933 \ No newline at end of file diff --git a/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap b/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap index d3523cfe..aeba6588 100644 --- a/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap +++ b/.forge-snapshots/autocompound_exactUnclaimedFees_exactCustodiedFees.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -198308 -======= -194829 ->>>>>>> sauce/posm-batch-execute +239395 \ No newline at end of file diff --git a/.forge-snapshots/autocompound_excessFeesCredit.snap b/.forge-snapshots/autocompound_excessFeesCredit.snap index 1e6fb253..4c0c834b 100644 --- a/.forge-snapshots/autocompound_excessFeesCredit.snap +++ b/.forge-snapshots/autocompound_excessFeesCredit.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -286474 -======= -282995 ->>>>>>> sauce/posm-batch-execute +320472 \ No newline at end of file diff --git a/.forge-snapshots/decreaseLiquidity_erc20.snap b/.forge-snapshots/decreaseLiquidity_erc20.snap index 141b00b4..7b956e12 100644 --- a/.forge-snapshots/decreaseLiquidity_erc20.snap +++ b/.forge-snapshots/decreaseLiquidity_erc20.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -184325 -======= -180479 ->>>>>>> sauce/posm-batch-execute +215156 \ No newline at end of file diff --git a/.forge-snapshots/decreaseLiquidity_erc6909.snap b/.forge-snapshots/decreaseLiquidity_erc6909.snap index ab4499cf..f251c34d 100644 --- a/.forge-snapshots/decreaseLiquidity_erc6909.snap +++ b/.forge-snapshots/decreaseLiquidity_erc6909.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -184337 -======= -180491 ->>>>>>> sauce/posm-batch-execute +215168 \ No newline at end of file diff --git a/.forge-snapshots/increaseLiquidity_erc20.snap b/.forge-snapshots/increaseLiquidity_erc20.snap index 2c35a837..ae25726e 100644 --- a/.forge-snapshots/increaseLiquidity_erc20.snap +++ b/.forge-snapshots/increaseLiquidity_erc20.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -178692 -======= -175213 ->>>>>>> sauce/posm-batch-execute +194836 \ No newline at end of file diff --git a/.forge-snapshots/increaseLiquidity_erc6909.snap b/.forge-snapshots/increaseLiquidity_erc6909.snap index 64062a9b..218e7c7a 100644 --- a/.forge-snapshots/increaseLiquidity_erc6909.snap +++ b/.forge-snapshots/increaseLiquidity_erc6909.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -154281 -======= -150802 ->>>>>>> sauce/posm-batch-execute +194848 \ No newline at end of file diff --git a/.forge-snapshots/mintWithLiquidity.snap b/.forge-snapshots/mintWithLiquidity.snap index 0e661026..185c902a 100644 --- a/.forge-snapshots/mintWithLiquidity.snap +++ b/.forge-snapshots/mintWithLiquidity.snap @@ -1,5 +1 @@ -<<<<<<< HEAD -476792 -======= -472424 ->>>>>>> sauce/posm-batch-execute +511680 \ No newline at end of file diff --git a/contracts/NonfungiblePositionManager.sol b/contracts/NonfungiblePositionManager.sol index ea8d9ab4..5a9ec848 100644 --- a/contracts/NonfungiblePositionManager.sol +++ b/contracts/NonfungiblePositionManager.sol @@ -36,6 +36,9 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit // maps the ERC721 tokenId to the keys that uniquely identify a liquidity position (owner, range) mapping(uint256 tokenId => TokenPosition position) public tokenPositions; + // TODO: TSTORE this jawn + address internal msgSender; + constructor(IPoolManager _manager) BaseLiquidityManagement(_manager) ERC721Permit("Uniswap V4 Positions NFT-V1", "UNI-V4-POS", "1") @@ -58,9 +61,14 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit // close the deltas for (uint256 i; i < currencies.length; i++) { - currencies[i].close(manager, msg.sender); + currencies[i].close(manager, msgSender); currencies[i].close(manager, address(this)); } + + // TODO: @sara handle the return + // vanilla: return int128[2] + // batch: return int128[data.length] + return returnData; } // NOTE: more gas efficient as LiquidityAmounts is used offchain @@ -81,9 +89,14 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit _mint(recipient, (tokenId = nextTokenId++)); tokenPositions[tokenId] = TokenPosition({owner: recipient, range: range}); } else { + msgSender = msg.sender; bytes[] memory data = new bytes[](1); data[0] = abi.encodeWithSelector(this.mint.selector, range, liquidity, deadline, recipient, hookData); - bytes memory result = unlockAndExecute(data); + + Currency[] memory currencies = new Currency[](2); + currencies[0] = range.poolKey.currency0; + currencies[1] = range.poolKey.currency1; + bytes memory result = unlockAndExecute(data, currencies); delta = abi.decode(result, (BalanceDelta)); } } @@ -118,9 +131,14 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit if (manager.isUnlocked()) { _increaseLiquidity(tokenPos.owner, tokenPos.range, liquidity, hookData); } else { + msgSender = msg.sender; bytes[] memory data = new bytes[](1); data[0] = abi.encodeWithSelector(this.increaseLiquidity.selector, tokenId, liquidity, hookData, claims); - bytes memory result = unlockAndExecute(data); + + Currency[] memory currencies = new Currency[](2); + currencies[0] = tokenPos.range.poolKey.currency0; + currencies[1] = tokenPos.range.poolKey.currency1; + bytes memory result = unlockAndExecute(data, currencies); delta = abi.decode(result, (BalanceDelta)); } } @@ -135,9 +153,14 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit if (manager.isUnlocked()) { _decreaseLiquidity(tokenPos.owner, tokenPos.range, liquidity, hookData); } else { + msgSender = msg.sender; bytes[] memory data = new bytes[](1); data[0] = abi.encodeWithSelector(this.decreaseLiquidity.selector, tokenId, liquidity, hookData, claims); - bytes memory result = unlockAndExecute(data); + + Currency[] memory currencies = new Currency[](2); + currencies[0] = tokenPos.range.poolKey.currency0; + currencies[1] = tokenPos.range.poolKey.currency1; + bytes memory result = unlockAndExecute(data, currencies); delta = abi.decode(result, (BalanceDelta)); } } @@ -175,9 +198,14 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit if (manager.isUnlocked()) { _collect(recipient, tokenPos.range, hookData); } else { + msgSender = msg.sender; bytes[] memory data = new bytes[](1); data[0] = abi.encodeWithSelector(this.collect.selector, tokenId, recipient, hookData, claims); - bytes memory result = unlockAndExecute(data); + + Currency[] memory currencies = new Currency[](2); + currencies[0] = tokenPos.range.poolKey.currency0; + currencies[1] = tokenPos.range.poolKey.currency1; + bytes memory result = unlockAndExecute(data, currencies); delta = abi.decode(result, (BalanceDelta)); } } diff --git a/contracts/libraries/TransientLiquidityDelta.sol b/contracts/libraries/TransientLiquidityDelta.sol index ef9926af..86f85c85 100644 --- a/contracts/libraries/TransientLiquidityDelta.sol +++ b/contracts/libraries/TransientLiquidityDelta.sol @@ -52,8 +52,8 @@ library TransientLiquidityDelta { function close(Currency currency, IPoolManager manager, address holder) internal { // getDelta(currency, holder); - bytes32 hashSlot = _computeSlot(caller, currency); - int128 delta; + bytes32 hashSlot = _computeSlot(holder, currency); + int256 delta; assembly { delta := tload(hashSlot) } diff --git a/test/position-managers/Execute.t.sol b/test/position-managers/Execute.t.sol index 1c1144d8..5d6b7d39 100644 --- a/test/position-managers/Execute.t.sol +++ b/test/position-managers/Execute.t.sol @@ -87,7 +87,10 @@ contract ExecuteTest is Test, Deployers, GasSnapshot, LiquidityFuzzers { INonfungiblePositionManager.increaseLiquidity.selector, tokenId, liquidityToAdd, ZERO_BYTES, false ); - lpm.unlockAndExecute(data); + Currency[] memory currencies = new Currency[](2); + currencies[0] = currency0; + currencies[1] = currency1; + lpm.unlockAndExecute(data, currencies); (,, uint256 liquidity,,,,) = lpm.positions(address(this), range.toId()); assertEq(liquidity, initialLiquidity + liquidityToAdd); @@ -112,7 +115,10 @@ contract ExecuteTest is Test, Deployers, GasSnapshot, LiquidityFuzzers { INonfungiblePositionManager.increaseLiquidity.selector, tokenId, liquidityToAdd2, ZERO_BYTES, false ); - lpm.unlockAndExecute(data); + Currency[] memory currencies = new Currency[](2); + currencies[0] = currency0; + currencies[1] = currency1; + lpm.unlockAndExecute(data, currencies); (,, uint256 liquidity,,,,) = lpm.positions(address(this), range.toId()); assertEq(liquidity, initialiLiquidity + liquidityToAdd + liquidityToAdd2); @@ -137,7 +143,10 @@ contract ExecuteTest is Test, Deployers, GasSnapshot, LiquidityFuzzers { INonfungiblePositionManager.increaseLiquidity.selector, tokenId, liquidityToAdd, ZERO_BYTES, false ); - lpm.unlockAndExecute(data); + Currency[] memory currencies = new Currency[](2); + currencies[0] = currency0; + currencies[1] = currency1; + lpm.unlockAndExecute(data, currencies); (,, uint256 liquidity,,,,) = lpm.positions(address(this), range.toId()); assertEq(liquidity, intialLiquidity + liquidityToAdd);