From 7891ec0cc517a824eb16578b6ae090a8eb3352dc Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Thu, 26 Sep 2024 12:48:08 +0200 Subject: [PATCH 1/3] change gas limit fallback --- packages/transaction-controller/src/utils/gas.test.ts | 8 ++++---- packages/transaction-controller/src/utils/gas.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/transaction-controller/src/utils/gas.test.ts b/packages/transaction-controller/src/utils/gas.test.ts index b5dfebdf155..dec34fbca4b 100644 --- a/packages/transaction-controller/src/utils/gas.test.ts +++ b/packages/transaction-controller/src/utils/gas.test.ts @@ -264,8 +264,8 @@ 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 * 0.35); mockQuery({ getBlockByNumberResponse: { @@ -352,8 +352,8 @@ 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 * 0.35); mockQuery({ getBlockByNumberResponse: { diff --git a/packages/transaction-controller/src/utils/gas.ts b/packages/transaction-controller/src/utils/gas.ts index d0095481f9c..818dcf736da 100644 --- a/packages/transaction-controller/src/utils/gas.ts +++ b/packages/transaction-controller/src/utils/gas.ts @@ -60,7 +60,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(fractionBN(gasLimitBN, 7, 20)); request.value = value || '0x0'; let estimatedGas = request.gas; @@ -96,7 +96,7 @@ export function addGasBuffer( multiplier: number, ) { const estimatedGasBN = hexToBN(estimatedGas); - const maxGasBN = hexToBN(blockGasLimit).muln(0.9); + const maxGasBN = hexToBN(blockGasLimit).muln(0.3); const paddedGasBN = estimatedGasBN.muln(multiplier); if (estimatedGasBN.gt(maxGasBN)) { From 1e6be12b8623be66520ef96bc13cebb9ea839203 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Mon, 30 Sep 2024 13:22:53 +0200 Subject: [PATCH 2/3] Fix gas limit fallback --- .../src/utils/gas.test.ts | 9 +++++++-- .../transaction-controller/src/utils/gas.ts | 20 +++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/transaction-controller/src/utils/gas.test.ts b/packages/transaction-controller/src/utils/gas.test.ts index dec34fbca4b..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', () => ({ @@ -265,7 +266,9 @@ describe('gas', () => { describe('on estimate query error', () => { it('sets gas to 35% of block gas limit', async () => { - const fallbackGas = Math.floor(BLOCK_GAS_LIMIT_MOCK * 0.35); + const fallbackGas = Math.floor( + BLOCK_GAS_LIMIT_MOCK * GAS_ESTIMATE_FALLBACK_MULTIPLIER, + ); mockQuery({ getBlockByNumberResponse: { @@ -353,7 +356,9 @@ describe('gas', () => { }); it('returns estimated gas as 35% of block gas limit on error', async () => { - const fallbackGas = Math.floor(BLOCK_GAS_LIMIT_MOCK * 0.35); + 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 818dcf736da..066ba8ebd0e 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, 7, 20)); + request.gas = BNToHex(gasLimitBN.muln(GAS_ESTIMATE_FALLBACK_MULTIPLIER)); request.value = value || '0x0'; let estimatedGas = request.gas; @@ -96,7 +92,7 @@ export function addGasBuffer( multiplier: number, ) { const estimatedGasBN = hexToBN(estimatedGas); - const maxGasBN = hexToBN(blockGasLimit).muln(0.3); + const maxGasBN = hexToBN(blockGasLimit).muln(0.9); const paddedGasBN = estimatedGasBN.muln(multiplier); if (estimatedGasBN.gt(maxGasBN)) { @@ -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 estimate as simulation failed', + ); return [estimatedGas, simulationFails]; } From 9c94efb1d3058e0b34bf011734a7b72dd2519fef Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Fri, 4 Oct 2024 11:44:12 +0200 Subject: [PATCH 3/3] Adjust log --- packages/transaction-controller/src/utils/gas.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transaction-controller/src/utils/gas.ts b/packages/transaction-controller/src/utils/gas.ts index 066ba8ebd0e..91f83778d54 100644 --- a/packages/transaction-controller/src/utils/gas.ts +++ b/packages/transaction-controller/src/utils/gas.ts @@ -136,7 +136,7 @@ async function getGas( log( isCustomNetwork ? 'Using original estimate as custom network' - : 'Using original estimate as simulation failed', + : 'Using original fallback estimate as simulation failed', ); return [estimatedGas, simulationFails]; }