Skip to content

Commit

Permalink
being supporting batched ops on vanilla functions
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Jun 26, 2024
1 parent e21e847 commit 1e52f82
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/autocompound_exactUnclaimedFees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
260688
261455
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193061
193828
2 changes: 1 addition & 1 deletion .forge-snapshots/autocompound_excessFeesCredit.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
281227
281994
2 changes: 1 addition & 1 deletion .forge-snapshots/decreaseLiquidity_erc20.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
191813
191784
2 changes: 1 addition & 1 deletion .forge-snapshots/decreaseLiquidity_erc6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170680
170651
2 changes: 1 addition & 1 deletion .forge-snapshots/increaseLiquidity_erc20.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173452
174219
2 changes: 1 addition & 1 deletion .forge-snapshots/increaseLiquidity_erc6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149034
149801
2 changes: 1 addition & 1 deletion .forge-snapshots/mintWithLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
468881
469640
36 changes: 31 additions & 5 deletions contracts/NonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {BalanceDelta, toBalanceDelta} from "@uniswap/v4-core/src/types/BalanceDe
import {LiquidityAmounts} from "./libraries/LiquidityAmounts.sol";
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";

contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidityManagement, ERC721Permit {
Expand All @@ -24,6 +25,7 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit
using PoolIdLibrary for PoolKey;
using LiquidityRangeIdLibrary for LiquidityRange;
using StateLibrary for IPoolManager;
using TransientStateLibrary for IPoolManager;
using SafeCast for uint256;

/// @dev The ID of the next token that will be minted. Skips 0
Expand Down Expand Up @@ -66,8 +68,17 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit
address recipient,
bytes calldata hookData
) public payable returns (uint256 tokenId, BalanceDelta delta) {
// delta = modifyLiquidity(range, liquidity.toInt256(), hookData, false);
delta = _lockAndIncreaseLiquidity(msg.sender, range, liquidity, hookData, false);
// TODO: optimization, read/write manager.isUnlocked to avoid repeated external calls for batched execution
if (manager.isUnlocked()) {
BalanceDelta thisDelta;
(delta, thisDelta) = _increaseLiquidity(recipient, range, liquidity, hookData);

// TODO: should be triggered by zeroOut in _execute...
_closeCallerDeltas(delta, range.poolKey.currency0, range.poolKey.currency1, recipient, false);
_closeThisDeltas(thisDelta, range.poolKey.currency0, range.poolKey.currency1);
} else {
delta = _unlockAndIncreaseLiquidity(msg.sender, range, liquidity, hookData, false);
}

// mint receipt token
_mint(recipient, (tokenId = _nextId++));
Expand Down Expand Up @@ -100,7 +111,19 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit
returns (BalanceDelta delta)
{
TokenPosition memory tokenPos = tokenPositions[tokenId];
delta = _lockAndIncreaseLiquidity(tokenPos.owner, tokenPos.range, liquidity, hookData, claims);

if (manager.isUnlocked()) {
BalanceDelta thisDelta;
(delta, thisDelta) = _increaseLiquidity(tokenPos.owner, tokenPos.range, liquidity, hookData);

// TODO: should be triggered by zeroOut in _execute...
_closeCallerDeltas(
delta, tokenPos.range.poolKey.currency0, tokenPos.range.poolKey.currency1, tokenPos.owner, claims
);
_closeThisDeltas(thisDelta, tokenPos.range.poolKey.currency0, tokenPos.range.poolKey.currency1);
} else {
delta = _unlockAndIncreaseLiquidity(tokenPos.owner, tokenPos.range, liquidity, hookData, claims);
}
}

function decreaseLiquidity(uint256 tokenId, uint256 liquidity, bytes calldata hookData, bool claims)
Expand All @@ -109,7 +132,9 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit
returns (BalanceDelta delta)
{
TokenPosition memory tokenPos = tokenPositions[tokenId];
delta = _lockAndDecreaseLiquidity(tokenPos.owner, tokenPos.range, liquidity, hookData, claims);

// TODO: @sauce update once _decreaseLiquidity returns callerDelta/thisDelta
delta = _unlockAndDecreaseLiquidity(tokenPos.owner, tokenPos.range, liquidity, hookData, claims);
}

function burn(uint256 tokenId, address recipient, bytes calldata hookData, bool claims)
Expand Down Expand Up @@ -138,7 +163,8 @@ contract NonfungiblePositionManager is INonfungiblePositionManager, BaseLiquidit
returns (BalanceDelta delta)
{
TokenPosition memory tokenPos = tokenPositions[tokenId];
delta = _lockAndCollect(tokenPos.owner, tokenPos.range, hookData, claims);
// TODO: @sauce update once _collect returns callerDelta/thisDel
delta = _unlockAndCollect(tokenPos.owner, tokenPos.range, hookData, claims);
}

function feesOwed(uint256 tokenId) external view returns (uint256 token0Owed, uint256 token1Owed) {
Expand Down
6 changes: 3 additions & 3 deletions contracts/base/BaseLiquidityManagement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb
return (tokensOwed, callerDelta, thisDelta);
}

function _lockAndIncreaseLiquidity(
function _unlockAndIncreaseLiquidity(
address owner,
LiquidityRange memory range,
uint256 liquidityToAdd,
Expand Down Expand Up @@ -289,7 +289,7 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb
_closeAllDeltas(range.poolKey.currency0, range.poolKey.currency1);
}

function _lockAndDecreaseLiquidity(
function _unlockAndDecreaseLiquidity(
address owner,
LiquidityRange memory range,
uint256 liquidityToRemove,
Expand Down Expand Up @@ -341,7 +341,7 @@ abstract contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallb
_closeAllDeltas(range.poolKey.currency0, range.poolKey.currency1);
}

function _lockAndCollect(address owner, LiquidityRange memory range, bytes memory hookData, bool claims)
function _unlockAndCollect(address owner, LiquidityRange memory range, bytes memory hookData, bool claims)
internal
returns (BalanceDelta)
{
Expand Down

0 comments on commit 1e52f82

Please sign in to comment.