Skip to content

Commit c21235f

Browse files
authored
Fix use estimate gas instead of fixed gas (#3694)
## Explanation Fix following the full release of the core `TransactionController` in the extension: - Change to use estimate gas instead of fixed gas (21k) when a contract is deployed and the `gas` is not specified. ### `@metamask/transaction-controller` - **FIXED**: Fix use estimate gas when `to` property is not provided.
1 parent 6884767 commit c21235f

2 files changed

Lines changed: 26 additions & 27 deletions

File tree

packages/transaction-controller/src/utils/gas.test.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import type EthQuery from '@metamask/eth-query';
66
import { CHAIN_IDS } from '../constants';
77
import type { TransactionMeta } from '../types';
88
import type { UpdateGasRequest } from './gas';
9-
import { addGasBuffer, estimateGas, updateGas, FIXED_GAS } from './gas';
9+
import {
10+
addGasBuffer,
11+
estimateGas,
12+
updateGas,
13+
FIXED_GAS,
14+
DEFAULT_GAS_MULTIPLIER,
15+
} from './gas';
1016

1117
jest.mock('@metamask/controller-utils', () => ({
1218
...jest.requireActual('@metamask/controller-utils'),
@@ -16,7 +22,6 @@ jest.mock('@metamask/controller-utils', () => ({
1622
const GAS_MOCK = 100;
1723
const BLOCK_GAS_LIMIT_MOCK = 1234567;
1824
const BLOCK_NUMBER_MOCK = '0x5678';
19-
const CODE_MOCK = '0x987';
2025
// TODO: Replace `any` with type
2126
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2227
const ETH_QUERY_MOCK = {} as any as EthQuery;
@@ -127,11 +132,27 @@ describe('gas', () => {
127132
);
128133
});
129134

135+
it('to estimate if not custom network and no to parameter', async () => {
136+
updateGasRequest.providerConfig.type = NetworkType.mainnet;
137+
const gasEstimation = Math.ceil(GAS_MOCK * DEFAULT_GAS_MULTIPLIER);
138+
delete updateGasRequest.txMeta.txParams.to;
139+
mockQuery({
140+
getBlockByNumberResponse: { gasLimit: toHex(BLOCK_GAS_LIMIT_MOCK) },
141+
estimateGasResponse: toHex(GAS_MOCK),
142+
});
143+
144+
await updateGas(updateGasRequest);
145+
146+
expect(updateGasRequest.txMeta.txParams.gas).toBe(toHex(gasEstimation));
147+
expect(updateGasRequest.txMeta.originalGasEstimate).toBe(
148+
updateGasRequest.txMeta.txParams.gas,
149+
);
150+
});
151+
130152
it('to estimate if estimate greater than 90% of block gas limit', async () => {
131153
const estimatedGas = Math.ceil(BLOCK_GAS_LIMIT_MOCK * 0.9 + 10);
132154

133155
mockQuery({
134-
getCodeResponse: CODE_MOCK,
135156
getBlockByNumberResponse: { gasLimit: toHex(BLOCK_GAS_LIMIT_MOCK) },
136157
estimateGasResponse: toHex(estimatedGas),
137158
});
@@ -150,7 +171,6 @@ describe('gas', () => {
150171
const estimatedGas = Math.round(estimatedGasPadded / 1.5);
151172

152173
mockQuery({
153-
getCodeResponse: CODE_MOCK,
154174
getBlockByNumberResponse: { gasLimit: toHex(BLOCK_GAS_LIMIT_MOCK) },
155175
estimateGasResponse: toHex(estimatedGas),
156176
});
@@ -173,7 +193,6 @@ describe('gas', () => {
173193
updateGasRequest.providerConfig.chainId = CHAIN_IDS.OPTIMISM;
174194

175195
mockQuery({
176-
getCodeResponse: CODE_MOCK,
177196
getBlockByNumberResponse: { gasLimit: toHex(BLOCK_GAS_LIMIT_MOCK) },
178197
estimateGasResponse: toHex(estimatedGas),
179198
});
@@ -194,7 +213,6 @@ describe('gas', () => {
194213
const estimatedGas = Math.ceil(estimatedGasPadded / 1.5);
195214

196215
mockQuery({
197-
getCodeResponse: CODE_MOCK,
198216
getBlockByNumberResponse: { gasLimit: toHex(BLOCK_GAS_LIMIT_MOCK) },
199217
estimateGasResponse: toHex(estimatedGas),
200218
});
@@ -210,19 +228,6 @@ describe('gas', () => {
210228
});
211229

212230
describe('to fixed value', () => {
213-
it('if not custom network and no to parameter', async () => {
214-
updateGasRequest.providerConfig.type = NetworkType.mainnet;
215-
delete updateGasRequest.txMeta.txParams.to;
216-
217-
await updateGas(updateGasRequest);
218-
219-
expect(updateGasRequest.txMeta.txParams.gas).toBe(FIXED_GAS);
220-
expect(updateGasRequest.txMeta.originalGasEstimate).toBe(
221-
updateGasRequest.txMeta.txParams.gas,
222-
);
223-
expectEstimateGasNotCalled();
224-
});
225-
226231
it('if not custom network and to parameter and no data and no code', async () => {
227232
updateGasRequest.providerConfig.type = NetworkType.mainnet;
228233
delete updateGasRequest.txMeta.txParams.data;
@@ -264,7 +269,6 @@ describe('gas', () => {
264269
const fallbackGas = Math.floor(BLOCK_GAS_LIMIT_MOCK * 0.95);
265270

266271
mockQuery({
267-
getCodeResponse: CODE_MOCK,
268272
getBlockByNumberResponse: {
269273
gasLimit: toHex(BLOCK_GAS_LIMIT_MOCK),
270274
},
@@ -281,7 +285,6 @@ describe('gas', () => {
281285

282286
it('sets simulationFails property', async () => {
283287
mockQuery({
284-
getCodeResponse: CODE_MOCK,
285288
getBlockByNumberResponse: {
286289
gasLimit: toHex(BLOCK_GAS_LIMIT_MOCK),
287290
number: BLOCK_NUMBER_MOCK,

packages/transaction-controller/src/utils/gas.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,13 @@ async function requiresFixedGas({
167167
txParams: { to, data },
168168
} = txMeta;
169169

170-
if (isCustomNetwork) {
170+
if (isCustomNetwork || !to || data) {
171171
return false;
172172
}
173173

174-
if (!to) {
175-
return true;
176-
}
177-
178174
const code = await getCode(ethQuery, to);
179175

180-
return !data && (!code || code === '0x');
176+
return !code || code === '0x';
181177
}
182178

183179
async function getCode(

0 commit comments

Comments
 (0)