Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/transaction-controller/src/utils/gas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
updateGas,
FIXED_GAS,
DEFAULT_GAS_MULTIPLIER,
GAS_ESTIMATE_FALLBACK_MULTIPLIER,
} from './gas';

jest.mock('@metamask/controller-utils', () => ({
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
18 changes: 9 additions & 9 deletions packages/transaction-controller/src/utils/gas.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down