Skip to content

Commit

Permalink
chore: use v4 quoter gas estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 committed Sep 20, 2024
1 parent ba20c8b commit ef4b691
Show file tree
Hide file tree
Showing 2 changed files with 53 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,
};
}
}
24 changes: 18 additions & 6 deletions src/util/gas-factory-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
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 { Pool as V4Pool } from '@uniswap/v4-sdk/dist/entities/pool';
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 +38,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 @@ -388,6 +389,14 @@ export function initSwapRouteFromExisting(
const tradeType = swapRoute.trade.tradeType.valueOf()
? TradeType.EXACT_OUTPUT
: TradeType.EXACT_INPUT;
const mixedRoutesContainsV4Pool = swapRoute.route.some(
(route) =>
route.protocol === Protocol.MIXED &&
(route as MixedRouteWithValidQuote).route.pools.some(
(pool) => pool instanceof V4Pool
)
);

const routesWithValidQuote = swapRoute.route.map((route) => {
switch (route.protocol) {
case Protocol.V4:
Expand Down Expand Up @@ -475,7 +484,10 @@ export function initSwapRouteFromExisting(
BigNumber.from(num)
),
initializedTicksCrossedList: [...route.initializedTicksCrossedList],
quoterGasEstimate: BigNumber.from(route.gasEstimate),
// If any mixed route contains v4 pool, then it used the mixed quoter v2 that includes v4 pool
quoterGasEstimate: mixedRoutesContainsV4Pool
? BigNumber.from(route.quoterGasEstimate)
: BigNumber.from(route.gasEstimate),
percent: route.percent,
route: route.route,
mixedRouteGasModel: route.gasModel,
Expand Down

0 comments on commit ef4b691

Please sign in to comment.