diff --git a/packages/transaction-controller/src/utils/gas.test.ts b/packages/transaction-controller/src/utils/gas.test.ts index b5dfebdf155..97c34c0e3cc 100644 --- a/packages/transaction-controller/src/utils/gas.test.ts +++ b/packages/transaction-controller/src/utils/gas.test.ts @@ -12,6 +12,7 @@ import { updateGas, FIXED_GAS, DEFAULT_GAS_MULTIPLIER, + GAS_ESTIMATE_FALLBACK_MULTIPLIER, } from './gas'; jest.mock('@metamask/controller-utils', () => ({ @@ -264,8 +265,10 @@ describe('gas', () => { }); describe('on estimate query error', () => { - it('sets gas to 95% of block gas limit', async () => { - const fallbackGas = Math.floor(BLOCK_GAS_LIMIT_MOCK * 0.95); + it('sets gas to 35% of block gas limit', async () => { + const fallbackGas = Math.floor( + BLOCK_GAS_LIMIT_MOCK * GAS_ESTIMATE_FALLBACK_MULTIPLIER, + ); mockQuery({ getBlockByNumberResponse: { @@ -352,8 +355,10 @@ describe('gas', () => { }); }); - it('returns estimated gas as 95% of block gas limit on error', async () => { - const fallbackGas = Math.floor(BLOCK_GAS_LIMIT_MOCK * 0.95); + it('returns estimated gas as 35% of block gas limit on error', async () => { + const fallbackGas = Math.floor( + BLOCK_GAS_LIMIT_MOCK * GAS_ESTIMATE_FALLBACK_MULTIPLIER, + ); mockQuery({ getBlockByNumberResponse: { diff --git a/packages/transaction-controller/src/utils/gas.ts b/packages/transaction-controller/src/utils/gas.ts index d0095481f9c..91f83778d54 100644 --- a/packages/transaction-controller/src/utils/gas.ts +++ b/packages/transaction-controller/src/utils/gas.ts @@ -1,11 +1,6 @@ /* eslint-disable jsdoc/require-jsdoc */ -import { - BNToHex, - fractionBN, - hexToBN, - query, -} from '@metamask/controller-utils'; +import { BNToHex, hexToBN, query } from '@metamask/controller-utils'; import type EthQuery from '@metamask/eth-query'; import type { Hex } from '@metamask/utils'; import { add0x, createModuleLogger } from '@metamask/utils'; @@ -25,6 +20,7 @@ export const log = createModuleLogger(projectLogger, 'gas'); export const FIXED_GAS = '0x5208'; export const DEFAULT_GAS_MULTIPLIER = 1.5; +export const GAS_ESTIMATE_FALLBACK_MULTIPLIER = 0.35; export async function updateGas(request: UpdateGasRequest) { const { txMeta } = request; @@ -60,7 +56,7 @@ export async function estimateGas( const gasLimitBN = hexToBN(gasLimitHex); request.data = data ? add0x(data) : data; - request.gas = BNToHex(fractionBN(gasLimitBN, 19, 20)); + request.gas = BNToHex(gasLimitBN.muln(GAS_ESTIMATE_FALLBACK_MULTIPLIER)); request.value = value || '0x0'; let estimatedGas = request.gas; @@ -136,8 +132,12 @@ async function getGas( request.ethQuery, ); - if (isCustomNetwork) { - log('Using original estimate as custom network'); + if (isCustomNetwork || simulationFails) { + log( + isCustomNetwork + ? 'Using original estimate as custom network' + : 'Using original fallback estimate as simulation failed', + ); return [estimatedGas, simulationFails]; }