From b1d030a15544eaa643a4d92c4b7b0d2ce9130f44 Mon Sep 17 00:00:00 2001 From: Vasilis Xouris Date: Tue, 3 Dec 2024 15:13:33 -0800 Subject: [PATCH 1/4] fix: Use syntheticGasCostInTermsOfQuoteToken only if within 30% of gasCostInTermsOfQuoteToken --- .../gas-models/tick-based-heuristic-gas-model.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 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..bcb3efb2b 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 @@ -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(0.3)) + )) ) { log.info( { From 0e7ea935459cbf68b545ba8ba566725bc6b62495 Mon Sep 17 00:00:00 2001 From: Vasilis Xouris Date: Tue, 3 Dec 2024 15:15:43 -0800 Subject: [PATCH 2/4] asFraction --- .../alpha-router/gas-models/tick-based-heuristic-gas-model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 bcb3efb2b..0cae51706 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 @@ -213,7 +213,7 @@ export abstract class TickBasedHeuristicGasModelFactory< gasCostInTermsOfQuoteToken.asFraction ) && gasCostInTermsOfQuoteToken.subtract(syntheticGasCostInTermsOfQuoteToken) - .lessThan(gasCostInTermsOfQuoteToken.multiply(0.3)) + .lessThan(gasCostInTermsOfQuoteToken.multiply(0.3).asFraction) )) ) { log.info( From 269b7d07bce87b67485ead9865ca962a12fa832d Mon Sep 17 00:00:00 2001 From: Vasilis Xouris Date: Tue, 3 Dec 2024 15:46:22 -0800 Subject: [PATCH 3/4] multiply percent --- .../alpha-router/gas-models/tick-based-heuristic-gas-model.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 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 0cae51706..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'; @@ -213,7 +213,7 @@ export abstract class TickBasedHeuristicGasModelFactory< gasCostInTermsOfQuoteToken.asFraction ) && gasCostInTermsOfQuoteToken.subtract(syntheticGasCostInTermsOfQuoteToken) - .lessThan(gasCostInTermsOfQuoteToken.multiply(0.3).asFraction) + .lessThan(gasCostInTermsOfQuoteToken.multiply(new Percent(30, 100)).asFraction) )) ) { log.info( From 0e135c6ce2ff0d622b86006dbd9893b26d5ee4ee Mon Sep 17 00:00:00 2001 From: Vasilis Xouris Date: Tue, 3 Dec 2024 16:05:13 -0800 Subject: [PATCH 4/4] increase gas test deviation percent --- .../integ/routers/alpha-router/alpha-router.integration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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,