Skip to content

Commit

Permalink
feat: mixed route support native currency routing based on whether v4…
Browse files Browse the repository at this point in the history
… pool is involved
  • Loading branch information
jsy1218 committed Sep 23, 2024
1 parent f9f1901 commit 6865019
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2341,11 +2341,10 @@ export class AlphaRouter
v4Candidates: v4CandidatePools,
});

// TODO: ROUTE-291 - special treat mixed route native currency routing, only when the route contains v4 pools
return this.mixedQuoter
.getRoutesThenQuotes(
tokenIn,
tokenOut,
currencyIn,
currencyOut,
amount,
amounts,
percents,
Expand Down
45 changes: 44 additions & 1 deletion src/routers/alpha-router/quoters/mixed-quoter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,52 @@ export class MixedQuoter extends BaseQuoter<
const V2poolsRaw = V2poolAccessor.getAllPools();

const poolsRaw = [];
const v2V3OnlyPoolsRaw = [];
if (!routingConfig.excludedProtocolsFromMixed?.includes(Protocol.V4)) {
poolsRaw.push(...V4poolsRaw);
}
if (!routingConfig.excludedProtocolsFromMixed?.includes(Protocol.V3)) {
poolsRaw.push(...V3poolsRaw);
v2V3OnlyPoolsRaw.push(...V3poolsRaw);
}
if (!routingConfig.excludedProtocolsFromMixed?.includes(Protocol.V2)) {
poolsRaw.push(...V2poolsRaw);
v2V3OnlyPoolsRaw.push(...V2poolsRaw);
}

const candidatePools = mixedRouteCandidatePools;

// Drop any pools that contain fee on transfer tokens (not supported by v3) or have issues with being transferred.
const pools = await this.applyTokenValidatorToPools(
v2V3OnlyPoolsRaw,
(
token: Currency,
tokenValidation: TokenValidationResult | undefined
): boolean => {
// If there is no available validation result we assume the token is fine.
if (!tokenValidation) {
return false;
}

// Only filters out *intermediate* pools that involve tokens that we detect
// cant be transferred. This prevents us trying to route through tokens that may
// not be transferrable, but allows users to still swap those tokens if they
// specify.
//
if (
tokenValidation == TokenValidationResult.STF &&
(token.equals(currencyIn) || token.equals(currencyOut))
) {
return false;
}

return (
tokenValidation == TokenValidationResult.FOT ||
tokenValidation == TokenValidationResult.STF
);
}
);
const v2V3OnlyPools = await this.applyTokenValidatorToPools(
poolsRaw,
(
token: Currency,
Expand Down Expand Up @@ -186,14 +218,25 @@ export class MixedQuoter extends BaseQuoter<
maxSwapsPerPath
);

// special case mixed route w/ native currency routing
// there's a possibility upstream is requesting the native currency,
// but we might want to wrap into the wrapped token because maybe only v2/v3 have the routes.
// in that case, we have to remain the non-native currency routing as is today for mixed routing.
const v2V3OnlyRoutes = computeAllMixedRoutes(
currencyIn.wrapped,
currencyOut.wrapped,
v2V3OnlyPools,
maxSwapsPerPath
);

metric.putMetric(
'MixedGetRoutesLoad',
Date.now() - beforeGetRoutes,
MetricLoggerUnit.Milliseconds
);

return {
routes: routes,
routes: routes.concat(v2V3OnlyRoutes),
candidatePools,
};
}
Expand Down

0 comments on commit 6865019

Please sign in to comment.