Skip to content

Commit

Permalink
update core; address liquidity salt and modify liquidity return values
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed May 10, 2024
1 parent d46a0c2 commit 558860b
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
312666
312768
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124137
124239
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
83453
83956
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1016548
1016591
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
111408
111650
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
244434
245587
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
48415
48918
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
82198
82712
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122543
122544
8 changes: 4 additions & 4 deletions contracts/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ abstract contract BaseHook is IHooks {
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
bytes calldata
) external virtual returns (bytes4) {
) external virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}

Expand All @@ -105,22 +105,22 @@ abstract contract BaseHook is IHooks {
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
bytes calldata
) external virtual returns (bytes4) {
) external virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}

function beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, int128)
{
revert HookNotImplemented();
}

function afterSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, int128)
{
revert HookNotImplemented();
}
Expand Down
32 changes: 18 additions & 14 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ contract FullRange is BaseHook, IUnlockCallback {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

Expand Down Expand Up @@ -143,7 +147,8 @@ contract FullRange is BaseHook, IUnlockCallback {
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: liquidity.toInt256()
liquidityDelta: liquidity.toInt256(),
salt: 0
})
);

Expand Down Expand Up @@ -187,7 +192,8 @@ contract FullRange is BaseHook, IUnlockCallback {
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: -(params.liquidity.toInt256())
liquidityDelta: -(params.liquidity.toInt256()),
salt: 0
})
);

Expand Down Expand Up @@ -235,7 +241,7 @@ contract FullRange is BaseHook, IUnlockCallback {
function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, bytes calldata)
external
override
returns (bytes4)
returns (bytes4, int128)
{
PoolId poolId = key.toId();

Expand All @@ -244,7 +250,7 @@ contract FullRange is BaseHook, IUnlockCallback {
pool.hasAccruedFees = true;
}

return IHooks.beforeSwap.selector;
return (IHooks.beforeSwap.selector, 0);
}

function modifyLiquidity(PoolKey memory key, IPoolManager.ModifyLiquidityParams memory params)
Expand Down Expand Up @@ -297,8 +303,7 @@ contract FullRange is BaseHook, IUnlockCallback {
);

params.liquidityDelta = -(liquidityToRemove.toInt256());
(BalanceDelta _delta, BalanceDelta _feeDelta) = poolManager.modifyLiquidity(key, params, ZERO_BYTES);
delta = _delta + _feeDelta;
(delta,) = poolManager.modifyLiquidity(key, params, ZERO_BYTES);
pool.hasAccruedFees = false;
}

Expand All @@ -315,26 +320,24 @@ contract FullRange is BaseHook, IUnlockCallback {
delta = _removeLiquidity(data.key, data.params);
_takeDeltas(data.sender, data.key, delta);
} else {
(BalanceDelta _delta, BalanceDelta _feeDelta) =
poolManager.modifyLiquidity(data.key, data.params, ZERO_BYTES);
delta = _delta + _feeDelta;
(delta,) = poolManager.modifyLiquidity(data.key, data.params, ZERO_BYTES);
_settleDeltas(data.sender, data.key, delta);
}
return abi.encode(delta);
}

function _rebalance(PoolKey memory key) public {
PoolId poolId = key.toId();
(BalanceDelta _delta, BalanceDelta _feeDelta) = poolManager.modifyLiquidity(
(BalanceDelta balanceDelta,) = poolManager.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: -(poolManager.getLiquidity(poolId).toInt256())
liquidityDelta: -(poolManager.getLiquidity(poolId).toInt256()),
salt: 0
}),
ZERO_BYTES
);
BalanceDelta balanceDelta = _delta + _feeDelta;

uint160 newSqrtPriceX96 = (
FixedPointMathLib.sqrt(
Expand Down Expand Up @@ -367,7 +370,8 @@ contract FullRange is BaseHook, IUnlockCallback {
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: liquidity.toInt256()
liquidityDelta: liquidity.toInt256(),
salt: 0
}),
ZERO_BYTES
);
Expand Down
10 changes: 7 additions & 3 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ contract GeomeanOracle is BaseHook {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

Expand Down Expand Up @@ -140,10 +144,10 @@ contract GeomeanOracle is BaseHook {
external
override
poolManagerOnly
returns (bytes4)
returns (bytes4, int128)
{
_updatePool(key);
return GeomeanOracle.beforeSwap.selector;
return (GeomeanOracle.beforeSwap.selector, 0);
}

/// @notice Observe the given pool for the timestamps
Expand Down
31 changes: 21 additions & 10 deletions contracts/hooks/examples/LimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ contract LimitOrder is BaseHook {
beforeSwap: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

Expand Down Expand Up @@ -136,9 +140,9 @@ contract LimitOrder is BaseHook {
IPoolManager.SwapParams calldata params,
BalanceDelta,
bytes calldata
) external override poolManagerOnly returns (bytes4) {
) external override poolManagerOnly returns (bytes4, int128) {
(int24 tickLower, int24 lower, int24 upper) = _getCrossedTicks(key.toId(), key.tickSpacing);
if (lower > upper) return LimitOrder.afterSwap.selector;
if (lower > upper) return (LimitOrder.afterSwap.selector, 0);

// note that a zeroForOne swap means that the pool is actually gaining token0, so limit
// order fills are the opposite of swap fills, hence the inversion below
Expand All @@ -148,7 +152,7 @@ contract LimitOrder is BaseHook {
}

setTickLowerLast(key.toId(), tickLower);
return LimitOrder.afterSwap.selector;
return (LimitOrder.afterSwap.selector, 0);
}

function _fillEpoch(PoolKey calldata key, int24 lower, bool zeroForOne) internal {
Expand Down Expand Up @@ -194,16 +198,16 @@ contract LimitOrder is BaseHook {
poolManagerOnly
returns (uint128 amount0, uint128 amount1)
{
(BalanceDelta _delta, BalanceDelta _feeDelta) = poolManager.modifyLiquidity(
(BalanceDelta delta,) = poolManager.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({
tickLower: tickLower,
tickUpper: tickLower + key.tickSpacing,
liquidityDelta: liquidityDelta
liquidityDelta: liquidityDelta,
salt: 0
}),
ZERO_BYTES
);
BalanceDelta delta = _delta + _feeDelta;

if (delta.amount0() > 0) {
poolManager.mint(address(this), key.currency0.toId(), amount0 = uint128(delta.amount0()));
Expand Down Expand Up @@ -262,7 +266,8 @@ contract LimitOrder is BaseHook {
IPoolManager.ModifyLiquidityParams({
tickLower: tickLower,
tickUpper: tickLower + key.tickSpacing,
liquidityDelta: liquidityDelta
liquidityDelta: liquidityDelta,
salt: 0
}),
ZERO_BYTES
);
Expand Down Expand Up @@ -325,7 +330,12 @@ contract LimitOrder is BaseHook {
if (!removingAllLiquidity) {
(, BalanceDelta deltaFee) = poolManager.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({tickLower: tickLower, tickUpper: tickUpper, liquidityDelta: 0}),
IPoolManager.ModifyLiquidityParams({
tickLower: tickLower,
tickUpper: tickUpper,
liquidityDelta: 0,
salt: 0
}),
ZERO_BYTES
);

Expand All @@ -342,7 +352,8 @@ contract LimitOrder is BaseHook {
IPoolManager.ModifyLiquidityParams({
tickLower: tickLower,
tickUpper: tickUpper,
liquidityDelta: liquidityDelta
liquidityDelta: liquidityDelta,
salt: 0
}),
ZERO_BYTES
);
Expand Down
10 changes: 7 additions & 3 deletions contracts/hooks/examples/TWAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ contract TWAMM is BaseHook, ITWAMM {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

Expand Down Expand Up @@ -103,10 +107,10 @@ contract TWAMM is BaseHook, ITWAMM {
external
override
poolManagerOnly
returns (bytes4)
returns (bytes4, int128)
{
executeTWAMMOrders(key);
return BaseHook.beforeSwap.selector;
return (BaseHook.beforeSwap.selector, 0);
}

function lastVirtualOrderTimestamp(PoolId key) external view returns (uint256) {
Expand Down
6 changes: 5 additions & 1 deletion contracts/hooks/examples/VolatilityOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ contract VolatilityOracle is BaseHook {
beforeSwap: false,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/v4-core
Submodule v4-core updated 81 files
+1 −0 .forge-snapshots/add liquidity to already existing position with salt.snap
+1 −0 .forge-snapshots/addLiquidity CA fee.snap
+1 −1 .forge-snapshots/addLiquidity with empty hook.snap
+1 −1 .forge-snapshots/addLiquidity with native token.snap
+1 −1 .forge-snapshots/addLiquidity.snap
+1 −0 .forge-snapshots/create new liquidity to a position with salt.snap
+1 −1 .forge-snapshots/donate gas with 1 token.snap
+1 −1 .forge-snapshots/donate gas with 2 tokens.snap
+1 −1 .forge-snapshots/erc20 collect protocol fees.snap
+1 −1 .forge-snapshots/initialize.snap
+1 −1 .forge-snapshots/native collect protocol fees.snap
+1 −1 .forge-snapshots/poolManager bytecode size.snap
+1 −0 .forge-snapshots/removeLiquidity CA fee.snap
+1 −1 .forge-snapshots/removeLiquidity with empty hook.snap
+1 −1 .forge-snapshots/removeLiquidity with native token.snap
+1 −1 .forge-snapshots/removeLiquidity.snap
+1 −1 .forge-snapshots/simple swap with native.snap
+1 −1 .forge-snapshots/simple swap.snap
+1 −0 .forge-snapshots/swap CA custom curve + swap noop.snap
+1 −0 .forge-snapshots/swap CA fee on unspecified.snap
+1 −1 .forge-snapshots/swap against liquidity with native token.snap
+1 −1 .forge-snapshots/swap against liquidity.snap
+1 −1 .forge-snapshots/swap burn 6909 for input.snap
+1 −1 .forge-snapshots/swap burn native 6909 for input.snap
+1 −1 .forge-snapshots/swap mint native output as 6909.snap
+1 −1 .forge-snapshots/swap mint output as 6909.snap
+1 −1 .forge-snapshots/swap skips hook call if hook is caller.snap
+1 −1 .forge-snapshots/swap with dynamic fee.snap
+1 −1 .forge-snapshots/swap with hooks.snap
+1 −1 .forge-snapshots/swap with lp fee and protocol fee.snap
+1 −1 .forge-snapshots/update dynamic fee in before swap.snap
+7 −5 README.md
+101 −33 src/PoolManager.sol
+8 −4 src/interfaces/IHooks.sol
+17 −4 src/interfaces/IPoolManager.sol
+5 −1 src/libraries/CurrencySettleTake.sol
+120 −30 src/libraries/Hooks.sol
+25 −25 src/libraries/Pool.sol
+7 −3 src/libraries/Position.sol
+1 −1 src/libraries/TickBitmap.sol
+3 −3 src/test/ActionsRouter.sol
+4 −4 src/test/BaseTestHooks.sol
+80 −0 src/test/CustomCurveHook.sol
+92 −0 src/test/DeltaReturningHook.sol
+2 −2 src/test/DynamicFeesTestHook.sol
+14 −10 src/test/EmptyTestHooks.sol
+88 −0 src/test/FeeTakingHook.sol
+9 −9 src/test/MockHooks.sol
+14 −0 src/test/PoolModifyLiquidityTest.sol
+10 −10 src/test/PoolNestedActionsTest.sol
+6 −2 src/test/PoolSwapTest.sol
+10 −10 src/test/SkipCallsTestHook.sol
+7 −1 src/types/BalanceDelta.sol
+6 −17 test/DynamicFees.t.sol
+186 −0 test/ModifyLiquidity.t.sol
+371 −62 test/PoolManager.t.sol
+1 −1 test/Reserves.t.sol
+9 −16 test/SkipCallsTestHook.t.sol
+1 −1 test/Sync.t.sol
+9 −9 test/Tick.t.sol
+12 −12 test/libraries/BitMath.t.sol
+14 −14 test/libraries/FullMath.t.sol
+238 −72 test/libraries/Hooks.t.sol
+6 −6 test/libraries/LPFeeLibrary.t.sol
+1 −1 test/libraries/LiquidityMath.t.sol
+1 −1 test/libraries/Lock.t.sol
+1 −1 test/libraries/NonZeroDeltaCount.t.sol
+25 −19 test/libraries/Pool.t.sol
+1 −1 test/libraries/PoolId.t.sol
+3 −3 test/libraries/Position.t.sol
+7 −7 test/libraries/ProtocolFeeLibrary.t.sol
+36 −5 test/libraries/SafeCast.t.sol
+21 −20 test/libraries/SqrtPriceMath.t.sol
+10 −10 test/libraries/SwapMath.t.sol
+22 −16 test/libraries/TickBitmap.t.sol
+12 −12 test/libraries/TickMath.t.sol
+4 −4 test/libraries/UnsafeMath.t.sol
+6 −6 test/types/BalanceDelta.t.sol
+10 −10 test/types/Currency.t.sol
+10 −6 test/utils/Deployers.sol
+2 −2 test/utils/NestedActions.t.sol
2 changes: 1 addition & 1 deletion test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
vm.expectRevert(FullRange.SenderMustBeHook.selector);
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({tickLower: MIN_TICK, tickUpper: MAX_TICK, liquidityDelta: 100}),
IPoolManager.ModifyLiquidityParams({tickLower: MIN_TICK, tickUpper: MAX_TICK, liquidityDelta: 100, salt: 0}),
ZERO_BYTES
);
}
Expand Down
10 changes: 5 additions & 5 deletions test/GeomeanOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ contract TestGeomeanOracle is Test, Deployers {
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000, 0
),
ZERO_BYTES
);
Expand All @@ -143,7 +143,7 @@ contract TestGeomeanOracle is Test, Deployers {
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000, 0
),
ZERO_BYTES
);
Expand Down Expand Up @@ -172,7 +172,7 @@ contract TestGeomeanOracle is Test, Deployers {
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000, 0
),
ZERO_BYTES
);
Expand Down Expand Up @@ -204,7 +204,7 @@ contract TestGeomeanOracle is Test, Deployers {
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000, 0
),
ZERO_BYTES
);
Expand All @@ -213,7 +213,7 @@ contract TestGeomeanOracle is Test, Deployers {
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), -1000
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), -1000, 0
),
ZERO_BYTES
);
Expand Down
Loading

0 comments on commit 558860b

Please sign in to comment.