Skip to content

Commit

Permalink
gas savings from unchecked math
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilz committed Sep 5, 2023
1 parent 607ea3b commit cf82527
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterBytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4829
4676
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactIn1Hop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
194162
193938
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
271461
271014
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348765
348095
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactInSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192195
192194
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactOut1Hop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187374
193086
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
271562
271039
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349804
349020
74 changes: 39 additions & 35 deletions contracts/Routing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit cf82527

Please sign in to comment.