From 053900c54e70574de4a112ec8fa190588e842bdd Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Wed, 8 May 2024 21:20:06 -0400 Subject: [PATCH 1/4] Refactor imports --- .gas-snapshot | 162 ++++++++++++++++++++------------------- package.json | 2 +- test/NPMCaller.t.sol | 2 +- test/PoolCaller.t.sol | 4 +- test/SafeCast.t.sol | 4 +- test/SqrtPriceMath.t.sol | 3 +- test/TickBitmap.t.sol | 9 ++- test/TickMath.t.sol | 2 +- 8 files changed, 98 insertions(+), 90 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 052cb05..f5a0295 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,38 +1,38 @@ -BitMathTest:testFuzz_LSB(uint256) (runs: 65545, μ: 17627, ~: 17625) -BitMathTest:testFuzz_MSB(uint256) (runs: 65545, μ: 17649, ~: 17643) +BitMathTest:testFuzz_LSB(uint256) (runs: 65536, μ: 17627, ~: 17625) +BitMathTest:testFuzz_MSB(uint256) (runs: 65536, μ: 17649, ~: 17643) BitMathTest:testGas_LSB() (gas: 239714) BitMathTest:testGas_LSB_Og() (gas: 305805) BitMathTest:testGas_MSB() (gas: 255269) BitMathTest:testGas_MSB_Og() (gas: 288889) -FullMathTest:testFuzz_MulDivQ128(uint256,uint256) (runs: 65543, μ: 4047, ~: 3965) -FullMathTest:testFuzz_MulDivQ96(uint256,uint256) (runs: 65543, μ: 4022, ~: 3950) -FullMathTest:testFuzz_MulDivUp_OZ(uint256,uint256,uint256) (runs: 65545, μ: 4943, ~: 4743) -FullMathTest:testFuzz_MulDivUp_Og(uint256,uint256,uint256) (runs: 65545, μ: 10030, ~: 9829) -FullMathTest:testFuzz_MulDiv_OZ(uint256,uint256,uint256) (runs: 65545, μ: 4208, ~: 4075) -FullMathTest:testFuzz_MulDiv_Og(uint256,uint256,uint256) (runs: 65545, μ: 9567, ~: 9435) -FullMathTest:testFuzz_Sqrt(uint256) (runs: 65545, μ: 4411, ~: 4411) -LiquidityAmountsTest:testFuzz_GetAmount0ForLiquidity(uint160,uint160,uint128) (runs: 65545, μ: 20520, ~: 20778) -LiquidityAmountsTest:testFuzz_GetAmount1ForLiquidity(uint160,uint160,uint128) (runs: 65545, μ: 20074, ~: 20016) -LiquidityAmountsTest:testFuzz_GetAmountsForLiquidity(uint160,uint160,uint160,uint128) (runs: 65545, μ: 23047, ~: 23154) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmount0(uint160,uint160,uint256) (runs: 65545, μ: 20592, ~: 20845) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmount1(uint160,uint160,uint256) (runs: 65545, μ: 20176, ~: 20159) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmounts(uint160,uint160,uint160,uint256,uint256) (runs: 65545, μ: 22569, ~: 22643) +FullMathTest:testFuzz_MulDivQ128(uint256,uint256) (runs: 65536, μ: 4047, ~: 3965) +FullMathTest:testFuzz_MulDivQ96(uint256,uint256) (runs: 65536, μ: 4022, ~: 3950) +FullMathTest:testFuzz_MulDivUp_OZ(uint256,uint256,uint256) (runs: 65536, μ: 4943, ~: 4743) +FullMathTest:testFuzz_MulDivUp_Og(uint256,uint256,uint256) (runs: 65536, μ: 10030, ~: 9829) +FullMathTest:testFuzz_MulDiv_OZ(uint256,uint256,uint256) (runs: 65536, μ: 4208, ~: 4075) +FullMathTest:testFuzz_MulDiv_Og(uint256,uint256,uint256) (runs: 65536, μ: 9567, ~: 9435) +FullMathTest:testFuzz_Sqrt(uint256) (runs: 65536, μ: 4411, ~: 4411) +LiquidityAmountsTest:testFuzz_GetAmount0ForLiquidity(uint160,uint160,uint128) (runs: 65536, μ: 20520, ~: 20778) +LiquidityAmountsTest:testFuzz_GetAmount1ForLiquidity(uint160,uint160,uint128) (runs: 65536, μ: 20074, ~: 20016) +LiquidityAmountsTest:testFuzz_GetAmountsForLiquidity(uint160,uint160,uint160,uint128) (runs: 65536, μ: 23047, ~: 23154) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmount0(uint160,uint160,uint256) (runs: 65536, μ: 20592, ~: 20845) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmount1(uint160,uint160,uint256) (runs: 65536, μ: 20176, ~: 20159) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmounts(uint160,uint160,uint160,uint256,uint256) (runs: 65536, μ: 22569, ~: 22643) LiquidityAmountsTest:testGas_GetAmountsForLiquidity() (gas: 261590) LiquidityAmountsTest:testGas_GetAmountsForLiquidity_Og() (gas: 243788) LiquidityAmountsTest:testGas_GetLiquidityForAmounts() (gas: 264457) LiquidityAmountsTest:testGas_GetLiquidityForAmounts_Og() (gas: 280733) -LiquidityMathTest:testFuzz_AddDelta(uint128,int128) (runs: 65545, μ: 11955, ~: 14772) +LiquidityMathTest:testFuzz_AddDelta(uint128,int128) (runs: 65536, μ: 11956, ~: 14772) LiquidityMathTest:testGas_AddDelta() (gas: 139593) LiquidityMathTest:testGas_AddDelta_Og() (gas: 137264) LiquidityMathTest:testRevert_LA() (gas: 8858) LiquidityMathTest:testRevert_LS() (gas: 8846) -NPMCallerPCSTest:testFuzz_BalanceOf(address) (runs: 25, μ: 17700, ~: 17700) -NPMCallerPCSTest:testFuzz_GetApproved(uint256) (runs: 25, μ: 23214, ~: 23225) -NPMCallerPCSTest:testFuzz_IsApprovedForAll(uint256) (runs: 25, μ: 31315, ~: 19570) -NPMCallerPCSTest:testFuzz_OwnerOf(uint256) (runs: 25, μ: 24228, ~: 23905) -NPMCallerPCSTest:testFuzz_Positions(uint256) (runs: 25, μ: 31050, ~: 28027) -NPMCallerPCSTest:testFuzz_PositionsFull(uint256) (runs: 25, μ: 31379, ~: 28338) -NPMCallerPCSTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 25, μ: 74269, ~: 16548) +NPMCallerPCSTest:testFuzz_BalanceOf(address) (runs: 16, μ: 17700, ~: 17700) +NPMCallerPCSTest:testFuzz_GetApproved(uint256) (runs: 16, μ: 23231, ~: 23225) +NPMCallerPCSTest:testFuzz_IsApprovedForAll(uint256) (runs: 16, μ: 34270, ~: 19570) +NPMCallerPCSTest:testFuzz_OwnerOf(uint256) (runs: 16, μ: 24328, ~: 23905) +NPMCallerPCSTest:testFuzz_Positions(uint256) (runs: 16, μ: 31825, ~: 28027) +NPMCallerPCSTest:testFuzz_PositionsFull(uint256) (runs: 16, μ: 32159, ~: 28338) +NPMCallerPCSTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 16, μ: 105747, ~: 16548) NPMCallerPCSTest:testRevert_GetApproved() (gas: 13940) NPMCallerPCSTest:testRevert_OwnerOf() (gas: 14230) NPMCallerPCSTest:testRevert_Positions() (gas: 24310) @@ -47,13 +47,13 @@ NPMCallerPCSTest:test_Mint() (gas: 731515) NPMCallerPCSTest:test_Permit() (gas: 820018) NPMCallerPCSTest:test_SetApprovalForAll() (gas: 61346) NPMCallerPCSTest:test_TotalSupply() (gas: 16574) -NPMCallerTest:testFuzz_BalanceOf(address) (runs: 25, μ: 17726, ~: 17726) -NPMCallerTest:testFuzz_GetApproved(uint256) (runs: 25, μ: 23999, ~: 24213) -NPMCallerTest:testFuzz_IsApprovedForAll(uint256) (runs: 25, μ: 79097, ~: 79311) -NPMCallerTest:testFuzz_OwnerOf(uint256) (runs: 25, μ: 26302, ~: 26516) -NPMCallerTest:testFuzz_Positions(uint256) (runs: 25, μ: 43970, ~: 44184) -NPMCallerTest:testFuzz_PositionsFull(uint256) (runs: 25, μ: 44372, ~: 44586) -NPMCallerTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 25, μ: 68660, ~: 45699) +NPMCallerTest:testFuzz_BalanceOf(address) (runs: 16, μ: 17726, ~: 17726) +NPMCallerTest:testFuzz_GetApproved(uint256) (runs: 16, μ: 23922, ~: 24151) +NPMCallerTest:testFuzz_IsApprovedForAll(uint256) (runs: 16, μ: 79020, ~: 79249) +NPMCallerTest:testFuzz_OwnerOf(uint256) (runs: 16, μ: 26225, ~: 26454) +NPMCallerTest:testFuzz_Positions(uint256) (runs: 16, μ: 43893, ~: 44122) +NPMCallerTest:testFuzz_PositionsFull(uint256) (runs: 16, μ: 44295, ~: 44524) +NPMCallerTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 16, μ: 42937, ~: 42884) NPMCallerTest:testRevert_GetApproved() (gas: 13940) NPMCallerTest:testRevert_OwnerOf() (gas: 14207) NPMCallerTest:testRevert_Positions() (gas: 24310) @@ -68,26 +68,26 @@ NPMCallerTest:test_Mint() (gas: 607783) NPMCallerTest:test_Permit() (gas: 696349) NPMCallerTest:test_SetApprovalForAll() (gas: 61346) NPMCallerTest:test_TotalSupply() (gas: 16585) -PoolAddressPCSTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65545, μ: 20454, ~: 20459) -PoolAddressPCSTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65545, μ: 15645, ~: 15650) -PoolAddressPCSTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65545, μ: 22208, ~: 22213) -PoolAddressPCSTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65545, μ: 17374, ~: 17379) -PoolAddressPCSTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65545, μ: 15993, ~: 15998) +PoolAddressPCSTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65536, μ: 20454, ~: 20459) +PoolAddressPCSTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65536, μ: 15645, ~: 15650) +PoolAddressPCSTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65536, μ: 22208, ~: 22213) +PoolAddressPCSTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65536, μ: 17374, ~: 17379) +PoolAddressPCSTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65536, μ: 15993, ~: 15998) PoolAddressPCSTest:testGas_ComputeAddress() (gas: 12229) PoolAddressPCSTest:testGas_ComputeAddress_Og() (gas: 12774) -PoolAddressTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65545, μ: 20454, ~: 20459) -PoolAddressTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65545, μ: 15645, ~: 15650) -PoolAddressTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65545, μ: 22208, ~: 22213) -PoolAddressTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65545, μ: 17374, ~: 17379) -PoolAddressTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65545, μ: 15993, ~: 15998) +PoolAddressTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65536, μ: 20454, ~: 20459) +PoolAddressTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65536, μ: 15645, ~: 15650) +PoolAddressTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65536, μ: 22208, ~: 22213) +PoolAddressTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65536, μ: 17374, ~: 17379) +PoolAddressTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65536, μ: 15993, ~: 15998) PoolAddressTest:testGas_ComputeAddress() (gas: 12229) PoolAddressTest:testGas_ComputeAddress_Og() (gas: 12774) -PoolCallerPCSTest:testFuzz_LiquidityNet(int24) (runs: 25, μ: 22049, ~: 22049) -PoolCallerPCSTest:testFuzz_Observations(uint256) (runs: 10, μ: 278, ~: 278) -PoolCallerPCSTest:testFuzz_Positions(bytes32) (runs: 25, μ: 24545, ~: 24545) -PoolCallerPCSTest:testFuzz_Swap(bool,uint256,bytes) (runs: 25, μ: 364659, ~: 362861) -PoolCallerPCSTest:testFuzz_TickBitmap(int16) (runs: 25, μ: 14017, ~: 14017) -PoolCallerPCSTest:testFuzz_Ticks(int24) (runs: 25, μ: 27808, ~: 27808) +PoolCallerPCSTest:testFuzz_LiquidityNet(int24) (runs: 16, μ: 22049, ~: 22049) +PoolCallerPCSTest:testFuzz_Observations(uint256) (runs: 1, μ: 278, ~: 278) +PoolCallerPCSTest:testFuzz_Positions(bytes32) (runs: 16, μ: 24545, ~: 24545) +PoolCallerPCSTest:testFuzz_Swap(bool,uint256,bytes) (runs: 16, μ: 345228, ~: 339758) +PoolCallerPCSTest:testFuzz_TickBitmap(int16) (runs: 16, μ: 14017, ~: 14017) +PoolCallerPCSTest:testFuzz_Ticks(int24) (runs: 16, μ: 27808, ~: 27808) PoolCallerPCSTest:testRevert_AS_Swap() (gas: 35364) PoolCallerPCSTest:testRevert_SPL_Swap() (gas: 40085) PoolCallerPCSTest:test_Fee() (gas: 11396) @@ -100,12 +100,12 @@ PoolCallerPCSTest:test_Slot0() (gas: 20497) PoolCallerPCSTest:test_SqrtPriceX96AndTick() (gas: 16086) PoolCallerPCSTest:test_Swap() (gas: 476655) PoolCallerPCSTest:test_TickSpacing() (gas: 11445) -PoolCallerTest:testFuzz_LiquidityNet(int24) (runs: 25, μ: 22023, ~: 22023) -PoolCallerTest:testFuzz_Observations(uint256) (runs: 25, μ: 24399, ~: 24514) -PoolCallerTest:testFuzz_Positions(bytes32) (runs: 25, μ: 24560, ~: 24560) -PoolCallerTest:testFuzz_Swap(bool,uint256,bytes) (runs: 25, μ: 310430, ~: 284899) -PoolCallerTest:testFuzz_TickBitmap(int16) (runs: 25, μ: 14032, ~: 14032) -PoolCallerTest:testFuzz_Ticks(int24) (runs: 25, μ: 27782, ~: 27782) +PoolCallerTest:testFuzz_LiquidityNet(int24) (runs: 16, μ: 22023, ~: 22023) +PoolCallerTest:testFuzz_Observations(uint256) (runs: 16, μ: 24377, ~: 24514) +PoolCallerTest:testFuzz_Positions(bytes32) (runs: 16, μ: 24560, ~: 24560) +PoolCallerTest:testFuzz_Swap(bool,uint256,bytes) (runs: 16, μ: 294585, ~: 270377) +PoolCallerTest:testFuzz_TickBitmap(int16) (runs: 16, μ: 14032, ~: 14032) +PoolCallerTest:testFuzz_Ticks(int24) (runs: 16, μ: 27782, ~: 27782) PoolCallerTest:testRevert_AS_Swap() (gas: 35416) PoolCallerTest:testRevert_SPL_Swap() (gas: 38054) PoolCallerTest:test_Fee() (gas: 11396) @@ -123,20 +123,20 @@ SafeCastTest:testRevert_ToInt256() (gas: 3101) SafeCastTest:testRevert_ToUint128() (gas: 3121) SafeCastTest:testRevert_ToUint160() (gas: 3077) SafeCastTest:testToInt128() (gas: 3166) -SafeCastTest:testToInt128(int256) (runs: 65545, μ: 3328, ~: 3360) -SafeCastTest:testToInt128(uint256) (runs: 65545, μ: 3231, ~: 3257) +SafeCastTest:testToInt128(int256) (runs: 65536, μ: 3328, ~: 3360) +SafeCastTest:testToInt128(uint256) (runs: 65536, μ: 3231, ~: 3257) SafeCastTest:testToInt256() (gas: 3160) -SafeCastTest:testToInt256(uint256) (runs: 65545, μ: 3266, ~: 3270) -SafeCastTest:testToUint128(uint256) (runs: 65545, μ: 3229, ~: 3255) -SafeCastTest:testToUint160(uint256) (runs: 65545, μ: 3258, ~: 3278) -SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,int128) (runs: 65545, μ: 15642, ~: 15557) -SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,uint128,bool) (runs: 65545, μ: 15741, ~: 15660) -SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,int128) (runs: 65545, μ: 15239, ~: 15183) -SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,uint128,bool) (runs: 65545, μ: 15385, ~: 15432) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65545, μ: 19145, ~: 18742) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65545, μ: 21234, ~: 21304) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65545, μ: 15573, ~: 15506) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65545, μ: 15388, ~: 14913) +SafeCastTest:testToInt256(uint256) (runs: 65536, μ: 3266, ~: 3270) +SafeCastTest:testToUint128(uint256) (runs: 65536, μ: 3229, ~: 3255) +SafeCastTest:testToUint160(uint256) (runs: 65536, μ: 3258, ~: 3278) +SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,int128) (runs: 65536, μ: 15642, ~: 15557) +SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 15741, ~: 15660) +SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,int128) (runs: 65536, μ: 15239, ~: 15183) +SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 15385, ~: 15432) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65536, μ: 19145, ~: 18742) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65536, μ: 21234, ~: 21304) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15573, ~: 15506) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15388, ~: 14913) SqrtPriceMathTest:testGas_GetAmount0Delta() (gas: 322902) SqrtPriceMathTest:testGas_GetAmount0Delta_Og() (gas: 318258) SqrtPriceMathTest:testGas_GetAmount1Delta() (gas: 281567) @@ -145,37 +145,43 @@ SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput() (gas: 303816) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput_Og() (gas: 285701) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput() (gas: 286028) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput_Og() (gas: 269021) -SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65545, μ: 27058, ~: 27142) -SwapMathTest:testFuzz_ComputeSwapStepExactIn(uint160,uint160,uint128,uint256,uint24) (runs: 65545, μ: 29078, ~: 29027) -SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65545, μ: 28629, ~: 28529) +SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65536, μ: 27058, ~: 27142) +SwapMathTest:testFuzz_ComputeSwapStepExactIn(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 29078, ~: 29027) +SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 28629, ~: 28529) SwapMathTest:testGas_ComputeSwapStep() (gas: 515479) SwapMathTest:testGas_ComputeSwapStepExactIn() (gas: 534633) SwapMathTest:testGas_ComputeSwapStepExactIn_Og() (gas: 563266) SwapMathTest:testGas_ComputeSwapStepExactOut() (gas: 482455) SwapMathTest:testGas_ComputeSwapStepExactOut_Og() (gas: 557534) SwapMathTest:testGas_ComputeSwapStep_Og() (gas: 577793) -TickBitmapPCSTest:testFuzz_Compress(int24,int24) (runs: 65545, μ: 9447, ~: 9545) -TickBitmapPCSTest:testFuzz_FlipTick(int24) (runs: 65545, μ: 111941, ~: 112231) -TickBitmapPCSTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65545, μ: 112852, ~: 112809) -TickBitmapPCSTest:testFuzz_Position(int24) (runs: 65545, μ: 3856, ~: 3856) +TernaryLibTest:test_Abs() (gas: 3548) +TernaryLibTest:test_AbsDiff() (gas: 3501) +TernaryLibTest:test_AbsDiffU160() (gas: 3516) +TernaryLibTest:test_Max() (gas: 3490) +TernaryLibTest:test_Min() (gas: 3523) +TernaryLibTest:test_Ternary() (gas: 3523) +TickBitmapPCSTest:testFuzz_Compress(int24,int24) (runs: 65536, μ: 9447, ~: 9545) +TickBitmapPCSTest:testFuzz_FlipTick(int24) (runs: 65536, μ: 111941, ~: 112231) +TickBitmapPCSTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 112852, ~: 112809) +TickBitmapPCSTest:testFuzz_Position(int24) (runs: 65536, μ: 3856, ~: 3856) TickBitmapPCSTest:testGas_NextInitializedTickWithinOneWord() (gas: 12546976) TickBitmapPCSTest:testGas_NextInitializedTickWithinOneWord_Og() (gas: 12635559) TickBitmapPCSTest:test_NextInitializedTickWithinOneWord_GT() (gas: 108376) TickBitmapPCSTest:test_NextInitializedTickWithinOneWord_LTE() (gas: 108883) TickBitmapPCSTest:test_NextInitializedTick_GT() (gas: 119576) TickBitmapPCSTest:test_NextInitializedTick_LTE() (gas: 124247) -TickBitmapTest:testFuzz_Compress(int24,int24) (runs: 65545, μ: 9447, ~: 9545) -TickBitmapTest:testFuzz_FlipTick(int24) (runs: 65545, μ: 111942, ~: 112231) -TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65545, μ: 112853, ~: 112809) -TickBitmapTest:testFuzz_Position(int24) (runs: 65545, μ: 3856, ~: 3856) +TickBitmapTest:testFuzz_Compress(int24,int24) (runs: 65536, μ: 9447, ~: 9545) +TickBitmapTest:testFuzz_FlipTick(int24) (runs: 65536, μ: 111942, ~: 112231) +TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 112853, ~: 112809) +TickBitmapTest:testFuzz_Position(int24) (runs: 65536, μ: 3856, ~: 3856) TickBitmapTest:testGas_NextInitializedTickWithinOneWord() (gas: 12546976) TickBitmapTest:testGas_NextInitializedTickWithinOneWord_Og() (gas: 12635559) TickBitmapTest:test_NextInitializedTickWithinOneWord_GT() (gas: 286334) TickBitmapTest:test_NextInitializedTickWithinOneWord_LTE() (gas: 299879) TickBitmapTest:test_NextInitializedTick_GT() (gas: 491345) TickBitmapTest:test_NextInitializedTick_LTE() (gas: 610898) -TickMathTest:testFuzz_GetSqrtRatioAtTick(int24) (runs: 65545, μ: 18073, ~: 17908) -TickMathTest:testFuzz_GetTickAtSqrtRatio(uint160) (runs: 65545, μ: 16602, ~: 16618) +TickMathTest:testFuzz_GetSqrtRatioAtTick(int24) (runs: 65536, μ: 18073, ~: 17908) +TickMathTest:testFuzz_GetTickAtSqrtRatio(uint160) (runs: 65536, μ: 16602, ~: 16618) TickMathTest:testGas_GetSqrtRatioAtTick() (gas: 174214) TickMathTest:testGas_GetSqrtRatioAtTick_Og() (gas: 180351) TickMathTest:testGas_GetTickAtSqrtRatio() (gas: 284999) diff --git a/package.json b/package.json index 38b6f8a..0f84cea 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@aperture_finance/uni-v3-lib", "description": "A suite of Solidity libraries that have been imported and rewritten from Uniswap's v3-core and v3-periphery", - "version": "2.1.0", + "version": "2.1.1", "author": "Aperture Finance", "homepage": "https://aperture.finance/", "license": "GPL-2.0-or-later", diff --git a/test/NPMCaller.t.sol b/test/NPMCaller.t.sol index 67fdb85..20bb374 100644 --- a/test/NPMCaller.t.sol +++ b/test/NPMCaller.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol"; +import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol"; import {LiquidityAmounts} from "src/LiquidityAmounts.sol"; import "src/NPMCaller.sol"; import "./Base.t.sol"; diff --git a/test/PoolCaller.t.sol b/test/PoolCaller.t.sol index e42c3f7..f1fc314 100644 --- a/test/PoolCaller.t.sol +++ b/test/PoolCaller.t.sol @@ -1,8 +1,8 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -import "@pancakeswap/v3-core/contracts/interfaces/IPancakeV3Pool.sol"; -import "src/PoolCaller.sol"; +import {IPancakeV3Pool} from "@pancakeswap/v3-core/contracts/interfaces/IPancakeV3Pool.sol"; +import {PoolCaller, V3PoolCallee} from "src/PoolCaller.sol"; import "./Base.t.sol"; /// @dev Expose internal functions to test the PoolCaller library. diff --git a/test/SafeCast.t.sol b/test/SafeCast.t.sol index 12801c5..032d2c8 100644 --- a/test/SafeCast.t.sol +++ b/test/SafeCast.t.sol @@ -1,8 +1,8 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -import "forge-std/Test.sol"; -import "src/SafeCast.sol"; +import {Test} from "forge-std/Test.sol"; +import {SafeCast} from "src/SafeCast.sol"; contract SafeCastTest is Test { function testRevert_ToUint160() public { diff --git a/test/SqrtPriceMath.t.sol b/test/SqrtPriceMath.t.sol index a1f1fef..9500a98 100644 --- a/test/SqrtPriceMath.t.sol +++ b/test/SqrtPriceMath.t.sol @@ -2,7 +2,8 @@ pragma solidity ^0.8.0; import {ISqrtPriceMath} from "src/test/interfaces/ISqrtPriceMath.sol"; -import "src/SqrtPriceMath.sol"; +import {FullMath} from "src/FullMath.sol"; +import {SqrtPriceMath} from "src/SqrtPriceMath.sol"; import "./Base.t.sol"; contract SqrtPriceMathWrapper is ISqrtPriceMath { diff --git a/test/TickBitmap.t.sol b/test/TickBitmap.t.sol index 1cc7539..abaf0c4 100644 --- a/test/TickBitmap.t.sol +++ b/test/TickBitmap.t.sol @@ -2,7 +2,8 @@ pragma solidity ^0.8.0; import {ITickBitmap} from "src/test/interfaces/ITickBitmap.sol"; -import "src/TickBitmap.sol"; +import {V3PoolCallee} from "src/PoolCaller.sol"; +import {TickBitmap} from "src/TickBitmap.sol"; import "./Base.t.sol"; contract TickBitmapWrapper is ITickBitmap { @@ -45,7 +46,7 @@ contract TickBitmapTest is BaseTest { assembly { // signed arithmetic shift right wordPos := sar(8, tick) - bitPos := and(tick, 255) + bitPos := and(tick, 0xff) } assertEq(int256(wordPos), tick >> 8); assertEq(bitPos, uint8(int8(tick % 256))); @@ -93,7 +94,7 @@ contract TickBitmapTest is BaseTest { int24 tick; bool initialized; for (int16 wordPos = -128; wordPos < 128; ++wordPos) { - uint8 bitPos = uint8(pseudoRandom(uint16(wordPos)) & 255); + uint8 bitPos = uint8(pseudoRandom(uint16(wordPos)) & 0xff); assembly { tick := add(shl(8, wordPos), bitPos) } @@ -112,7 +113,7 @@ contract TickBitmapTest is BaseTest { int24 tick; bool initialized; for (int16 wordPos = -128; wordPos < 128; ++wordPos) { - uint8 bitPos = uint8(pseudoRandom(uint16(wordPos)) & 255); + uint8 bitPos = uint8(pseudoRandom(uint16(wordPos)) & 0xff); assembly { tick := add(shl(8, wordPos), bitPos) } diff --git a/test/TickMath.t.sol b/test/TickMath.t.sol index 17e5320..f619e48 100644 --- a/test/TickMath.t.sol +++ b/test/TickMath.t.sol @@ -1,9 +1,9 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; +import {Test} from "forge-std/Test.sol"; import {ITickMath} from "src/test/interfaces/ITickMath.sol"; import {TickMath} from "src/TickMath.sol"; -import "./Base.t.sol"; contract TickMathWrapper is ITickMath { function getSqrtRatioAtTick(int24 tick) external pure returns (uint160 sqrtPriceX96) { From 7b16736172e70b4590ad0ac054b70a12702b7134 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Wed, 8 May 2024 21:31:15 -0400 Subject: [PATCH 2/4] Optimize `SafeCast` --- .gas-snapshot | 44 ++++++++++++++++++++++---------------------- src/SafeCast.sol | 40 ++++++++++------------------------------ 2 files changed, 32 insertions(+), 52 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index f5a0295..9589702 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -13,15 +13,15 @@ FullMathTest:testFuzz_MulDiv_Og(uint256,uint256,uint256) (runs: 65536, μ: 9567, FullMathTest:testFuzz_Sqrt(uint256) (runs: 65536, μ: 4411, ~: 4411) LiquidityAmountsTest:testFuzz_GetAmount0ForLiquidity(uint160,uint160,uint128) (runs: 65536, μ: 20520, ~: 20778) LiquidityAmountsTest:testFuzz_GetAmount1ForLiquidity(uint160,uint160,uint128) (runs: 65536, μ: 20074, ~: 20016) -LiquidityAmountsTest:testFuzz_GetAmountsForLiquidity(uint160,uint160,uint160,uint128) (runs: 65536, μ: 23047, ~: 23154) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmount0(uint160,uint160,uint256) (runs: 65536, μ: 20592, ~: 20845) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmount1(uint160,uint160,uint256) (runs: 65536, μ: 20176, ~: 20159) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmounts(uint160,uint160,uint160,uint256,uint256) (runs: 65536, μ: 22569, ~: 22643) +LiquidityAmountsTest:testFuzz_GetAmountsForLiquidity(uint160,uint160,uint160,uint128) (runs: 65536, μ: 23046, ~: 23147) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmount0(uint160,uint160,uint256) (runs: 65536, μ: 20589, ~: 20842) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmount1(uint160,uint160,uint256) (runs: 65536, μ: 20172, ~: 20156) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmounts(uint160,uint160,uint160,uint256,uint256) (runs: 65536, μ: 22565, ~: 22640) LiquidityAmountsTest:testGas_GetAmountsForLiquidity() (gas: 261590) LiquidityAmountsTest:testGas_GetAmountsForLiquidity_Og() (gas: 243788) -LiquidityAmountsTest:testGas_GetLiquidityForAmounts() (gas: 264457) +LiquidityAmountsTest:testGas_GetLiquidityForAmounts() (gas: 263962) LiquidityAmountsTest:testGas_GetLiquidityForAmounts_Og() (gas: 280733) -LiquidityMathTest:testFuzz_AddDelta(uint128,int128) (runs: 65536, μ: 11956, ~: 14772) +LiquidityMathTest:testFuzz_AddDelta(uint128,int128) (runs: 65536, μ: 11955, ~: 14772) LiquidityMathTest:testGas_AddDelta() (gas: 139593) LiquidityMathTest:testGas_AddDelta_Og() (gas: 137264) LiquidityMathTest:testRevert_LA() (gas: 8858) @@ -118,37 +118,37 @@ PoolCallerTest:test_Slot0() (gas: 19717) PoolCallerTest:test_SqrtPriceX96AndTick() (gas: 13653) PoolCallerTest:test_Swap() (gas: 425347) PoolCallerTest:test_TickSpacing() (gas: 11445) -SafeCastTest:testRevert_ToInt128() (gas: 3099) +SafeCastTest:testRevert_ToInt128() (gas: 3096) SafeCastTest:testRevert_ToInt256() (gas: 3101) -SafeCastTest:testRevert_ToUint128() (gas: 3121) -SafeCastTest:testRevert_ToUint160() (gas: 3077) -SafeCastTest:testToInt128() (gas: 3166) -SafeCastTest:testToInt128(int256) (runs: 65536, μ: 3328, ~: 3360) -SafeCastTest:testToInt128(uint256) (runs: 65536, μ: 3231, ~: 3257) +SafeCastTest:testRevert_ToUint128() (gas: 3118) +SafeCastTest:testRevert_ToUint160() (gas: 3074) +SafeCastTest:testToInt128() (gas: 3163) +SafeCastTest:testToInt128(int256) (runs: 65536, μ: 3325, ~: 3357) +SafeCastTest:testToInt128(uint256) (runs: 65536, μ: 3228, ~: 3254) SafeCastTest:testToInt256() (gas: 3160) SafeCastTest:testToInt256(uint256) (runs: 65536, μ: 3266, ~: 3270) -SafeCastTest:testToUint128(uint256) (runs: 65536, μ: 3229, ~: 3255) -SafeCastTest:testToUint160(uint256) (runs: 65536, μ: 3258, ~: 3278) +SafeCastTest:testToUint128(uint256) (runs: 65536, μ: 3226, ~: 3252) +SafeCastTest:testToUint160(uint256) (runs: 65536, μ: 3255, ~: 3275) SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,int128) (runs: 65536, μ: 15642, ~: 15557) SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 15741, ~: 15660) SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,int128) (runs: 65536, μ: 15239, ~: 15183) SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 15385, ~: 15432) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65536, μ: 19145, ~: 18742) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65536, μ: 21234, ~: 21304) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15573, ~: 15506) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15388, ~: 14913) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65536, μ: 19146, ~: 18742) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65536, μ: 21233, ~: 21304) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15572, ~: 15506) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15389, ~: 14913) SqrtPriceMathTest:testGas_GetAmount0Delta() (gas: 322902) SqrtPriceMathTest:testGas_GetAmount0Delta_Og() (gas: 318258) SqrtPriceMathTest:testGas_GetAmount1Delta() (gas: 281567) SqrtPriceMathTest:testGas_GetAmount1Delta_Og() (gas: 306610) -SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput() (gas: 303816) +SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput() (gas: 303666) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput_Og() (gas: 285701) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput() (gas: 286028) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput_Og() (gas: 269021) -SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65536, μ: 27058, ~: 27142) +SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65536, μ: 27057, ~: 27142) SwapMathTest:testFuzz_ComputeSwapStepExactIn(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 29078, ~: 29027) -SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 28629, ~: 28529) -SwapMathTest:testGas_ComputeSwapStep() (gas: 515479) +SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 28628, ~: 28529) +SwapMathTest:testGas_ComputeSwapStep() (gas: 515419) SwapMathTest:testGas_ComputeSwapStepExactIn() (gas: 534633) SwapMathTest:testGas_ComputeSwapStepExactIn_Og() (gas: 563266) SwapMathTest:testGas_ComputeSwapStepExactOut() (gas: 482455) diff --git a/src/SafeCast.sol b/src/SafeCast.sol index c96a15a..a580936 100644 --- a/src/SafeCast.sol +++ b/src/SafeCast.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity >=0.8.4; +// SPDX-License-Identifier: MIT +pragma solidity >=0.5.0; /// @title Safe casting methods /// @author Aperture Finance @@ -10,39 +10,24 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint160 function toUint160(uint256 y) internal pure returns (uint160 z) { - /// @solidity memory-safe-assembly - assembly { - if shr(160, y) { - revert(0, 0) - } - z := y - } + if (y >= 1 << 160) revert(); + z = uint160(y); } /// @notice Cast a uint256 to a uint128, revert on overflow /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint128(uint256 y) internal pure returns (uint128 z) { - /// @solidity memory-safe-assembly - assembly { - if shr(128, y) { - revert(0, 0) - } - z := y - } + if (y >= 1 << 128) revert(); + z = uint128(y); } /// @notice Cast a int256 to a int128, revert on overflow or underflow /// @param y The int256 to be downcasted /// @return z The downcasted integer, now type int128 function toInt128(int256 y) internal pure returns (int128 z) { - /// @solidity memory-safe-assembly - assembly { - if sub(y, signextend(15, y)) { - revert(0, 0) - } - z := y - } + if (y != int128(y)) revert(); + z = int128(y); } /// @notice Cast a uint256 to a int256, revert on overflow @@ -62,12 +47,7 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type int128 function toInt128(uint256 y) internal pure returns (int128 z) { - /// @solidity memory-safe-assembly - assembly { - if shr(127, y) { - revert(0, 0) - } - z := y - } + if (y >= 1 << 127) revert(); + z = int128(int256(y)); } } From 846f7d497f77a7af62ae99166c1a81c5437c03dc Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Wed, 8 May 2024 21:40:49 -0400 Subject: [PATCH 3/4] Add tests for `TernaryLib` contract This commit includes new test cases for the TernaryLib contract. It tests all the contract's methods with typical and edge cases to ensure they function as expected. Key functionalities include absolute difference, minimum and maximum values, switch conditions, and numerical sorting. --- .gas-snapshot | 179 ++++++++++++++++++++++-------------------- test/TernaryLib.t.sol | 133 +++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 84 deletions(-) create mode 100644 test/TernaryLib.t.sol diff --git a/.gas-snapshot b/.gas-snapshot index 9589702..b18013b 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,38 +1,38 @@ -BitMathTest:testFuzz_LSB(uint256) (runs: 65536, μ: 17627, ~: 17625) -BitMathTest:testFuzz_MSB(uint256) (runs: 65536, μ: 17649, ~: 17643) +BitMathTest:testFuzz_LSB(uint256) (runs: 65537, μ: 17627, ~: 17625) +BitMathTest:testFuzz_MSB(uint256) (runs: 65537, μ: 17649, ~: 17643) BitMathTest:testGas_LSB() (gas: 239714) BitMathTest:testGas_LSB_Og() (gas: 305805) BitMathTest:testGas_MSB() (gas: 255269) BitMathTest:testGas_MSB_Og() (gas: 288889) -FullMathTest:testFuzz_MulDivQ128(uint256,uint256) (runs: 65536, μ: 4047, ~: 3965) -FullMathTest:testFuzz_MulDivQ96(uint256,uint256) (runs: 65536, μ: 4022, ~: 3950) -FullMathTest:testFuzz_MulDivUp_OZ(uint256,uint256,uint256) (runs: 65536, μ: 4943, ~: 4743) -FullMathTest:testFuzz_MulDivUp_Og(uint256,uint256,uint256) (runs: 65536, μ: 10030, ~: 9829) -FullMathTest:testFuzz_MulDiv_OZ(uint256,uint256,uint256) (runs: 65536, μ: 4208, ~: 4075) -FullMathTest:testFuzz_MulDiv_Og(uint256,uint256,uint256) (runs: 65536, μ: 9567, ~: 9435) -FullMathTest:testFuzz_Sqrt(uint256) (runs: 65536, μ: 4411, ~: 4411) -LiquidityAmountsTest:testFuzz_GetAmount0ForLiquidity(uint160,uint160,uint128) (runs: 65536, μ: 20520, ~: 20778) -LiquidityAmountsTest:testFuzz_GetAmount1ForLiquidity(uint160,uint160,uint128) (runs: 65536, μ: 20074, ~: 20016) -LiquidityAmountsTest:testFuzz_GetAmountsForLiquidity(uint160,uint160,uint160,uint128) (runs: 65536, μ: 23046, ~: 23147) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmount0(uint160,uint160,uint256) (runs: 65536, μ: 20589, ~: 20842) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmount1(uint160,uint160,uint256) (runs: 65536, μ: 20172, ~: 20156) -LiquidityAmountsTest:testFuzz_GetLiquidityForAmounts(uint160,uint160,uint160,uint256,uint256) (runs: 65536, μ: 22565, ~: 22640) +FullMathTest:testFuzz_MulDivQ128(uint256,uint256) (runs: 65537, μ: 4047, ~: 3965) +FullMathTest:testFuzz_MulDivQ96(uint256,uint256) (runs: 65537, μ: 4022, ~: 3950) +FullMathTest:testFuzz_MulDivUp_OZ(uint256,uint256,uint256) (runs: 65537, μ: 4943, ~: 4743) +FullMathTest:testFuzz_MulDivUp_Og(uint256,uint256,uint256) (runs: 65537, μ: 10030, ~: 9829) +FullMathTest:testFuzz_MulDiv_OZ(uint256,uint256,uint256) (runs: 65537, μ: 4208, ~: 4075) +FullMathTest:testFuzz_MulDiv_Og(uint256,uint256,uint256) (runs: 65537, μ: 9567, ~: 9435) +FullMathTest:testFuzz_Sqrt(uint256) (runs: 65537, μ: 4411, ~: 4411) +LiquidityAmountsTest:testFuzz_GetAmount0ForLiquidity(uint160,uint160,uint128) (runs: 65537, μ: 20520, ~: 20778) +LiquidityAmountsTest:testFuzz_GetAmount1ForLiquidity(uint160,uint160,uint128) (runs: 65537, μ: 20074, ~: 20016) +LiquidityAmountsTest:testFuzz_GetAmountsForLiquidity(uint160,uint160,uint160,uint128) (runs: 65537, μ: 23046, ~: 23147) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmount0(uint160,uint160,uint256) (runs: 65537, μ: 20589, ~: 20842) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmount1(uint160,uint160,uint256) (runs: 65537, μ: 20172, ~: 20156) +LiquidityAmountsTest:testFuzz_GetLiquidityForAmounts(uint160,uint160,uint160,uint256,uint256) (runs: 65537, μ: 22565, ~: 22640) LiquidityAmountsTest:testGas_GetAmountsForLiquidity() (gas: 261590) LiquidityAmountsTest:testGas_GetAmountsForLiquidity_Og() (gas: 243788) LiquidityAmountsTest:testGas_GetLiquidityForAmounts() (gas: 263962) LiquidityAmountsTest:testGas_GetLiquidityForAmounts_Og() (gas: 280733) -LiquidityMathTest:testFuzz_AddDelta(uint128,int128) (runs: 65536, μ: 11955, ~: 14772) +LiquidityMathTest:testFuzz_AddDelta(uint128,int128) (runs: 65537, μ: 11956, ~: 14772) LiquidityMathTest:testGas_AddDelta() (gas: 139593) LiquidityMathTest:testGas_AddDelta_Og() (gas: 137264) LiquidityMathTest:testRevert_LA() (gas: 8858) LiquidityMathTest:testRevert_LS() (gas: 8846) -NPMCallerPCSTest:testFuzz_BalanceOf(address) (runs: 16, μ: 17700, ~: 17700) -NPMCallerPCSTest:testFuzz_GetApproved(uint256) (runs: 16, μ: 23231, ~: 23225) -NPMCallerPCSTest:testFuzz_IsApprovedForAll(uint256) (runs: 16, μ: 34270, ~: 19570) -NPMCallerPCSTest:testFuzz_OwnerOf(uint256) (runs: 16, μ: 24328, ~: 23905) -NPMCallerPCSTest:testFuzz_Positions(uint256) (runs: 16, μ: 31825, ~: 28027) -NPMCallerPCSTest:testFuzz_PositionsFull(uint256) (runs: 16, μ: 32159, ~: 28338) -NPMCallerPCSTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 16, μ: 105747, ~: 16548) +NPMCallerPCSTest:testFuzz_BalanceOf(address) (runs: 17, μ: 17700, ~: 17700) +NPMCallerPCSTest:testFuzz_GetApproved(uint256) (runs: 17, μ: 23267, ~: 23225) +NPMCallerPCSTest:testFuzz_IsApprovedForAll(uint256) (runs: 17, μ: 36899, ~: 19570) +NPMCallerPCSTest:testFuzz_OwnerOf(uint256) (runs: 17, μ: 24437, ~: 23905) +NPMCallerPCSTest:testFuzz_Positions(uint256) (runs: 17, μ: 32530, ~: 28027) +NPMCallerPCSTest:testFuzz_PositionsFull(uint256) (runs: 17, μ: 32868, ~: 28338) +NPMCallerPCSTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 17, μ: 101861, ~: 16548) NPMCallerPCSTest:testRevert_GetApproved() (gas: 13940) NPMCallerPCSTest:testRevert_OwnerOf() (gas: 14230) NPMCallerPCSTest:testRevert_Positions() (gas: 24310) @@ -47,13 +47,13 @@ NPMCallerPCSTest:test_Mint() (gas: 731515) NPMCallerPCSTest:test_Permit() (gas: 820018) NPMCallerPCSTest:test_SetApprovalForAll() (gas: 61346) NPMCallerPCSTest:test_TotalSupply() (gas: 16574) -NPMCallerTest:testFuzz_BalanceOf(address) (runs: 16, μ: 17726, ~: 17726) -NPMCallerTest:testFuzz_GetApproved(uint256) (runs: 16, μ: 23922, ~: 24151) -NPMCallerTest:testFuzz_IsApprovedForAll(uint256) (runs: 16, μ: 79020, ~: 79249) -NPMCallerTest:testFuzz_OwnerOf(uint256) (runs: 16, μ: 26225, ~: 26454) -NPMCallerTest:testFuzz_Positions(uint256) (runs: 16, μ: 43893, ~: 44122) -NPMCallerTest:testFuzz_PositionsFull(uint256) (runs: 16, μ: 44295, ~: 44524) -NPMCallerTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 16, μ: 42937, ~: 42884) +NPMCallerTest:testFuzz_BalanceOf(address) (runs: 17, μ: 17726, ~: 17726) +NPMCallerTest:testFuzz_GetApproved(uint256) (runs: 17, μ: 23915, ~: 24089) +NPMCallerTest:testFuzz_IsApprovedForAll(uint256) (runs: 17, μ: 79013, ~: 79187) +NPMCallerTest:testFuzz_OwnerOf(uint256) (runs: 17, μ: 26218, ~: 26392) +NPMCallerTest:testFuzz_Positions(uint256) (runs: 17, μ: 43886, ~: 44060) +NPMCallerTest:testFuzz_PositionsFull(uint256) (runs: 17, μ: 44288, ~: 44462) +NPMCallerTest:testFuzz_TokenOfOwnerByIndex(uint256) (runs: 17, μ: 43488, ~: 45699) NPMCallerTest:testRevert_GetApproved() (gas: 13940) NPMCallerTest:testRevert_OwnerOf() (gas: 14207) NPMCallerTest:testRevert_Positions() (gas: 24310) @@ -68,26 +68,26 @@ NPMCallerTest:test_Mint() (gas: 607783) NPMCallerTest:test_Permit() (gas: 696349) NPMCallerTest:test_SetApprovalForAll() (gas: 61346) NPMCallerTest:test_TotalSupply() (gas: 16585) -PoolAddressPCSTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65536, μ: 20454, ~: 20459) -PoolAddressPCSTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65536, μ: 15645, ~: 15650) -PoolAddressPCSTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65536, μ: 22208, ~: 22213) -PoolAddressPCSTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65536, μ: 17374, ~: 17379) -PoolAddressPCSTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65536, μ: 15993, ~: 15998) +PoolAddressPCSTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65537, μ: 20454, ~: 20459) +PoolAddressPCSTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65537, μ: 15645, ~: 15650) +PoolAddressPCSTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65537, μ: 22208, ~: 22213) +PoolAddressPCSTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65537, μ: 17374, ~: 17379) +PoolAddressPCSTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65537, μ: 15993, ~: 15998) PoolAddressPCSTest:testGas_ComputeAddress() (gas: 12229) PoolAddressPCSTest:testGas_ComputeAddress_Og() (gas: 12774) -PoolAddressTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65536, μ: 20454, ~: 20459) -PoolAddressTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65536, μ: 15645, ~: 15650) -PoolAddressTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65536, μ: 22208, ~: 22213) -PoolAddressTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65536, μ: 17374, ~: 17379) -PoolAddressTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65536, μ: 15993, ~: 15998) +PoolAddressTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65537, μ: 20454, ~: 20459) +PoolAddressTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65537, μ: 15645, ~: 15650) +PoolAddressTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65537, μ: 22208, ~: 22213) +PoolAddressTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65537, μ: 17374, ~: 17379) +PoolAddressTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65537, μ: 15993, ~: 15998) PoolAddressTest:testGas_ComputeAddress() (gas: 12229) PoolAddressTest:testGas_ComputeAddress_Og() (gas: 12774) -PoolCallerPCSTest:testFuzz_LiquidityNet(int24) (runs: 16, μ: 22049, ~: 22049) -PoolCallerPCSTest:testFuzz_Observations(uint256) (runs: 1, μ: 278, ~: 278) -PoolCallerPCSTest:testFuzz_Positions(bytes32) (runs: 16, μ: 24545, ~: 24545) -PoolCallerPCSTest:testFuzz_Swap(bool,uint256,bytes) (runs: 16, μ: 345228, ~: 339758) -PoolCallerPCSTest:testFuzz_TickBitmap(int16) (runs: 16, μ: 14017, ~: 14017) -PoolCallerPCSTest:testFuzz_Ticks(int24) (runs: 16, μ: 27808, ~: 27808) +PoolCallerPCSTest:testFuzz_LiquidityNet(int24) (runs: 17, μ: 22049, ~: 22049) +PoolCallerPCSTest:testFuzz_Observations(uint256) (runs: 2, μ: 278, ~: 278) +PoolCallerPCSTest:testFuzz_Positions(bytes32) (runs: 17, μ: 24545, ~: 24545) +PoolCallerPCSTest:testFuzz_Swap(bool,uint256,bytes) (runs: 17, μ: 353078, ~: 362861) +PoolCallerPCSTest:testFuzz_TickBitmap(int16) (runs: 17, μ: 14017, ~: 14017) +PoolCallerPCSTest:testFuzz_Ticks(int24) (runs: 17, μ: 27808, ~: 27808) PoolCallerPCSTest:testRevert_AS_Swap() (gas: 35364) PoolCallerPCSTest:testRevert_SPL_Swap() (gas: 40085) PoolCallerPCSTest:test_Fee() (gas: 11396) @@ -100,12 +100,12 @@ PoolCallerPCSTest:test_Slot0() (gas: 20497) PoolCallerPCSTest:test_SqrtPriceX96AndTick() (gas: 16086) PoolCallerPCSTest:test_Swap() (gas: 476655) PoolCallerPCSTest:test_TickSpacing() (gas: 11445) -PoolCallerTest:testFuzz_LiquidityNet(int24) (runs: 16, μ: 22023, ~: 22023) -PoolCallerTest:testFuzz_Observations(uint256) (runs: 16, μ: 24377, ~: 24514) -PoolCallerTest:testFuzz_Positions(bytes32) (runs: 16, μ: 24560, ~: 24560) -PoolCallerTest:testFuzz_Swap(bool,uint256,bytes) (runs: 16, μ: 294585, ~: 270377) -PoolCallerTest:testFuzz_TickBitmap(int16) (runs: 16, μ: 14032, ~: 14032) -PoolCallerTest:testFuzz_Ticks(int24) (runs: 16, μ: 27782, ~: 27782) +PoolCallerTest:testFuzz_LiquidityNet(int24) (runs: 17, μ: 22023, ~: 22023) +PoolCallerTest:testFuzz_Observations(uint256) (runs: 17, μ: 24345, ~: 24514) +PoolCallerTest:testFuzz_Positions(bytes32) (runs: 17, μ: 24560, ~: 24560) +PoolCallerTest:testFuzz_Swap(bool,uint256,bytes) (runs: 17, μ: 307955, ~: 284899) +PoolCallerTest:testFuzz_TickBitmap(int16) (runs: 17, μ: 14032, ~: 14032) +PoolCallerTest:testFuzz_Ticks(int24) (runs: 17, μ: 27782, ~: 27782) PoolCallerTest:testRevert_AS_Swap() (gas: 35416) PoolCallerTest:testRevert_SPL_Swap() (gas: 38054) PoolCallerTest:test_Fee() (gas: 11396) @@ -123,20 +123,20 @@ SafeCastTest:testRevert_ToInt256() (gas: 3101) SafeCastTest:testRevert_ToUint128() (gas: 3118) SafeCastTest:testRevert_ToUint160() (gas: 3074) SafeCastTest:testToInt128() (gas: 3163) -SafeCastTest:testToInt128(int256) (runs: 65536, μ: 3325, ~: 3357) -SafeCastTest:testToInt128(uint256) (runs: 65536, μ: 3228, ~: 3254) +SafeCastTest:testToInt128(int256) (runs: 65537, μ: 3325, ~: 3357) +SafeCastTest:testToInt128(uint256) (runs: 65537, μ: 3228, ~: 3254) SafeCastTest:testToInt256() (gas: 3160) -SafeCastTest:testToInt256(uint256) (runs: 65536, μ: 3266, ~: 3270) -SafeCastTest:testToUint128(uint256) (runs: 65536, μ: 3226, ~: 3252) -SafeCastTest:testToUint160(uint256) (runs: 65536, μ: 3255, ~: 3275) -SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,int128) (runs: 65536, μ: 15642, ~: 15557) -SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 15741, ~: 15660) -SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,int128) (runs: 65536, μ: 15239, ~: 15183) -SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 15385, ~: 15432) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65536, μ: 19146, ~: 18742) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65536, μ: 21233, ~: 21304) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15572, ~: 15506) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65536, μ: 15389, ~: 14913) +SafeCastTest:testToInt256(uint256) (runs: 65537, μ: 3266, ~: 3270) +SafeCastTest:testToUint128(uint256) (runs: 65537, μ: 3226, ~: 3252) +SafeCastTest:testToUint160(uint256) (runs: 65537, μ: 3255, ~: 3275) +SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,int128) (runs: 65537, μ: 15642, ~: 15557) +SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,uint128,bool) (runs: 65537, μ: 15741, ~: 15660) +SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,int128) (runs: 65537, μ: 15239, ~: 15183) +SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,uint128,bool) (runs: 65537, μ: 15385, ~: 15432) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65537, μ: 19146, ~: 18742) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65537, μ: 21233, ~: 21304) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65537, μ: 15572, ~: 15506) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65537, μ: 15389, ~: 14913) SqrtPriceMathTest:testGas_GetAmount0Delta() (gas: 322902) SqrtPriceMathTest:testGas_GetAmount0Delta_Og() (gas: 318258) SqrtPriceMathTest:testGas_GetAmount1Delta() (gas: 281567) @@ -145,43 +145,54 @@ SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput() (gas: 303666) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput_Og() (gas: 285701) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput() (gas: 286028) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput_Og() (gas: 269021) -SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65536, μ: 27057, ~: 27142) -SwapMathTest:testFuzz_ComputeSwapStepExactIn(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 29078, ~: 29027) -SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 28628, ~: 28529) +SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65537, μ: 27057, ~: 27142) +SwapMathTest:testFuzz_ComputeSwapStepExactIn(uint160,uint160,uint128,uint256,uint24) (runs: 65537, μ: 29078, ~: 29027) +SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65537, μ: 28628, ~: 28529) SwapMathTest:testGas_ComputeSwapStep() (gas: 515419) SwapMathTest:testGas_ComputeSwapStepExactIn() (gas: 534633) SwapMathTest:testGas_ComputeSwapStepExactIn_Og() (gas: 563266) SwapMathTest:testGas_ComputeSwapStepExactOut() (gas: 482455) SwapMathTest:testGas_ComputeSwapStepExactOut_Og() (gas: 557534) SwapMathTest:testGas_ComputeSwapStep_Og() (gas: 577793) -TernaryLibTest:test_Abs() (gas: 3548) -TernaryLibTest:test_AbsDiff() (gas: 3501) -TernaryLibTest:test_AbsDiffU160() (gas: 3516) -TernaryLibTest:test_Max() (gas: 3490) -TernaryLibTest:test_Min() (gas: 3523) -TernaryLibTest:test_Ternary() (gas: 3523) -TickBitmapPCSTest:testFuzz_Compress(int24,int24) (runs: 65536, μ: 9447, ~: 9545) -TickBitmapPCSTest:testFuzz_FlipTick(int24) (runs: 65536, μ: 111941, ~: 112231) -TickBitmapPCSTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 112852, ~: 112809) -TickBitmapPCSTest:testFuzz_Position(int24) (runs: 65536, μ: 3856, ~: 3856) +TernaryLibTest:testFuzz_Abs(int256) (runs: 65536, μ: 3626, ~: 3611) +TernaryLibTest:testFuzz_AbsDiff(uint256,uint256) (runs: 65537, μ: 3376, ~: 3377) +TernaryLibTest:testFuzz_AbsDiffU160(uint160,uint160) (runs: 65537, μ: 3511, ~: 3512) +TernaryLibTest:testFuzz_Max(uint256,uint256) (runs: 65537, μ: 3268, ~: 3269) +TernaryLibTest:testFuzz_Min(uint256,uint256) (runs: 65537, μ: 3258, ~: 3258) +TernaryLibTest:testFuzz_Sort2(uint256,uint256) (runs: 65537, μ: 3659, ~: 3665) +TernaryLibTest:testFuzz_SwitchIf(bool,uint256,uint256) (runs: 65537, μ: 3781, ~: 3776) +TernaryLibTest:testFuzz_Ternary(bool,address,address) (runs: 65537, μ: 3489, ~: 3489) +TernaryLibTest:testFuzz_Ternary(bool,uint256,uint256) (runs: 65537, μ: 3377, ~: 3378) +TernaryLibTest:test_Abs() (gas: 3963) +TernaryLibTest:test_AbsDiff() (gas: 3547) +TernaryLibTest:test_AbsDiffU160() (gas: 3585) +TernaryLibTest:test_Max() (gas: 3492) +TernaryLibTest:test_Min() (gas: 3569) +TernaryLibTest:test_Sort2() (gas: 4346) +TernaryLibTest:test_SwitchIf() (gas: 3491) +TernaryLibTest:test_Ternary() (gas: 3547) +TickBitmapPCSTest:testFuzz_Compress(int24,int24) (runs: 65537, μ: 9447, ~: 9545) +TickBitmapPCSTest:testFuzz_FlipTick(int24) (runs: 65537, μ: 111941, ~: 112231) +TickBitmapPCSTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65537, μ: 112852, ~: 112809) +TickBitmapPCSTest:testFuzz_Position(int24) (runs: 65537, μ: 3856, ~: 3856) TickBitmapPCSTest:testGas_NextInitializedTickWithinOneWord() (gas: 12546976) TickBitmapPCSTest:testGas_NextInitializedTickWithinOneWord_Og() (gas: 12635559) TickBitmapPCSTest:test_NextInitializedTickWithinOneWord_GT() (gas: 108376) TickBitmapPCSTest:test_NextInitializedTickWithinOneWord_LTE() (gas: 108883) TickBitmapPCSTest:test_NextInitializedTick_GT() (gas: 119576) TickBitmapPCSTest:test_NextInitializedTick_LTE() (gas: 124247) -TickBitmapTest:testFuzz_Compress(int24,int24) (runs: 65536, μ: 9447, ~: 9545) -TickBitmapTest:testFuzz_FlipTick(int24) (runs: 65536, μ: 111942, ~: 112231) -TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 112853, ~: 112809) -TickBitmapTest:testFuzz_Position(int24) (runs: 65536, μ: 3856, ~: 3856) +TickBitmapTest:testFuzz_Compress(int24,int24) (runs: 65537, μ: 9447, ~: 9545) +TickBitmapTest:testFuzz_FlipTick(int24) (runs: 65537, μ: 111942, ~: 112231) +TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65537, μ: 112853, ~: 112809) +TickBitmapTest:testFuzz_Position(int24) (runs: 65537, μ: 3856, ~: 3856) TickBitmapTest:testGas_NextInitializedTickWithinOneWord() (gas: 12546976) TickBitmapTest:testGas_NextInitializedTickWithinOneWord_Og() (gas: 12635559) TickBitmapTest:test_NextInitializedTickWithinOneWord_GT() (gas: 286334) TickBitmapTest:test_NextInitializedTickWithinOneWord_LTE() (gas: 299879) TickBitmapTest:test_NextInitializedTick_GT() (gas: 491345) TickBitmapTest:test_NextInitializedTick_LTE() (gas: 610898) -TickMathTest:testFuzz_GetSqrtRatioAtTick(int24) (runs: 65536, μ: 18073, ~: 17908) -TickMathTest:testFuzz_GetTickAtSqrtRatio(uint160) (runs: 65536, μ: 16602, ~: 16618) +TickMathTest:testFuzz_GetSqrtRatioAtTick(int24) (runs: 65537, μ: 18073, ~: 17908) +TickMathTest:testFuzz_GetTickAtSqrtRatio(uint160) (runs: 65537, μ: 16602, ~: 16618) TickMathTest:testGas_GetSqrtRatioAtTick() (gas: 174214) TickMathTest:testGas_GetSqrtRatioAtTick_Og() (gas: 180351) TickMathTest:testGas_GetTickAtSqrtRatio() (gas: 284999) diff --git a/test/TernaryLib.t.sol b/test/TernaryLib.t.sol new file mode 100644 index 0000000..e7d2972 --- /dev/null +++ b/test/TernaryLib.t.sol @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import {Test} from "forge-std/Test.sol"; +import {TernaryLib} from "src/TernaryLib.sol"; + +/// @dev Test contract for TernaryLib +contract TernaryLibTest is Test { + function test_Ternary() public pure { + assertEq(TernaryLib.ternary(true, 1, 2), 1); + assertEq(TernaryLib.ternary(false, 1, 2), 2); + } + + function testFuzz_Ternary(bool condition, uint256 a, uint256 b) public pure { + if (condition) { + assertEq(TernaryLib.ternary(condition, a, b), a); + } else { + assertEq(TernaryLib.ternary(condition, a, b), b); + } + } + + function testFuzz_Ternary(bool condition, address a, address b) public pure { + if (condition) { + assertEq(TernaryLib.ternary(condition, a, b), a); + } else { + assertEq(TernaryLib.ternary(condition, a, b), b); + } + } + + function test_Abs() public pure { + assertEq(TernaryLib.abs(1), 1); + assertEq(TernaryLib.abs(-1), 1); + assertEq(TernaryLib.abs(type(int256).min), 1 << 255); + } + + function testFuzz_Abs(int256 x) public pure { + vm.assume(x != type(int256).min); + if (x >= 0) { + assertEq(TernaryLib.abs(x), uint256(x)); + } else { + assertEq(TernaryLib.abs(x), uint256(-x)); + } + } + + function test_AbsDiff() public pure { + assertEq(TernaryLib.absDiff(1, 2), 1); + assertEq(TernaryLib.absDiff(2, 1), 1); + } + + function testFuzz_AbsDiff(uint256 a, uint256 b) public pure { + if (a > b) { + assertEq(TernaryLib.absDiff(a, b), a - b); + } else { + assertEq(TernaryLib.absDiff(a, b), b - a); + } + } + + function test_AbsDiffU160() public pure { + assertEq(TernaryLib.absDiffU160(1, 2), 1); + assertEq(TernaryLib.absDiffU160(2, 1), 1); + } + + function testFuzz_AbsDiffU160(uint160 a, uint160 b) public pure { + if (a > b) { + assertEq(TernaryLib.absDiffU160(a, b), a - b); + } else { + assertEq(TernaryLib.absDiffU160(a, b), b - a); + } + } + + function test_Min() public pure { + assertEq(TernaryLib.min(1, 2), 1); + assertEq(TernaryLib.min(2, 1), 1); + } + + function testFuzz_Min(uint256 a, uint256 b) public pure { + if (a < b) { + assertEq(TernaryLib.min(a, b), a); + } else { + assertEq(TernaryLib.min(a, b), b); + } + } + + function test_Max() public pure { + assertEq(TernaryLib.max(1, 2), 2); + assertEq(TernaryLib.max(2, 1), 2); + } + + function testFuzz_Max(uint256 a, uint256 b) public pure { + if (a > b) { + assertEq(TernaryLib.max(a, b), a); + } else { + assertEq(TernaryLib.max(a, b), b); + } + } + + function test_SwitchIf() public pure { + (uint256 a, uint256 b) = TernaryLib.switchIf(true, 1, 2); + assertEq(a, 2); + assertEq(b, 1); + } + + function testFuzz_SwitchIf(bool condition, uint256 a, uint256 b) public pure { + (uint256 x, uint256 y) = TernaryLib.switchIf(condition, a, b); + if (condition) { + assertEq(x, b); + assertEq(y, a); + } else { + assertEq(x, a); + assertEq(y, b); + } + } + + function test_Sort2() public pure { + (uint256 a, uint256 b) = TernaryLib.sort2(1, 2); + assertEq(a, 1); + assertEq(b, 2); + (a, b) = TernaryLib.sort2(2, 1); + assertEq(a, 1); + assertEq(b, 2); + } + + function testFuzz_Sort2(uint256 a, uint256 b) public pure { + (uint256 x, uint256 y) = TernaryLib.sort2(a, b); + if (a < b) { + assertEq(x, a); + assertEq(y, b); + } else { + assertEq(x, b); + assertEq(y, a); + } + } +} From f01a9c2178eb7df2e87e45ad3ad68b38ce11bc45 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Sun, 12 May 2024 02:35:49 -0400 Subject: [PATCH 4/4] Refactor SafeCast library for cleaner and more efficient code Changed variable naming convention, adjusted downcasting functions, and simplified type casting processes. Removed excess variables and optimized the functions by using direct returns. This improves the performance and maintainability of the library. --- .gas-snapshot | 8 +++---- src/SafeCast.sol | 57 +++++++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index b18013b..9920b60 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -119,14 +119,14 @@ PoolCallerTest:test_SqrtPriceX96AndTick() (gas: 13653) PoolCallerTest:test_Swap() (gas: 425347) PoolCallerTest:test_TickSpacing() (gas: 11445) SafeCastTest:testRevert_ToInt128() (gas: 3096) -SafeCastTest:testRevert_ToInt256() (gas: 3101) +SafeCastTest:testRevert_ToInt256() (gas: 3099) SafeCastTest:testRevert_ToUint128() (gas: 3118) SafeCastTest:testRevert_ToUint160() (gas: 3074) SafeCastTest:testToInt128() (gas: 3163) -SafeCastTest:testToInt128(int256) (runs: 65537, μ: 3325, ~: 3357) +SafeCastTest:testToInt128(int256) (runs: 65537, μ: 3322, ~: 3354) SafeCastTest:testToInt128(uint256) (runs: 65537, μ: 3228, ~: 3254) -SafeCastTest:testToInt256() (gas: 3160) -SafeCastTest:testToInt256(uint256) (runs: 65537, μ: 3266, ~: 3270) +SafeCastTest:testToInt256() (gas: 3156) +SafeCastTest:testToInt256(uint256) (runs: 65537, μ: 3262, ~: 3266) SafeCastTest:testToUint128(uint256) (runs: 65537, μ: 3226, ~: 3252) SafeCastTest:testToUint160(uint256) (runs: 65537, μ: 3255, ~: 3275) SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,int128) (runs: 65537, μ: 15642, ~: 15557) diff --git a/src/SafeCast.sol b/src/SafeCast.sol index a580936..9ecb5ab 100644 --- a/src/SafeCast.sol +++ b/src/SafeCast.sol @@ -7,47 +7,44 @@ pragma solidity >=0.5.0; /// @notice Contains methods for safely casting between types library SafeCast { /// @notice Cast a uint256 to a uint160, revert on overflow - /// @param y The uint256 to be downcasted - /// @return z The downcasted integer, now type uint160 - function toUint160(uint256 y) internal pure returns (uint160 z) { - if (y >= 1 << 160) revert(); - z = uint160(y); + /// @param x The uint256 to be downcasted + /// @return The downcasted integer, now type uint160 + function toUint160(uint256 x) internal pure returns (uint160) { + if (x >= 1 << 160) revert(); + return uint160(x); } /// @notice Cast a uint256 to a uint128, revert on overflow - /// @param y The uint256 to be downcasted - /// @return z The downcasted integer, now type uint128 - function toUint128(uint256 y) internal pure returns (uint128 z) { - if (y >= 1 << 128) revert(); - z = uint128(y); + /// @param x The uint256 to be downcasted + /// @return The downcasted integer, now type uint128 + function toUint128(uint256 x) internal pure returns (uint128) { + if (x >= 1 << 128) revert(); + return uint128(x); } /// @notice Cast a int256 to a int128, revert on overflow or underflow - /// @param y The int256 to be downcasted - /// @return z The downcasted integer, now type int128 - function toInt128(int256 y) internal pure returns (int128 z) { - if (y != int128(y)) revert(); - z = int128(y); + /// @param x The int256 to be downcasted + /// @return The downcasted integer, now type int128 + function toInt128(int256 x) internal pure returns (int128) { + unchecked { + if (((1 << 127) + uint256(x)) >> 128 == uint256(0)) return int128(x); + revert(); + } } /// @notice Cast a uint256 to a int256, revert on overflow - /// @param y The uint256 to be casted - /// @return z The casted integer, now type int256 - function toInt256(uint256 y) internal pure returns (int256 z) { - /// @solidity memory-safe-assembly - assembly { - if slt(y, 0) { - revert(0, 0) - } - z := y - } + /// @param x The uint256 to be casted + /// @return The casted integer, now type int256 + function toInt256(uint256 x) internal pure returns (int256) { + if (int256(x) >= 0) return int256(x); + revert(); } /// @notice Cast a uint256 to a int128, revert on overflow - /// @param y The uint256 to be downcasted - /// @return z The downcasted integer, now type int128 - function toInt128(uint256 y) internal pure returns (int128 z) { - if (y >= 1 << 127) revert(); - z = int128(int256(y)); + /// @param x The uint256 to be downcasted + /// @return The downcasted integer, now type int128 + function toInt128(uint256 x) internal pure returns (int128) { + if (x >= 1 << 127) revert(); + return int128(int256(x)); } }