Skip to content

Commit

Permalink
cache path key
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 10, 2024
1 parent bb2b983 commit 251c104
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128285
127764
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153188
152656
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87211
86679
2 changes: 1 addition & 1 deletion .forge-snapshots/Quoter_quoteExactInput_twoHops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212035
210976
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148025
147458
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178162
177595
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148093
147526
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124792
124225
2 changes: 1 addition & 1 deletion .forge-snapshots/Quoter_quoteExactOutput_twoHops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
252373
251247
16 changes: 10 additions & 6 deletions src/lens/Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ contract Quoter is IQuoter, SafeCallback {
BalanceDelta swapDelta;
uint128 amountIn = params.exactAmount;
Currency inputCurrency = params.exactCurrency;
PathKey calldata pathKey;

for (uint256 i = 0; i < pathLength; i++) {
(PoolKey memory poolKey, bool zeroForOne) = params.path[i].getPoolAndSwapDirection(inputCurrency);
pathKey = params.path[i];
(PoolKey memory poolKey, bool zeroForOne) = pathKey.getPoolAndSwapDirection(inputCurrency);

(swapDelta, result.sqrtPriceX96AfterList[i]) =
_swap(poolKey, zeroForOne, -int256(int128(amountIn)), 0, params.path[i].hookData);
_swap(poolKey, zeroForOne, -int256(int128(amountIn)), 0, pathKey.hookData);

if (zeroForOne) {
result.deltaAmounts[i] += -swapDelta.amount0();
Expand All @@ -122,7 +124,7 @@ contract Quoter is IQuoter, SafeCallback {
result.deltaAmounts[i + 1] += -swapDelta.amount0();
amountIn = uint128(swapDelta.amount0());
}
inputCurrency = params.path[i].intermediateCurrency;
inputCurrency = pathKey.intermediateCurrency;
}
bytes memory encodedResult = abi.encode(result.deltaAmounts, result.sqrtPriceX96AfterList);
encodedResult.revertWith();
Expand Down Expand Up @@ -156,14 +158,16 @@ contract Quoter is IQuoter, SafeCallback {
BalanceDelta swapDelta;
uint128 amountOut = params.exactAmount;
Currency outputCurrency = params.exactCurrency;
PathKey calldata pathKey;

for (uint256 i = pathLength; i > 0; i--) {
pathKey = params.path[i - 1];
amountOutCached = amountOut;

(PoolKey memory poolKey, bool oneForZero) = params.path[i - 1].getPoolAndSwapDirection(outputCurrency);
(PoolKey memory poolKey, bool oneForZero) = pathKey.getPoolAndSwapDirection(outputCurrency);

(swapDelta, result.sqrtPriceX96AfterList[i - 1]) =
_swap(poolKey, !oneForZero, int256(uint256(amountOut)), 0, params.path[i - 1].hookData);
_swap(poolKey, !oneForZero, int256(uint256(amountOut)), 0, pathKey.hookData);

// always clear because sqrtPriceLimitX96 is set to 0 always
delete amountOutCached;
Expand All @@ -178,7 +182,7 @@ contract Quoter is IQuoter, SafeCallback {
amountOut = uint128(-swapDelta.amount1());
}

outputCurrency = params.path[i - 1].intermediateCurrency;
outputCurrency = pathKey.intermediateCurrency;
}
bytes memory encodedResult = abi.encode(result.deltaAmounts, result.sqrtPriceX96AfterList);
encodedResult.revertWith();
Expand Down

0 comments on commit 251c104

Please sign in to comment.