Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TWAMM: fix single sell liquidity calc #410

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
78977
82817
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1008717
1092315
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109724
114904
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
239812
242492
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
44555
45895
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
78130
81970
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMM executTWAMMOrders 1 interval.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
484776
489956
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMM executTWAMMOrders 2 intervals.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
590266
595446
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMM executTWAMMOrders 3 intervals.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
687728
692908
Original file line number Diff line number Diff line change
@@ -1 +1 @@
256690
262040
Original file line number Diff line number Diff line change
@@ -1 +1 @@
288800
294150
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
256437
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
290599
6 changes: 3 additions & 3 deletions contracts/hooks/examples/TWAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 34 additions & 2 deletions test/TWAMM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand Down
Loading