diff --git a/.forge-snapshots/TickMathGetSqrtPriceAtTick.snap b/.forge-snapshots/TickMathGetSqrtPriceAtTick.snap new file mode 100644 index 000000000..47c561f7b --- /dev/null +++ b/.forge-snapshots/TickMathGetSqrtPriceAtTick.snap @@ -0,0 +1 @@ +87496 \ No newline at end of file diff --git a/.forge-snapshots/TickMathGetTickAtSqrtPrice.snap b/.forge-snapshots/TickMathGetTickAtSqrtPrice.snap new file mode 100644 index 000000000..90f41c594 --- /dev/null +++ b/.forge-snapshots/TickMathGetTickAtSqrtPrice.snap @@ -0,0 +1 @@ +233945 \ No newline at end of file diff --git a/test/libraries/TickMath.t.sol b/test/libraries/TickMath.t.sol index 05eb505dc..93714f97a 100644 --- a/test/libraries/TickMath.t.sol +++ b/test/libraries/TickMath.t.sol @@ -1,13 +1,14 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.20; +import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; import {Test} from "forge-std/Test.sol"; import {Vm} from "forge-std/Vm.sol"; import {TickMathTest} from "src/test/TickMathTest.sol"; import {TickMath} from "src/libraries/TickMath.sol"; import {JavascriptFfi} from "test/utils/JavascriptFfi.sol"; -contract TickMathTestTest is Test, JavascriptFfi { +contract TickMathTestTest is Test, JavascriptFfi, GasSnapshot { int24 constant MIN_TICK = -887272; int24 constant MAX_TICK = -MIN_TICK; @@ -93,7 +94,7 @@ contract TickMathTestTest is Test, JavascriptFfi { function test_getTickAtSqrtPrice_throwsForTooHigh() public { vm.expectRevert(TickMath.InvalidSqrtPrice.selector); - tickMath.getTickAtSqrtPrice(MAX_SQRT_PRICE + 1); + tickMath.getTickAtSqrtPrice(MAX_SQRT_PRICE); } function test_getTickAtSqrtPrice_isValidMinSqrtPrice() public view { @@ -173,4 +174,27 @@ contract TickMathTestTest is Test, JavascriptFfi { assertLt(resultsDiff, 2); } } + + /// @notice Benchmark the gas cost of `getSqrtPriceAtTick` + function test_getSqrtPriceAtTick_gasCost() public { + snapStart("TickMathGetSqrtPriceAtTick"); + unchecked { + for (int24 tick = -50; tick < 50;) { + TickMath.getSqrtPriceAtTick(tick++); + } + } + snapEnd(); + } + + /// @notice Benchmark the gas cost of `getTickAtSqrtPrice` + function test_getTickAtSqrtPrice_gasCost() public { + snapStart("TickMathGetTickAtSqrtPrice"); + unchecked { + uint160 sqrtPriceX96 = 1 << 33; + for (uint256 i; i++ < 100; sqrtPriceX96 <<= 1) { + TickMath.getTickAtSqrtPrice(sqrtPriceX96); + } + } + snapEnd(); + } }