Skip to content

Commit

Permalink
fix: Keep fixed execution price for stable swaps in gas estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
xrsv committed Dec 3, 2024
1 parent edc92dd commit 782a94c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -165,28 +170,41 @@ 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
const nativeAndAmountTokenPrice = inputIsToken0
? 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) {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/util/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 782a94c

Please sign in to comment.