From 3c68789177ed08a9059411d1285e472aec341a9f Mon Sep 17 00:00:00 2001 From: "Siyu Jiang (See-You John)" <91580504+jsy1218@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:36:54 -0700 Subject: [PATCH] chore: use v4 quoter gas estimate (#713) * chore: use v4 quoter gas estimate * quoterGasEstimate in gas-factory-helper should always use the RouteWithValidQuote.gasEstimate, because of the additional gas overhead from provider.additionalGasOverride --- .../gas-models/v4/v4-heuristic-gas-model.ts | 35 +++++++++++++++++++ src/util/gas-factory-helpers.ts | 12 +++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/routers/alpha-router/gas-models/v4/v4-heuristic-gas-model.ts b/src/routers/alpha-router/gas-models/v4/v4-heuristic-gas-model.ts index 5d7c50bc9..c54483ca0 100644 --- a/src/routers/alpha-router/gas-models/v4/v4-heuristic-gas-model.ts +++ b/src/routers/alpha-router/gas-models/v4/v4-heuristic-gas-model.ts @@ -1,12 +1,17 @@ +import { BigNumber } from '@ethersproject/bignumber'; import { BaseProvider } from '@ethersproject/providers'; +import { CurrencyAmount, WRAPPED_NATIVE_CURRENCY } from '../../../../util'; import { V4RouteWithValidQuote } from '../../entities'; import { BuildOnChainGasModelFactoryType, + GasModelProviderConfig, IGasModel, IOnChainGasModelFactory, } from '../gas-model'; import { TickBasedHeuristicGasModelFactory } from '../tick-based-heuristic-gas-model'; +import { ChainId } from '@uniswap/sdk-core'; + export class V4HeuristicGasModelFactory extends TickBasedHeuristicGasModelFactory implements IOnChainGasModelFactory @@ -38,4 +43,34 @@ export class V4HeuristicGasModelFactory providerConfig, }); } + + protected override estimateGas( + routeWithValidQuote: V4RouteWithValidQuote, + gasPriceWei: BigNumber, + chainId: ChainId, + providerConfig?: GasModelProviderConfig + ) { + const totalInitializedTicksCrossed = this.totalInitializedTicksCrossed( + routeWithValidQuote.initializedTicksCrossedList + ); + + const baseGasUse = routeWithValidQuote.gasEstimate + // we still need the gas override for native wrap/unwrap, because quoter doesn't simulate on universal router level + .add(providerConfig?.additionalGasOverhead ?? BigNumber.from(0)); + + const baseGasCostWei = gasPriceWei.mul(baseGasUse); + + const wrappedCurrency = WRAPPED_NATIVE_CURRENCY[chainId]!; + + const totalGasCostNativeCurrency = CurrencyAmount.fromRawAmount( + wrappedCurrency, + baseGasCostWei.toString() + ); + + return { + totalGasCostNativeCurrency, + totalInitializedTicksCrossed, + baseGasUse, + }; + } } diff --git a/src/util/gas-factory-helpers.ts b/src/util/gas-factory-helpers.ts index 63413f3f6..00a852dd4 100644 --- a/src/util/gas-factory-helpers.ts +++ b/src/util/gas-factory-helpers.ts @@ -1,6 +1,10 @@ +import { estimateL1Gas, estimateL1GasCost } from '@eth-optimism/sdk'; import { BigNumber } from '@ethersproject/bignumber'; +import { BaseProvider, TransactionRequest } from '@ethersproject/providers'; import { Protocol } from '@uniswap/router-sdk'; import { ChainId, Percent, Token, TradeType } from '@uniswap/sdk-core'; +import { UniversalRouterVersion } from '@uniswap/universal-router-sdk'; +import { Pair } from '@uniswap/v2-sdk'; import { FeeAmount, Pool } from '@uniswap/v3-sdk'; import brotli from 'brotli'; import JSBI from 'jsbi'; @@ -8,6 +12,7 @@ import _ from 'lodash'; import { IV2PoolProvider, IV4PoolProvider } from '../providers'; import { IPortionProvider } from '../providers/portion-provider'; +import { ProviderConfig } from '../providers/provider'; import { ArbitrumGasData } from '../providers/v3/gas-data-provider'; import { IV3PoolProvider } from '../providers/v3/pool-provider'; import { @@ -32,11 +37,6 @@ import { WRAPPED_NATIVE_CURRENCY, } from '../util'; -import { estimateL1Gas, estimateL1GasCost } from '@eth-optimism/sdk'; -import { BaseProvider, TransactionRequest } from '@ethersproject/providers'; -import { UniversalRouterVersion } from '@uniswap/universal-router-sdk'; -import { Pair } from '@uniswap/v2-sdk'; -import { ProviderConfig } from '../providers/provider'; import { opStackChains } from './l2FeeChains'; import { buildSwapMethodParameters, buildTrade } from './methodParameters'; @@ -402,7 +402,7 @@ export function initSwapRouteFromExisting( BigNumber.from(num) ), initializedTicksCrossedList: [...route.initializedTicksCrossedList], - quoterGasEstimate: BigNumber.from(route.quoterGasEstimate), + quoterGasEstimate: BigNumber.from(route.gasEstimate), percent: route.percent, route: route.route, gasModel: route.gasModel,