Skip to content

feat: Add RandomisedEstimationsGasFeeFlow to gas fee flows #5511

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

Merged
merged 25 commits into from
Mar 28, 2025
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
5 changes: 5 additions & 0 deletions packages/transaction-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Adds `RandomisedEstimationsGasFeeFlow` to gas fee flows in `TransactionController` ([#5511](https://github.com/MetaMask/core/pull/5511))
- Added flow only will be activated if chainId is defined in feature flags.

### Fixed

- Fix simulation of type-4 transactions ([#5552](https://github.com/MetaMask/core/pull/5552))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { getAccountAddressRelationship } from './api/accounts-api';
import { CHAIN_IDS } from './constants';
import { DefaultGasFeeFlow } from './gas-flows/DefaultGasFeeFlow';
import { LineaGasFeeFlow } from './gas-flows/LineaGasFeeFlow';
import { RandomisedEstimationsGasFeeFlow } from './gas-flows/RandomisedEstimationsGasFeeFlow';
import { TestGasFeeFlow } from './gas-flows/TestGasFeeFlow';
import { GasFeePoller } from './helpers/GasFeePoller';
import { IncomingTransactionHelper } from './helpers/IncomingTransactionHelper';
Expand Down Expand Up @@ -115,6 +116,7 @@ const ORIGIN_MOCK = 'test.com';
jest.mock('@metamask/eth-query');
jest.mock('./api/accounts-api');
jest.mock('./gas-flows/DefaultGasFeeFlow');
jest.mock('./gas-flows/RandomisedEstimationsGasFeeFlow');
jest.mock('./gas-flows/LineaGasFeeFlow');
jest.mock('./gas-flows/TestGasFeeFlow');
jest.mock('./helpers/GasFeePoller');
Expand Down Expand Up @@ -514,6 +516,9 @@ describe('TransactionController', () => {
);
const defaultGasFeeFlowClassMock = jest.mocked(DefaultGasFeeFlow);
const lineaGasFeeFlowClassMock = jest.mocked(LineaGasFeeFlow);
const randomisedEstimationsGasFeeFlowClassMock = jest.mocked(
RandomisedEstimationsGasFeeFlow,
);
const testGasFeeFlowClassMock = jest.mocked(TestGasFeeFlow);
const gasFeePollerClassMock = jest.mocked(GasFeePoller);
const getSimulationDataMock = jest.mocked(getSimulationData);
Expand All @@ -536,6 +541,7 @@ describe('TransactionController', () => {
let multichainTrackingHelperMock: jest.Mocked<MultichainTrackingHelper>;
let defaultGasFeeFlowMock: jest.Mocked<DefaultGasFeeFlow>;
let lineaGasFeeFlowMock: jest.Mocked<LineaGasFeeFlow>;
let randomisedEstimationsGasFeeFlowMock: jest.Mocked<RandomisedEstimationsGasFeeFlow>;
let testGasFeeFlowMock: jest.Mocked<TestGasFeeFlow>;
let gasFeePollerMock: jest.Mocked<GasFeePoller>;
let methodDataHelperMock: jest.Mocked<MethodDataHelper>;
Expand Down Expand Up @@ -919,6 +925,13 @@ describe('TransactionController', () => {
return lineaGasFeeFlowMock;
});

randomisedEstimationsGasFeeFlowClassMock.mockImplementation(() => {
randomisedEstimationsGasFeeFlowMock = {
matchesTransaction: () => false,
} as unknown as jest.Mocked<RandomisedEstimationsGasFeeFlow>;
return randomisedEstimationsGasFeeFlowMock;
});

testGasFeeFlowClassMock.mockImplementation(() => {
testGasFeeFlowMock = {
matchesTransaction: () => false,
Expand Down Expand Up @@ -971,7 +984,11 @@ describe('TransactionController', () => {
expect(gasFeePollerClassMock).toHaveBeenCalledTimes(1);
expect(gasFeePollerClassMock).toHaveBeenCalledWith(
expect.objectContaining({
gasFeeFlows: [lineaGasFeeFlowMock, defaultGasFeeFlowMock],
gasFeeFlows: [
randomisedEstimationsGasFeeFlowMock,
lineaGasFeeFlowMock,
defaultGasFeeFlowMock,
],
}),
);
});
Expand Down Expand Up @@ -2018,9 +2035,14 @@ describe('TransactionController', () => {
expect(updateGasFeesMock).toHaveBeenCalledWith({
eip1559: true,
ethQuery: expect.any(Object),
gasFeeFlows: [lineaGasFeeFlowMock, defaultGasFeeFlowMock],
gasFeeFlows: [
randomisedEstimationsGasFeeFlowMock,
lineaGasFeeFlowMock,
defaultGasFeeFlowMock,
],
getGasFeeEstimates: expect.any(Function),
getSavedGasFees: expect.any(Function),
messenger: expect.any(Object),
txMeta: expect.any(Object),
});
});
Expand Down
14 changes: 13 additions & 1 deletion packages/transaction-controller/src/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
import { DefaultGasFeeFlow } from './gas-flows/DefaultGasFeeFlow';
import { LineaGasFeeFlow } from './gas-flows/LineaGasFeeFlow';
import { OptimismLayer1GasFeeFlow } from './gas-flows/OptimismLayer1GasFeeFlow';
import { RandomisedEstimationsGasFeeFlow } from './gas-flows/RandomisedEstimationsGasFeeFlow';
import { ScrollLayer1GasFeeFlow } from './gas-flows/ScrollLayer1GasFeeFlow';
import { TestGasFeeFlow } from './gas-flows/TestGasFeeFlow';
import { AccountsApiRemoteTransactionSource } from './helpers/AccountsApiRemoteTransactionSource';
Expand Down Expand Up @@ -912,6 +913,7 @@ export class TransactionController extends BaseController<
getProvider: (networkClientId) => this.#getProvider({ networkClientId }),
getTransactions: () => this.state.transactions,
layer1GasFeeFlows: this.layer1GasFeeFlows,
messenger: this.messagingSystem,
onStateChange: (listener) => {
this.messagingSystem.subscribe(
'TransactionController:stateChange',
Expand Down Expand Up @@ -1984,6 +1986,7 @@ export class TransactionController extends BaseController<

await updateTransactionLayer1GasFee({
layer1GasFeeFlows: this.layer1GasFeeFlows,
messenger: this.messagingSystem,
provider,
transactionMeta: updatedTransaction,
});
Expand Down Expand Up @@ -2293,6 +2296,7 @@ export class TransactionController extends BaseController<
const gasFeeFlow = getGasFeeFlow(
transactionMeta,
this.gasFeeFlows,
this.messagingSystem,
) as GasFeeFlow;

const ethQuery = new EthQuery(provider);
Expand All @@ -2304,6 +2308,7 @@ export class TransactionController extends BaseController<
return gasFeeFlow.getGasFees({
ethQuery,
gasFeeControllerData,
messenger: this.messagingSystem,
transactionMeta,
});
}
Expand Down Expand Up @@ -2333,6 +2338,7 @@ export class TransactionController extends BaseController<

return await getTransactionLayer1GasFee({
layer1GasFeeFlows: this.layer1GasFeeFlows,
messenger: this.messagingSystem,
provider,
transactionMeta: {
txParams: transactionParams,
Expand Down Expand Up @@ -2581,6 +2587,7 @@ export class TransactionController extends BaseController<
gasFeeFlows: this.gasFeeFlows,
getGasFeeEstimates: this.getGasFeeEstimates,
getSavedGasFees: this.getSavedGasFees.bind(this),
messenger: this.messagingSystem,
txMeta: transactionMeta,
}),
);
Expand All @@ -2590,6 +2597,7 @@ export class TransactionController extends BaseController<
async () =>
await updateTransactionLayer1GasFee({
layer1GasFeeFlows: this.layer1GasFeeFlows,
messenger: this.messagingSystem,
provider,
transactionMeta,
}),
Expand Down Expand Up @@ -3749,7 +3757,11 @@ export class TransactionController extends BaseController<
return [new TestGasFeeFlow()];
}

return [new LineaGasFeeFlow(), new DefaultGasFeeFlow()];
return [
new RandomisedEstimationsGasFeeFlow(),
new LineaGasFeeFlow(),
new DefaultGasFeeFlow(),
];
}

#getLayer1GasFeeFlows(): Layer1GasFeeFlow[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import { GAS_ESTIMATE_TYPES } from '@metamask/gas-fee-controller';

import { DefaultGasFeeFlow } from './DefaultGasFeeFlow';
import type { TransactionControllerMessenger } from '../TransactionController';
import type {
FeeMarketGasFeeEstimates,
GasPriceGasFeeEstimates,
Expand Down Expand Up @@ -99,9 +100,7 @@ describe('DefaultGasFeeFlow', () => {
describe('matchesTransaction', () => {
it('returns true', () => {
const defaultGasFeeFlow = new DefaultGasFeeFlow();
const result = defaultGasFeeFlow.matchesTransaction(
TRANSACTION_META_MOCK,
);
const result = defaultGasFeeFlow.matchesTransaction();
expect(result).toBe(true);
});
});
Expand All @@ -113,6 +112,7 @@ describe('DefaultGasFeeFlow', () => {
const response = await defaultGasFeeFlow.getGasFees({
ethQuery: ETH_QUERY_MOCK,
gasFeeControllerData: FEE_MARKET_RESPONSE_MOCK,
messenger: {} as TransactionControllerMessenger,
transactionMeta: TRANSACTION_META_MOCK,
});

Expand All @@ -127,6 +127,7 @@ describe('DefaultGasFeeFlow', () => {
const response = await defaultGasFeeFlow.getGasFees({
ethQuery: ETH_QUERY_MOCK,
gasFeeControllerData: LEGACY_RESPONSE_MOCK,
messenger: {} as TransactionControllerMessenger,
transactionMeta: TRANSACTION_META_MOCK,
});

Expand All @@ -141,6 +142,7 @@ describe('DefaultGasFeeFlow', () => {
const response = await defaultGasFeeFlow.getGasFees({
ethQuery: ETH_QUERY_MOCK,
gasFeeControllerData: GAS_PRICE_RESPONSE_MOCK,
messenger: {} as TransactionControllerMessenger,
transactionMeta: TRANSACTION_META_MOCK,
});

Expand All @@ -157,6 +159,7 @@ describe('DefaultGasFeeFlow', () => {
gasFeeControllerData: {
gasEstimateType: GAS_ESTIMATE_TYPES.NONE,
} as GasFeeState,
messenger: {} as TransactionControllerMessenger,
transactionMeta: TRANSACTION_META_MOCK,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
GasFeeFlowResponse,
GasPriceGasFeeEstimates,
LegacyGasFeeEstimates,
TransactionMeta,
} from '../types';
import { GasFeeEstimateLevel, GasFeeEstimateType } from '../types';
import { gweiDecimalToWeiHex } from '../utils/gas-fees';
Expand All @@ -28,7 +27,7 @@ const log = createModuleLogger(projectLogger, 'default-gas-fee-flow');
* The standard implementation of a gas fee flow that obtains gas fee estimates using only the GasFeeController.
*/
export class DefaultGasFeeFlow implements GasFeeFlow {
matchesTransaction(_transactionMeta: TransactionMeta): boolean {
matchesTransaction(): boolean {
return true;
}

Expand Down Expand Up @@ -56,8 +55,6 @@ export class DefaultGasFeeFlow implements GasFeeFlow {
);
break;
default:
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Unsupported gas estimate type: ${gasEstimateType}`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type EthQuery from '@metamask/eth-query';
import { DefaultGasFeeFlow } from './DefaultGasFeeFlow';
import { LineaGasFeeFlow } from './LineaGasFeeFlow';
import { CHAIN_IDS } from '../constants';
import type { TransactionControllerMessenger } from '../TransactionController';
import type {
FeeMarketGasFeeEstimates,
GasFeeFlowRequest,
Expand Down Expand Up @@ -82,7 +83,12 @@ describe('LineaGasFeeFlow', () => {
chainId,
};

expect(flow.matchesTransaction(transaction)).toBe(true);
expect(
flow.matchesTransaction({
transactionMeta: transaction,
messenger: {} as TransactionControllerMessenger,
}),
).toBe(true);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type BN from 'bn.js';

import { DefaultGasFeeFlow } from './DefaultGasFeeFlow';
import { projectLogger } from '../logger';
import type { TransactionControllerMessenger } from '../TransactionController';
import type {
GasFeeEstimates,
GasFeeFlow,
Expand Down Expand Up @@ -49,7 +50,12 @@ const PRIORITY_FEE_MULTIPLIERS = {
* - Static multipliers to increase the base and priority fees.
*/
export class LineaGasFeeFlow implements GasFeeFlow {
matchesTransaction(transactionMeta: TransactionMeta): boolean {
matchesTransaction({
transactionMeta,
}: {
transactionMeta: TransactionMeta;
messenger: TransactionControllerMessenger;
}): boolean {
return LINEA_CHAIN_IDS.includes(transactionMeta.chainId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OptimismLayer1GasFeeFlow } from './OptimismLayer1GasFeeFlow';
import { CHAIN_IDS } from '../constants';
import type { TransactionControllerMessenger } from '../TransactionController';
import type { TransactionMeta } from '../types';
import { TransactionStatus } from '../types';

Expand Down Expand Up @@ -28,7 +29,12 @@ describe('OptimismLayer1GasFeeFlow', () => {
chainId,
};

expect(flow.matchesTransaction(transaction)).toBe(true);
expect(
flow.matchesTransaction({
transactionMeta: transaction,
messenger: {} as TransactionControllerMessenger,
}),
).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type Hex } from '@metamask/utils';

import { OracleLayer1GasFeeFlow } from './OracleLayer1GasFeeFlow';
import { CHAIN_IDS } from '../constants';
import type { TransactionControllerMessenger } from '../TransactionController';
import type { TransactionMeta } from '../types';

const OPTIMISM_STACK_CHAIN_IDS: Hex[] = [
Expand All @@ -26,7 +27,12 @@ export class OptimismLayer1GasFeeFlow extends OracleLayer1GasFeeFlow {
super(OPTIMISM_GAS_PRICE_ORACLE_ADDRESS);
}

matchesTransaction(transactionMeta: TransactionMeta): boolean {
matchesTransaction({
transactionMeta,
}: {
transactionMeta: TransactionMeta;
messenger: TransactionControllerMessenger;
}): boolean {
return OPTIMISM_STACK_CHAIN_IDS.includes(transactionMeta.chainId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function createMockTypedTransaction(serializedBuffer: Buffer) {
}

class MockOracleLayer1GasFeeFlow extends OracleLayer1GasFeeFlow {
matchesTransaction(_transactionMeta: TransactionMeta): boolean {
matchesTransaction(): boolean {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Hex } from '@metamask/utils';
import { createModuleLogger } from '@metamask/utils';

import { projectLogger } from '../logger';
import type { TransactionControllerMessenger } from '../TransactionController';
import type {
Layer1GasFeeFlow,
Layer1GasFeeFlowRequest,
Expand Down Expand Up @@ -40,7 +41,13 @@ export abstract class OracleLayer1GasFeeFlow implements Layer1GasFeeFlow {
this.#signTransaction = signTransaction ?? false;
}

abstract matchesTransaction(transactionMeta: TransactionMeta): boolean;
abstract matchesTransaction({
transactionMeta,
messenger,
}: {
transactionMeta: TransactionMeta;
messenger: TransactionControllerMessenger;
}): boolean;

async getLayer1Fee(
request: Layer1GasFeeFlowRequest,
Expand Down
Loading
Loading