From 782a94caa82d3e1493bb9af900fb52d8933993a1 Mon Sep 17 00:00:00 2001 From: Vasilis Xouris Date: Tue, 3 Dec 2024 12:54:50 -0800 Subject: [PATCH] fix: Keep fixed execution price for stable swaps in gas estimation --- .../tick-based-heuristic-gas-model.ts | 34 ++++++++++++++++--- src/util/chains.ts | 8 +++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/routers/alpha-router/gas-models/tick-based-heuristic-gas-model.ts b/src/routers/alpha-router/gas-models/tick-based-heuristic-gas-model.ts index 992f43310..6db64310b 100644 --- a/src/routers/alpha-router/gas-models/tick-based-heuristic-gas-model.ts +++ b/src/routers/alpha-router/gas-models/tick-based-heuristic-gas-model.ts @@ -2,7 +2,12 @@ import { BigNumber } from '@ethersproject/bignumber'; import { BaseProvider } from '@ethersproject/providers'; import { ChainId, Price } from '@uniswap/sdk-core'; import { Pool } from '@uniswap/v3-sdk'; -import { CurrencyAmount, log, WRAPPED_NATIVE_CURRENCY } from '../../../util'; +import { + CurrencyAmount, + log, + STABLE_COINS_BY_CHAIN_ID, + WRAPPED_NATIVE_CURRENCY +} from '../../../util'; import { calculateL1GasFeesHelper } from '../../../util/gas-factory-helpers'; import { V3RouteWithValidQuote, V4RouteWithValidQuote } from '../entities'; import { @@ -165,13 +170,24 @@ export abstract class TickBasedHeuristicGasModelFactory< // A pool with the non quote token / ETH should not be required and errors should be handled separately if (nativeAmountPool) { // get current execution price (amountToken / quoteToken) - const executionPrice = new Price( + let executionPrice = new Price( routeWithValidQuote.amount.currency, routeWithValidQuote.quote.currency, routeWithValidQuote.amount.quotient, routeWithValidQuote.quote.quotient ); + // If this is a stable pair, always use execution price of 1 for now. + // TODO: ROUTE-356 Use direct swap pool for swap execution price calculation. + if (this.routeIsStableCoinPair(chainId, routeWithValidQuote)) { + executionPrice = new Price( + routeWithValidQuote.amount.currency, + routeWithValidQuote.quote.currency, + routeWithValidQuote.amount.quotient, + routeWithValidQuote.amount.quotient, + ); + } + const inputIsToken0 = nativeAmountPool.token0.address == nativeCurrency.address; // ratio of input / native @@ -179,14 +195,16 @@ export abstract class TickBasedHeuristicGasModelFactory< ? nativeAmountPool.token0Price : nativeAmountPool.token1Price; - const gasCostInTermsOfAmountToken = nativeAndAmountTokenPrice.quote( + // gasCostInTermsOfAmountToken = 29.487425 | 11.0 + const gasCostInTermsOfAmountToken = nativeAndAmountTokenPrice.quote( // nativeAndAmountTokenPrice = 3554.58 totalGasCostNativeCurrency ) as CurrencyAmount; // Convert gasCostInTermsOfAmountToken to quote token using execution price let syntheticGasCostInTermsOfQuoteToken: CurrencyAmount | null; try { - syntheticGasCostInTermsOfQuoteToken = executionPrice.quote( + // syntheticGasCostInTermsOfQuoteToken = 29 | 19 | 11 + syntheticGasCostInTermsOfQuoteToken = executionPrice.quote( // executionPrice = 0.99 gasCostInTermsOfAmountToken ); } catch (err) { @@ -258,6 +276,14 @@ export abstract class TickBasedHeuristicGasModelFactory< }; } + protected routeIsStableCoinPair(chainId: ChainId, routeWithValidQuote: TRouteWithValidQuote): boolean { + const stableCoins = STABLE_COINS_BY_CHAIN_ID[chainId] || []; + return ( + stableCoins.includes(routeWithValidQuote.amount.currency.wrapped.address.toLowerCase()) && + stableCoins.includes(routeWithValidQuote.quote.currency.wrapped.address.toLowerCase()) + ); + } + protected estimateGas( routeWithValidQuote: TRouteWithValidQuote, gasPriceWei: BigNumber, diff --git a/src/util/chains.ts b/src/util/chains.ts index dd0e1badf..91dd8dcc1 100644 --- a/src/util/chains.ts +++ b/src/util/chains.ts @@ -167,6 +167,14 @@ export enum NativeCurrencyName { AVALANCHE = 'AVAX', } +export const STABLE_COINS_BY_CHAIN_ID: { [chainId: number]: string[] } = { + [ChainId.MAINNET]: [ + '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // USDC + '0x6b175474e89094c44da98b954eedeac495271d0f', // DAI + '0xdac17f958d2ee523a2206206994597c13d831ec7', // USDT + ], +} + export const NATIVE_NAMES_BY_ID: { [chainId: number]: string[] } = { [ChainId.MAINNET]: [ 'ETH',