Skip to content

Commit

Permalink
fix: route generation
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillDogadin-std committed Dec 5, 2022
1 parent 76f647d commit f2861a3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/src/calleeFunctions/UniswapV3Callee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 12 additions & 7 deletions core/src/calleeFunctions/helpers/pools.ts
Original file line number Diff line number Diff line change
@@ -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<Pool[]> => {
const fees = uniswapFees || routes.map(() => 3000);
const routeSteps = getRouteSteps(routes, fees);
export const routeToPool = async (
network: string,
route: string[],
collateralSymbol: CollateralSymbol,
uniswapFees?: number[]
): Promise<Pool[]> => {
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(
Expand Down
10 changes: 9 additions & 1 deletion core/src/calleeFunctions/helpers/uniswapAutoRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.');
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/calleeFunctions/helpers/uniswapV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
};

Expand Down
2 changes: 1 addition & 1 deletion core/src/calleeFunctions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit f2861a3

Please sign in to comment.