From 07985799c939f7503397fe6f163f15f1a44b40c7 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Sun, 31 Dec 2023 03:00:26 -0800 Subject: [PATCH 1/2] Update dependencies and optimize `computeSwapStep` The commit includes an update to the package dependencies with new versions. It also contains a correction in the SwapMath.sol file where the process to get the absolute value of `amountRemaining` has been simplified. There is also a small modification in package.json changing the version. --- .gas-snapshot | 34 ++++---- package.json | 12 +-- src/SwapMath.sol | 15 ++-- yarn.lock | 201 +++++++++++++++++++++++------------------------ 4 files changed, 127 insertions(+), 135 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index aefb657..2008702 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -37,26 +37,26 @@ NPMCallerTest:testRevert_OwnerOf() (gas: 14407) NPMCallerTest:testRevert_Positions() (gas: 24241) NPMCallerTest:test_Approve() (gas: 73011) NPMCallerTest:test_Burn() (gas: 611858) -NPMCallerTest:test_Collect():(uint256) (gas: 599278) -NPMCallerTest:test_DecreaseLiquidity():(uint256) (gas: 564896) +NPMCallerTest:test_Collect() (gas: 599278) +NPMCallerTest:test_DecreaseLiquidity() (gas: 564896) NPMCallerTest:test_Factory() (gas: 10194) NPMCallerTest:test_IncreaseLiquidity() (gas: 840075) NPMCallerTest:test_IsApprovedForAll() (gas: 21253) -NPMCallerTest:test_Mint():(uint256) (gas: 483283) +NPMCallerTest:test_Mint() (gas: 483283) NPMCallerTest:test_Permit() (gas: 534552) NPMCallerTest:test_SetApprovalForAll() (gas: 34758) NPMCallerTest:test_TotalSupply() (gas: 11953) PoolAddressTest:testFuzz_ComputeAddress(address,address,uint24) (runs: 65536, μ: 12175, ~: 12161) PoolAddressTest:testFuzz_ComputeAddressCalldata(address,address,uint24) (runs: 65536, μ: 10260, ~: 10247) PoolAddressTest:testFuzz_ComputeAddressFromKey(address,address,uint24) (runs: 65536, μ: 12465, ~: 12451) -PoolAddressTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65536, μ: 11510, ~: 11497) -PoolAddressTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65536, μ: 10752, ~: 10739) +PoolAddressTest:testFuzz_VerifyCallback(address,address,uint24) (runs: 65536, μ: 11510, ~: 11524) +PoolAddressTest:testFuzz_VerifyCallbackCalldata(address,address,uint24) (runs: 65536, μ: 10752, ~: 10766) PoolAddressTest:testGas_ComputeAddress() (gas: 7766) PoolAddressTest:testGas_ComputeAddress_Og() (gas: 8464) PoolCallerTest:testFuzz_LiquidityNet(int24) (runs: 16, μ: 18777, ~: 18777) PoolCallerTest:testFuzz_Observations(uint256) (runs: 16, μ: 18976, ~: 19049) PoolCallerTest:testFuzz_Positions(bytes32) (runs: 16, μ: 19317, ~: 19317) -PoolCallerTest:testFuzz_Swap(bool,uint256,bytes) (runs: 16, μ: 240356, ~: 227379) +PoolCallerTest:testFuzz_Swap(bool,uint256,bytes) (runs: 16, μ: 247341, ~: 231108) PoolCallerTest:testFuzz_TickBitmap(int16) (runs: 16, μ: 11022, ~: 11022) PoolCallerTest:testFuzz_Ticks(int24) (runs: 16, μ: 20800, ~: 20800) PoolCallerTest:testRevert_AS_Swap() (gas: 13272) @@ -83,13 +83,13 @@ SafeCastTest:testToInt256(uint256) (runs: 65536, μ: 1182, ~: 686) SafeCastTest:testToUint128(uint256) (runs: 65536, μ: 1529, ~: 538) SafeCastTest:testToUint160(uint256) (runs: 65536, μ: 1192, ~: 318) SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,int128) (runs: 65536, μ: 12859, ~: 12606) -SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 12501, ~: 12249) +SqrtPriceMathTest:testFuzz_GetAmount0Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 12535, ~: 12258) SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,int128) (runs: 65536, μ: 12507, ~: 12426) -SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 12512, ~: 12445) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65536, μ: 16943, ~: 17528) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65536, μ: 19439, ~: 20070) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65536, μ: 13267, ~: 12487) -SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65536, μ: 13846, ~: 13600) +SqrtPriceMathTest:testFuzz_GetAmount1Delta(uint160,uint160,uint128,bool) (runs: 65536, μ: 12535, ~: 12563) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount0RoundingUp(uint160,uint128,uint256,bool) (runs: 65536, μ: 16548, ~: 16401) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromAmount1RoundingDown(uint160,uint128,uint256,bool) (runs: 65536, μ: 19142, ~: 18700) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromInput(uint160,uint128,uint256,bool) (runs: 65536, μ: 13307, ~: 12857) +SqrtPriceMathTest:testFuzz_GetNextSqrtPriceFromOutput(uint160,uint128,uint256,bool) (runs: 65536, μ: 14064, ~: 14705) SqrtPriceMathTest:testGas_GetAmount0Delta() (gas: 256572) SqrtPriceMathTest:testGas_GetAmount0Delta_Og() (gas: 280254) SqrtPriceMathTest:testGas_GetAmount1Delta() (gas: 209212) @@ -98,10 +98,10 @@ SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput() (gas: 227868) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromInput_Og() (gas: 243885) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput() (gas: 213109) SqrtPriceMathTest:testGas_GetNextSqrtPriceFromOutput_Og() (gas: 225117) -SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65536, μ: 22139, ~: 22254) -SwapMathTest:testFuzz_ComputeSwapStepExactIn(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 23607, ~: 23636) -SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 22950, ~: 22846) -SwapMathTest:testGas_ComputeSwapStep() (gas: 386504) +SwapMathTest:testFuzz_ComputeSwapStep(uint160,uint160,uint128,int256,uint24) (runs: 65536, μ: 22118, ~: 22228) +SwapMathTest:testFuzz_ComputeSwapStepExactIn(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 23606, ~: 23636) +SwapMathTest:testFuzz_ComputeSwapStepExactOut(uint160,uint160,uint128,uint256,uint24) (runs: 65536, μ: 22951, ~: 22846) +SwapMathTest:testGas_ComputeSwapStep() (gas: 384720) SwapMathTest:testGas_ComputeSwapStepExactIn() (gas: 394759) SwapMathTest:testGas_ComputeSwapStepExactIn_Og() (gas: 515722) SwapMathTest:testGas_ComputeSwapStepExactOut() (gas: 370755) @@ -109,7 +109,7 @@ SwapMathTest:testGas_ComputeSwapStepExactOut_Og() (gas: 510266) SwapMathTest:testGas_ComputeSwapStep_Og() (gas: 526558) TickBitmapTest:testFuzz_Compress(int24,int24) (runs: 65536, μ: 8348, ~: 8393) TickBitmapTest:testFuzz_FlipTick(int24) (runs: 65536, μ: 61584, ~: 61590) -TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 67949, ~: 67992) +TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 67891, ~: 67871) TickBitmapTest:testFuzz_Position(int24) (runs: 65536, μ: 498, ~: 498) TickBitmapTest:testGas_NextInitializedTickWithinOneWord() (gas: 6776007) TickBitmapTest:testGas_NextInitializedTickWithinOneWord_Og() (gas: 7050256) diff --git a/package.json b/package.json index bcff899..1f2ccba 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": "1.2.0", + "version": "1.2.1", "author": "Aperture Finance", "homepage": "https://aperture.finance/", "license": "GPL-2.0-or-later", @@ -32,15 +32,15 @@ "prettier:fix": "prettier -w {src,test}/**/*.sol" }, "dependencies": { - "@openzeppelin/contracts": "^5.0.0", + "@openzeppelin/contracts": "^5.0.1", "@uniswap/v3-core": "^1.0.1", "@uniswap/v3-periphery": "^1.4.4", - "solady": "^0.0.138" + "solady": "^0.0.155" }, "devDependencies": { - "prettier": "^3.0.3", - "prettier-plugin-solidity": "^1.1.3", - "typescript": "^5.2.2" + "prettier": "^3.1.1", + "prettier-plugin-solidity": "^1.3.1", + "typescript": "^5.3.3" }, "prettier": { "plugins": [ diff --git a/src/SwapMath.sol b/src/SwapMath.sol index 0eb29fe..db3604c 100644 --- a/src/SwapMath.sol +++ b/src/SwapMath.sol @@ -54,17 +54,13 @@ library SwapMath { unchecked { bool zeroForOne = sqrtRatioCurrentX96 >= sqrtRatioTargetX96; uint256 feeComplement = MAX_FEE_PIPS - feePips; - bool exactOut; + bool exactOut = amountRemaining < 0; uint256 amountRemainingAbs; - assembly { - // exactOut = 1 if amountRemaining < 0 else 0 - exactOut := slt(amountRemaining, 0) - // mask = -1 if amountRemaining < 0 else 0 - let mask := sub(0, exactOut) - amountRemainingAbs := xor(mask, add(mask, amountRemaining)) - } if (!exactOut) { + assembly { + amountRemainingAbs := amountRemaining + } uint256 amountRemainingLessFee = FullMath.mulDiv(amountRemainingAbs, feeComplement, MAX_FEE_PIPS); amountIn = zeroForOne ? SqrtPriceMath.getAmount0Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true) @@ -92,6 +88,9 @@ library SwapMath { amountOut = zeroForOne ? SqrtPriceMath.getAmount1Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false) : SqrtPriceMath.getAmount0Delta(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, false); + assembly { + amountRemainingAbs := sub(0, amountRemaining) + } if (amountRemainingAbs >= amountOut) { // `amountOut` is capped by the target price sqrtRatioNextX96 = sqrtRatioTargetX96; diff --git a/yarn.lock b/yarn.lock index 2e1763d..07bb18d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1,104 +1,97 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - -"@openzeppelin/contracts@3.4.2-solc-0.7": - version "3.4.2-solc-0.7" - resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-3.4.2-solc-0.7.tgz#38f4dbab672631034076ccdf2f3201fab1726635" - integrity sha512-W6QmqgkADuFcTLzHL8vVoNBtkwjvQRpYIAom7KiUNoLKghyx3FgH0GBjt8NRvigV1ZmMOBllvE1By1C+bi8WpA== - -"@openzeppelin/contracts@^5.0.0": - version "5.0.0" - resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-5.0.0.tgz#ee0e4b4564f101a5c4ee398cd4d73c0bd92b289c" - integrity sha512-bv2sdS6LKqVVMLI5+zqnNrNU/CA+6z6CmwFXm/MzmOPBRSO5reEJN7z0Gbzvs0/bv/MZZXNklubpwy3v2+azsw== - -"@solidity-parser/parser@^0.16.0": - version "0.16.1" - resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.16.1.tgz#f7c8a686974e1536da0105466c4db6727311253c" - integrity sha512-PdhRFNhbTtu3x8Axm0uYpqOy/lODYQK+MlYSgqIsq2L8SFYEHJPHNUiOTAJbDGzNjjr1/n9AcIayxafR/fWmYw== - dependencies: - antlr4ts "^0.5.0-alpha.4" - -"@uniswap/lib@^4.0.1-alpha": - version "4.0.1-alpha" - resolved "https://registry.yarnpkg.com/@uniswap/lib/-/lib-4.0.1-alpha.tgz#2881008e55f075344675b3bca93f020b028fbd02" - integrity sha512-f6UIliwBbRsgVLxIaBANF6w09tYqc6Y/qXdsrbEmXHyFA7ILiKrIwRFXe1yOg8M3cksgVsO9N7yuL2DdCGQKBA== - -"@uniswap/v2-core@^1.0.1": - version "1.0.1" - resolved "https://registry.yarnpkg.com/@uniswap/v2-core/-/v2-core-1.0.1.tgz#af8f508bf183204779938969e2e54043e147d425" - integrity sha512-MtybtkUPSyysqLY2U210NBDeCHX+ltHt3oADGdjqoThZaFRDKwM6k1Nb3F0A3hk5hwuQvytFWhrWHOEq6nVJ8Q== - -"@uniswap/v3-core@^1.0.0", "@uniswap/v3-core@^1.0.1": - version "1.0.1" - resolved "https://registry.yarnpkg.com/@uniswap/v3-core/-/v3-core-1.0.1.tgz#b6d2bdc6ba3c3fbd610bdc502395d86cd35264a0" - integrity sha512-7pVk4hEm00j9tc71Y9+ssYpO6ytkeI0y7WE9P6UcmNzhxPePwyAxImuhVsTqWK9YFvzgtvzJHi64pBl4jUzKMQ== - -"@uniswap/v3-periphery@^1.4.4": - version "1.4.4" - resolved "https://registry.yarnpkg.com/@uniswap/v3-periphery/-/v3-periphery-1.4.4.tgz#d2756c23b69718173c5874f37fd4ad57d2f021b7" - integrity sha512-S4+m+wh8HbWSO3DKk4LwUCPZJTpCugIsHrWR86m/OrUyvSqGDTXKFfc2sMuGXCZrD1ZqO3rhQsKgdWg3Hbb2Kw== - dependencies: - "@openzeppelin/contracts" "3.4.2-solc-0.7" - "@uniswap/lib" "^4.0.1-alpha" - "@uniswap/v2-core" "^1.0.1" - "@uniswap/v3-core" "^1.0.0" - base64-sol "1.0.1" - -antlr4ts@^0.5.0-alpha.4: - version "0.5.0-alpha.4" - resolved "https://registry.yarnpkg.com/antlr4ts/-/antlr4ts-0.5.0-alpha.4.tgz#71702865a87478ed0b40c0709f422cf14d51652a" - integrity sha512-WPQDt1B74OfPv/IMS2ekXAKkTZIHl88uMetg6q3OTqgFxZ/dxDXI0EWLyZid/1Pe6hTftyg5N7gel5wNAGxXyQ== - -base64-sol@1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/base64-sol/-/base64-sol-1.0.1.tgz#91317aa341f0bc763811783c5729f1c2574600f6" - integrity sha512-ld3cCNMeXt4uJXmLZBHFGMvVpK9KsLVEhPpFRXnvSVAqABKbuNZg/+dsq3NuM+wxFLb/UrVkz7m1ciWmkMfTbg== - -lru-cache@^6.0.0: - version "6.0.0" - resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" - integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA== - dependencies: - yallist "^4.0.0" - -prettier-plugin-solidity@^1.1.3: - version "1.1.3" - resolved "https://registry.yarnpkg.com/prettier-plugin-solidity/-/prettier-plugin-solidity-1.1.3.tgz#9a35124f578404caf617634a8cab80862d726cba" - integrity sha512-fQ9yucPi2sBbA2U2Xjh6m4isUTJ7S7QLc/XDDsktqqxYfTwdYKJ0EnnywXHwCGAaYbQNK+HIYPL1OemxuMsgeg== - dependencies: - "@solidity-parser/parser" "^0.16.0" - semver "^7.3.8" - solidity-comments-extractor "^0.0.7" - -prettier@^3.0.3: - version "3.0.3" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.3.tgz#432a51f7ba422d1469096c0fdc28e235db8f9643" - integrity sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg== - -semver@^7.3.8: - version "7.5.4" - resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e" - integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA== - dependencies: - lru-cache "^6.0.0" - -solady@^0.0.138: - version "0.0.138" - resolved "https://registry.yarnpkg.com/solady/-/solady-0.0.138.tgz#efa7849b35e46301d2f334db04ac731c59efd466" - integrity sha512-5jRi1IwgoE3+2kVo/zIPRYaSdQhypl2qmmZQj03y9tq6OiswXLYaag7kvhvp6v+iBFSlO2bYgsk8KFXQcX256w== - -solidity-comments-extractor@^0.0.7: - version "0.0.7" - resolved "https://registry.yarnpkg.com/solidity-comments-extractor/-/solidity-comments-extractor-0.0.7.tgz#99d8f1361438f84019795d928b931f4e5c39ca19" - integrity sha512-wciNMLg/Irp8OKGrh3S2tfvZiZ0NEyILfcRCXCD4mp7SgK/i9gzLfhY2hY7VMCQJ3kH9UB9BzNdibIVMchzyYw== - -typescript@^5.2.2: - version "5.2.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78" - integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w== - -yallist@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" - integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@openzeppelin/contracts@3.4.2-solc-0.7": + version "3.4.2-solc-0.7" + resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-3.4.2-solc-0.7.tgz#38f4dbab672631034076ccdf2f3201fab1726635" + integrity sha512-W6QmqgkADuFcTLzHL8vVoNBtkwjvQRpYIAom7KiUNoLKghyx3FgH0GBjt8NRvigV1ZmMOBllvE1By1C+bi8WpA== + +"@openzeppelin/contracts@^5.0.1": + version "5.0.1" + resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-5.0.1.tgz#93da90fc209a0a4ff09c1deb037fbb35e4020890" + integrity sha512-yQJaT5HDp9hYOOp4jTYxMsR02gdFZFXhewX5HW9Jo4fsqSVqqyIO/xTHdWDaKX5a3pv1txmf076Lziz+sO7L1w== + +"@solidity-parser/parser@^0.17.0": + version "0.17.0" + resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.17.0.tgz#52a2fcc97ff609f72011014e4c5b485ec52243ef" + integrity sha512-Nko8R0/kUo391jsEHHxrGM07QFdnPGvlmox4rmH0kNiNAashItAilhy4Mv4pK5gQmW5f4sXAF58fwJbmlkGcVw== + +"@uniswap/lib@^4.0.1-alpha": + version "4.0.1-alpha" + resolved "https://registry.yarnpkg.com/@uniswap/lib/-/lib-4.0.1-alpha.tgz#2881008e55f075344675b3bca93f020b028fbd02" + integrity sha512-f6UIliwBbRsgVLxIaBANF6w09tYqc6Y/qXdsrbEmXHyFA7ILiKrIwRFXe1yOg8M3cksgVsO9N7yuL2DdCGQKBA== + +"@uniswap/v2-core@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@uniswap/v2-core/-/v2-core-1.0.1.tgz#af8f508bf183204779938969e2e54043e147d425" + integrity sha512-MtybtkUPSyysqLY2U210NBDeCHX+ltHt3oADGdjqoThZaFRDKwM6k1Nb3F0A3hk5hwuQvytFWhrWHOEq6nVJ8Q== + +"@uniswap/v3-core@^1.0.0", "@uniswap/v3-core@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@uniswap/v3-core/-/v3-core-1.0.1.tgz#b6d2bdc6ba3c3fbd610bdc502395d86cd35264a0" + integrity sha512-7pVk4hEm00j9tc71Y9+ssYpO6ytkeI0y7WE9P6UcmNzhxPePwyAxImuhVsTqWK9YFvzgtvzJHi64pBl4jUzKMQ== + +"@uniswap/v3-periphery@^1.4.4": + version "1.4.4" + resolved "https://registry.yarnpkg.com/@uniswap/v3-periphery/-/v3-periphery-1.4.4.tgz#d2756c23b69718173c5874f37fd4ad57d2f021b7" + integrity sha512-S4+m+wh8HbWSO3DKk4LwUCPZJTpCugIsHrWR86m/OrUyvSqGDTXKFfc2sMuGXCZrD1ZqO3rhQsKgdWg3Hbb2Kw== + dependencies: + "@openzeppelin/contracts" "3.4.2-solc-0.7" + "@uniswap/lib" "^4.0.1-alpha" + "@uniswap/v2-core" "^1.0.1" + "@uniswap/v3-core" "^1.0.0" + base64-sol "1.0.1" + +base64-sol@1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/base64-sol/-/base64-sol-1.0.1.tgz#91317aa341f0bc763811783c5729f1c2574600f6" + integrity sha512-ld3cCNMeXt4uJXmLZBHFGMvVpK9KsLVEhPpFRXnvSVAqABKbuNZg/+dsq3NuM+wxFLb/UrVkz7m1ciWmkMfTbg== + +lru-cache@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" + integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA== + dependencies: + yallist "^4.0.0" + +prettier-plugin-solidity@^1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/prettier-plugin-solidity/-/prettier-plugin-solidity-1.3.1.tgz#59944d3155b249f7f234dee29f433524b9a4abcf" + integrity sha512-MN4OP5I2gHAzHZG1wcuJl0FsLS3c4Cc5494bbg+6oQWBPuEamjwDvmGfFMZ6NFzsh3Efd9UUxeT7ImgjNH4ozA== + dependencies: + "@solidity-parser/parser" "^0.17.0" + semver "^7.5.4" + solidity-comments-extractor "^0.0.8" + +prettier@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.1.1.tgz#6ba9f23165d690b6cbdaa88cb0807278f7019848" + integrity sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw== + +semver@^7.5.4: + version "7.5.4" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e" + integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA== + dependencies: + lru-cache "^6.0.0" + +solady@^0.0.155: + version "0.0.155" + resolved "https://registry.yarnpkg.com/solady/-/solady-0.0.155.tgz#855cd63190902c4167e8501d74d72272a0285067" + integrity sha512-UVFZxRNe+Iv3B2Z0dfo2Pngg4lyeHE1QqpI5wDS5bdhsCUfgEzhFZxQN7DJ6lKy3hSyF39ldGLKlCCEJp7EM4Q== + +solidity-comments-extractor@^0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/solidity-comments-extractor/-/solidity-comments-extractor-0.0.8.tgz#f6e148ab0c49f30c1abcbecb8b8df01ed8e879f8" + integrity sha512-htM7Vn6LhHreR+EglVMd2s+sZhcXAirB1Zlyrv5zBuTxieCvjfnRpd7iZk75m/u6NOlEyQ94C6TWbBn2cY7w8g== + +typescript@^5.3.3: + version "5.3.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37" + integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw== + +yallist@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" + integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== From 2c531361747afd3b417d91a4160c50fc2058efec Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Sun, 31 Dec 2023 03:12:29 -0800 Subject: [PATCH 2/2] Import optimized bit math from Solady --- .gas-snapshot | 22 +++++++++++----------- src/BitMath.sol | 20 ++++++-------------- src/TickMath.sol | 15 ++++----------- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 2008702..eead584 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,8 +1,8 @@ BitMathTest:testFuzz_LSB(uint256) (runs: 65536, μ: 14911, ~: 14909) -BitMathTest:testFuzz_MSB(uint256) (runs: 65536, μ: 14791, ~: 14816) +BitMathTest:testFuzz_MSB(uint256) (runs: 65536, μ: 14771, ~: 14796) BitMathTest:testGas_LSB() (gas: 193393) BitMathTest:testGas_LSB_Og() (gas: 291088) -BitMathTest:testGas_MSB() (gas: 213472) +BitMathTest:testGas_MSB() (gas: 208372) BitMathTest:testGas_MSB_Og() (gas: 274437) FullMathTest:testFuzz_MulDiv96(uint256,uint256) (runs: 65536, μ: 3601, ~: 3585) FullMathTest:testFuzz_MulDivUp_OZ(uint256,uint256,uint256) (runs: 65536, μ: 4391, ~: 4237) @@ -107,21 +107,21 @@ SwapMathTest:testGas_ComputeSwapStepExactIn_Og() (gas: 515722) SwapMathTest:testGas_ComputeSwapStepExactOut() (gas: 370755) SwapMathTest:testGas_ComputeSwapStepExactOut_Og() (gas: 510266) SwapMathTest:testGas_ComputeSwapStep_Og() (gas: 526558) -TickBitmapTest:testFuzz_Compress(int24,int24) (runs: 65536, μ: 8348, ~: 8393) -TickBitmapTest:testFuzz_FlipTick(int24) (runs: 65536, μ: 61584, ~: 61590) -TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 67891, ~: 67871) +TickBitmapTest:testFuzz_Compress(int24,int24) (runs: 65536, μ: 8348, ~: 8382) +TickBitmapTest:testFuzz_FlipTick(int24) (runs: 65536, μ: 61570, ~: 61576) +TickBitmapTest:testFuzz_NextInitializedTickWithinOneWord(int24,uint8,bool) (runs: 65536, μ: 67880, ~: 67868) TickBitmapTest:testFuzz_Position(int24) (runs: 65536, μ: 498, ~: 498) -TickBitmapTest:testGas_NextInitializedTickWithinOneWord() (gas: 6776007) +TickBitmapTest:testGas_NextInitializedTickWithinOneWord() (gas: 6769095) TickBitmapTest:testGas_NextInitializedTickWithinOneWord_Og() (gas: 7050256) TickBitmapTest:test_NextInitializedTickWithinOneWord_GT() (gas: 203148) -TickBitmapTest:test_NextInitializedTickWithinOneWord_LTE() (gas: 212522) +TickBitmapTest:test_NextInitializedTickWithinOneWord_LTE() (gas: 204586) TickBitmapTest:test_NextInitializedTick_GT() (gas: 342054) -TickBitmapTest:test_NextInitializedTick_LTE() (gas: 399975) -TickMathTest:testFuzz_GetSqrtRatioAtTick(int24) (runs: 65536, μ: 16536, ~: 16831) -TickMathTest:testFuzz_GetTickAtSqrtRatio(uint160) (runs: 65536, μ: 13140, ~: 13159) +TickBitmapTest:test_NextInitializedTick_LTE() (gas: 388455) +TickMathTest:testFuzz_GetSqrtRatioAtTick(int24) (runs: 65536, μ: 16522, ~: 16817) +TickMathTest:testFuzz_GetTickAtSqrtRatio(uint160) (runs: 65536, μ: 13125, ~: 13145) TickMathTest:testGas_GetSqrtRatioAtTick() (gas: 146478) TickMathTest:testGas_GetSqrtRatioAtTick_Og() (gas: 168547) -TickMathTest:testGas_GetTickAtSqrtRatio() (gas: 252577) +TickMathTest:testGas_GetTickAtSqrtRatio() (gas: 251177) TickMathTest:testGas_GetTickAtSqrtRatio_Og() (gas: 307790) TickMathTest:testRevert_GetSqrtRatioAtTick() (gas: 10445) TickMathTest:testRevert_GetTickAtSqrtRatio() (gas: 10522) \ No newline at end of file diff --git a/src/BitMath.sol b/src/BitMath.sol index 25fef87..74c7573 100644 --- a/src/BitMath.sol +++ b/src/BitMath.sol @@ -21,21 +21,13 @@ library BitMath { r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x)))) // r += (x >> r) >= 2**32 ? 32 : 0 r := or(r, shl(5, lt(0xffffffff, shr(r, x)))) - - // For the remaining 32 bits, use a De Bruijn lookup. - // https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn - x := shr(r, x) - x := or(x, shr(1, x)) - x := or(x, shr(2, x)) - x := or(x, shr(4, x)) - x := or(x, shr(8, x)) - x := or(x, shr(16, x)) - + r := or(r, shl(4, lt(0xffff, shr(r, x)))) + r := or(r, shl(3, lt(0xff, shr(r, x)))) r := or( r, byte( - shr(251, mul(x, shl(224, 0x07c4acdd))), - 0x0009010a0d15021d0b0e10121619031e080c141c0f111807131b17061a05041f + and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)), + 0x0706060506020504060203020504030106050205030304010505030400000000 ) ) } @@ -65,8 +57,8 @@ library BitMath { r := or( r, byte( - shr(251, mul(shr(r, x), shl(224, 0x077cb531))), - 0x00011c021d0e18031e16140f191104081f1b0d17151310071a0c12060b050a09 + and(div(0xd76453e0, shr(r, x)), 0x1f), + 0x001f0d1e100c1d070f090b19131c1706010e11080a1a141802121b1503160405 ) ) } diff --git a/src/TickMath.sol b/src/TickMath.sol index 72b682a..f0ccade 100644 --- a/src/TickMath.sol +++ b/src/TickMath.sol @@ -135,20 +135,13 @@ library TickMath { msb := shl(7, lt(0xffffffffffffffffffffffffffffffff, x)) msb := or(msb, shl(6, lt(0xffffffffffffffff, shr(msb, x)))) msb := or(msb, shl(5, lt(0xffffffff, shr(msb, x)))) - - // For the remaining 32 bits, use a De Bruijn lookup. - x := shr(msb, x) - x := or(x, shr(1, x)) - x := or(x, shr(2, x)) - x := or(x, shr(4, x)) - x := or(x, shr(8, x)) - x := or(x, shr(16, x)) - + msb := or(msb, shl(4, lt(0xffff, shr(msb, x)))) + msb := or(msb, shl(3, lt(0xff, shr(msb, x)))) msb := or( msb, byte( - shr(251, mul(x, shl(224, 0x07c4acdd))), - 0x0009010a0d15021d0b0e10121619031e080c141c0f111807131b17061a05041f + and(0x1f, shr(shr(msb, x), 0x8421084210842108cc6318c6db6d54be)), + 0x0706060506020504060203020504030106050205030304010505030400000000 ) ) }