From 33565dc777c6530d17307989df5b5e54300d9a8f Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Mon, 29 Apr 2024 11:30:41 +0800 Subject: [PATCH 1/6] chore: minor gas opt safeCast --- .../CLPoolManagerTest#addLiquidity_fromEmpty.snap | 2 +- ...CLPoolManagerTest#addLiquidity_fromNonEmpty.snap | 2 +- .../CLPoolManagerTest#addLiquidity_nativeToken.snap | 2 +- ...LPoolManagerTest#removeLiquidity_toNonEmpty.snap | 2 +- .../CLPoolManagerTest#swap_runOutOfLiquidity.snap | 2 +- .../CLPositionTest#Position_update_remove.snap | 2 +- .../LiquidityMathTest#addDeltaNegtive.snap | 2 +- .../LiquidityMathTest#addDeltaPositive.snap | 2 +- .forge-snapshots/TickTest#update.snap | 2 +- src/pool-cl/libraries/LiquidityMath.sol | 13 ++++++++----- test/pool-cl/CLPoolManager.t.sol | 3 ++- test/pool-cl/libraries/CLPool.t.sol | 2 +- test/pool-cl/libraries/CLPosition.t.sol | 3 ++- test/pool-cl/libraries/LiquidityMath.t.sol | 7 ++++--- 14 files changed, 26 insertions(+), 20 deletions(-) diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap index ed36e4f9..85a06d2e 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap @@ -1 +1 @@ -348848 \ No newline at end of file +348420 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap index 234f701a..01aafa28 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap @@ -1 +1 @@ -59396 \ No newline at end of file +58968 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap index 2c5a90c6..b1752458 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap @@ -1 +1 @@ -242441 \ No newline at end of file +242013 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index 1e077618..32adeaa6 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -42374 \ No newline at end of file +41768 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap index 4aee4462..165dc651 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap @@ -1 +1 @@ -25040458 \ No newline at end of file +25040256 \ No newline at end of file diff --git a/.forge-snapshots/CLPositionTest#Position_update_remove.snap b/.forge-snapshots/CLPositionTest#Position_update_remove.snap index bc7f6228..d9bc155c 100644 --- a/.forge-snapshots/CLPositionTest#Position_update_remove.snap +++ b/.forge-snapshots/CLPositionTest#Position_update_remove.snap @@ -1 +1 @@ -2014 \ No newline at end of file +1812 \ No newline at end of file diff --git a/.forge-snapshots/LiquidityMathTest#addDeltaNegtive.snap b/.forge-snapshots/LiquidityMathTest#addDeltaNegtive.snap index 0e92c3c0..d2c5ed21 100644 --- a/.forge-snapshots/LiquidityMathTest#addDeltaNegtive.snap +++ b/.forge-snapshots/LiquidityMathTest#addDeltaNegtive.snap @@ -1 +1 @@ -306 \ No newline at end of file +116 \ No newline at end of file diff --git a/.forge-snapshots/LiquidityMathTest#addDeltaPositive.snap b/.forge-snapshots/LiquidityMathTest#addDeltaPositive.snap index b7c52fb1..95c8a676 100644 --- a/.forge-snapshots/LiquidityMathTest#addDeltaPositive.snap +++ b/.forge-snapshots/LiquidityMathTest#addDeltaPositive.snap @@ -1 +1 @@ -212 \ No newline at end of file +113 \ No newline at end of file diff --git a/.forge-snapshots/TickTest#update.snap b/.forge-snapshots/TickTest#update.snap index 7eff560d..7b7e6841 100644 --- a/.forge-snapshots/TickTest#update.snap +++ b/.forge-snapshots/TickTest#update.snap @@ -1 +1 @@ -134926 \ No newline at end of file +134819 \ No newline at end of file diff --git a/src/pool-cl/libraries/LiquidityMath.sol b/src/pool-cl/libraries/LiquidityMath.sol index 49e00cc6..c2ae640b 100644 --- a/src/pool-cl/libraries/LiquidityMath.sol +++ b/src/pool-cl/libraries/LiquidityMath.sol @@ -9,11 +9,14 @@ library LiquidityMath { /// @param y The delta by which liquidity should be changed /// @return z The liquidity delta function addDelta(uint128 x, int128 y) internal pure returns (uint128 z) { - // after 0.8.0 overflow is checked by default - if (y < 0) { - z = x - uint128(-y); - } else { - z = x + uint128(y); + assembly { + z := add(x, y) + + if shr(128, z) { + // store 0x93dafdf1, error SafeCastOverflow at memory 0 address and revert from pointer 28, to byte 32 + mstore(0x0, 0x93dafdf1) + revert(0x1c, 0x04) + } } } } diff --git a/test/pool-cl/CLPoolManager.t.sol b/test/pool-cl/CLPoolManager.t.sol index 58f83040..ace90e21 100644 --- a/test/pool-cl/CLPoolManager.t.sol +++ b/test/pool-cl/CLPoolManager.t.sol @@ -34,6 +34,7 @@ import {ProtocolFeeControllerTest} from "./helpers/ProtocolFeeControllerTest.sol import {IProtocolFeeController} from "../../src/interfaces/IProtocolFeeController.sol"; import {CLFeeManagerHook} from "./helpers/CLFeeManagerHook.sol"; import {CLNoOpTestHook} from "./helpers/CLNoOpTestHook.sol"; +import {SafeCast} from "../../src/libraries/SafeCast.sol"; contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot { using PoolIdLibrary for PoolKey; @@ -866,7 +867,7 @@ contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot { IERC20(Currency.unwrap(currency0)).approve(address(router), 1e30 ether); IERC20(Currency.unwrap(currency1)).approve(address(router), 1e30 ether); - vm.expectRevert(stdError.arithmeticError); + vm.expectRevert(SafeCast.SafeCastOverflow.selector); router.modifyPosition( key, ICLPoolManager.ModifyLiquidityParams({tickLower: 46000, tickUpper: 46050, liquidityDelta: -1}), "" ); diff --git a/test/pool-cl/libraries/CLPool.t.sol b/test/pool-cl/libraries/CLPool.t.sol index 76c678da..5ce73acb 100644 --- a/test/pool-cl/libraries/CLPool.t.sol +++ b/test/pool-cl/libraries/CLPool.t.sol @@ -55,7 +55,7 @@ contract PoolTest is Test { } else if (params.tickUpper > TickMath.MAX_TICK) { vm.expectRevert(abi.encodeWithSelector(Tick.TickUpperOutOfBounds.selector, params.tickUpper)); } else if (params.liquidityDelta < 0) { - vm.expectRevert(abi.encodeWithSignature("Panic(uint256)", 0x11)); + vm.expectRevert(SafeCast.SafeCastOverflow.selector); } else if (params.liquidityDelta == 0) { vm.expectRevert(CLPosition.CannotUpdateEmptyPosition.selector); } else if (params.liquidityDelta > int128(Tick.tickSpacingToMaxLiquidityPerTick(params.tickSpacing))) { diff --git a/test/pool-cl/libraries/CLPosition.t.sol b/test/pool-cl/libraries/CLPosition.t.sol index 45194f11..cd97adba 100644 --- a/test/pool-cl/libraries/CLPosition.t.sol +++ b/test/pool-cl/libraries/CLPosition.t.sol @@ -7,6 +7,7 @@ import {Test} from "forge-std/Test.sol"; import {CLPosition} from "../../../src/pool-cl/libraries/CLPosition.sol"; import {CLPool} from "../../../src/pool-cl/libraries/CLPool.sol"; import {FixedPoint128} from "../../../src/pool-cl/libraries/FixedPoint128.sol"; +import {SafeCast} from "../../../src/libraries/SafeCast.sol"; contract CLPositionTest is Test, GasSnapshot { using CLPosition for mapping(bytes32 => CLPosition.Info); @@ -31,7 +32,7 @@ contract CLPositionTest is Test, GasSnapshot { if (liquidityDelta == 0) { vm.expectRevert(CLPosition.CannotUpdateEmptyPosition.selector); } else if (liquidityDelta < 0) { - vm.expectRevert(stdError.arithmeticError); + vm.expectRevert(SafeCast.SafeCastOverflow.selector); } (uint256 feesOwed0, uint256 feesOwed1) = info.update(liquidityDelta, feeGrowthInside0X128, feeGrowthInside1X128); diff --git a/test/pool-cl/libraries/LiquidityMath.t.sol b/test/pool-cl/libraries/LiquidityMath.t.sol index cee9bd89..8562be9c 100644 --- a/test/pool-cl/libraries/LiquidityMath.t.sol +++ b/test/pool-cl/libraries/LiquidityMath.t.sol @@ -4,6 +4,7 @@ pragma solidity ^0.8.24; import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; import {stdError} from "forge-std/StdError.sol"; import {Test} from "forge-std/Test.sol"; +import {SafeCast} from "../../../src/libraries/SafeCast.sol"; import {LiquidityMath} from "../../../src/pool-cl/libraries/LiquidityMath.sol"; contract LiquidityMathTest is Test, GasSnapshot { @@ -22,16 +23,16 @@ contract LiquidityMathTest is Test, GasSnapshot { } function testAddDeltaOverflow() public { - vm.expectRevert(stdError.arithmeticError); + vm.expectRevert(SafeCast.SafeCastOverflow.selector); LiquidityMath.addDelta(2 ** 128 - 15, 15); } function testAddDeltaUnderflow() public { // underflow - vm.expectRevert(stdError.arithmeticError); + vm.expectRevert(SafeCast.SafeCastOverflow.selector); LiquidityMath.addDelta(0, -1); - vm.expectRevert(stdError.arithmeticError); + vm.expectRevert(SafeCast.SafeCastOverflow.selector); LiquidityMath.addDelta(3, -4); } } From b0cd33ffe539f7a79495e8b4cde917e223dbd518 Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Mon, 29 Apr 2024 14:38:34 +0800 Subject: [PATCH 2/6] test: update test --- test/libraries/BalanceDelta.t.sol | 87 ++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/test/libraries/BalanceDelta.t.sol b/test/libraries/BalanceDelta.t.sol index dc1dcf5b..f611d6a2 100644 --- a/test/libraries/BalanceDelta.t.sol +++ b/test/libraries/BalanceDelta.t.sol @@ -5,7 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {BalanceDelta, toBalanceDelta} from "../../src/types/BalanceDelta.sol"; contract TestBalanceDelta is Test { - function testToBalanceDelta() public { + function test_toBalanceDelta() public { BalanceDelta balanceDelta = toBalanceDelta(0, 0); assertEq(balanceDelta.amount0(), 0); assertEq(balanceDelta.amount1(), 0); @@ -27,17 +27,52 @@ contract TestBalanceDelta is Test { assertEq(balanceDelta.amount1(), type(int128).min); } - function testToBalanceDelta(int128 x, int128 y) public { + function test_fuzz_toBalanceDelta(int128 x, int128 y) public { + BalanceDelta balanceDelta = toBalanceDelta(x, y); + int256 expectedBD = int256(uint256(bytes32(abi.encodePacked(x, y)))); + assertEq(BalanceDelta.unwrap(balanceDelta), expectedBD); + } + + function test_fuzz_amount0_amount1(int128 x, int128 y) public { BalanceDelta balanceDelta = toBalanceDelta(x, y); assertEq(balanceDelta.amount0(), x); assertEq(balanceDelta.amount1(), y); } - function testAdd(int128 a, int128 b, int128 c, int128 d) public { + function test_add() public { + BalanceDelta balanceDelta = toBalanceDelta(0, 0) + toBalanceDelta(0, 0); + assertEq(balanceDelta.amount0(), 0); + assertEq(balanceDelta.amount1(), 0); + + balanceDelta = toBalanceDelta(-1000, 1000) + toBalanceDelta(1000, -1000); + assertEq(balanceDelta.amount0(), 0); + assertEq(balanceDelta.amount1(), 0); + + balanceDelta = + toBalanceDelta(type(int128).min, type(int128).max) + toBalanceDelta(type(int128).max, type(int128).min); + assertEq(balanceDelta.amount0(), -1); + assertEq(balanceDelta.amount1(), -1); + + balanceDelta = toBalanceDelta(type(int128).max / 2 + 1, type(int128).max / 2 + 1) + + toBalanceDelta(type(int128).max / 2, type(int128).max / 2); + assertEq(balanceDelta.amount0(), type(int128).max); + assertEq(balanceDelta.amount1(), type(int128).max); + } + + function test_add_revertsOnOverflow() public { + // should revert because type(int128).max + 1 is not possible + vm.expectRevert(); + toBalanceDelta(type(int128).max, 0) + toBalanceDelta(1, 0); + + vm.expectRevert(); + toBalanceDelta(0, type(int128).max) + toBalanceDelta(0, 1); + } + + function test_fuzz_add(int128 a, int128 b, int128 c, int128 d) public { int256 ac = int256(a) + c; int256 bd = int256(b) + d; - // make sure the addition doesn't overflow + // if the addition overflows it should revert if (ac != int128(ac) || bd != int128(bd)) { vm.expectRevert(); } @@ -47,16 +82,52 @@ contract TestBalanceDelta is Test { assertEq(balanceDelta.amount1(), bd); } - function testSub(int128 a, int128 b, int128 c, int128 d) public { + function test_sub() public { + BalanceDelta balanceDelta = toBalanceDelta(0, 0) - toBalanceDelta(0, 0); + assertEq(balanceDelta.amount0(), 0); + assertEq(balanceDelta.amount1(), 0); + + balanceDelta = toBalanceDelta(-1000, 1000) - toBalanceDelta(1000, -1000); + assertEq(balanceDelta.amount0(), -2000); + assertEq(balanceDelta.amount1(), 2000); + + balanceDelta = + toBalanceDelta(-1000, -1000) - toBalanceDelta(-(type(int128).min + 1000), -(type(int128).min + 1000)); + assertEq(balanceDelta.amount0(), type(int128).min); + assertEq(balanceDelta.amount1(), type(int128).min); + + balanceDelta = toBalanceDelta(type(int128).min / 2, type(int128).min / 2) + - toBalanceDelta(-(type(int128).min / 2), -(type(int128).min / 2)); + assertEq(balanceDelta.amount0(), type(int128).min); + assertEq(balanceDelta.amount1(), type(int128).min); + } + + function test_sub_revertsOnUnderflow() public { + // should revert because type(int128).min - 1 is not possible + vm.expectRevert(); + toBalanceDelta(type(int128).min, 0) - toBalanceDelta(1, 0); + + vm.expectRevert(); + toBalanceDelta(0, type(int128).min) - toBalanceDelta(0, 1); + } + + function test_fuzz_sub(int128 a, int128 b, int128 c, int128 d) public { int256 ac = int256(a) - c; int256 bd = int256(b) - d; - // make sure the subtraction doesn't underflow - vm.assume(ac == int128(ac)); - vm.assume(bd == int128(bd)); + // if the subtraction underflows it should revert + if (ac != int128(ac) || bd != int128(bd)) { + vm.expectRevert(); + } BalanceDelta balanceDelta = toBalanceDelta(a, b) - toBalanceDelta(c, d); assertEq(balanceDelta.amount0(), ac); assertEq(balanceDelta.amount1(), bd); } + + function test_fuzz_eq(int128 a, int128 b, int128 c, int128 d) public { + bool isEqual = (toBalanceDelta(a, b) == toBalanceDelta(c, d)); + if (a == c && b == d) assertTrue(isEqual); + else assertFalse(isEqual); + } } From e1710a0bf3df12b26434de0c3e525aea5f7e6d73 Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Mon, 29 Apr 2024 15:24:18 +0800 Subject: [PATCH 3/6] tweak: use unchecked --- ...PoolManagerTest#testBurnNativeCurrency.snap | 2 +- .../BinPoolManagerTest#testGasBurnHalfBin.snap | 2 +- ...BinPoolManagerTest#testGasBurnNineBins.snap | 2 +- .../BinPoolManagerTest#testGasBurnOneBin.snap | 2 +- ...oolManagerTest#testGasSwapMultipleBins.snap | 2 +- ...anagerTest#testGasSwapOverBigBinIdGate.snap | 2 +- ...inPoolManagerTest#testGasSwapSingleBin.snap | 2 +- ...ManagerTest#removeLiquidity_toNonEmpty.snap | 2 +- ...LPoolManagerTest#swap_againstLiquidity.snap | 2 +- ...PoolManagerTest#swap_runOutOfLiquidity.snap | 2 +- ...anagerTest#swap_useSurplusTokenAsInput.snap | 2 +- .forge-snapshots/VaultTest#Vault.snap | 2 +- .../VaultTest#lockSettledWhenFlashloan.snap | 2 +- .../VaultTest#lockSettledWhenMultiHopSwap.snap | 2 +- .../VaultTest#lockSettledWhenSwap.snap | 2 +- src/Vault.sol | 18 ++++++++++++------ 16 files changed, 27 insertions(+), 21 deletions(-) diff --git a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap index f57e6954..6c5a46a4 100644 --- a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap +++ b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap @@ -1 +1 @@ -90479 \ No newline at end of file +90473 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap index 0d077daa..a8a10e7b 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap @@ -1 +1 @@ -65672 \ No newline at end of file +65666 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap index 636780b6..7a35ebc6 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap @@ -1 +1 @@ -149227 \ No newline at end of file +149221 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap index 5d28fd52..25d5dbb8 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap @@ -1 +1 @@ -66851 \ No newline at end of file +66845 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap index 1d4e2bf7..83a1133b 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap @@ -1 +1 @@ -89312 \ No newline at end of file +89309 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap index 2035602e..cc1ae061 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap @@ -1 +1 @@ -91297 \ No newline at end of file +91294 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap index bd838801..c39c1b21 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap @@ -1 +1 @@ -70741 \ No newline at end of file +70738 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index 32adeaa6..9db678c0 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -41768 \ No newline at end of file +41765 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap index 8470dee1..1acaf921 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap @@ -1 +1 @@ -54672 \ No newline at end of file +54669 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap index 165dc651..98d92630 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap @@ -1 +1 @@ -25040256 \ No newline at end of file +25040253 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap b/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap index 017f6b15..fc7da802 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap @@ -1 +1 @@ -101510 \ No newline at end of file +101507 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#Vault.snap b/.forge-snapshots/VaultTest#Vault.snap index a75c38b3..7052d3dc 100644 --- a/.forge-snapshots/VaultTest#Vault.snap +++ b/.forge-snapshots/VaultTest#Vault.snap @@ -1 +1 @@ -7314 \ No newline at end of file +7318 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap b/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap index 02c149c9..3fe567b6 100644 --- a/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap +++ b/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap @@ -1 +1 @@ -156938 \ No newline at end of file +156932 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap b/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap index d816d6d9..3100de7c 100644 --- a/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap +++ b/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap @@ -1 +1 @@ -45186 \ No newline at end of file +45183 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap b/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap index 0a374883..4e984be7 100644 --- a/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap +++ b/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap @@ -1 +1 @@ -45185 \ No newline at end of file +45182 \ No newline at end of file diff --git a/src/Vault.sol b/src/Vault.sol index 749104db..9fc7d89a 100644 --- a/src/Vault.sol +++ b/src/Vault.sol @@ -106,15 +106,19 @@ contract Vault is IVault, VaultToken, Ownable { /// @inheritdoc IVault function take(Currency currency, address to, uint256 amount) external override isLocked { - SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); + unchecked { + SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); + currency.transfer(to, amount); + } reservesOfVault[currency] -= amount; - currency.transfer(to, amount); } /// @inheritdoc IVault function mint(address to, Currency currency, uint256 amount) external override isLocked { - SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); - _mint(to, currency, amount); + unchecked { + SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); + _mint(to, currency, amount); + } } /// @inheritdoc IVault @@ -156,8 +160,10 @@ contract Vault is IVault, VaultToken, Ownable { /// @notice settle all outstanding debt if amount is 0 /// It will revert if target has negative delta if (amount == 0) amount = SettlementGuard.getCurrencyDelta(target, currency).toUint256(); - SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); - SettlementGuard.accountDelta(target, currency, -(amount.toInt128())); + unchecked { + SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); + SettlementGuard.accountDelta(target, currency, -(amount.toInt128())); + } } /// @inheritdoc IVault From bdfd2df049f765479fa112d4fcec179bb9ce8de4 Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Tue, 7 May 2024 16:24:40 +0800 Subject: [PATCH 4/6] gas: updated forge gas --- .forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap | 2 +- .../CLPoolManagerTest#addLiquidity_fromNonEmpty.snap | 2 +- .../CLPoolManagerTest#addLiquidity_nativeToken.snap | 2 +- .../CLPoolManagerTest#removeLiquidity_toNonEmpty.snap | 2 +- .forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap | 2 +- .forge-snapshots/CLPositionTest#Position_update_remove.snap | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap index 4fa5c45e..5441f030 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap @@ -1 +1 @@ -348420 +401422 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap index b1e9461c..87bb66dc 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap @@ -1 +1 @@ -58968 +182410 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap index 4e6ffb3e..7ada3d1d 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap @@ -1 +1 @@ -242013 +247549 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index 81c6f63c..887d205b 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -41768 +128995 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap index 2ea4ec88..134ba728 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap @@ -1 +1 @@ -25040256 +24940739 \ No newline at end of file diff --git a/.forge-snapshots/CLPositionTest#Position_update_remove.snap b/.forge-snapshots/CLPositionTest#Position_update_remove.snap index 7d941807..870e31e0 100644 --- a/.forge-snapshots/CLPositionTest#Position_update_remove.snap +++ b/.forge-snapshots/CLPositionTest#Position_update_remove.snap @@ -1 +1 @@ -1812 +1697 \ No newline at end of file From b1a02ef18699f5cbbeb34e53db85e5f44952340b Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Tue, 7 May 2024 16:32:57 +0800 Subject: [PATCH 5/6] test: updated forge gas --- .../BinPoolManagerTest#testBurnNativeCurrency.snap | 2 +- .forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap | 2 +- .forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap | 2 +- .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap | 2 +- .../BinPoolManagerTest#testGasSwapMultipleBins.snap | 2 +- .../BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap | 2 +- .forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap | 2 +- .../CLPoolManagerTest#removeLiquidity_toNonEmpty.snap | 2 +- .forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap | 2 +- .../CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap | 2 +- .../CLPoolManagerTest#swap_runOutOfLiquidity.snap | 2 +- .../CLPoolManagerTest#swap_useSurplusTokenAsInput.snap | 2 +- .../CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap | 2 +- .forge-snapshots/VaultTest#Vault.snap | 2 +- .forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap | 2 +- .forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap | 2 +- .forge-snapshots/VaultTest#lockSettledWhenSwap.snap | 2 +- src/Vault.sol | 4 ++-- 18 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap index 0f30b9ff..0b928036 100644 --- a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap +++ b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap @@ -1 +1 @@ -90473 +142037 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap index d71c9420..4fce794a 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap @@ -1 +1 @@ -65666 +158209 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap index 3dc1cf36..522fa6a2 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap @@ -1 +1 @@ -149221 +303832 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap index 0194587f..15bd74bc 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap @@ -1 +1 @@ -66845 +139397 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap index 4263e5be..4aec7641 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap @@ -1 +1 @@ -89309 +187314 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap index 98e69e43..fda68605 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap @@ -1 +1 @@ -91294 +193299 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap index 8db42d41..df05d1ad 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap @@ -1 +1 @@ -70738 +145663 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index a617a046..676a6e35 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -41765 +128996 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap index e06585b4..61b2fc11 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_againstLiquidity.snap @@ -1 +1 @@ -54669 +148768 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap b/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap index 5668dc15..81d44198 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_leaveSurplusTokenInVault.snap @@ -1 +1 @@ -177177 \ No newline at end of file +177178 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap index 2ac0bf0e..33173691 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap @@ -1 +1 @@ -25040253 +24940740 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap b/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap index fda5fb30..9534e749 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_useSurplusTokenAsInput.snap @@ -1 +1 @@ -101507 +158285 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap b/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap index 6d0e4885..df945337 100644 --- a/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap +++ b/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap @@ -1 +1 @@ -32257 \ No newline at end of file +32269 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#Vault.snap b/.forge-snapshots/VaultTest#Vault.snap index dddd76e6..b70512a0 100644 --- a/.forge-snapshots/VaultTest#Vault.snap +++ b/.forge-snapshots/VaultTest#Vault.snap @@ -1 +1 @@ -7318 +7437 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap b/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap index d4814c9b..4fdddea1 100644 --- a/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap +++ b/.forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap @@ -1 +1 @@ -156932 +103575 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap b/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap index 45611885..b9a17161 100644 --- a/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap +++ b/.forge-snapshots/VaultTest#lockSettledWhenMultiHopSwap.snap @@ -1 +1 @@ -45183 +118274 \ No newline at end of file diff --git a/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap b/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap index 86dcb52e..1f7bb15b 100644 --- a/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap +++ b/.forge-snapshots/VaultTest#lockSettledWhenSwap.snap @@ -1 +1 @@ -45182 +118273 \ No newline at end of file diff --git a/src/Vault.sol b/src/Vault.sol index dd92de11..914f1c2d 100644 --- a/src/Vault.sol +++ b/src/Vault.sol @@ -104,8 +104,8 @@ contract Vault is IVault, VaultToken, Ownable { /// @inheritdoc IVault function take(Currency currency, address to, uint256 amount) external override isLocked { unchecked { - SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); - currency.transfer(to, amount); + SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128()); + currency.transfer(to, amount); } if (!currency.isNative()) reservesOfVault[currency] -= amount; } From 4bff1648733be7d9ee4369a79f553f86a195bccf Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Wed, 8 May 2024 13:00:26 +0800 Subject: [PATCH 6/6] gas: updated forge test --isolate gas snapshot --- .forge-snapshots/BinPoolManagerTest#testFuzz_SetMaxBinStep.snap | 2 +- .../CLPoolManagerTest#removeLiquidity_toNonEmpty.snap | 2 +- .forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap | 2 +- .../CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.forge-snapshots/BinPoolManagerTest#testFuzz_SetMaxBinStep.snap b/.forge-snapshots/BinPoolManagerTest#testFuzz_SetMaxBinStep.snap index b23c0f1f..7094de73 100644 --- a/.forge-snapshots/BinPoolManagerTest#testFuzz_SetMaxBinStep.snap +++ b/.forge-snapshots/BinPoolManagerTest#testFuzz_SetMaxBinStep.snap @@ -1 +1 @@ -30395 \ No newline at end of file +30383 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index 1ebc6b05..676a6e35 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -128996 +128996 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap index db50deee..33173691 100644 --- a/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap +++ b/.forge-snapshots/CLPoolManagerTest#swap_runOutOfLiquidity.snap @@ -1 +1 @@ -24940740 +24940740 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap b/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap index df945337..6d0e4885 100644 --- a/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap +++ b/.forge-snapshots/CLPoolManagerTest#testFuzzUpdateDynamicSwapFee.snap @@ -1 +1 @@ -32269 \ No newline at end of file +32257 \ No newline at end of file