Skip to content

Commit

Permalink
Merge pull request #37 from Uniswap/alice-refactor-quoter
Browse files Browse the repository at this point in the history
Refactor of mixed quoter
  • Loading branch information
hensha256 authored Sep 16, 2024
2 parents 096132a + c48d3fc commit 961ea03
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 616 deletions.
2 changes: 1 addition & 1 deletion lib/v4-core
Submodule v4-core updated 118 files
2 changes: 1 addition & 1 deletion lib/v4-periphery
Submodule v4-periphery updated 173 files
254 changes: 55 additions & 199 deletions src/MixedRouteQuoterV2.sol

Large diffs are not rendered by default.

23 changes: 5 additions & 18 deletions src/interfaces/IMixedRouteQuoterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
/// @dev These functions are not marked view because they rely on calling non-view functions and reverting
/// to compute the result. They are also not gas efficient and should not be called on-chain.
interface IMixedRouteQuoterV2 {
error UnexpectedRevertBytes(bytes revertData);
error InsufficientAmountOut();
error LockFailure();
error InvalidPoolVersion(uint256 poolVersion);
error NoLiquidityV3();

struct QuoteExactInputSingleV2Params {
address tokenIn;
Expand Down Expand Up @@ -64,12 +62,10 @@ interface IMixedRouteQuoterV2 {
/// amountIn The desired input amount
/// sqrtPriceLimitX96 The price limit of the pool that cannot be exceeded by the swap
/// @return amountOut The amount of `tokenOut` that would be received
/// @return sqrtPriceX96After The sqrt price of the pool after the swap
/// @return initializedTicksCrossed The number of initialized ticks that the swap crossed
/// @return gasEstimate The estimate of the gas that the swap consumes
function quoteExactInputSingleV3(QuoteExactInputSingleV3Params calldata params)
external
returns (uint256 amountOut, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed, uint256 gasEstimate);
returns (uint256 amountOut, uint256 gasEstimate);

/// @notice Returns the delta amounts for a given exact input swap of a single pool
/// @param params The params for the quote, encoded as `QuoteExactSingleParams`
Expand All @@ -79,27 +75,18 @@ interface IMixedRouteQuoterV2 {
/// sqrtPriceLimitX96 The price limit of the pool that cannot be exceeded by the swap
/// hookData arbitrary hookData to pass into the associated hooks
/// @return amountOut The amount of `tokenOut` that would be received
/// @return sqrtPriceX96After The sqrt price of the pool after the swap
/// @return initializedTicksLoaded The number of initialized ticks that the swap loaded
/// @return gasEstimate The estimate of the gas that the swap consumes
function quoteExactInputSingleV4(QuoteExactInputSingleV4Params calldata params)
external
returns (uint256 amountOut, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded, uint256 gasEstimate);
returns (uint256 amountOut, uint256 gasEstimate);

/// @notice Returns the amount out received for a given exact input swap without executing the swap
/// @param path The path of the swap, i.e. each token pair and the pool fee
/// @param param the remaining non abi encodable data
/// @param amountIn The amount of the first token to swap
/// @return amountOut The amount of the last token that would be received
/// @return sqrtPriceX96AfterList List of the sqrt price after the swap for each v3 pool in the path, 0 for v2 pools
/// @return initializedTicksCrossedList List of the initialized ticks that the swap crossed for each v3 pool in the path, 0 for v2 pools
/// @return swapGasEstimate The estimate of the gas that the v3 swaps in the path consume
/// @return gasEstimate The estimate of the gas that the v3 and v4 swaps in the path consume
function quoteExactInput(bytes memory path, ExtraQuoteExactInputParams calldata param, uint256 amountIn)
external
returns (
uint256 amountOut,
uint160[] memory sqrtPriceX96AfterList,
uint32[] memory initializedTicksCrossedList,
uint256 swapGasEstimate
);
returns (uint256 amountOut, uint256 gasEstimate);
}
6 changes: 3 additions & 3 deletions src/libraries/Path.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ library Path {
pure
returns (PoolKey memory)
{
(address tokenIn, address tokenOut) = token0 < token1 ? (token0, token1) : (token1, token0);
Currency currency0 = Currency.wrap(tokenIn);
Currency currency1 = Currency.wrap(tokenOut);
(token0, token1) = token0 < token1 ? (token0, token1) : (token1, token0);
Currency currency0 = Currency.wrap(token0);
Currency currency1 = Currency.wrap(token1);
return PoolKey({
currency0: currency0,
currency1: currency1,
Expand Down
20 changes: 4 additions & 16 deletions test/MixedRouteQuoterV2OnMainnet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ contract MixedRouteQuoterV2TestOnMainnet is Test {
bytes memory mixedRouteQuoterV1Path = abi.encodePacked(WTAO, WTAO_WETH_v3Fee, WETH, flagBitmask, OPSEC);

uint256 gasBeforeQuoteMixedQuoterV1 = gasleft();
(
uint256 amountOut,
uint160[] memory v3SqrtPriceX96AfterList,
uint32[] memory v3InitializedTicksCrossedList,
uint256 v3SwapGasEstimate
) = mixedRouteQuoterV1.quoteExactInput(mixedRouteQuoterV1Path, amountIn);
(uint256 amountOut,,, uint256 v3SwapGasEstimate) =
mixedRouteQuoterV1.quoteExactInput(mixedRouteQuoterV1Path, amountIn);
uint256 gasAfterQuoteMixedQuoterV1 = gasleft();

uint8 v3PoolVersion = uint8(3);
Expand All @@ -61,19 +57,11 @@ contract MixedRouteQuoterV2TestOnMainnet is Test {
IMixedRouteQuoterV2.ExtraQuoteExactInputParams({nonEncodableData: nonEncodableData});

uint256 gasBeforeQuoteMixedQuoterV2 = gasleft();
(
uint256 amountOutV2,
uint160[] memory sqrtPriceX96AfterListV2,
uint32[] memory initializedTicksCrossedListV2,
uint256 swapGasEstimateV2
) = mixedRouteQuoterV2.quoteExactInput(mixedRouteQuoterV2Path, extraParams, amountIn);
(uint256 amountOutV2, uint256 swapGasEstimateV2) =
mixedRouteQuoterV2.quoteExactInput(mixedRouteQuoterV2Path, extraParams, amountIn);
uint256 gasAfterQuoteMixedQuoterV2 = gasleft();

assertEqUint(amountOut, amountOutV2);
assertEqUint(v3SqrtPriceX96AfterList[0], sqrtPriceX96AfterListV2[0]);
assertEqUint(v3SqrtPriceX96AfterList[1], sqrtPriceX96AfterListV2[1]);
assertEqUint(v3InitializedTicksCrossedList[0], initializedTicksCrossedListV2[0]);
assertEqUint(v3InitializedTicksCrossedList[1], initializedTicksCrossedListV2[1]);

// Due to mixed quoter v2 having more compact pool version + fee tier encoding for v2,
// Overall gas cost of mixed quoter v2 should always be less than mixed quoter v1
Expand Down
Loading

0 comments on commit 961ea03

Please sign in to comment.