From 936a873b3c6fe5ff204546d6353953e1c429a66a Mon Sep 17 00:00:00 2001 From: xrsv Date: Tue, 3 Dec 2024 16:14:55 -0800 Subject: [PATCH] fix: Use syntheticGasCostInTermsOfQuoteToken only if within 30% of gasCostInTermsOfQuoteToken (#776) * fix: Use syntheticGasCostInTermsOfQuoteToken only if within 30% of gasCostInTermsOfQuoteToken * asFraction * multiply percent * increase gas test deviation percent --- .../gas-models/tick-based-heuristic-gas-model.ts | 10 +++++++--- .../alpha-router/alpha-router.integration.test.ts | 2 +- 2 files changed, 8 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..fa4475b69 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 @@ -1,6 +1,6 @@ import { BigNumber } from '@ethersproject/bignumber'; import { BaseProvider } from '@ethersproject/providers'; -import { ChainId, Price } from '@uniswap/sdk-core'; +import { ChainId, Percent, Price } from '@uniswap/sdk-core'; import { Pool } from '@uniswap/v3-sdk'; import { CurrencyAmount, log, WRAPPED_NATIVE_CURRENCY } from '../../../util'; import { calculateL1GasFeesHelper } from '../../../util/gas-factory-helpers'; @@ -205,12 +205,16 @@ export abstract class TickBasedHeuristicGasModelFactory< // Note that the syntheticGasCost being lessThan the original quoted value is not always strictly better // e.g. the scenario where the amountToken/ETH pool is very illiquid as well and returns an extremely small number // however, it is better to have the gasEstimation be almost 0 than almost infinity, as the user will still receive a quote + // Only use syntheticGasCostInTermsOfQuoteToken if it's within 30% of the original gasCostInTermsOfQuoteToken as a safeguard. if ( syntheticGasCostInTermsOfQuoteToken !== null && - (gasCostInTermsOfQuoteToken === null || + (gasCostInTermsOfQuoteToken === null || ( syntheticGasCostInTermsOfQuoteToken.lessThan( gasCostInTermsOfQuoteToken.asFraction - )) + ) && + gasCostInTermsOfQuoteToken.subtract(syntheticGasCostInTermsOfQuoteToken) + .lessThan(gasCostInTermsOfQuoteToken.multiply(new Percent(30, 100)).asFraction) + )) ) { log.info( { diff --git a/test/integ/routers/alpha-router/alpha-router.integration.test.ts b/test/integ/routers/alpha-router/alpha-router.integration.test.ts index 8162f0f8a..27895d5eb 100644 --- a/test/integ/routers/alpha-router/alpha-router.integration.test.ts +++ b/test/integ/routers/alpha-router/alpha-router.integration.test.ts @@ -140,7 +140,7 @@ const GAS_ESTIMATE_DEVIATION_PERCENT: { [chainId in ChainId]: number } = { [ChainId.MAINNET]: 50, [ChainId.GOERLI]: 62, [ChainId.SEPOLIA]: 75, - [ChainId.OPTIMISM]: 71, + [ChainId.OPTIMISM]: 75, [ChainId.OPTIMISM_GOERLI]: 30, [ChainId.OPTIMISM_SEPOLIA]: 30, [ChainId.ARBITRUM_ONE]: 53,