diff --git a/.forge-snapshots/FullRangeFirstSwap.snap b/.forge-snapshots/FullRangeFirstSwap.snap index 34462841..9a67827d 100644 --- a/.forge-snapshots/FullRangeFirstSwap.snap +++ b/.forge-snapshots/FullRangeFirstSwap.snap @@ -1 +1 @@ -78977 \ No newline at end of file +82817 \ No newline at end of file diff --git a/.forge-snapshots/FullRangeInitialize.snap b/.forge-snapshots/FullRangeInitialize.snap index 3451c605..01b368e7 100644 --- a/.forge-snapshots/FullRangeInitialize.snap +++ b/.forge-snapshots/FullRangeInitialize.snap @@ -1 +1 @@ -1008717 \ No newline at end of file +1092315 \ No newline at end of file diff --git a/.forge-snapshots/FullRangeRemoveLiquidity.snap b/.forge-snapshots/FullRangeRemoveLiquidity.snap index de65ef10..e7581287 100644 --- a/.forge-snapshots/FullRangeRemoveLiquidity.snap +++ b/.forge-snapshots/FullRangeRemoveLiquidity.snap @@ -1 +1 @@ -109724 \ No newline at end of file +114904 \ No newline at end of file diff --git a/.forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap b/.forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap index b1b786de..a22474a0 100644 --- a/.forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap +++ b/.forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap @@ -1 +1 @@ -239812 \ No newline at end of file +242492 \ No newline at end of file diff --git a/.forge-snapshots/FullRangeSecondSwap.snap b/.forge-snapshots/FullRangeSecondSwap.snap index 4a24b0e5..937d1fe9 100644 --- a/.forge-snapshots/FullRangeSecondSwap.snap +++ b/.forge-snapshots/FullRangeSecondSwap.snap @@ -1 +1 @@ -44555 \ No newline at end of file +45895 \ No newline at end of file diff --git a/.forge-snapshots/FullRangeSwap.snap b/.forge-snapshots/FullRangeSwap.snap index 7d69ddee..64ff7315 100644 --- a/.forge-snapshots/FullRangeSwap.snap +++ b/.forge-snapshots/FullRangeSwap.snap @@ -1 +1 @@ -78130 \ No newline at end of file +81970 \ No newline at end of file diff --git a/.forge-snapshots/TWAMM executTWAMMOrders 1 interval.snap b/.forge-snapshots/TWAMM executTWAMMOrders 1 interval.snap index 101f9831..73d35f77 100644 --- a/.forge-snapshots/TWAMM executTWAMMOrders 1 interval.snap +++ b/.forge-snapshots/TWAMM executTWAMMOrders 1 interval.snap @@ -1 +1 @@ -484776 \ No newline at end of file +489956 \ No newline at end of file diff --git a/.forge-snapshots/TWAMM executTWAMMOrders 2 intervals.snap b/.forge-snapshots/TWAMM executTWAMMOrders 2 intervals.snap index 07d527f5..930472fb 100644 --- a/.forge-snapshots/TWAMM executTWAMMOrders 2 intervals.snap +++ b/.forge-snapshots/TWAMM executTWAMMOrders 2 intervals.snap @@ -1 +1 @@ -590266 \ No newline at end of file +595446 \ No newline at end of file diff --git a/.forge-snapshots/TWAMM executTWAMMOrders 3 intervals.snap b/.forge-snapshots/TWAMM executTWAMMOrders 3 intervals.snap index 44ba2ff9..ed0bcaea 100644 --- a/.forge-snapshots/TWAMM executTWAMMOrders 3 intervals.snap +++ b/.forge-snapshots/TWAMM executTWAMMOrders 3 intervals.snap @@ -1 +1 @@ -687728 \ No newline at end of file +692908 \ No newline at end of file diff --git a/.forge-snapshots/TWAMM executTWAMMOrders singleSell 1 interval.snap b/.forge-snapshots/TWAMM executTWAMMOrders singleSell 1 interval.snap index b645958e..cd90b5ba 100644 --- a/.forge-snapshots/TWAMM executTWAMMOrders singleSell 1 interval.snap +++ b/.forge-snapshots/TWAMM executTWAMMOrders singleSell 1 interval.snap @@ -1 +1 @@ -256690 \ No newline at end of file +262040 \ No newline at end of file diff --git a/.forge-snapshots/TWAMM executTWAMMOrders singleSell 2 intervals.snap b/.forge-snapshots/TWAMM executTWAMMOrders singleSell 2 intervals.snap index b03bae48..45e0da90 100644 --- a/.forge-snapshots/TWAMM executTWAMMOrders singleSell 2 intervals.snap +++ b/.forge-snapshots/TWAMM executTWAMMOrders singleSell 2 intervals.snap @@ -1 +1 @@ -288800 \ No newline at end of file +294150 \ No newline at end of file diff --git a/.forge-snapshots/TWAMM executTWAMMOrders singleSell oneForZero 1 interval.snap b/.forge-snapshots/TWAMM executTWAMMOrders singleSell oneForZero 1 interval.snap new file mode 100644 index 00000000..6ad6b87e --- /dev/null +++ b/.forge-snapshots/TWAMM executTWAMMOrders singleSell oneForZero 1 interval.snap @@ -0,0 +1 @@ +256437 \ No newline at end of file diff --git a/.forge-snapshots/TWAMM executTWAMMOrders singleSell oneForZero 2 intervals.snap b/.forge-snapshots/TWAMM executTWAMMOrders singleSell oneForZero 2 intervals.snap new file mode 100644 index 00000000..e74ab30b --- /dev/null +++ b/.forge-snapshots/TWAMM executTWAMMOrders singleSell oneForZero 2 intervals.snap @@ -0,0 +1 @@ +290599 \ No newline at end of file diff --git a/contracts/hooks/examples/TWAMM.sol b/contracts/hooks/examples/TWAMM.sol index fc1521d9..42c4444c 100644 --- a/contracts/hooks/examples/TWAMM.sol +++ b/contracts/hooks/examples/TWAMM.sol @@ -22,6 +22,7 @@ import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {CurrencySettler} from "@uniswap/v4-core/test/utils/CurrencySettler.sol"; import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol"; +import {LiquidityMath} from "@uniswap/v4-core/src/libraries/LiquidityMath.sol"; contract TWAMM is BaseHook, ITWAMM { using TransferHelper for IERC20Minimal; @@ -535,9 +536,8 @@ contract TWAMM is BaseHook, ITWAMM { params.pool.sqrtPriceX96, initializedSqrtPrice, params.pool.liquidity, true ); - params.pool.liquidity = params.zeroForOne - ? params.pool.liquidity - uint128(liquidityNetAtTick) - : params.pool.liquidity + uint128(-liquidityNetAtTick); + if (params.zeroForOne) liquidityNetAtTick = -liquidityNetAtTick; + params.pool.liquidity = LiquidityMath.addDelta(params.pool.liquidity, liquidityNetAtTick); params.pool.sqrtPriceX96 = initializedSqrtPrice; unchecked { diff --git a/test/TWAMM.t.sol b/test/TWAMM.t.sol index 06548142..48871c01 100644 --- a/test/TWAMM.t.sol +++ b/test/TWAMM.t.sol @@ -609,7 +609,7 @@ contract TWAMMTest is Test, Deployers, GasSnapshot { snapEnd(); } - function testTWAMM_executeTWAMMOrders_singlePoolSell_OneIntervalGas() public { + function testTWAMM_executeTWAMMOrders_singlePoolSell_zeroForOne_OneIntervalGas() public { ITWAMM.OrderKey memory orderKey1 = ITWAMM.OrderKey(address(this), 30000, true); token0.approve(address(twamm), 100 ether); @@ -624,7 +624,22 @@ contract TWAMMTest is Test, Deployers, GasSnapshot { snapEnd(); } - function testTWAMM_executeTWAMMOrders_SinglePoolSell_twoIntervalsGas() public { + function testTWAMM_executeTWAMMOrders_singlePoolSell_oneForZero_OneIntervalGas() public { + ITWAMM.OrderKey memory orderKey1 = ITWAMM.OrderKey(address(this), 30000, false); + + token1.approve(address(twamm), 100 ether); + + vm.warp(10000); + + twamm.submitOrder(poolKey, orderKey1, 1 ether); + + vm.warp(60000); + snapStart("TWAMM executTWAMMOrders singleSell oneForZero 1 interval"); + twamm.executeTWAMMOrders(poolKey); + snapEnd(); + } + + function testTWAMM_executeTWAMMOrders_SinglePoolSell_zeroForOne_twoIntervalsGas() public { ITWAMM.OrderKey memory orderKey1 = ITWAMM.OrderKey(address(this), 30000, true); ITWAMM.OrderKey memory orderKey2 = ITWAMM.OrderKey(address(this), 40000, true); @@ -641,6 +656,23 @@ contract TWAMMTest is Test, Deployers, GasSnapshot { snapEnd(); } + function testTWAMM_executeTWAMMOrders_SinglePoolSell_oneForZero_twoIntervalsGas() public { + ITWAMM.OrderKey memory orderKey1 = ITWAMM.OrderKey(address(this), 30000, false); + ITWAMM.OrderKey memory orderKey2 = ITWAMM.OrderKey(address(this), 40000, false); + + token1.approve(address(twamm), 100 ether); + + vm.warp(10000); + + twamm.submitOrder(poolKey, orderKey1, 5 ether); + twamm.submitOrder(poolKey, orderKey2, 1 ether); + + vm.warp(60000); + snapStart("TWAMM executTWAMMOrders singleSell oneForZero 2 intervals"); + twamm.executeTWAMMOrders(poolKey); + snapEnd(); + } + function testTWAMMEndToEndSimSymmetricalOrderPools() public { uint256 orderAmount = 1e18; ITWAMM.OrderKey memory orderKey1 = ITWAMM.OrderKey(address(this), 30000, true);