Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Jun 30, 2024
1 parent 2f42584 commit a2186d7
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 50 deletions.
6 changes: 1 addition & 5 deletions .forge-snapshots/autocompound_exactUnclaimedFees.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
265935
=======
262456
>>>>>>> sauce/posm-batch-execute
299933
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
198308
=======
194829
>>>>>>> sauce/posm-batch-execute
239395
6 changes: 1 addition & 5 deletions .forge-snapshots/autocompound_excessFeesCredit.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
286474
=======
282995
>>>>>>> sauce/posm-batch-execute
320472
6 changes: 1 addition & 5 deletions .forge-snapshots/decreaseLiquidity_erc20.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
184325
=======
180479
>>>>>>> sauce/posm-batch-execute
215156
6 changes: 1 addition & 5 deletions .forge-snapshots/decreaseLiquidity_erc6909.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
184337
=======
180491
>>>>>>> sauce/posm-batch-execute
215168
6 changes: 1 addition & 5 deletions .forge-snapshots/increaseLiquidity_erc20.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
178692
=======
175213
>>>>>>> sauce/posm-batch-execute
194836
6 changes: 1 addition & 5 deletions .forge-snapshots/increaseLiquidity_erc6909.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
154281
=======
150802
>>>>>>> sauce/posm-batch-execute
194848
6 changes: 1 addition & 5 deletions .forge-snapshots/mintWithLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<<<<<<< HEAD
476792
=======
472424
>>>>>>> sauce/posm-batch-execute
511680
38 changes: 33 additions & 5 deletions contracts/NonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -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));
}
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/libraries/TransientLiquidityDelta.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 12 additions & 3 deletions test/position-managers/Execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a2186d7

Please sign in to comment.