|
1 | 1 | import { |
| 2 | + GasFeeEstimateLevel, |
2 | 3 | TransactionController, |
3 | 4 | TransactionType, |
| 5 | + UserFeeLevel, |
| 6 | + type SavedGasFees, |
4 | 7 | type TransactionMeta, |
5 | 8 | type TransactionControllerOptions, |
6 | 9 | } from '@metamask/transaction-controller'; |
@@ -32,6 +35,7 @@ import { |
32 | 35 | import { handleShowNotification } from '../../controllers/transaction-controller/event-handlers/notification'; |
33 | 36 | import { handleUnapprovedTransactionAddedForMoneyAccount } from '../../controllers/transaction-controller/event-handlers/money-account-override'; |
34 | 37 | import { TransactionControllerInitMessenger } from '../messengers/transaction-controller-messenger'; |
| 38 | +import type { PreferencesStateWithSavedGasFees } from '../../controllers/preferences-controller-types'; |
35 | 39 |
|
36 | 40 | type TransactionControllerInstanceOptions = NonNullable< |
37 | 41 | WalletOptions['instanceOptions']['transactionController'] |
@@ -75,6 +79,8 @@ export function getTransactionControllerInstanceOptions({ |
75 | 79 | isSimulationEnabled: () => |
76 | 80 | initMessenger.call('PreferencesController:getState') |
77 | 81 | .useTransactionSimulations, |
| 82 | + getSavedGasFees: (chainIdOrTransactionMeta) => |
| 83 | + getSavedGasFees(chainIdOrTransactionMeta, initMessenger), |
78 | 84 | publicKeyEIP7702: AppConstants.EIP_7702_PUBLIC_KEY as Hex | undefined, |
79 | 85 | // Expected type mismatch with TransactionControllerOptions['trace'] |
80 | 86 | trace: trace as unknown as TransactionControllerOptions['trace'], |
@@ -285,6 +291,66 @@ async function isEIP7702GasFeeTokensEnabled( |
285 | 291 | ); |
286 | 292 | } |
287 | 293 |
|
| 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 | + |
288 | 354 | function isAutomaticGasFeeUpdateEnabled(transaction: TransactionMeta) { |
289 | 355 | if (hasTransactionType(transaction, RELAY_DEPOSIT_TYPES)) { |
290 | 356 | return false; |
|
0 commit comments