From cf825272053e321a3d5e451e7c50e6e65aa36bdf Mon Sep 17 00:00:00 2001 From: Emily Williams Date: Tue, 5 Sep 2023 16:19:14 -0400 Subject: [PATCH] gas savings from unchecked math --- .forge-snapshots/RouterBytecode.snap | 2 +- .forge-snapshots/RouterExactIn1Hop.snap | 2 +- .forge-snapshots/RouterExactIn2Hops.snap | 2 +- .forge-snapshots/RouterExactIn3Hops.snap | 2 +- .forge-snapshots/RouterExactInSingle.snap | 2 +- .forge-snapshots/RouterExactOut1Hop.snap | 2 +- .forge-snapshots/RouterExactOut2Hops.snap | 2 +- .forge-snapshots/RouterExactOut3Hops.snap | 2 +- contracts/Routing.sol | 74 ++++++++++++----------- 9 files changed, 47 insertions(+), 43 deletions(-) diff --git a/.forge-snapshots/RouterBytecode.snap b/.forge-snapshots/RouterBytecode.snap index fbbc587a..a44cea40 100644 --- a/.forge-snapshots/RouterBytecode.snap +++ b/.forge-snapshots/RouterBytecode.snap @@ -1 +1 @@ -4829 \ No newline at end of file +4676 \ No newline at end of file diff --git a/.forge-snapshots/RouterExactIn1Hop.snap b/.forge-snapshots/RouterExactIn1Hop.snap index 02ca69ef..129f55e9 100644 --- a/.forge-snapshots/RouterExactIn1Hop.snap +++ b/.forge-snapshots/RouterExactIn1Hop.snap @@ -1 +1 @@ -194162 \ No newline at end of file +193938 \ No newline at end of file diff --git a/.forge-snapshots/RouterExactIn2Hops.snap b/.forge-snapshots/RouterExactIn2Hops.snap index 0bb7aa9d..4d93b0d6 100644 --- a/.forge-snapshots/RouterExactIn2Hops.snap +++ b/.forge-snapshots/RouterExactIn2Hops.snap @@ -1 +1 @@ -271461 \ No newline at end of file +271014 \ No newline at end of file diff --git a/.forge-snapshots/RouterExactIn3Hops.snap b/.forge-snapshots/RouterExactIn3Hops.snap index ae20bc3c..42b6e94b 100644 --- a/.forge-snapshots/RouterExactIn3Hops.snap +++ b/.forge-snapshots/RouterExactIn3Hops.snap @@ -1 +1 @@ -348765 \ No newline at end of file +348095 \ No newline at end of file diff --git a/.forge-snapshots/RouterExactInSingle.snap b/.forge-snapshots/RouterExactInSingle.snap index 3a60ad66..e4929481 100644 --- a/.forge-snapshots/RouterExactInSingle.snap +++ b/.forge-snapshots/RouterExactInSingle.snap @@ -1 +1 @@ -192195 \ No newline at end of file +192194 \ No newline at end of file diff --git a/.forge-snapshots/RouterExactOut1Hop.snap b/.forge-snapshots/RouterExactOut1Hop.snap index ab704c89..86fc2330 100644 --- a/.forge-snapshots/RouterExactOut1Hop.snap +++ b/.forge-snapshots/RouterExactOut1Hop.snap @@ -1 +1 @@ -187374 \ No newline at end of file +193086 \ No newline at end of file diff --git a/.forge-snapshots/RouterExactOut2Hops.snap b/.forge-snapshots/RouterExactOut2Hops.snap index b3d510d5..d508b257 100644 --- a/.forge-snapshots/RouterExactOut2Hops.snap +++ b/.forge-snapshots/RouterExactOut2Hops.snap @@ -1 +1 @@ -271562 \ No newline at end of file +271039 \ No newline at end of file diff --git a/.forge-snapshots/RouterExactOut3Hops.snap b/.forge-snapshots/RouterExactOut3Hops.snap index eeb3e65d..0bc57d45 100644 --- a/.forge-snapshots/RouterExactOut3Hops.snap +++ b/.forge-snapshots/RouterExactOut3Hops.snap @@ -1 +1 @@ -349804 \ No newline at end of file +349020 \ No newline at end of file diff --git a/contracts/Routing.sol b/contracts/Routing.sol index 91b6445a..52b07410 100644 --- a/contracts/Routing.sol +++ b/contracts/Routing.sol @@ -110,46 +110,50 @@ abstract contract Routing { } function _swapExactInput(ExactInputParams memory params, address msgSender) private { - for (uint256 i = 0; i < params.path.length; i++) { - (PoolKey memory poolKey, bool zeroForOne) = _getPoolAndSwapDirection(params.path[i], params.currencyIn); - uint128 amountOut = uint128( - -_swapExactPrivate( - poolKey, - zeroForOne, - int256(int128(params.amountIn)), - 0, - msgSender, - i == 0, - i == params.path.length - 1 - ) - ); - - params.amountIn = amountOut; - params.currencyIn = params.path[i].tradeCurrency; + unchecked { + for (uint256 i = 0; i < params.path.length; i++) { + (PoolKey memory poolKey, bool zeroForOne) = _getPoolAndSwapDirection(params.path[i], params.currencyIn); + uint128 amountOut = uint128( + -_swapExactPrivate( + poolKey, + zeroForOne, + int256(int128(params.amountIn)), + 0, + msgSender, + i == 0, + i == params.path.length - 1 + ) + ); + + params.amountIn = amountOut; + params.currencyIn = params.path[i].tradeCurrency; + } + + if (params.amountIn < params.amountOutMinimum) revert TooLittleReceived(); } - - if (params.amountIn < params.amountOutMinimum) revert TooLittleReceived(); } function _swapExactOutput(ExactOutputParams memory params, address msgSender) private { - for (uint256 i = params.path.length; i > 0; i--) { - (PoolKey memory poolKey, bool oneForZero) = _getPoolAndSwapDirection(params.path[i - 1], params.currencyOut); - uint128 amountIn = uint128( - _swapExactPrivate( - poolKey, - !oneForZero, - -int256(int128(params.amountOut)), - 0, - msgSender, - i == 1, - i == params.path.length - ) - ); - - params.amountOut = amountIn; - params.currencyOut = params.path[i - 1].tradeCurrency; + unchecked { + for (uint256 i = params.path.length; i > 0; i--) { + (PoolKey memory poolKey, bool oneForZero) = _getPoolAndSwapDirection(params.path[i - 1], params.currencyOut); + uint128 amountIn = uint128( + _swapExactPrivate( + poolKey, + !oneForZero, + -int256(int128(params.amountOut)), + 0, + msgSender, + i == 1, + i == params.path.length + ) + ); + + params.amountOut = amountIn; + params.currencyOut = params.path[i - 1].tradeCurrency; + } + if (params.amountOut > params.amountInMaximum) revert TooMuchRequested(); } - if (params.amountOut > params.amountInMaximum) revert TooMuchRequested(); } function _swapExactPrivate(