@metamask/transaction-controller 52.0.0 → 52.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/dist/TransactionController.cjs +24 -12
  3. package/dist/TransactionController.cjs.map +1 -1
  4. package/dist/TransactionController.d.cts +3 -0
  5. package/dist/TransactionController.d.cts.map +1 -1
  6. package/dist/TransactionController.d.mts +3 -0
  7. package/dist/TransactionController.d.mts.map +1 -1
  8. package/dist/TransactionController.mjs +25 -13
  9. package/dist/TransactionController.mjs.map +1 -1
  10. package/dist/helpers/GasFeePoller.cjs +73 -1
  11. package/dist/helpers/GasFeePoller.cjs.map +1 -1
  12. package/dist/helpers/GasFeePoller.d.cts +20 -2
  13. package/dist/helpers/GasFeePoller.d.cts.map +1 -1
  14. package/dist/helpers/GasFeePoller.d.mts +20 -2
  15. package/dist/helpers/GasFeePoller.d.mts.map +1 -1
  16. package/dist/helpers/GasFeePoller.mjs +72 -1
  17. package/dist/helpers/GasFeePoller.mjs.map +1 -1
  18. package/dist/utils/feature-flags.cjs +2 -2
  19. package/dist/utils/feature-flags.cjs.map +1 -1
  20. package/dist/utils/feature-flags.d.cts +2 -2
  21. package/dist/utils/feature-flags.d.mts +2 -2
  22. package/dist/utils/feature-flags.mjs +2 -2
  23. package/dist/utils/feature-flags.mjs.map +1 -1
  24. package/dist/utils/gas.cjs +108 -8
  25. package/dist/utils/gas.cjs.map +1 -1
  26. package/dist/utils/gas.d.cts +16 -5
  27. package/dist/utils/gas.d.cts.map +1 -1
  28. package/dist/utils/gas.d.mts +16 -5
  29. package/dist/utils/gas.d.mts.map +1 -1
  30. package/dist/utils/gas.mjs +108 -8
  31. package/dist/utils/gas.mjs.map +1 -1
  32. package/dist/utils/simulation-api.cjs.map +1 -1
  33. package/dist/utils/simulation-api.d.cts +9 -5
  34. package/dist/utils/simulation-api.d.cts.map +1 -1
  35. package/dist/utils/simulation-api.d.mts +9 -5
  36. package/dist/utils/simulation-api.d.mts.map +1 -1
  37. package/dist/utils/simulation-api.mjs.map +1 -1
  38. package/package.json +2 -2
@@ -15,7 +15,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  };
16
16
  var _GasFeePoller_instances, _GasFeePoller_findNetworkClientIdByChainId, _GasFeePoller_gasFeeFlows, _GasFeePoller_getGasFeeControllerEstimates, _GasFeePoller_getProvider, _GasFeePoller_getTransactions, _GasFeePoller_layer1GasFeeFlows, _GasFeePoller_timeout, _GasFeePoller_running, _GasFeePoller_start, _GasFeePoller_stop, _GasFeePoller_onTimeout, _GasFeePoller_updateUnapprovedTransactions, _GasFeePoller_updateUnapprovedTransaction, _GasFeePoller_updateTransactionGasFeeEstimates, _GasFeePoller_updateTransactionLayer1GasFee, _GasFeePoller_getUnapprovedTransactions, _GasFeePoller_getGasFeeControllerData;
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.GasFeePoller = void 0;
18
+ exports.updateTransactionGasFees = exports.GasFeePoller = void 0;
19
19
  const eth_query_1 = __importDefault(require("@metamask/eth-query"));
20
20
  const utils_1 = require("@metamask/utils");
21
21
  // This package purposefully relies on Node's EventEmitter module.
@@ -185,4 +185,76 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
185
185
  });
186
186
  return new Map(await Promise.all(entryPromises));
187
187
  };
188
+ /**
189
+ * Update the gas fees for a transaction.
190
+ *
191
+ * @param args - Argument bag.
192
+ * @param args.txMeta - The transaction meta.
193
+ * @param args.gasFeeEstimates - The gas fee estimates.
194
+ * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
195
+ * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
196
+ * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
197
+ * @param args.layer1GasFee - The layer 1 gas fee.
198
+ */
199
+ async function updateTransactionGasFees({ txMeta, getEIP1559Compatibility, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }) {
200
+ const userFeeLevel = txMeta.userFeeLevel;
201
+ const isUsingGasFeeEstimateLevel = Object.values(types_1.GasFeeEstimateLevel).includes(userFeeLevel);
202
+ const { type: gasEstimateType } = gasFeeEstimates ?? {};
203
+ if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {
204
+ const isEIP1559Compatible = txMeta.txParams.type !== types_1.TransactionEnvelopeType.legacy &&
205
+ (await getEIP1559Compatibility(txMeta.networkClientId));
206
+ if (isEIP1559Compatible) {
207
+ // Handle EIP-1559 compatible transactions
208
+ if (gasEstimateType === types_1.GasFeeEstimateType.FeeMarket) {
209
+ const feeMarketGasFeeEstimates = gasFeeEstimates;
210
+ txMeta.txParams.maxFeePerGas =
211
+ feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;
212
+ txMeta.txParams.maxPriorityFeePerGas =
213
+ feeMarketGasFeeEstimates[userFeeLevel].maxPriorityFeePerGas;
214
+ }
215
+ if (gasEstimateType === types_1.GasFeeEstimateType.GasPrice) {
216
+ const gasPriceGasFeeEstimates = gasFeeEstimates;
217
+ txMeta.txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;
218
+ txMeta.txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;
219
+ }
220
+ if (gasEstimateType === types_1.GasFeeEstimateType.Legacy) {
221
+ const legacyGasFeeEstimates = gasFeeEstimates;
222
+ const gasPrice = legacyGasFeeEstimates[userFeeLevel];
223
+ txMeta.txParams.maxFeePerGas = gasPrice;
224
+ txMeta.txParams.maxPriorityFeePerGas = gasPrice;
225
+ }
226
+ // Remove gasPrice for EIP-1559 transactions
227
+ delete txMeta.txParams.gasPrice;
228
+ }
229
+ else {
230
+ // Handle non-EIP-1559 transactions
231
+ if (gasEstimateType === types_1.GasFeeEstimateType.FeeMarket) {
232
+ const feeMarketGasFeeEstimates = gasFeeEstimates;
233
+ txMeta.txParams.gasPrice =
234
+ feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;
235
+ }
236
+ if (gasEstimateType === types_1.GasFeeEstimateType.GasPrice) {
237
+ const gasPriceGasFeeEstimates = gasFeeEstimates;
238
+ txMeta.txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;
239
+ }
240
+ if (gasEstimateType === types_1.GasFeeEstimateType.Legacy) {
241
+ const legacyGasFeeEstimates = gasFeeEstimates;
242
+ txMeta.txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];
243
+ }
244
+ // Remove EIP-1559 specific parameters for legacy transactions
245
+ delete txMeta.txParams.maxFeePerGas;
246
+ delete txMeta.txParams.maxPriorityFeePerGas;
247
+ }
248
+ }
249
+ if (gasFeeEstimates) {
250
+ txMeta.gasFeeEstimates = gasFeeEstimates;
251
+ }
252
+ if (gasFeeEstimatesLoaded !== undefined) {
253
+ txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;
254
+ }
255
+ if (layer1GasFee) {
256
+ txMeta.layer1GasFee = layer1GasFee;
257
+ }
258
+ }
259
+ exports.updateTransactionGasFees = updateTransactionGasFees;
188
260
  //# sourceMappingURL=GasFeePoller.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"GasFeePoller.cjs","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,oEAA2C;AAO3C,2CAAqD;AACrD,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAElC,0CAA0C;AAO1C,wCAAmE;AACnE,oDAAkD;AAClD,0EAA0E;AAE1E,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC;;GAEG;AACH,MAAa,YAAY;IAuBvB;;;;;;;;;;;OAWG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA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GAAG,CAAC,0CAA0C,EAAE,yBAAyB,CAAC,CAAC;IAE3E,MAAM,aAAa,GAAG,KAAK,CAAC,IAAI,CAAC,yBAAyB,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CACvE,KAAK,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC,EAAE,EAAE;QACnC,OAAO;YACL,OAAO;YACP,MAAM,uBAAA,IAAI,kDAA8B,MAAlC,IAAI,EAA+B,EAAE,eAAe,EAAE,CAAC;SACrD,CAAC;IACb,CAAC,CACF,CAAC;IAEF,OAAO,IAAI,GAAG,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { projectLogger } from '../logger';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n Layer1GasFeeFlow,\n} from '../types';\nimport { TransactionStatus, type TransactionMeta } from '../types';\nimport { getGasFeeFlow } from '../utils/gas-flow';\nimport { getTransactionLayer1GasFee } from '../utils/layer1-gas-fee-flow';\n\nconst log = createModuleLogger(projectLogger, 'gas-fee-poller');\n\nconst INTERVAL_MILLISECONDS = 10000;\n\n/**\n * Automatically polls and updates suggested gas fees on unapproved transactions.\n */\nexport class GasFeePoller {\n hub: EventEmitter = new EventEmitter();\n\n readonly #findNetworkClientIdByChainId: (\n chainId: Hex,\n ) => NetworkClientId | undefined;\n\n readonly #gasFeeFlows: GasFeeFlow[];\n\n readonly #getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n\n readonly #getProvider: (networkClientId: NetworkClientId) => Provider;\n\n readonly #getTransactions: () => TransactionMeta[];\n\n readonly #layer1GasFeeFlows: Layer1GasFeeFlow[];\n\n #timeout: ReturnType<typeof setTimeout> | undefined;\n\n #running = false;\n\n /**\n * Constructs a new instance of the GasFeePoller.\n *\n * @param options - The options for this instance.\n * @param options.findNetworkClientIdByChainId - Callback to find the network client ID by chain ID.\n * @param options.gasFeeFlows - The gas fee flows to use to obtain suitable gas fees.\n * @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.\n * @param options.getProvider - Callback to obtain a provider instance.\n * @param options.getTransactions - Callback to obtain the transaction data.\n * @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.\n * @param options.onStateChange - Callback to register a listener for controller state changes.\n */\n constructor({\n findNetworkClientIdByChainId,\n gasFeeFlows,\n getGasFeeControllerEstimates,\n getProvider,\n getTransactions,\n layer1GasFeeFlows,\n onStateChange,\n }: {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getProvider: (networkClientId: NetworkClientId) => Provider;\n getTransactions: () => TransactionMeta[];\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n onStateChange: (listener: () => void) => void;\n }) {\n this.#findNetworkClientIdByChainId = findNetworkClientIdByChainId;\n this.#gasFeeFlows = gasFeeFlows;\n this.#layer1GasFeeFlows = layer1GasFeeFlows;\n this.#getGasFeeControllerEstimates = getGasFeeControllerEstimates;\n this.#getProvider = getProvider;\n this.#getTransactions = getTransactions;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (unapprovedTransactions.length) {\n this.#start();\n } else {\n this.#stop();\n }\n });\n }\n\n #start() {\n if (this.#running) {\n return;\n }\n\n // Intentionally not awaiting since this starts the timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#onTimeout();\n\n this.#running = true;\n\n log('Started polling');\n }\n\n #stop() {\n if (!this.#running) {\n return;\n }\n\n clearTimeout(this.#timeout);\n\n this.#timeout = undefined;\n this.#running = false;\n\n log('Stopped polling');\n }\n\n async #onTimeout() {\n await this.#updateUnapprovedTransactions();\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeout = setTimeout(() => this.#onTimeout(), INTERVAL_MILLISECONDS);\n }\n\n async #updateUnapprovedTransactions() {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (!unapprovedTransactions.length) {\n return;\n }\n\n log('Found unapproved transactions', unapprovedTransactions.length);\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactions,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactions.flatMap((tx) => {\n const { chainId } = tx;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransaction(tx, gasFeeControllerData);\n }),\n );\n }\n\n async #updateUnapprovedTransaction(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = transactionMeta;\n\n const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([\n this.#updateTransactionGasFeeEstimates(\n transactionMeta,\n gasFeeControllerData,\n ),\n this.#updateTransactionLayer1GasFee(transactionMeta),\n ]);\n\n if (!gasFeeEstimatesResponse && !layer1GasFee) {\n return;\n }\n\n this.hub.emit('transaction-updated', {\n transactionId: id,\n gasFeeEstimates: gasFeeEstimatesResponse?.gasFeeEstimates,\n gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,\n layer1GasFee,\n });\n }\n\n async #updateTransactionGasFeeEstimates(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ): Promise<\n | { gasFeeEstimates?: GasFeeEstimates; gasFeeEstimatesLoaded: boolean }\n | undefined\n > {\n const { networkClientId } = transactionMeta;\n\n const ethQuery = new EthQuery(this.#getProvider(networkClientId));\n const gasFeeFlow = getGasFeeFlow(transactionMeta, this.#gasFeeFlows);\n\n if (gasFeeFlow) {\n log(\n 'Found gas fee flow',\n gasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n }\n\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n transactionMeta,\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n if (gasFeeFlow) {\n try {\n const response = await gasFeeFlow.getGasFees(request);\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get suggested gas fees', transactionMeta.id, error);\n }\n }\n\n if (!gasFeeEstimates && transactionMeta.gasFeeEstimatesLoaded) {\n return undefined;\n }\n\n log('Updated gas fee estimates', {\n gasFeeEstimates,\n transaction: transactionMeta.id,\n });\n\n return { gasFeeEstimates, gasFeeEstimatesLoaded: true };\n }\n\n async #updateTransactionLayer1GasFee(\n transactionMeta: TransactionMeta,\n ): Promise<Hex | undefined> {\n const { networkClientId } = transactionMeta;\n const provider = this.#getProvider(networkClientId);\n\n const layer1GasFee = await getTransactionLayer1GasFee({\n layer1GasFeeFlows: this.#layer1GasFeeFlows,\n provider,\n transactionMeta,\n });\n\n if (layer1GasFee) {\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n }\n\n return layer1GasFee;\n }\n\n #getUnapprovedTransactions() {\n return this.#getTransactions().filter(\n (tx) => tx.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n"]}
1
+ {"version":3,"file":"GasFeePoller.cjs","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,oEAA2C;AAO3C,2CAAqD;AACrD,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAElC,0CAA0C;AAW1C,wCAKkB;AAClB,oDAAkD;AAClD,0EAA0E;AAE1E,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC;;GAEG;AACH,MAAa,YAAY;IAuBvB;;;;;;;;;;;OAWG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,aAAa,GAWd;;QApDD,QAAG,GAAiB,IAAI,gBAAY,EAAE,CAAC;QAE9B,6DAEwB;QAExB,4CAA2B;QAE3B,6DAEiB;QAEjB,4CAA6D;QAE7D,gDAA0C;QAE1C,kDAAuC;QAEhD,wCAAoD;QAEpD,gCAAW,KAAK,EAAC;QAiCf,uBAAA,IAAI,8CAAiC,4BAA4B,MAAA,CAAC;QAClE,uBAAA,IAAI,6BAAgB,WAAW,MAAA,CAAC;QAChC,uBAAA,IAAI,mCAAsB,iBAAiB,MAAA,CAAC;QAC5C,uBAAA,IAAI,8CAAiC,4BAA4B,MAAA,CAAC;QAClE,uBAAA,IAAI,6BAAgB,WAAW,MAAA,CAAC;QAChC,uBAAA,IAAI,iCAAoB,eAAe,MAAA,CAAC;QAExC,aAAa,CAAC,GAAG,E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MAAM,CAAC,QAAQ,CAAC,oBAAoB,GAAG,QAAQ,CAAC;aACjD;YAED,4CAA4C;YAC5C,OAAO,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC;SACjC;aAAM;YACL,mCAAmC;YACnC,IAAI,eAAe,KAAK,0BAAkB,CAAC,SAAS,EAAE;gBACpD,MAAM,wBAAwB,GAC5B,eAA2C,CAAC;gBAC9C,MAAM,CAAC,QAAQ,CAAC,QAAQ;oBACtB,wBAAwB,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC;aACvD;YAED,IAAI,eAAe,KAAK,0BAAkB,CAAC,QAAQ,EAAE;gBACnD,MAAM,uBAAuB,GAC3B,eAA0C,CAAC;gBAC7C,MAAM,CAAC,QAAQ,CAAC,QAAQ,GAAG,uBAAuB,CAAC,QAAQ,CAAC;aAC7D;YAED,IAAI,eAAe,KAAK,0BAAkB,CAAC,MAAM,EAAE;gBACjD,MAAM,qBAAqB,GAAG,eAAwC,CAAC;gBACvE,MAAM,CAAC,QAAQ,CAAC,QAAQ,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;aAChE;YAED,8DAA8D;YAC9D,OAAO,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC;YACpC,OAAO,MAAM,CAAC,QAAQ,CAAC,oBAAoB,CAAC;SAC7C;KACF;IAED,IAAI,eAAe,EAAE;QACnB,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC;KAC1C;IAED,IAAI,qBAAqB,KAAK,SAAS,EAAE;QACvC,MAAM,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;KACtD;IAED,IAAI,YAAY,EAAE;QAChB,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;KACpC;AACH,CAAC;AA9FD,4DA8FC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { projectLogger } from '../logger';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n} from '../types';\nimport {\n GasFeeEstimateLevel,\n GasFeeEstimateType,\n TransactionStatus,\n TransactionEnvelopeType,\n} from '../types';\nimport { getGasFeeFlow } from '../utils/gas-flow';\nimport { getTransactionLayer1GasFee } from '../utils/layer1-gas-fee-flow';\n\nconst log = createModuleLogger(projectLogger, 'gas-fee-poller');\n\nconst INTERVAL_MILLISECONDS = 10000;\n\n/**\n * Automatically polls and updates suggested gas fees on unapproved transactions.\n */\nexport class GasFeePoller {\n hub: EventEmitter = new EventEmitter();\n\n readonly #findNetworkClientIdByChainId: (\n chainId: Hex,\n ) => NetworkClientId | undefined;\n\n readonly #gasFeeFlows: GasFeeFlow[];\n\n readonly #getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n\n readonly #getProvider: (networkClientId: NetworkClientId) => Provider;\n\n readonly #getTransactions: () => TransactionMeta[];\n\n readonly #layer1GasFeeFlows: Layer1GasFeeFlow[];\n\n #timeout: ReturnType<typeof setTimeout> | undefined;\n\n #running = false;\n\n /**\n * Constructs a new instance of the GasFeePoller.\n *\n * @param options - The options for this instance.\n * @param options.findNetworkClientIdByChainId - Callback to find the network client ID by chain ID.\n * @param options.gasFeeFlows - The gas fee flows to use to obtain suitable gas fees.\n * @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.\n * @param options.getProvider - Callback to obtain a provider instance.\n * @param options.getTransactions - Callback to obtain the transaction data.\n * @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.\n * @param options.onStateChange - Callback to register a listener for controller state changes.\n */\n constructor({\n findNetworkClientIdByChainId,\n gasFeeFlows,\n getGasFeeControllerEstimates,\n getProvider,\n getTransactions,\n layer1GasFeeFlows,\n onStateChange,\n }: {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getProvider: (networkClientId: NetworkClientId) => Provider;\n getTransactions: () => TransactionMeta[];\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n onStateChange: (listener: () => void) => void;\n }) {\n this.#findNetworkClientIdByChainId = findNetworkClientIdByChainId;\n this.#gasFeeFlows = gasFeeFlows;\n this.#layer1GasFeeFlows = layer1GasFeeFlows;\n this.#getGasFeeControllerEstimates = getGasFeeControllerEstimates;\n this.#getProvider = getProvider;\n this.#getTransactions = getTransactions;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (unapprovedTransactions.length) {\n this.#start();\n } else {\n this.#stop();\n }\n });\n }\n\n #start() {\n if (this.#running) {\n return;\n }\n\n // Intentionally not awaiting since this starts the timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#onTimeout();\n\n this.#running = true;\n\n log('Started polling');\n }\n\n #stop() {\n if (!this.#running) {\n return;\n }\n\n clearTimeout(this.#timeout);\n\n this.#timeout = undefined;\n this.#running = false;\n\n log('Stopped polling');\n }\n\n async #onTimeout() {\n await this.#updateUnapprovedTransactions();\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeout = setTimeout(() => this.#onTimeout(), INTERVAL_MILLISECONDS);\n }\n\n async #updateUnapprovedTransactions() {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (!unapprovedTransactions.length) {\n return;\n }\n\n log('Found unapproved transactions', unapprovedTransactions.length);\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactions,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactions.flatMap((tx) => {\n const { chainId } = tx;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransaction(tx, gasFeeControllerData);\n }),\n );\n }\n\n async #updateUnapprovedTransaction(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = transactionMeta;\n\n const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([\n this.#updateTransactionGasFeeEstimates(\n transactionMeta,\n gasFeeControllerData,\n ),\n this.#updateTransactionLayer1GasFee(transactionMeta),\n ]);\n\n if (!gasFeeEstimatesResponse && !layer1GasFee) {\n return;\n }\n\n this.hub.emit('transaction-updated', {\n transactionId: id,\n gasFeeEstimates: gasFeeEstimatesResponse?.gasFeeEstimates,\n gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,\n layer1GasFee,\n });\n }\n\n async #updateTransactionGasFeeEstimates(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ): Promise<\n | { gasFeeEstimates?: GasFeeEstimates; gasFeeEstimatesLoaded: boolean }\n | undefined\n > {\n const { networkClientId } = transactionMeta;\n\n const ethQuery = new EthQuery(this.#getProvider(networkClientId));\n const gasFeeFlow = getGasFeeFlow(transactionMeta, this.#gasFeeFlows);\n\n if (gasFeeFlow) {\n log(\n 'Found gas fee flow',\n gasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n }\n\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n transactionMeta,\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n if (gasFeeFlow) {\n try {\n const response = await gasFeeFlow.getGasFees(request);\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get suggested gas fees', transactionMeta.id, error);\n }\n }\n\n if (!gasFeeEstimates && transactionMeta.gasFeeEstimatesLoaded) {\n return undefined;\n }\n\n log('Updated gas fee estimates', {\n gasFeeEstimates,\n transaction: transactionMeta.id,\n });\n\n return { gasFeeEstimates, gasFeeEstimatesLoaded: true };\n }\n\n async #updateTransactionLayer1GasFee(\n transactionMeta: TransactionMeta,\n ): Promise<Hex | undefined> {\n const { networkClientId } = transactionMeta;\n const provider = this.#getProvider(networkClientId);\n\n const layer1GasFee = await getTransactionLayer1GasFee({\n layer1GasFeeFlows: this.#layer1GasFeeFlows,\n provider,\n transactionMeta,\n });\n\n if (layer1GasFee) {\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n }\n\n return layer1GasFee;\n }\n\n #getUnapprovedTransactions() {\n return this.#getTransactions().filter(\n (tx) => tx.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n\n/**\n * Update the gas fees for a transaction.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.gasFeeEstimates - The gas fee estimates.\n * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.\n * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.\n * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport async function updateTransactionGasFees({\n txMeta,\n getEIP1559Compatibility,\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n}: {\n txMeta: TransactionMeta;\n getEIP1559Compatibility: (\n networkClientId: NetworkClientId,\n ) => Promise<boolean>;\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: boolean;\n layer1GasFee?: Hex;\n}): Promise<void> {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const { type: gasEstimateType } = gasFeeEstimates ?? {};\n\n if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy &&\n (await getEIP1559Compatibility(txMeta.networkClientId));\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n txMeta.txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n\n txMeta.txParams.maxFeePerGas = gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txMeta.txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txMeta.txParams.gasPrice =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txMeta.txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txMeta.txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txMeta.txParams.maxFeePerGas;\n delete txMeta.txParams.maxPriorityFeePerGas;\n }\n }\n\n if (gasFeeEstimates) {\n txMeta.gasFeeEstimates = gasFeeEstimates;\n }\n\n if (gasFeeEstimatesLoaded !== undefined) {\n txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;\n }\n\n if (layer1GasFee) {\n txMeta.layer1GasFee = layer1GasFee;\n }\n}\n"]}
@@ -3,8 +3,7 @@ import type { FetchGasFeeEstimateOptions, GasFeeState } from "@metamask/gas-fee-
3
3
  import type { NetworkClientId, Provider } from "@metamask/network-controller";
4
4
  import type { Hex } from "@metamask/utils";
5
5
  import EventEmitter from "events";
6
- import type { GasFeeFlow, Layer1GasFeeFlow } from "../types.cjs";
7
- import { type TransactionMeta } from "../types.cjs";
6
+ import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta } from "../types.cjs";
8
7
  /**
9
8
  * Automatically polls and updates suggested gas fees on unapproved transactions.
10
9
  */
@@ -33,4 +32,23 @@ export declare class GasFeePoller {
33
32
  onStateChange: (listener: () => void) => void;
34
33
  });
35
34
  }
35
+ /**
36
+ * Update the gas fees for a transaction.
37
+ *
38
+ * @param args - Argument bag.
39
+ * @param args.txMeta - The transaction meta.
40
+ * @param args.gasFeeEstimates - The gas fee estimates.
41
+ * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
42
+ * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
43
+ * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
44
+ * @param args.layer1GasFee - The layer 1 gas fee.
45
+ */
46
+ export declare function updateTransactionGasFees({ txMeta, getEIP1559Compatibility, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }: {
47
+ txMeta: TransactionMeta;
48
+ getEIP1559Compatibility: (networkClientId: NetworkClientId) => Promise<boolean>;
49
+ gasFeeEstimates?: GasFeeEstimates;
50
+ gasFeeEstimatesLoaded?: boolean;
51
+ isTxParamsGasFeeUpdatesEnabled: boolean;
52
+ layer1GasFee?: Hex;
53
+ }): Promise<void>;
36
54
  //# sourceMappingURL=GasFeePoller.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"GasFeePoller.d.cts","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EACV,0BAA0B,EAC1B,WAAW,EACZ,qCAAqC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,qCAAqC;AAC9E,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAI3C,OAAO,YAAY,eAAe;AAGlC,OAAO,KAAK,EAEV,UAAU,EAEV,gBAAgB,EACjB,qBAAiB;AAClB,OAAO,EAAqB,KAAK,eAAe,EAAE,qBAAiB;AAQnE;;GAEG;AACH,qBAAa,YAAY;;IACvB,GAAG,EAAE,YAAY,CAAsB;IAsBv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AAE,CAC5B,OAAO,EAAE,0BAA0B,KAChC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC1B,WAAW,EAAE,CAAC,eAAe,EAAE,eAAe,KAAK,QAAQ,CAAC;QAC5D,eAAe,EAAE,MAAM,eAAe,EAAE,CAAC;QACzC,iBAAiB,EAAE,gBAAgB,EAAE,CAAC;QACtC,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;KAC/C;CAsNF;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,wBAAwB,CAAC,EAC7C,MAAM,EACN,uBAAuB,EACvB,eAAe,EACf,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,GACb,EAAE;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,uBAAuB,EAAE,CACvB,eAAe,EAAE,eAAe,KAC7B,OAAO,CAAC,OAAO,CAAC,CAAC;IACtB,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8BAA8B,EAAE,OAAO,CAAC;IACxC,YAAY,CAAC,EAAE,GAAG,CAAC;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CA8EhB"}
@@ -3,8 +3,7 @@ import type { FetchGasFeeEstimateOptions, GasFeeState } from "@metamask/gas-fee-
3
3
  import type { NetworkClientId, Provider } from "@metamask/network-controller";
4
4
  import type { Hex } from "@metamask/utils";
5
5
  import EventEmitter from "events";
6
- import type { GasFeeFlow, Layer1GasFeeFlow } from "../types.mjs";
7
- import { type TransactionMeta } from "../types.mjs";
6
+ import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta } from "../types.mjs";
8
7
  /**
9
8
  * Automatically polls and updates suggested gas fees on unapproved transactions.
10
9
  */
@@ -33,4 +32,23 @@ export declare class GasFeePoller {
33
32
  onStateChange: (listener: () => void) => void;
34
33
  });
35
34
  }
35
+ /**
36
+ * Update the gas fees for a transaction.
37
+ *
38
+ * @param args - Argument bag.
39
+ * @param args.txMeta - The transaction meta.
40
+ * @param args.gasFeeEstimates - The gas fee estimates.
41
+ * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
42
+ * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
43
+ * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
44
+ * @param args.layer1GasFee - The layer 1 gas fee.
45
+ */
46
+ export declare function updateTransactionGasFees({ txMeta, getEIP1559Compatibility, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }: {
47
+ txMeta: TransactionMeta;
48
+ getEIP1559Compatibility: (networkClientId: NetworkClientId) => Promise<boolean>;
49
+ gasFeeEstimates?: GasFeeEstimates;
50
+ gasFeeEstimatesLoaded?: boolean;
51
+ isTxParamsGasFeeUpdatesEnabled: boolean;
52
+ layer1GasFee?: Hex;
53
+ }): Promise<void>;
36
54
  //# sourceMappingURL=GasFeePoller.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"GasFeePoller.d.mts","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EACV,0BAA0B,EAC1B,WAAW,EACZ,qCAAqC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,qCAAqC;AAC9E,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAI3C,OAAO,YAAY,eAAe;AAGlC,OAAO,KAAK,EAEV,UAAU,EAEV,gBAAgB,EACjB,qBAAiB;AAClB,OAAO,EAAqB,KAAK,eAAe,EAAE,qBAAiB;AAQnE;;GAEG;AACH,qBAAa,YAAY;;IACvB,GAAG,EAAE,YAAY,CAAsB;IAsBvC;;;;;;;;;;;OAWG;gBACS,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,aAAa,GACd,EAAE;QACD,4BAA4B,EAAE,CAA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,MAAM,eAAe,EAAE,CAAC;QACzC,iBAAiB,EAAE,gBAAgB,EAAE,CAAC;QACtC,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;KAC/C;CAsNF;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,wBAAwB,CAAC,EAC7C,MAAM,EACN,uBAAuB,EACvB,eAAe,EACf,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,GACb,EAAE;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,uBAAuB,EAAE,CACvB,eAAe,EAAE,eAAe,KAC7B,OAAO,CAAC,OAAO,CAAC,CAAC;IACtB,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8BAA8B,EAAE,OAAO,CAAC;IACxC,YAAY,CAAC,EAAE,GAAG,CAAC;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CA8EhB"}
@@ -23,7 +23,7 @@ import { createModuleLogger } from "@metamask/utils";
23
23
  // eslint-disable-next-line import-x/no-nodejs-modules
24
24
  import EventEmitter from "events";
25
25
  import { projectLogger } from "../logger.mjs";
26
- import { TransactionStatus } from "../types.mjs";
26
+ import { GasFeeEstimateLevel, GasFeeEstimateType, TransactionStatus, TransactionEnvelopeType } from "../types.mjs";
27
27
  import { getGasFeeFlow } from "../utils/gas-flow.mjs";
28
28
  import { getTransactionLayer1GasFee } from "../utils/layer1-gas-fee-flow.mjs";
29
29
  const log = createModuleLogger(projectLogger, 'gas-fee-poller');
@@ -185,4 +185,75 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
185
185
  });
186
186
  return new Map(await Promise.all(entryPromises));
187
187
  };
188
+ /**
189
+ * Update the gas fees for a transaction.
190
+ *
191
+ * @param args - Argument bag.
192
+ * @param args.txMeta - The transaction meta.
193
+ * @param args.gasFeeEstimates - The gas fee estimates.
194
+ * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
195
+ * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
196
+ * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
197
+ * @param args.layer1GasFee - The layer 1 gas fee.
198
+ */
199
+ export async function updateTransactionGasFees({ txMeta, getEIP1559Compatibility, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }) {
200
+ const userFeeLevel = txMeta.userFeeLevel;
201
+ const isUsingGasFeeEstimateLevel = Object.values(GasFeeEstimateLevel).includes(userFeeLevel);
202
+ const { type: gasEstimateType } = gasFeeEstimates ?? {};
203
+ if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {
204
+ const isEIP1559Compatible = txMeta.txParams.type !== TransactionEnvelopeType.legacy &&
205
+ (await getEIP1559Compatibility(txMeta.networkClientId));
206
+ if (isEIP1559Compatible) {
207
+ // Handle EIP-1559 compatible transactions
208
+ if (gasEstimateType === GasFeeEstimateType.FeeMarket) {
209
+ const feeMarketGasFeeEstimates = gasFeeEstimates;
210
+ txMeta.txParams.maxFeePerGas =
211
+ feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;
212
+ txMeta.txParams.maxPriorityFeePerGas =
213
+ feeMarketGasFeeEstimates[userFeeLevel].maxPriorityFeePerGas;
214
+ }
215
+ if (gasEstimateType === GasFeeEstimateType.GasPrice) {
216
+ const gasPriceGasFeeEstimates = gasFeeEstimates;
217
+ txMeta.txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;
218
+ txMeta.txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;
219
+ }
220
+ if (gasEstimateType === GasFeeEstimateType.Legacy) {
221
+ const legacyGasFeeEstimates = gasFeeEstimates;
222
+ const gasPrice = legacyGasFeeEstimates[userFeeLevel];
223
+ txMeta.txParams.maxFeePerGas = gasPrice;
224
+ txMeta.txParams.maxPriorityFeePerGas = gasPrice;
225
+ }
226
+ // Remove gasPrice for EIP-1559 transactions
227
+ delete txMeta.txParams.gasPrice;
228
+ }
229
+ else {
230
+ // Handle non-EIP-1559 transactions
231
+ if (gasEstimateType === GasFeeEstimateType.FeeMarket) {
232
+ const feeMarketGasFeeEstimates = gasFeeEstimates;
233
+ txMeta.txParams.gasPrice =
234
+ feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;
235
+ }
236
+ if (gasEstimateType === GasFeeEstimateType.GasPrice) {
237
+ const gasPriceGasFeeEstimates = gasFeeEstimates;
238
+ txMeta.txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;
239
+ }
240
+ if (gasEstimateType === GasFeeEstimateType.Legacy) {
241
+ const legacyGasFeeEstimates = gasFeeEstimates;
242
+ txMeta.txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];
243
+ }
244
+ // Remove EIP-1559 specific parameters for legacy transactions
245
+ delete txMeta.txParams.maxFeePerGas;
246
+ delete txMeta.txParams.maxPriorityFeePerGas;
247
+ }
248
+ }
249
+ if (gasFeeEstimates) {
250
+ txMeta.gasFeeEstimates = gasFeeEstimates;
251
+ }
252
+ if (gasFeeEstimatesLoaded !== undefined) {
253
+ txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;
254
+ }
255
+ if (layer1GasFee) {
256
+ txMeta.layer1GasFee = layer1GasFee;
257
+ }
258
+ }
188
259
  //# sourceMappingURL=GasFeePoller.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"GasFeePoller.mjs","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,SAAQ,4BAA4B;;AAO3C,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AACrD,kEAAkE;AAClE,sDAAsD;AACtD,OAAO,YAAY,eAAe;AAElC,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAO1C,OAAO,EAAE,iBAAiB,EAAwB,qBAAiB;AACnE,OAAO,EAAE,aAAa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;QAEd,IAAI,yBAAyB,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;YAC1C,SAAS;SACV;QAED,MAAM,eAAe,GACnB,0BAA0B;YACzB,uBAAA,IAAI,kDAA8B,MAAlC,IAAI,EAA+B,OAAO,CAAY,CAAC;QAE1D,yBAAyB,CAAC,GAAG,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;KACzD;IAED,GAAG,CAAC,0CAA0C,EAAE,yBAAyB,CAAC,CAAC;IAE3E,MAAM,aAAa,GAAG,KAAK,CAAC,IAAI,CAAC,yBAAyB,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CACvE,KAAK,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC,EAAE,EAAE;QACnC,OAAO;YACL,OAAO;YACP,MAAM,uBAAA,IAAI,kDAA8B,MAAlC,IAAI,EAA+B,EAAE,eAAe,EAAE,CAAC;SACrD,CAAC;IACb,CAAC,CACF,CAAC;IAEF,OAAO,IAAI,GAAG,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { projectLogger } from '../logger';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n Layer1GasFeeFlow,\n} from '../types';\nimport { TransactionStatus, type TransactionMeta } from '../types';\nimport { getGasFeeFlow } from '../utils/gas-flow';\nimport { getTransactionLayer1GasFee } from '../utils/layer1-gas-fee-flow';\n\nconst log = createModuleLogger(projectLogger, 'gas-fee-poller');\n\nconst INTERVAL_MILLISECONDS = 10000;\n\n/**\n * Automatically polls and updates suggested gas fees on unapproved transactions.\n */\nexport class GasFeePoller {\n hub: EventEmitter = new EventEmitter();\n\n readonly #findNetworkClientIdByChainId: (\n chainId: Hex,\n ) => NetworkClientId | undefined;\n\n readonly #gasFeeFlows: GasFeeFlow[];\n\n readonly #getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n\n readonly #getProvider: (networkClientId: NetworkClientId) => Provider;\n\n readonly #getTransactions: () => TransactionMeta[];\n\n readonly #layer1GasFeeFlows: Layer1GasFeeFlow[];\n\n #timeout: ReturnType<typeof setTimeout> | undefined;\n\n #running = false;\n\n /**\n * Constructs a new instance of the GasFeePoller.\n *\n * @param options - The options for this instance.\n * @param options.findNetworkClientIdByChainId - Callback to find the network client ID by chain ID.\n * @param options.gasFeeFlows - The gas fee flows to use to obtain suitable gas fees.\n * @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.\n * @param options.getProvider - Callback to obtain a provider instance.\n * @param options.getTransactions - Callback to obtain the transaction data.\n * @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.\n * @param options.onStateChange - Callback to register a listener for controller state changes.\n */\n constructor({\n findNetworkClientIdByChainId,\n gasFeeFlows,\n getGasFeeControllerEstimates,\n getProvider,\n getTransactions,\n layer1GasFeeFlows,\n onStateChange,\n }: {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getProvider: (networkClientId: NetworkClientId) => Provider;\n getTransactions: () => TransactionMeta[];\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n onStateChange: (listener: () => void) => void;\n }) {\n this.#findNetworkClientIdByChainId = findNetworkClientIdByChainId;\n this.#gasFeeFlows = gasFeeFlows;\n this.#layer1GasFeeFlows = layer1GasFeeFlows;\n this.#getGasFeeControllerEstimates = getGasFeeControllerEstimates;\n this.#getProvider = getProvider;\n this.#getTransactions = getTransactions;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (unapprovedTransactions.length) {\n this.#start();\n } else {\n this.#stop();\n }\n });\n }\n\n #start() {\n if (this.#running) {\n return;\n }\n\n // Intentionally not awaiting since this starts the timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#onTimeout();\n\n this.#running = true;\n\n log('Started polling');\n }\n\n #stop() {\n if (!this.#running) {\n return;\n }\n\n clearTimeout(this.#timeout);\n\n this.#timeout = undefined;\n this.#running = false;\n\n log('Stopped polling');\n }\n\n async #onTimeout() {\n await this.#updateUnapprovedTransactions();\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeout = setTimeout(() => this.#onTimeout(), INTERVAL_MILLISECONDS);\n }\n\n async #updateUnapprovedTransactions() {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (!unapprovedTransactions.length) {\n return;\n }\n\n log('Found unapproved transactions', unapprovedTransactions.length);\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactions,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactions.flatMap((tx) => {\n const { chainId } = tx;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransaction(tx, gasFeeControllerData);\n }),\n );\n }\n\n async #updateUnapprovedTransaction(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = transactionMeta;\n\n const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([\n this.#updateTransactionGasFeeEstimates(\n transactionMeta,\n gasFeeControllerData,\n ),\n this.#updateTransactionLayer1GasFee(transactionMeta),\n ]);\n\n if (!gasFeeEstimatesResponse && !layer1GasFee) {\n return;\n }\n\n this.hub.emit('transaction-updated', {\n transactionId: id,\n gasFeeEstimates: gasFeeEstimatesResponse?.gasFeeEstimates,\n gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,\n layer1GasFee,\n });\n }\n\n async #updateTransactionGasFeeEstimates(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ): Promise<\n | { gasFeeEstimates?: GasFeeEstimates; gasFeeEstimatesLoaded: boolean }\n | undefined\n > {\n const { networkClientId } = transactionMeta;\n\n const ethQuery = new EthQuery(this.#getProvider(networkClientId));\n const gasFeeFlow = getGasFeeFlow(transactionMeta, this.#gasFeeFlows);\n\n if (gasFeeFlow) {\n log(\n 'Found gas fee flow',\n gasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n }\n\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n transactionMeta,\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n if (gasFeeFlow) {\n try {\n const response = await gasFeeFlow.getGasFees(request);\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get suggested gas fees', transactionMeta.id, error);\n }\n }\n\n if (!gasFeeEstimates && transactionMeta.gasFeeEstimatesLoaded) {\n return undefined;\n }\n\n log('Updated gas fee estimates', {\n gasFeeEstimates,\n transaction: transactionMeta.id,\n });\n\n return { gasFeeEstimates, gasFeeEstimatesLoaded: true };\n }\n\n async #updateTransactionLayer1GasFee(\n transactionMeta: TransactionMeta,\n ): Promise<Hex | undefined> {\n const { networkClientId } = transactionMeta;\n const provider = this.#getProvider(networkClientId);\n\n const layer1GasFee = await getTransactionLayer1GasFee({\n layer1GasFeeFlows: this.#layer1GasFeeFlows,\n provider,\n transactionMeta,\n });\n\n if (layer1GasFee) {\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n }\n\n return layer1GasFee;\n }\n\n #getUnapprovedTransactions() {\n return this.#getTransactions().filter(\n (tx) => tx.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n"]}
1
+ {"version":3,"file":"GasFeePoller.mjs","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,SAAQ,4BAA4B;;AAO3C,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AACrD,kEAAkE;AAClE,sDAAsD;AACtD,OAAO,YAAY,eAAe;AAElC,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAW1C,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,iBAAiB,EACjB,uBAAuB,EACxB,qBAAiB;AAClB,OAAO,EAAE,aAAa,EAAE,8BAA0B;AAClD,OAAO,EAAE,0BAA0B,EAAE,yCAAqC;AAE1E,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,YAAY;IAuBvB;;;;;;;;;;;OAWG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,aAAa,GAWd;;QApDD,QAAG,GAAiB,IAAI,YAAY,EAAE,CAAC;QAE9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;KACF;IAED,IAAI,eAAe,EAAE;QACnB,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC;KAC1C;IAED,IAAI,qBAAqB,KAAK,SAAS,EAAE;QACvC,MAAM,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;KACtD;IAED,IAAI,YAAY,EAAE;QAChB,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;KACpC;AACH,CAAC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { projectLogger } from '../logger';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n} from '../types';\nimport {\n GasFeeEstimateLevel,\n GasFeeEstimateType,\n TransactionStatus,\n TransactionEnvelopeType,\n} from '../types';\nimport { getGasFeeFlow } from '../utils/gas-flow';\nimport { getTransactionLayer1GasFee } from '../utils/layer1-gas-fee-flow';\n\nconst log = createModuleLogger(projectLogger, 'gas-fee-poller');\n\nconst INTERVAL_MILLISECONDS = 10000;\n\n/**\n * Automatically polls and updates suggested gas fees on unapproved transactions.\n */\nexport class GasFeePoller {\n hub: EventEmitter = new EventEmitter();\n\n readonly #findNetworkClientIdByChainId: (\n chainId: Hex,\n ) => NetworkClientId | undefined;\n\n readonly #gasFeeFlows: GasFeeFlow[];\n\n readonly #getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n\n readonly #getProvider: (networkClientId: NetworkClientId) => Provider;\n\n readonly #getTransactions: () => TransactionMeta[];\n\n readonly #layer1GasFeeFlows: Layer1GasFeeFlow[];\n\n #timeout: ReturnType<typeof setTimeout> | undefined;\n\n #running = false;\n\n /**\n * Constructs a new instance of the GasFeePoller.\n *\n * @param options - The options for this instance.\n * @param options.findNetworkClientIdByChainId - Callback to find the network client ID by chain ID.\n * @param options.gasFeeFlows - The gas fee flows to use to obtain suitable gas fees.\n * @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.\n * @param options.getProvider - Callback to obtain a provider instance.\n * @param options.getTransactions - Callback to obtain the transaction data.\n * @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.\n * @param options.onStateChange - Callback to register a listener for controller state changes.\n */\n constructor({\n findNetworkClientIdByChainId,\n gasFeeFlows,\n getGasFeeControllerEstimates,\n getProvider,\n getTransactions,\n layer1GasFeeFlows,\n onStateChange,\n }: {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getProvider: (networkClientId: NetworkClientId) => Provider;\n getTransactions: () => TransactionMeta[];\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n onStateChange: (listener: () => void) => void;\n }) {\n this.#findNetworkClientIdByChainId = findNetworkClientIdByChainId;\n this.#gasFeeFlows = gasFeeFlows;\n this.#layer1GasFeeFlows = layer1GasFeeFlows;\n this.#getGasFeeControllerEstimates = getGasFeeControllerEstimates;\n this.#getProvider = getProvider;\n this.#getTransactions = getTransactions;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (unapprovedTransactions.length) {\n this.#start();\n } else {\n this.#stop();\n }\n });\n }\n\n #start() {\n if (this.#running) {\n return;\n }\n\n // Intentionally not awaiting since this starts the timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#onTimeout();\n\n this.#running = true;\n\n log('Started polling');\n }\n\n #stop() {\n if (!this.#running) {\n return;\n }\n\n clearTimeout(this.#timeout);\n\n this.#timeout = undefined;\n this.#running = false;\n\n log('Stopped polling');\n }\n\n async #onTimeout() {\n await this.#updateUnapprovedTransactions();\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeout = setTimeout(() => this.#onTimeout(), INTERVAL_MILLISECONDS);\n }\n\n async #updateUnapprovedTransactions() {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (!unapprovedTransactions.length) {\n return;\n }\n\n log('Found unapproved transactions', unapprovedTransactions.length);\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactions,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactions.flatMap((tx) => {\n const { chainId } = tx;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransaction(tx, gasFeeControllerData);\n }),\n );\n }\n\n async #updateUnapprovedTransaction(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = transactionMeta;\n\n const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([\n this.#updateTransactionGasFeeEstimates(\n transactionMeta,\n gasFeeControllerData,\n ),\n this.#updateTransactionLayer1GasFee(transactionMeta),\n ]);\n\n if (!gasFeeEstimatesResponse && !layer1GasFee) {\n return;\n }\n\n this.hub.emit('transaction-updated', {\n transactionId: id,\n gasFeeEstimates: gasFeeEstimatesResponse?.gasFeeEstimates,\n gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,\n layer1GasFee,\n });\n }\n\n async #updateTransactionGasFeeEstimates(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ): Promise<\n | { gasFeeEstimates?: GasFeeEstimates; gasFeeEstimatesLoaded: boolean }\n | undefined\n > {\n const { networkClientId } = transactionMeta;\n\n const ethQuery = new EthQuery(this.#getProvider(networkClientId));\n const gasFeeFlow = getGasFeeFlow(transactionMeta, this.#gasFeeFlows);\n\n if (gasFeeFlow) {\n log(\n 'Found gas fee flow',\n gasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n }\n\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n transactionMeta,\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n if (gasFeeFlow) {\n try {\n const response = await gasFeeFlow.getGasFees(request);\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get suggested gas fees', transactionMeta.id, error);\n }\n }\n\n if (!gasFeeEstimates && transactionMeta.gasFeeEstimatesLoaded) {\n return undefined;\n }\n\n log('Updated gas fee estimates', {\n gasFeeEstimates,\n transaction: transactionMeta.id,\n });\n\n return { gasFeeEstimates, gasFeeEstimatesLoaded: true };\n }\n\n async #updateTransactionLayer1GasFee(\n transactionMeta: TransactionMeta,\n ): Promise<Hex | undefined> {\n const { networkClientId } = transactionMeta;\n const provider = this.#getProvider(networkClientId);\n\n const layer1GasFee = await getTransactionLayer1GasFee({\n layer1GasFeeFlows: this.#layer1GasFeeFlows,\n provider,\n transactionMeta,\n });\n\n if (layer1GasFee) {\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n }\n\n return layer1GasFee;\n }\n\n #getUnapprovedTransactions() {\n return this.#getTransactions().filter(\n (tx) => tx.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n\n/**\n * Update the gas fees for a transaction.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.gasFeeEstimates - The gas fee estimates.\n * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.\n * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.\n * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport async function updateTransactionGasFees({\n txMeta,\n getEIP1559Compatibility,\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n}: {\n txMeta: TransactionMeta;\n getEIP1559Compatibility: (\n networkClientId: NetworkClientId,\n ) => Promise<boolean>;\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: boolean;\n layer1GasFee?: Hex;\n}): Promise<void> {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const { type: gasEstimateType } = gasFeeEstimates ?? {};\n\n if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy &&\n (await getEIP1559Compatibility(txMeta.networkClientId));\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n txMeta.txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n\n txMeta.txParams.maxFeePerGas = gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txMeta.txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txMeta.txParams.gasPrice =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txMeta.txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txMeta.txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txMeta.txParams.maxFeePerGas;\n delete txMeta.txParams.maxPriorityFeePerGas;\n }\n }\n\n if (gasFeeEstimates) {\n txMeta.gasFeeEstimates = gasFeeEstimates;\n }\n\n if (gasFeeEstimatesLoaded !== undefined) {\n txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;\n }\n\n if (layer1GasFee) {\n txMeta.layer1GasFee = layer1GasFee;\n }\n}\n"]}
@@ -4,8 +4,8 @@ exports.getBatchSizeLimit = exports.getEIP7702UpgradeContractAddress = exports.g
4
4
  const utils_1 = require("@metamask/utils");
5
5
  const signature_1 = require("./signature.cjs");
6
6
  const logger_1 = require("../logger.cjs");
7
- exports.FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';
8
- exports.FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';
7
+ exports.FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';
8
+ exports.FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';
9
9
  const DEFAULT_BATCH_SIZE_LIMIT = 10;
10
10
  const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'feature-flags');
11
11
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"feature-flags.cjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":";;;AAAA,2CAA+D;AAE/D,+CAA+C;AAC/C,0CAA0C;AAG7B,QAAA,yBAAyB,GAAG,4BAA4B,CAAC;AACzD,QAAA,qBAAqB,GAAG,wBAAwB,CAAC;AAE9D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AA8BpC,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,eAAe,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,SAAgB,yBAAyB,CACvC,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,YAAY,EAAE,CAAC,6BAAqB,CAAC,EAAE,eAAe,IAAI,EAAE,CAAC;AACtE,CAAC;AALD,8DAKC;AAED;;;;;;;GAOG;AACH,SAAgB,2BAA2B,CACzC,OAAY,EACZ,SAAyC,EACzC,SAAc;IAEd,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAEhD,MAAM,SAAS,GACb,YAAY,EAAE,CAAC,6BAAqB,CAAC,EAAE,SAAS,EAAE,CAChD,OAAO,CAAC,WAAW,EAAS,CAC7B,IAAI,EAAE,CAAC;IAEV,OAAO,SAAS;SACb,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CACnB,IAAA,4BAAgB,EACd,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,EAC3B,QAAQ,CAAC,SAAS,EAClB,SAAS,CACV,CACF;SACA,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC;AArBD,kEAqBC;AAED;;;;;;;GAOG;AACH,SAAgB,gCAAgC,CAC9C,OAAY,EACZ,SAAyC,EACzC,SAAc;IAEd,OAAO,2BAA2B,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACzE,CAAC;AAND,4EAMC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAC/B,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,CACL,YAAY,EAAE,CAAC,iCAAyB,CAAC,EAAE,cAAc;QACzD,wBAAwB,CACzB,CAAC;AACJ,CAAC;AARD,8CAQC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CACtB,SAAyC;IAEzC,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CACjC,sCAAsC,CACvC,CAAC,kBAAkB,CAAC;IAErB,GAAG,CAAC,yBAAyB,EAAE,YAAY,CAAC,CAAC;IAE7C,OAAO,YAAiD,CAAC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}
1
+ {"version":3,"file":"feature-flags.cjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":";;;AAAA,2CAA+D;AAE/D,+CAA+C;AAC/C,0CAA0C;AAG7B,QAAA,yBAAyB,GAAG,4BAA4B,CAAC;AACzD,QAAA,qBAAqB,GAAG,wBAAwB,CAAC;AAE9D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AA8BpC,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,eAAe,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,SAAgB,yBAAyB,CACvC,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,YAAY,EAAE,CAAC,6BAAqB,CAAC,EAAE,eAAe,IAAI,EAAE,CAAC;AACtE,CAAC;AALD,8DAKC;AAED;;;;;;;GAOG;AACH,SAAgB,2BAA2B,CACzC,OAAY,EACZ,SAAyC,EACzC,SAAc;IAEd,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAEhD,MAAM,SAAS,GACb,YAAY,EAAE,CAAC,6BAAqB,CAAC,EAAE,SAAS,EAAE,CAChD,OAAO,CAAC,WAAW,EAAS,CAC7B,IAAI,EAAE,CAAC;IAEV,OAAO,SAAS;SACb,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CACnB,IAAA,4BAAgB,EACd,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,EAC3B,QAAQ,CAAC,SAAS,EAClB,SAAS,CACV,CACF;SACA,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC;AArBD,kEAqBC;AAED;;;;;;;GAOG;AACH,SAAgB,gCAAgC,CAC9C,OAAY,EACZ,SAAyC,EACzC,SAAc;IAEd,OAAO,2BAA2B,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACzE,CAAC;AAND,4EAMC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAC/B,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,CACL,YAAY,EAAE,CAAC,iCAAyB,CAAC,EAAE,cAAc;QACzD,wBAAwB,CACzB,CAAC;AACJ,CAAC;AARD,8CAQC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CACtB,SAAyC;IAEzC,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CACjC,sCAAsC,CACvC,CAAC,kBAAkB,CAAC;IAErB,GAAG,CAAC,yBAAyB,EAAE,YAAY,CAAC,CAAC;IAE7C,OAAO,YAAiD,CAAC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import { type Hex } from "@metamask/utils";
2
2
  import type { TransactionControllerMessenger } from "../TransactionController.cjs";
3
- export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations-transactions";
4
- export declare const FEATURE_FLAG_EIP_7702 = "confirmations-eip-7702";
3
+ export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations_transactions";
4
+ export declare const FEATURE_FLAG_EIP_7702 = "confirmations_eip_7702";
5
5
  export type TransactionControllerFeatureFlags = {
6
6
  [FEATURE_FLAG_EIP_7702]?: {
7
7
  /**
@@ -1,7 +1,7 @@
1
1
  import { type Hex } from "@metamask/utils";
2
2
  import type { TransactionControllerMessenger } from "../TransactionController.mjs";
3
- export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations-transactions";
4
- export declare const FEATURE_FLAG_EIP_7702 = "confirmations-eip-7702";
3
+ export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations_transactions";
4
+ export declare const FEATURE_FLAG_EIP_7702 = "confirmations_eip_7702";
5
5
  export type TransactionControllerFeatureFlags = {
6
6
  [FEATURE_FLAG_EIP_7702]?: {
7
7
  /**
@@ -1,8 +1,8 @@
1
1
  import { createModuleLogger } from "@metamask/utils";
2
2
  import { isValidSignature } from "./signature.mjs";
3
3
  import { projectLogger } from "../logger.mjs";
4
- export const FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';
5
- export const FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';
4
+ export const FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';
5
+ export const FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';
6
6
  const DEFAULT_BATCH_SIZE_LIMIT = 10;
7
7
  const log = createModuleLogger(projectLogger, 'feature-flags');
8
8
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"feature-flags.mjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAY,wBAAwB;AAE/D,OAAO,EAAE,gBAAgB,EAAE,wBAAoB;AAC/C,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAG1C,MAAM,CAAC,MAAM,yBAAyB,GAAG,4BAA4B,CAAC;AACtE,MAAM,CAAC,MAAM,qBAAqB,GAAG,wBAAwB,CAAC;AAE9D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AA8BpC,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,YAAY,EAAE,CAAC,qBAAqB,CAAC,EAAE,eAAe,IAAI,EAAE,CAAC;AACtE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,2BAA2B,CACzC,OAAY,EACZ,SAAyC,EACzC,SAAc;IAEd,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAEhD,MAAM,SAAS,GACb,YAAY,EAAE,CAAC,qBAAqB,CAAC,EAAE,SAAS,EAAE,CAChD,OAAO,CAAC,WAAW,EAAS,CAC7B,IAAI,EAAE,CAAC;IAEV,OAAO,SAAS;SACb,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CACnB,gBAAgB,CACd,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,EAC3B,QAAQ,CAAC,SAAS,EAClB,SAAS,CACV,CACF;SACA,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gCAAgC,CAC9C,OAAY,EACZ,SAAyC,EACzC,SAAc;IAEd,OAAO,2BAA2B,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,CACL,YAAY,EAAE,CAAC,yBAAyB,CAAC,EAAE,cAAc;QACzD,wBAAwB,CACzB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CACtB,SAAyC;IAEzC,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CACjC,sCAAsC,CACvC,CAAC,kBAAkB,CAAC;IAErB,GAAG,CAAC,yBAAyB,EAAE,YAAY,CAAC,CAAC;IAE7C,OAAO,YAAiD,CAAC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}
1
+ {"version":3,"file":"feature-flags.mjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAY,wBAAwB;AAE/D,OAAO,EAAE,gBAAgB,EAAE,wBAAoB;AAC/C,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAG1C,MAAM,CAAC,MAAM,yBAAyB,GAAG,4BAA4B,CAAC;AACtE,MAAM,CAAC,MAAM,qBAAqB,GAAG,wBAAwB,CAAC;AAE9D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AA8BpC,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS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AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}