diff --git a/src/routers/alpha-router/alpha-router.ts b/src/routers/alpha-router/alpha-router.ts index e770d9cf1..e2cc3cbe9 100644 --- a/src/routers/alpha-router/alpha-router.ts +++ b/src/routers/alpha-router/alpha-router.ts @@ -1903,6 +1903,7 @@ export class AlphaRouter ); const beforeGetQuotes = Date.now(); + const quoteToken = quoteCurrency.wrapped; quotePromises.push( this.v3Quoter @@ -1910,7 +1911,7 @@ export class AlphaRouter v3RoutesFromCache, amounts, percents, - quoteCurrency, + quoteToken, tradeType, routingConfig, undefined, @@ -1940,6 +1941,7 @@ export class AlphaRouter ); const beforeGetQuotes = Date.now(); + const quoteToken = quoteCurrency.wrapped; quotePromises.push( this.v2Quoter @@ -1949,7 +1951,7 @@ export class AlphaRouter v2RoutesFromCache, amounts, percents, - quoteCurrency, + quoteToken, tradeType, routingConfig, gasPriceWei @@ -2201,7 +2203,7 @@ export class AlphaRouter v4CandidatePools!, tradeType, routingConfig, - v3GasModel + v4GasModel ) .then((result) => { metric.putMetric( diff --git a/src/routers/alpha-router/entities/route-with-valid-quote.ts b/src/routers/alpha-router/entities/route-with-valid-quote.ts index c73fbdbc0..aea5cf872 100644 --- a/src/routers/alpha-router/entities/route-with-valid-quote.ts +++ b/src/routers/alpha-router/entities/route-with-valid-quote.ts @@ -74,7 +74,7 @@ export type V2RouteWithValidQuoteParams = { percent: number; route: V2Route; gasModel: IGasModel; - quoteCurrency: Currency; + quoteToken: Token; tradeType: TradeType; v2PoolProvider: IV2PoolProvider; }; @@ -95,7 +95,7 @@ export class V2RouteWithValidQuote implements IV2RouteWithValidQuote { public quoteAdjustedForGas: CurrencyAmount; public percent: number; public route: V2Route; - public quoteCurrency: Currency; + public quoteToken: Token; public gasModel: IGasModel; public gasEstimate: BigNumber; public gasCostInToken: CurrencyAmount; @@ -119,20 +119,17 @@ export class V2RouteWithValidQuote implements IV2RouteWithValidQuote { percent, route, gasModel, - quoteCurrency, + quoteToken, tradeType, v2PoolProvider, }: V2RouteWithValidQuoteParams) { this.amount = amount; this.rawQuote = rawQuote; - this.quote = CurrencyAmount.fromRawAmount( - quoteCurrency, - rawQuote.toString() - ); + this.quote = CurrencyAmount.fromRawAmount(quoteToken, rawQuote.toString()); this.percent = percent; this.route = route; this.gasModel = gasModel; - this.quoteCurrency = quoteCurrency; + this.quoteToken = quoteToken; this.tradeType = tradeType; const { gasEstimate, gasCostInToken, gasCostInUSD, gasCostInGasToken } = @@ -170,7 +167,7 @@ export type V3RouteWithValidQuoteParams = { percent: number; route: V3Route; gasModel: IGasModel; - quoteCurrency: Currency; + quoteToken: Token; tradeType: TradeType; v3PoolProvider: IV3PoolProvider; }; @@ -194,7 +191,7 @@ export class V3RouteWithValidQuote implements IV3RouteWithValidQuote { public quoterGasEstimate: BigNumber; public percent: number; public route: V3Route; - public quoteCurrency: Currency; + public quoteToken: Token; public gasModel: IGasModel; public gasEstimate: BigNumber; public gasCostInToken: CurrencyAmount; @@ -221,7 +218,7 @@ export class V3RouteWithValidQuote implements IV3RouteWithValidQuote { percent, route, gasModel, - quoteCurrency, + quoteToken, tradeType, v3PoolProvider, }: V3RouteWithValidQuoteParams) { @@ -230,14 +227,11 @@ export class V3RouteWithValidQuote implements IV3RouteWithValidQuote { this.sqrtPriceX96AfterList = sqrtPriceX96AfterList; this.initializedTicksCrossedList = initializedTicksCrossedList; this.quoterGasEstimate = quoterGasEstimate; - this.quote = CurrencyAmount.fromRawAmount( - quoteCurrency, - rawQuote.toString() - ); + this.quote = CurrencyAmount.fromRawAmount(quoteToken, rawQuote.toString()); this.percent = percent; this.route = route; this.gasModel = gasModel; - this.quoteCurrency = quoteCurrency; + this.quoteToken = quoteToken; this.tradeType = tradeType; const { gasEstimate, gasCostInToken, gasCostInUSD, gasCostInGasToken } = diff --git a/src/routers/alpha-router/functions/best-swap-route.ts b/src/routers/alpha-router/functions/best-swap-route.ts index 985a52ba5..19d25fd82 100644 --- a/src/routers/alpha-router/functions/best-swap-route.ts +++ b/src/routers/alpha-router/functions/best-swap-route.ts @@ -18,6 +18,7 @@ import { AlphaRouterConfig } from '../alpha-router'; import { IGasModel, L1ToL2GasCosts, usdGasTokensByChain } from '../gas-models'; import { + MixedRouteWithValidQuote, RouteWithValidQuote, V2RouteWithValidQuote, V3RouteWithValidQuote, @@ -492,8 +493,13 @@ export async function getBestSwapRouteBy( gasUsedL1OnL2: BigNumber.from(0), gasCostL1USD: CurrencyAmount.fromRawAmount(usdToken, 0), gasCostL1QuoteToken: CurrencyAmount.fromRawAmount( - // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain - bestSwap[0]?.quoteCurrency!, + bestSwap[0] && + (bestSwap[0] instanceof V4RouteWithValidQuote || + bestSwap[0] instanceof MixedRouteWithValidQuote) + ? // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain + bestSwap[0]?.quoteCurrency! + : // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain + bestSwap[0]?.quoteToken!, 0 ), }; diff --git a/src/routers/alpha-router/quoters/v2-quoter.ts b/src/routers/alpha-router/quoters/v2-quoter.ts index 53cd86f67..ccc8b76b5 100644 --- a/src/routers/alpha-router/quoters/v2-quoter.ts +++ b/src/routers/alpha-router/quoters/v2-quoter.ts @@ -136,7 +136,7 @@ export class V2Quoter extends BaseQuoter { routes: V2Route[], amounts: CurrencyAmount[], percents: number[], - quoteCurrency: Currency, + quoteToken: Token, tradeType: TradeType, routingConfig: AlphaRouterConfig, candidatePools?: CandidatePoolsBySelectionCriteria, @@ -186,14 +186,14 @@ export class V2Quoter extends BaseQuoter { chainId: this.chainId, gasPriceWei, poolProvider: this.v2PoolProvider, - token: quoteCurrency.wrapped, + token: quoteToken, l2GasDataProvider: this.l2GasDataProvider, providerConfig: { ...routingConfig, additionalGasOverhead: NATIVE_OVERHEAD( this.chainId, amountToken, - quoteCurrency + quoteToken ), gasToken, }, @@ -240,7 +240,7 @@ export class V2Quoter extends BaseQuoter { amount, percent, gasModel: v2GasModel, - quoteCurrency: quoteCurrency, + quoteToken, tradeType, v2PoolProvider: this.v2PoolProvider, }); @@ -267,7 +267,7 @@ export class V2Quoter extends BaseQuoter { routes: V2Route[], amounts: CurrencyAmount[], percents: number[], - quoteCurrency: Currency, + quoteToken: Token, tradeType: TradeType, routingConfig: AlphaRouterConfig, gasPriceWei?: BigNumber @@ -291,7 +291,7 @@ export class V2Quoter extends BaseQuoter { routes, amounts, percents, - quoteCurrency, + quoteToken, tradeType, routingConfig, undefined, diff --git a/src/routers/alpha-router/quoters/v3-quoter.ts b/src/routers/alpha-router/quoters/v3-quoter.ts index d69f97684..691044be6 100644 --- a/src/routers/alpha-router/quoters/v3-quoter.ts +++ b/src/routers/alpha-router/quoters/v3-quoter.ts @@ -128,7 +128,7 @@ export class V3Quoter extends BaseQuoter { routes: V3Route[], amounts: CurrencyAmount[], percents: number[], - quoteCurrency: Currency, + quoteToken: Token, tradeType: TradeType, routingConfig: AlphaRouterConfig, candidatePools?: CandidatePoolsBySelectionCriteria, @@ -223,7 +223,7 @@ export class V3Quoter extends BaseQuoter { initializedTicksCrossedList, quoterGasEstimate: gasEstimate, gasModel, - quoteCurrency, + quoteToken, tradeType, v3PoolProvider: this.v3PoolProvider, }); diff --git a/src/routers/legacy-router/legacy-router.ts b/src/routers/legacy-router/legacy-router.ts index 5eaa5e9d0..23213fd5f 100644 --- a/src/routers/legacy-router/legacy-router.ts +++ b/src/routers/legacy-router/legacy-router.ts @@ -317,7 +317,7 @@ export class LegacyRouter { initializedTicksCrossedList: [], quoterGasEstimate: BigNumber.from(0), tradeType: routeType, - quoteCurrency: quoteToken, + quoteToken: quoteToken, v3PoolProvider: this.poolProvider, }); }); diff --git a/src/util/gas-factory-helpers.ts b/src/util/gas-factory-helpers.ts index 1ea929129..624b43b75 100644 --- a/src/util/gas-factory-helpers.ts +++ b/src/util/gas-factory-helpers.ts @@ -435,12 +435,12 @@ export function initSwapRouteFromExisting( percent: route.percent, route: route.route, gasModel: route.gasModel, - quoteCurrency: new Token( + quoteToken: new Token( currencyIn.chainId, - route.quoteCurrency.wrapped.address, - route.quoteCurrency.decimals, - route.quoteCurrency.symbol, - route.quoteCurrency.name + route.quoteToken.address, + route.quoteToken.decimals, + route.quoteToken.symbol, + route.quoteToken.name ), tradeType: tradeType, v3PoolProvider: v3PoolProvider, @@ -456,12 +456,12 @@ export function initSwapRouteFromExisting( percent: route.percent, route: route.route, gasModel: route.gasModel, - quoteCurrency: new Token( + quoteToken: new Token( currencyIn.chainId, - route.quoteCurrency.wrapped.address, - route.quoteCurrency.decimals, - route.quoteCurrency.symbol, - route.quoteCurrency.name + route.quoteToken.address, + route.quoteToken.decimals, + route.quoteToken.symbol, + route.quoteToken.name ), tradeType: tradeType, v2PoolProvider: v2PoolProvider, diff --git a/test/unit/providers/caching/route/test-util/mocked-dependencies.ts b/test/unit/providers/caching/route/test-util/mocked-dependencies.ts index 87c4feed3..3c41971b6 100644 --- a/test/unit/providers/caching/route/test-util/mocked-dependencies.ts +++ b/test/unit/providers/caching/route/test-util/mocked-dependencies.ts @@ -43,7 +43,7 @@ export function getV2RouteWithValidQuoteStub( percent: 100, route, gasModel: getMockedV2GasModel(), - quoteCurrency: DAI, + quoteToken: DAI, tradeType: TradeType.EXACT_INPUT, v2PoolProvider: getMockedV2PoolProvider(), ...overrides, diff --git a/test/unit/routers/alpha-router/alpha-router.test.ts b/test/unit/routers/alpha-router/alpha-router.test.ts index 8ee1fe0a2..cbd126d43 100644 --- a/test/unit/routers/alpha-router/alpha-router.test.ts +++ b/test/unit/routers/alpha-router/alpha-router.test.ts @@ -399,7 +399,7 @@ describe('alpha router', () => { return { gasEstimate: BigNumber.from(10000), gasCostInToken: CurrencyAmount.fromRawAmount( - r.quoteCurrency, + r.quoteToken, r.quote.multiply(new Fraction(95, 100)).quotient ), gasCostInUSD: CurrencyAmount.fromRawAmount( @@ -445,7 +445,7 @@ describe('alpha router', () => { return { gasEstimate: BigNumber.from(10000), gasCostInToken: CurrencyAmount.fromRawAmount( - r.quoteCurrency, + r.quoteToken, r.quote.multiply(new Fraction(95, 100)).quotient ), gasCostInUSD: CurrencyAmount.fromRawAmount( @@ -493,6 +493,7 @@ describe('alpha router', () => { multicall2Provider: mockMulticallProvider as any, v4SubgraphProvider: mockV4SubgraphProvider, v4PoolProvider: mockV4PoolProvider, + v4GasModelFactory: mockV4GasModelFactory, v3SubgraphProvider: mockV3SubgraphProvider, v3PoolProvider: mockV3PoolProvider, onChainQuoteProvider: mockOnChainQuoteProvider, diff --git a/test/unit/routers/alpha-router/functions/best-swap-route.test.ts b/test/unit/routers/alpha-router/functions/best-swap-route.test.ts index 4e6db0341..5d6b49136 100644 --- a/test/unit/routers/alpha-router/functions/best-swap-route.test.ts +++ b/test/unit/routers/alpha-router/functions/best-swap-route.test.ts @@ -158,7 +158,7 @@ describe('get best swap route', () => { quote: number, percent: number ): V3RouteWithValidQuote => { - const quoteToken = + const quoteCurrency = tradeType == TradeType.EXACT_OUTPUT ? route.output : route.input; return new V3RouteWithValidQuote({ amount, @@ -169,7 +169,7 @@ describe('get best swap route', () => { percent, route, gasModel: mockV3GasModel, - quoteToken, + quoteToken: quoteCurrency, tradeType, v3PoolProvider: mockV3PoolProvider, }); @@ -208,7 +208,7 @@ describe('get best swap route', () => { percent, route, gasModel: mockV2GasModel, - quoteCurrency: quoteToken, + quoteToken, tradeType, v2PoolProvider: mockV2PoolProvider, }); diff --git a/test/unit/routers/alpha-router/gas-models/test-util/mocked-dependencies.ts b/test/unit/routers/alpha-router/gas-models/test-util/mocked-dependencies.ts index 7157ca899..afcda32c0 100644 --- a/test/unit/routers/alpha-router/gas-models/test-util/mocked-dependencies.ts +++ b/test/unit/routers/alpha-router/gas-models/test-util/mocked-dependencies.ts @@ -45,7 +45,7 @@ export function getMockedMixedGasModel(): IGasModel { mockMixedGasModel.estimateGasCost.callsFake((r) => { return { gasEstimate: BigNumber.from(10000), - gasCostInToken: CurrencyAmount.fromRawAmount(r.quoteToken, 0), + gasCostInToken: CurrencyAmount.fromRawAmount(r.quoteCurrency, 0), gasCostInUSD: CurrencyAmount.fromRawAmount(USDC, 0), }; }); diff --git a/test/unit/routers/alpha-router/util/gas-factory-helpers.test.ts b/test/unit/routers/alpha-router/util/gas-factory-helpers.test.ts index 141e35a83..91d6bf9f6 100644 --- a/test/unit/routers/alpha-router/util/gas-factory-helpers.test.ts +++ b/test/unit/routers/alpha-router/util/gas-factory-helpers.test.ts @@ -111,7 +111,7 @@ describe('gas factory helpers tests', () => { const mockPoolProvider = getMockedV3PoolProvider(); const amountToken = WRAPPED_NATIVE_CURRENCY[1]; - const quoteToken = DAI_MAINNET; + const quoteCurrency = DAI_MAINNET; const gasToken = USDC_MAINNET; const providerConfig = { gasToken @@ -119,7 +119,7 @@ describe('gas factory helpers tests', () => { const pools = await getPools( amountToken, - quoteToken, + quoteCurrency, mockPoolProvider, providerConfig, gasToken @@ -130,27 +130,27 @@ describe('gas factory helpers tests', () => { gasPriceWei, pools, amountToken, - quoteToken, + quoteToken: quoteCurrency, v2poolProvider: getMockedV2PoolProvider(), l2GasDataProvider: undefined, providerConfig }); const mockSwapRoute: SwapRoute = { - quote: CurrencyAmount.fromRawAmount(quoteToken, 100), - quoteGasAdjusted: CurrencyAmount.fromRawAmount(quoteToken, 100), + quote: CurrencyAmount.fromRawAmount(quoteCurrency, 100), + quoteGasAdjusted: CurrencyAmount.fromRawAmount(quoteCurrency, 100), // these are all 0 before the function is called estimatedGasUsed: BigNumber.from(0), - estimatedGasUsedQuoteToken: CurrencyAmount.fromRawAmount(quoteToken, 0), - estimatedGasUsedUSD: CurrencyAmount.fromRawAmount(quoteToken, 0), + estimatedGasUsedQuoteToken: CurrencyAmount.fromRawAmount(quoteCurrency, 0), + estimatedGasUsedUSD: CurrencyAmount.fromRawAmount(quoteCurrency, 0), estimatedGasUsedGasToken: undefined, gasPriceWei, trade: new Trade({ v4Routes: [], v3Routes: [{ - routev3: new Route([DAI_WETH_MEDIUM], amountToken, quoteToken), + routev3: new Route([DAI_WETH_MEDIUM], amountToken, quoteCurrency), inputAmount: CurrencyAmount.fromRawAmount(amountToken, 1), - outputAmount: CurrencyAmount.fromRawAmount(quoteToken, 100), + outputAmount: CurrencyAmount.fromRawAmount(quoteCurrency, 100), }], v2Routes: [], mixedRoutes: [], @@ -159,12 +159,12 @@ describe('gas factory helpers tests', () => { route: [new V3RouteWithValidQuote({ amount: CurrencyAmount.fromRawAmount(amountToken, 1), rawQuote: BigNumber.from('100'), - quoteToken, + quoteToken: quoteCurrency, sqrtPriceX96AfterList: [], initializedTicksCrossedList: [1], quoterGasEstimate: BigNumber.from(100000), percent: 100, - route: new V3Route([DAI_WETH_MEDIUM], amountToken, quoteToken), + route: new V3Route([DAI_WETH_MEDIUM], amountToken, quoteCurrency), tradeType: TradeType.EXACT_INPUT, v3PoolProvider: mockPoolProvider, gasModel: v3GasModel, @@ -187,7 +187,7 @@ describe('gas factory helpers tests', () => { quoteGasAdjusted } = await calculateGasUsed(chainId, mockSwapRoute, simulatedGasUsed, getMockedV2PoolProvider(), mockPoolProvider, sinon.createStubInstance(BaseProvider), providerConfig); - expect(estimatedGasUsedQuoteToken.currency.equals(quoteToken)).toBe(true); + expect(estimatedGasUsedQuoteToken.currency.equals(quoteCurrency)).toBe(true); expect(estimatedGasUsedQuoteToken.toExact()).not.toEqual('0'); expect(estimatedGasUsedUSD.toExact()).not.toEqual('0'); expect(estimatedGasUsedGasToken?.currency.equals(gasToken)).toBe(true); @@ -202,7 +202,7 @@ describe('gas factory helpers tests', () => { } = await calculateGasUsed(chainId, mockSwapRoute, simulatedGasUsed, getMockedV2PoolProvider(), mockPoolProvider, sinon.createStubInstance(BaseProvider), providerConfig); // Arbitrum gas data should not affect the quote gas or USD amounts - expect(estimatedGasUsedQuoteTokenArb.currency.equals(quoteToken)).toBe(true); + expect(estimatedGasUsedQuoteTokenArb.currency.equals(quoteCurrency)).toBe(true); expect(estimatedGasUsedUSDArb.equalTo(estimatedGasUsedUSD)).toBe(true); expect(estimatedGasUsedGasTokenArb?.currency.equals(gasToken)).toBe(true); expect(quoteGasAdjustedArb.equalTo(quoteGasAdjusted)).toBe(true);