From f2861a32a2d1ebe7d8c176b5bbd80e560083d644 Mon Sep 17 00:00:00 2001 From: Kirill Dogadin Date: Mon, 5 Dec 2022 13:07:28 +0100 Subject: [PATCH] fix: route generation --- core/src/calleeFunctions/UniswapV3Callee.ts | 2 +- core/src/calleeFunctions/helpers/pools.ts | 19 ++++++++++++------- .../helpers/uniswapAutoRouter.ts | 10 +++++++++- core/src/calleeFunctions/helpers/uniswapV3.ts | 2 +- core/src/calleeFunctions/index.ts | 2 +- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/core/src/calleeFunctions/UniswapV3Callee.ts b/core/src/calleeFunctions/UniswapV3Callee.ts index 7dc517d51..c3de48d50 100644 --- a/core/src/calleeFunctions/UniswapV3Callee.ts +++ b/core/src/calleeFunctions/UniswapV3Callee.ts @@ -46,7 +46,7 @@ const getMarketPrice = async function ( if (!route) { throw new Error(`No route found for ${collateral.symbol} to DAI`); } - const pools = await routeToPool(network, route, fees); + const pools = await routeToPool(network, route, collateral.symbol, fees); const daiAmount = await convertCollateralToDaiUsingPool( network, collateral.symbol, diff --git a/core/src/calleeFunctions/helpers/pools.ts b/core/src/calleeFunctions/helpers/pools.ts index 73a5b3d6a..85d948320 100644 --- a/core/src/calleeFunctions/helpers/pools.ts +++ b/core/src/calleeFunctions/helpers/pools.ts @@ -1,18 +1,23 @@ import { getTokenAddressByNetworkAndSymbol } from '../../tokens'; -import { Pool } from '../../types'; +import { CollateralSymbol, Pool } from '../../types'; const getRouteSteps = (route: string[], fees: number[]) => { - const fullRoute = route[route.length - 1] === 'DAI' ? [...route] : [...route, 'DAI']; const routeSteps = []; - for (let i = 0; i < fullRoute.length - 1; i++) { - routeSteps.push({ tokens: [fullRoute[i], fullRoute[i + 1]], fee: fees[i] }); + for (let i = 0; i < route.length - 1; i++) { + routeSteps.push({ tokens: [route[i], route[i + 1]], fee: fees[i] }); } return routeSteps; }; -export const routeToPool = async (network: string, routes: string[], uniswapFees?: number[]): Promise => { - const fees = uniswapFees || routes.map(() => 3000); - const routeSteps = getRouteSteps(routes, fees); +export const routeToPool = async ( + network: string, + route: string[], + collateralSymbol: CollateralSymbol, + uniswapFees?: number[] +): Promise => { + const fees = uniswapFees || Array.from({ length: route.length + 2 }, () => 3000); + const fullRoute = [collateralSymbol, ...route, 'DAI']; + const routeSteps = getRouteSteps(fullRoute, fees); return await Promise.all( routeSteps.map(async step => ({ addresses: await Promise.all( diff --git a/core/src/calleeFunctions/helpers/uniswapAutoRouter.ts b/core/src/calleeFunctions/helpers/uniswapAutoRouter.ts index 2979c0287..b42471461 100644 --- a/core/src/calleeFunctions/helpers/uniswapAutoRouter.ts +++ b/core/src/calleeFunctions/helpers/uniswapAutoRouter.ts @@ -64,6 +64,13 @@ export const getUniswapAutoRoute = async function ( return route; }; +const trimRoute = (route: string[]): string[] => { + if (route.length < 2) { + throw new Error('Route array must have at least 2 elements.'); + } + return route.slice(1, route.length - 1); +}; + const _fetchAutoRouteInformation = async function ( network: string, collateralSymbol: string, @@ -73,12 +80,13 @@ const _fetchAutoRouteInformation = async function ( try { const autoRouteData = await getUniswapAutoRoute(network, collateralSymbol, inputAmount, walletAddress); const bestRoute = autoRouteData.route[0]; - const route = bestRoute.tokenPath.map(p => { + const fullRoute = bestRoute.tokenPath.map(p => { if (!p.symbol) { throw new Error(`Could not get symbol for token "${p.address}".`); } return p.symbol; }); + const route = trimRoute(fullRoute); if (bestRoute.route.protocol !== Protocol.V3) { throw new Error('Only V3 routes are supported.'); } diff --git a/core/src/calleeFunctions/helpers/uniswapV3.ts b/core/src/calleeFunctions/helpers/uniswapV3.ts index e2d9208db..19320027a 100644 --- a/core/src/calleeFunctions/helpers/uniswapV3.ts +++ b/core/src/calleeFunctions/helpers/uniswapV3.ts @@ -52,7 +52,7 @@ export const getRouteAndGasQuote = async ( ); return { route, quoteGasAdjusted, fees }; } else { - return { route: [collateral.symbol, ...calleeConfig.route], quoteGasAdjusted: undefined, fees: undefined }; + return { route: calleeConfig.route, quoteGasAdjusted: undefined, fees: undefined }; } }; diff --git a/core/src/calleeFunctions/index.ts b/core/src/calleeFunctions/index.ts index 361dc6954..3a7269d34 100644 --- a/core/src/calleeFunctions/index.ts +++ b/core/src/calleeFunctions/index.ts @@ -77,7 +77,7 @@ export const getPools = async ( } const route = 'route' in calleeConfig ? calleeConfig.route : await getCalleeAutoRoute(network, collateral, marketId, amount); - return await routeToPool(network, route); + return await routeToPool(network, route, collateral.symbol); }; export const enrichCalleeConfigWithPools = async (