Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fuzz test exact input v4 against v4 quoter #31

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MixedRouteQuoterV2TestOnSepolia:test_FuzzQuoteExactInput(uint256) (runs: 5000, μ: 74013, ~: 74014)
MixedRouteQuoterV2TestOnSepolia:test_QuoteExactInput() (gas: 74116)
MixedRouteQuoterV2TestOnSepolia:test_QuoteExactInputSingleV4() (gas: 69120)
53 changes: 0 additions & 53 deletions package-lock.json

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"src/**/*.json"
],
"scripts": {
"fmt": "forge fmt",
"build": "forge build",
"test": "forge test"
},
Expand Down
64 changes: 61 additions & 3 deletions test/MixedRouteQuoterV2OnSepolia.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import {Test, console} from "forge-std/Test.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {IQuoter} from "@uniswap/v4-periphery/src/interfaces/IQuoter.sol";
import {Constants} from "../src/libraries/Constants.sol";
import {Quoter} from "v4-periphery/src/lens/Quoter.sol";
import {PathKey} from "@uniswap/v4-periphery/src/libraries/PathKey.sol";

contract MixedRouteQuoterV2TestOnSepolia is Test {
IMixedRouteQuoterV2 public mixedRouterQuoterV2;
IQuoter public quoter;
IPoolManager public poolManager;
address public immutable uniswapV4PoolManager = 0xc021A7Deb4a939fd7E661a0669faB5ac7Ba2D5d6;
address public immutable uniswapV3PoolFactory = 0x0227628f3F023bb0B980b67D528571c95c6DaC1c;
Expand All @@ -24,6 +28,7 @@ contract MixedRouteQuoterV2TestOnSepolia is Test {
vm.createSelectFork(vm.envString("SEPOLIA_RPC_URL"));
poolManager = IPoolManager(uniswapV4PoolManager);
mixedRouterQuoterV2 = new MixedRouteQuoterV2(poolManager, uniswapV3PoolFactory, uniswapV2PoolFactory);
quoter = new Quoter(poolManager);
}

function test_QuoteExactInputSingleV4() public {
Expand Down Expand Up @@ -94,8 +99,9 @@ contract MixedRouteQuoterV2TestOnSepolia is Test {
uint24 fee = 500;
uint24 tickSpacing = 10;
address hooks = address(0);
bytes memory hookData = "0x";
IMixedRouteQuoterV2.NonEncodableData[] memory nonEncodableData = new IMixedRouteQuoterV2.NonEncodableData[](1);
nonEncodableData[0] = (IMixedRouteQuoterV2.NonEncodableData({hookData: "0x"}));
nonEncodableData[0] = (IMixedRouteQuoterV2.NonEncodableData({hookData: hookData}));

// bytes memory path = abi.encodePacked(V4_SEPOLIA_OP_ADDRESS, fee,tickSpacing, hooks, V4_SEPOLIA_USDC_ADDRESS);
IMixedRouteQuoterV2.ExtraQuoteExactInputParams memory extraParams = IMixedRouteQuoterV2.ExtraQuoteExactInputParams({
Expand All @@ -106,8 +112,60 @@ contract MixedRouteQuoterV2TestOnSepolia is Test {
poolVersions, V4_SEPOLIA_OP_ADDRESS, fee, tickSpacing, hooks, V4_SEPOLIA_USDC_ADDRESS
);

(uint256 amountOut, , , uint256 gasEstimate) = mixedRouterQuoterV2.quoteExactInput(path, extraParams, amountIn);
assertGt(amountOut, 0);
(
uint256 amountOut,
uint160[] memory sqrtPriceX96After,
uint32[] memory initializedTicksLoaded,
uint256 gasEstimate
) = mixedRouterQuoterV2.quoteExactInput(path, extraParams, amountIn);

assertGt(gasEstimate, 0);

PathKey[] memory exactInPathKey = new PathKey[](1);
exactInPathKey[0] = PathKey({
intermediateCurrency: Currency.wrap(V4_SEPOLIA_USDC_ADDRESS),
fee: fee,
tickSpacing: int24(tickSpacing),
hooks: IHooks(hooks),
hookData: hookData
});

IQuoter.QuoteExactParams memory exactInParams = IQuoter.QuoteExactParams({
exactCurrency: Currency.wrap(V4_SEPOLIA_OP_ADDRESS),
path: exactInPathKey,
exactAmount: uint128(amountIn)
});

(
int128[] memory expectedDeltaAmounts,
uint160[] memory expectedSqrtPriceX96After,
uint32[] memory expectedInitializedTicksLoaded
) = quoter.quoteExactInput(exactInParams);

uint256 expectedAmountOut = uint256(uint128(-expectedDeltaAmounts[exactInPathKey.length])); // negate the final delta amount out
assertEqUint(amountOut, expectedAmountOut);
assertEqUint(sqrtPriceX96After[0], expectedSqrtPriceX96After[0]);
assertEqUint(initializedTicksLoaded[0], expectedInitializedTicksLoaded[0]);

// mixed quoter doesn't support exact out by design, but we can cross check the final amount in will equate the original input amount in,
// if we call the v4 quoter for the exact out quote. v3 and v4 quoter support exact out quote
PathKey[] memory exactOutPathKey = new PathKey[](1);
exactOutPathKey[0] = PathKey({
intermediateCurrency: Currency.wrap(V4_SEPOLIA_OP_ADDRESS),
fee: fee,
tickSpacing: int24(tickSpacing),
hooks: IHooks(hooks),
hookData: hookData
});

IQuoter.QuoteExactParams memory exactOutParams = IQuoter.QuoteExactParams({
exactCurrency: Currency.wrap(V4_SEPOLIA_USDC_ADDRESS),
path: exactOutPathKey,
exactAmount: uint128(expectedAmountOut)
});

(int128[] memory expectedDeltaAmountsIn, , ) = quoter.quoteExactOutput(exactOutParams);
uint256 expectedAmountIn = uint256(uint128(expectedDeltaAmountsIn[0])); // final delta amount in is the positive amount in the first array element
assertApproxEqAbs(amountIn, expectedAmountIn, 1);
}
}
27 changes: 27 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@openzeppelin/[email protected]":
version "4.7.0"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.7.0.tgz#3092d70ea60e3d1835466266b1d68ad47035a2d5"
integrity sha512-52Qb+A1DdOss8QvJrijYYPSf32GUg2pGaG/yCxtaA3cu4jduouTdg4XZSMLW9op54m1jH7J8hoajhHKOPsoJFw==

"@uniswap/[email protected]":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@uniswap/universal-router/-/universal-router-1.6.0.tgz#3d7372e98a0303c70587802ee6841b8b6b42fc6f"
integrity sha512-Gt0b0rtMV1vSrgXY3vz5R1RCZENB+rOkbOidY9GvcXrK1MstSrQSOAc+FCr8FSgsDhmRAdft0lk5YUxtM9i9Lg==
dependencies:
"@openzeppelin/contracts" "4.7.0"
"@uniswap/v2-core" "1.0.1"
"@uniswap/v3-core" "1.0.0"

"@uniswap/[email protected]":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@uniswap/v2-core/-/v2-core-1.0.1.tgz#af8f508bf183204779938969e2e54043e147d425"
integrity sha512-MtybtkUPSyysqLY2U210NBDeCHX+ltHt3oADGdjqoThZaFRDKwM6k1Nb3F0A3hk5hwuQvytFWhrWHOEq6nVJ8Q==

"@uniswap/[email protected]":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@uniswap/v3-core/-/v3-core-1.0.0.tgz#6c24adacc4c25dceee0ba3ca142b35adbd7e359d"
integrity sha512-kSC4djMGKMHj7sLMYVnn61k9nu+lHjMIxgg9CDQT+s2QYLoA56GbSK9Oxr+qJXzzygbkrmuY6cwgP6cW2JXPFA==