Skip to content

Commit 78de67e

Browse files
committed
feat: add getSavedGasFees to TC wallet-init options (sync with main TC 69.0.0)
Forward-port the getSavedGasFees hook added to transaction-controller-init.ts on main (TC 69.0.0) into our wallet-init instance-options equivalent. Reads advancedGasFee preferences from PreferencesController state and returns the saved fee tier/values for the transaction account + chain, allowing TC to apply user-saved gas fee preferences to non-internal transactions.
1 parent b0efe57 commit 78de67e

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

app/core/Engine/wallet-init/instance-options/transaction-controller.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {
2+
GasFeeEstimateLevel,
23
TransactionController,
34
TransactionType,
5+
UserFeeLevel,
6+
type SavedGasFees,
47
type TransactionMeta,
58
type TransactionControllerOptions,
69
} from '@metamask/transaction-controller';
@@ -32,6 +35,7 @@ import {
3235
import { handleShowNotification } from '../../controllers/transaction-controller/event-handlers/notification';
3336
import { handleUnapprovedTransactionAddedForMoneyAccount } from '../../controllers/transaction-controller/event-handlers/money-account-override';
3437
import { TransactionControllerInitMessenger } from '../messengers/transaction-controller-messenger';
38+
import type { PreferencesStateWithSavedGasFees } from '../../controllers/preferences-controller-types';
3539

3640
type TransactionControllerInstanceOptions = NonNullable<
3741
WalletOptions['instanceOptions']['transactionController']
@@ -75,6 +79,8 @@ export function getTransactionControllerInstanceOptions({
7579
isSimulationEnabled: () =>
7680
initMessenger.call('PreferencesController:getState')
7781
.useTransactionSimulations,
82+
getSavedGasFees: (chainIdOrTransactionMeta) =>
83+
getSavedGasFees(chainIdOrTransactionMeta, initMessenger),
7884
publicKeyEIP7702: AppConstants.EIP_7702_PUBLIC_KEY as Hex | undefined,
7985
// Expected type mismatch with TransactionControllerOptions['trace']
8086
trace: trace as unknown as TransactionControllerOptions['trace'],
@@ -285,6 +291,66 @@ async function isEIP7702GasFeeTokensEnabled(
285291
);
286292
}
287293

294+
/**
295+
* Retrieve saved gas fee preferences for the transaction account and chain.
296+
*/
297+
function getSavedGasFees(
298+
chainIdOrTransactionMeta: Hex | TransactionMeta,
299+
initMessenger: TransactionControllerInitMessenger,
300+
): SavedGasFees | undefined {
301+
const selectedAccount = initMessenger.call(
302+
'AccountsController:getSelectedAccount',
303+
)?.address;
304+
const isTransactionMeta = typeof chainIdOrTransactionMeta !== 'string';
305+
const transactionMeta = isTransactionMeta
306+
? chainIdOrTransactionMeta
307+
: undefined;
308+
const account = transactionMeta?.txParams.from ?? selectedAccount;
309+
310+
if (!account) {
311+
return undefined;
312+
}
313+
314+
const normalizedAccount = account.toLowerCase();
315+
const chainId = isTransactionMeta
316+
? chainIdOrTransactionMeta.chainId
317+
: chainIdOrTransactionMeta;
318+
319+
const preferencesState = initMessenger.call(
320+
'PreferencesController:getState',
321+
) as PreferencesStateWithSavedGasFees;
322+
323+
const savedGasFeePreference =
324+
preferencesState.advancedGasFee?.[chainId]?.[normalizedAccount];
325+
326+
if (!savedGasFeePreference) {
327+
return undefined;
328+
}
329+
330+
const { gasPrice, maxBaseFee, priorityFee, userFeeLevel } =
331+
savedGasFeePreference;
332+
333+
if (userFeeLevel !== UserFeeLevel.CUSTOM) {
334+
return {
335+
level: userFeeLevel as UserFeeLevel | GasFeeEstimateLevel,
336+
} as unknown as SavedGasFees;
337+
}
338+
339+
if (gasPrice) {
340+
return { gasPrice, level: UserFeeLevel.CUSTOM } as unknown as SavedGasFees;
341+
}
342+
343+
if (!maxBaseFee || !priorityFee) {
344+
return undefined;
345+
}
346+
347+
return {
348+
maxBaseFee,
349+
priorityFee,
350+
level: UserFeeLevel.CUSTOM,
351+
} as unknown as SavedGasFees;
352+
}
353+
288354
function isAutomaticGasFeeUpdateEnabled(transaction: TransactionMeta) {
289355
if (hasTransactionType(transaction, RELAY_DEPOSIT_TYPES)) {
290356
return false;

0 commit comments

Comments
 (0)