Skip to content

Commit

Permalink
chore: bump sor to 4.17.5 - fix: mixed route support v4 ETH pool to v…
Browse files Browse the repository at this point in the history
…2/v3 WETH pool new way (#814)

* chore: bump sor to 4.17.5 - fix: mixed route support v4 ETH pool to v2/v3 WETH pool new way

* router-sdk 1.12.2

* max hops + remove fake v4 pool

* feedback

* max hop only when eth-weth

* remove is-wrapped-native

* feedback
  • Loading branch information
jsy1218 authored Jan 31, 2025
1 parent eb518b4 commit 96294f4
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 37 deletions.
54 changes: 27 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
"@types/brotli": "^1.3.4",
"@uniswap/default-token-list": "^11.13.0",
"@uniswap/permit2-sdk": "^1.3.0",
"@uniswap/router-sdk": "^1.21.0",
"@uniswap/router-sdk": "^1.21.4",
"@uniswap/sdk-core": "^7.5.0",
"@uniswap/swap-router-contracts": "^1.3.1",
"@uniswap/token-lists": "^1.0.0-beta.31",
"@uniswap/universal-router": "^1.6.0",
"@uniswap/universal-router-sdk": "^4.14.0",
"@uniswap/universal-router-sdk": "^4.15.1",
"@uniswap/v2-sdk": "^4.13.0",
"@uniswap/v3-sdk": "^3.24.0",
"@uniswap/v4-sdk": "^1.18.0",
"@uniswap/v4-sdk": "^1.18.1",
"async-retry": "^1.3.1",
"await-timeout": "^1.1.1",
"axios": "^0.21.1",
Expand Down
16 changes: 13 additions & 3 deletions src/routers/alpha-router/entities/route-with-valid-quote.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { BigNumber } from '@ethersproject/bignumber';
import { Protocol } from '@uniswap/router-sdk';
import { Currency, Token, TradeType } from '@uniswap/sdk-core';
import { Pair } from '@uniswap/v2-sdk';
import { Pool as V3Pool } from '@uniswap/v3-sdk';
import { Pool as V4Pool } from '@uniswap/v4-sdk';
import _ from 'lodash';

import { Pair } from '@uniswap/v2-sdk';
import { IV4PoolProvider } from '../../../providers';
import { IV2PoolProvider } from '../../../providers/v2/pool-provider';
import { IV3PoolProvider } from '../../../providers/v3/pool-provider';
import { CurrencyAmount } from '../../../util/amounts';
import { FAKE_TICK_SPACING } from '../../../util/pools';
import { routeToString } from '../../../util/routes';
import {
MixedRoute,
Expand Down Expand Up @@ -430,14 +431,23 @@ export class MixedRouteWithValidQuote implements IMixedRouteWithValidQuote {
v3PoolProvider,
v2PoolProvider,
}: MixedRouteWithValidQuoteParams) {
const poolsWithoutFakePool = route.pools.filter(
(p) => !(p instanceof V4Pool && p.tickSpacing === FAKE_TICK_SPACING)
);
const routeWithoutEthWethFakePool = new MixedRoute(
poolsWithoutFakePool,
route.input,
route.output
);

this.amount = amount;
this.rawQuote = rawQuote;
this.sqrtPriceX96AfterList = sqrtPriceX96AfterList;
this.initializedTicksCrossedList = initializedTicksCrossedList;
this.quoterGasEstimate = quoterGasEstimate;
this.quote = CurrencyAmount.fromRawAmount(quoteToken, rawQuote.toString());
this.percent = percent;
this.route = route;
this.route = routeWithoutEthWethFakePool;
this.gasModel = mixedRouteGasModel;
this.quoteToken = quoteToken;
this.tradeType = tradeType;
Expand All @@ -459,7 +469,7 @@ export class MixedRouteWithValidQuote implements IMixedRouteWithValidQuote {
this.quoteAdjustedForGas = quoteGasAdjusted;
}

this.poolIdentifiers = _.map(route.pools, (p) => {
this.poolIdentifiers = _.map(routeWithoutEthWethFakePool.pools, (p) => {
if (p instanceof V4Pool) {
return v4PoolProvider.getPoolId(
p.token0,
Expand Down
35 changes: 31 additions & 4 deletions src/routers/alpha-router/functions/compute-all-routes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { TPool } from '@uniswap/router-sdk/dist/utils/TPool';
import { Currency, Token } from '@uniswap/sdk-core';
import { ChainId, Currency, Token } from '@uniswap/sdk-core';
import { Pair } from '@uniswap/v2-sdk';
import { Pool as V3Pool } from '@uniswap/v3-sdk';
import { Pool as V4Pool } from '@uniswap/v4-sdk';

import { getAddressLowerCase } from '../../../util';
import { getAddressLowerCase, nativeOnChain } from '../../../util';
import { log } from '../../../util/log';
import { FAKE_TICK_SPACING, V4_ETH_WETH_FAKE_POOL } from '../../../util/pools';
import { poolToString, routeToString } from '../../../util/routes';
import {
MixedRoute,
Expand Down Expand Up @@ -75,6 +76,17 @@ export function computeAllMixedRoutes(
parts: TPool[],
maxHops: number
): MixedRoute[] {
// only add fake v4 pool, if we see there's a native v4 pool in the candidate pool
const containsV4NativePools =
parts.filter(
(pool) =>
pool instanceof V4Pool &&
pool.involvesToken(nativeOnChain(ChainId.MAINNET))
).length > 0;
// NOTE: we added a fake v4 pool, in order for mixed route to connect the v3 weth pool with v4 eth pool
const amendedPools = containsV4NativePools
? parts.concat(V4_ETH_WETH_FAKE_POOL[currencyIn.chainId as ChainId])
: parts;
const routesRaw = computeAllRoutes<TPool, MixedRoute, Currency>(
currencyIn,
currencyOut,
Expand All @@ -85,7 +97,7 @@ export function computeAllMixedRoutes(
currency.isNative
? (pool as V4Pool).involvesToken(currency)
: pool.involvesToken(currency),
parts,
amendedPools,
maxHops
);
/// filter out pure v4 and v3 and v2 routes
Expand Down Expand Up @@ -125,7 +137,22 @@ export function computeAllRoutes<
tokensVisited: Set<string>,
_previousTokenOut?: TCurrency
) => {
if (currentRoute.length > maxHops) {
const currentRouteContainsFakeV4Pool =
currentRoute.filter(
(pool) =>
pool instanceof V4Pool && pool.tickSpacing === FAKE_TICK_SPACING
).length > 0;
const amendedMaxHops = currentRouteContainsFakeV4Pool
? maxHops + 1
: maxHops;

// amendedMaxHops is the maxHops + 1 if the current route contains a fake v4 pool
// b/c we want to allow the route to go through the fake v4 pool
// also gas wise, if a route goes through the fake v4 pool, mixed quoter will add the wrap/unwrap gas cost:
// https://github.com/Uniswap/mixed-quoter/pull/41/files#diff-a4d1289f264d1da22aac20cc55a9d01c8ba9cccd76ce1af8f952ec9034e7e1aaR189
// and SOR will use the gas cost from the mixed quoter:
// https://github.com/Uniswap/smart-order-router/blob/17da523f1af050e6430afb866d96681346c8fb8b/src/routers/alpha-router/gas-models/mixedRoute/mixed-route-heuristic-gas-model.ts#L222
if (currentRoute.length > amendedMaxHops) {
return;
}

Expand Down
Loading

0 comments on commit 96294f4

Please sign in to comment.