Skip to content

Commit

Permalink
Only quote the 100% amount when there's only 1 route (Uniswap#380)
Browse files Browse the repository at this point in the history
* Only quote 100% when there's just 1 route

* Add metrics for number of routes

* check for the presence of routes

* Update quoters to include their version
  • Loading branch information
mikeki authored Aug 30, 2023
1 parent 18d642f commit 7c580a4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ import {
} from './gas-models/gas-model';
import { MixedRouteHeuristicGasModelFactory } from './gas-models/mixedRoute/mixed-route-heuristic-gas-model';
import { V2HeuristicGasModelFactory } from './gas-models/v2/v2-heuristic-gas-model';
import { NATIVE_OVERHEAD } from './gas-models/v3/gas-costs';
import { V3HeuristicGasModelFactory } from './gas-models/v3/v3-heuristic-gas-model';
import { GetQuotesResult, MixedQuoter, V2Quoter, V3Quoter } from './quoters';
import { NATIVE_OVERHEAD } from './gas-models/v3/gas-costs';

export type AlphaRouterParams = {
/**
Expand Down Expand Up @@ -1243,7 +1243,7 @@ export class AlphaRouter
? await this.l2GasDataProvider!.getGasData()
: undefined,
{ blockNumber }
);
);
metric.putMetric(
'SimulateTransaction',
Date.now() - beforeSimulate,
Expand Down Expand Up @@ -1485,6 +1485,7 @@ export class AlphaRouter
this.v3Quoter.getRoutesThenQuotes(
tokenIn,
tokenOut,
amount,
amounts,
percents,
quoteToken,
Expand Down Expand Up @@ -1517,6 +1518,7 @@ export class AlphaRouter
this.v2Quoter.getRoutesThenQuotes(
tokenIn,
tokenOut,
amount,
amounts,
percents,
quoteToken,
Expand Down Expand Up @@ -1552,6 +1554,7 @@ export class AlphaRouter
this.mixedQuoter.getRoutesThenQuotes(
tokenIn,
tokenOut,
amount,
amounts,
percents,
quoteToken,
Expand Down
35 changes: 30 additions & 5 deletions src/routers/alpha-router/quoters/base-quoter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BigNumber } from '@ethersproject/bignumber';
import { Protocol } from '@uniswap/router-sdk';
import { ChainId, Currency, Token, TradeType } from '@uniswap/sdk-core';
import { Pair } from '@uniswap/v2-sdk';
import { Pool } from '@uniswap/v3-sdk';
import _ from 'lodash';

import { ITokenListProvider, ITokenProvider, ITokenValidatorProvider, TokenValidationResult } from '../../../providers';
import { CurrencyAmount, log, poolToString } from '../../../util';
import { CurrencyAmount, log, metric, MetricLoggerUnit, poolToString } from '../../../util';
import { MixedRoute, V2Route, V3Route } from '../../router';
import { AlphaRouterConfig } from '../alpha-router';
import { RouteWithValidQuote } from '../entities/route-with-valid-quote';
Expand All @@ -32,17 +33,20 @@ export abstract class BaseQuoter<
> {
protected tokenProvider: ITokenProvider;
protected chainId: ChainId;
protected protocol: Protocol;
protected blockedTokenListProvider?: ITokenListProvider;
protected tokenValidatorProvider?: ITokenValidatorProvider;

constructor(
tokenProvider: ITokenProvider,
chainId: ChainId,
protocol: Protocol,
blockedTokenListProvider?: ITokenListProvider,
tokenValidatorProvider?: ITokenValidatorProvider
) {
this.tokenProvider = tokenProvider;
this.chainId = chainId;
this.protocol = protocol;
this.blockedTokenListProvider = blockedTokenListProvider;
this.tokenValidatorProvider = tokenValidatorProvider;
}
Expand Down Expand Up @@ -110,6 +114,7 @@ export abstract class BaseQuoter<
public getRoutesThenQuotes(
tokenIn: Token,
tokenOut: Token,
amount: CurrencyAmount,
amounts: CurrencyAmount[],
percents: number[],
quoteToken: Token,
Expand All @@ -120,8 +125,28 @@ export abstract class BaseQuoter<
gasPriceWei?: BigNumber
): Promise<GetQuotesResult> {
return this.getRoutes(tokenIn, tokenOut, candidatePools, tradeType, routingConfig)
.then((routesResult) =>
this.getQuotes(
.then((routesResult) => {
if (routesResult.routes.length == 1) {
metric.putMetric(`${this.protocol}QuoterSingleRoute`, 1, MetricLoggerUnit.Count);
percents = [100];
amounts = [amount];
}

if (routesResult.routes.length > 0) {
metric.putMetric(
`${this.protocol}QuoterRoutesFound`,
routesResult.routes.length,
MetricLoggerUnit.Count
);
} else {
metric.putMetric(
`${this.protocol}QuoterNoRoutesFound`,
routesResult.routes.length,
MetricLoggerUnit.Count
);
}

return this.getQuotes(
routesResult.routes,
amounts,
percents,
Expand All @@ -131,8 +156,8 @@ export abstract class BaseQuoter<
routesResult.candidatePools,
gasModel,
gasPriceWei
)
);
);
});
}

protected async applyTokenValidatorToPools<T extends Pool | Pair>(
Expand Down
3 changes: 2 additions & 1 deletion src/routers/alpha-router/quoters/mixed-quoter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Protocol } from '@uniswap/router-sdk';
import { ChainId, Currency, Token, TradeType } from '@uniswap/sdk-core';
import _ from 'lodash';

Expand Down Expand Up @@ -46,7 +47,7 @@ export class MixedQuoter extends BaseQuoter<[V3CandidatePools, V2CandidatePools]
blockedTokenListProvider?: ITokenListProvider,
tokenValidatorProvider?: ITokenValidatorProvider
) {
super(tokenProvider, chainId, blockedTokenListProvider, tokenValidatorProvider);
super(tokenProvider, chainId, Protocol.MIXED, blockedTokenListProvider, tokenValidatorProvider);
this.v3SubgraphProvider = v3SubgraphProvider;
this.v3PoolProvider = v3PoolProvider;
this.v2SubgraphProvider = v2SubgraphProvider;
Expand Down
15 changes: 4 additions & 11 deletions src/routers/alpha-router/quoters/v2-quoter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber } from '@ethersproject/bignumber';
import { Protocol } from '@uniswap/router-sdk';
import { ChainId, Currency, Token, TradeType } from '@uniswap/sdk-core';
import _ from 'lodash';

Expand All @@ -11,21 +12,12 @@ import {
IV2SubgraphProvider,
TokenValidationResult,
} from '../../../providers';
import {
CurrencyAmount,
log,
metric,
MetricLoggerUnit,
routeToString,
} from '../../../util';
import { CurrencyAmount, log, metric, MetricLoggerUnit, routeToString, } from '../../../util';
import { V2Route } from '../../router';
import { AlphaRouterConfig } from '../alpha-router';
import { V2RouteWithValidQuote } from '../entities';
import { computeAllV2Routes } from '../functions/compute-all-routes';
import {
CandidatePoolsBySelectionCriteria,
V2CandidatePools,
} from '../functions/get-candidate-pools';
import { CandidatePoolsBySelectionCriteria, V2CandidatePools, } from '../functions/get-candidate-pools';
import { IGasModel, IV2GasModelFactory } from '../gas-models';

import { BaseQuoter } from './base-quoter';
Expand All @@ -51,6 +43,7 @@ export class V2Quoter extends BaseQuoter<V2CandidatePools, V2Route> {
super(
tokenProvider,
chainId,
Protocol.V2,
blockedTokenListProvider,
tokenValidatorProvider
);
Expand Down
3 changes: 2 additions & 1 deletion src/routers/alpha-router/quoters/v3-quoter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Protocol } from '@uniswap/router-sdk';
import { ChainId, Currency, Token, TradeType } from '@uniswap/sdk-core';
import _ from 'lodash';

Expand Down Expand Up @@ -36,7 +37,7 @@ export class V3Quoter extends BaseQuoter<V3CandidatePools, V3Route> {
blockedTokenListProvider?: ITokenListProvider,
tokenValidatorProvider?: ITokenValidatorProvider
) {
super(tokenProvider, chainId, blockedTokenListProvider, tokenValidatorProvider);
super(tokenProvider, chainId, Protocol.V3, blockedTokenListProvider, tokenValidatorProvider);
this.v3SubgraphProvider = v3SubgraphProvider;
this.v3PoolProvider = v3PoolProvider;
this.onChainQuoteProvider = onChainQuoteProvider;
Expand Down

0 comments on commit 7c580a4

Please sign in to comment.