Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change gas limit fallback #4739

Merged
Show file tree
Hide file tree
Changes from 3 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));
matthewwalsh0 marked this conversation as resolved.
Show resolved Hide resolved
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) {
matthewwalsh0 marked this conversation as resolved.
Show resolved Hide resolved
log(
isCustomNetwork
? 'Using original estimate as custom network'
: 'Using original estimate as simulation failed',
matthewwalsh0 marked this conversation as resolved.
Show resolved Hide resolved
);
return [estimatedGas, simulationFails];
}

Expand Down
Loading