Skip to content

Commit

Permalink
chore: use v4 quoter gas estimate (#713)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jsy1218 authored Sep 23, 2024
1 parent 3e045f4 commit 3c68789
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
35 changes: 35 additions & 0 deletions src/routers/alpha-router/gas-models/v4/v4-heuristic-gas-model.ts
Original file line number Diff line number Diff line change
@@ -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<V4RouteWithValidQuote>
implements IOnChainGasModelFactory<V4RouteWithValidQuote>
Expand Down Expand Up @@ -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,
};
}
}
12 changes: 6 additions & 6 deletions src/util/gas-factory-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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';
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 {
Expand All @@ -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';

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3c68789

Please sign in to comment.