@metamask/transaction-controller 56.0.0 → 56.2.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.
- package/CHANGELOG.md +32 -1
- package/dist/TransactionController.cjs +64 -18
- package/dist/TransactionController.cjs.map +1 -1
- package/dist/TransactionController.d.cts +10 -2
- package/dist/TransactionController.d.cts.map +1 -1
- package/dist/TransactionController.d.mts +10 -2
- package/dist/TransactionController.d.mts.map +1 -1
- package/dist/TransactionController.mjs +66 -20
- package/dist/TransactionController.mjs.map +1 -1
- package/dist/api/accounts-api.cjs +3 -2
- package/dist/api/accounts-api.cjs.map +1 -1
- package/dist/api/accounts-api.d.cts +1 -0
- package/dist/api/accounts-api.d.cts.map +1 -1
- package/dist/api/accounts-api.d.mts +1 -0
- package/dist/api/accounts-api.d.mts.map +1 -1
- package/dist/api/accounts-api.mjs +3 -2
- package/dist/api/accounts-api.mjs.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.cjs +2 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.cjs.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.cts.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.mts.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.mjs +2 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.mjs.map +1 -1
- package/dist/helpers/GasFeePoller.cjs +76 -48
- package/dist/helpers/GasFeePoller.cjs.map +1 -1
- package/dist/helpers/GasFeePoller.d.cts +15 -3
- package/dist/helpers/GasFeePoller.d.cts.map +1 -1
- package/dist/helpers/GasFeePoller.d.mts +15 -3
- package/dist/helpers/GasFeePoller.d.mts.map +1 -1
- package/dist/helpers/GasFeePoller.mjs +73 -46
- package/dist/helpers/GasFeePoller.mjs.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.cjs +22 -4
- package/dist/helpers/IncomingTransactionHelper.cjs.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.cts +5 -2
- package/dist/helpers/IncomingTransactionHelper.d.cts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.mts +5 -2
- package/dist/helpers/IncomingTransactionHelper.d.mts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.mjs +22 -4
- package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -1
- package/dist/helpers/PendingTransactionTracker.cjs +34 -3
- package/dist/helpers/PendingTransactionTracker.cjs.map +1 -1
- package/dist/helpers/PendingTransactionTracker.d.cts +12 -0
- package/dist/helpers/PendingTransactionTracker.d.cts.map +1 -1
- package/dist/helpers/PendingTransactionTracker.d.mts +12 -0
- package/dist/helpers/PendingTransactionTracker.d.mts.map +1 -1
- package/dist/helpers/PendingTransactionTracker.mjs +34 -3
- package/dist/helpers/PendingTransactionTracker.mjs.map +1 -1
- package/dist/hooks/SequentialPublishBatchHook.cjs +114 -0
- package/dist/hooks/SequentialPublishBatchHook.cjs.map +1 -0
- package/dist/hooks/SequentialPublishBatchHook.d.cts +24 -0
- package/dist/hooks/SequentialPublishBatchHook.d.cts.map +1 -0
- package/dist/hooks/SequentialPublishBatchHook.d.mts +24 -0
- package/dist/hooks/SequentialPublishBatchHook.d.mts.map +1 -0
- package/dist/hooks/SequentialPublishBatchHook.mjs +110 -0
- package/dist/hooks/SequentialPublishBatchHook.mjs.map +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +4 -0
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +4 -0
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/batch.cjs +12 -6
- package/dist/utils/batch.cjs.map +1 -1
- package/dist/utils/batch.d.cts +3 -0
- package/dist/utils/batch.d.cts.map +1 -1
- package/dist/utils/batch.d.mts +3 -0
- package/dist/utils/batch.d.mts.map +1 -1
- package/dist/utils/batch.mjs +12 -6
- package/dist/utils/batch.mjs.map +1 -1
- package/dist/utils/gas-fees.cjs +3 -7
- package/dist/utils/gas-fees.cjs.map +1 -1
- package/dist/utils/gas-fees.d.cts.map +1 -1
- package/dist/utils/gas-fees.d.mts.map +1 -1
- package/dist/utils/gas-fees.mjs +3 -7
- package/dist/utils/gas-fees.mjs.map +1 -1
- package/dist/utils/transaction-type.cjs +4 -1
- package/dist/utils/transaction-type.cjs.map +1 -1
- package/dist/utils/transaction-type.d.cts.map +1 -1
- package/dist/utils/transaction-type.d.mts.map +1 -1
- package/dist/utils/transaction-type.mjs +4 -1
- package/dist/utils/transaction-type.mjs.map +1 -1
- package/dist/utils/validation.cjs +1 -0
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.cts +2 -1
- package/dist/utils/validation.d.cts.map +1 -1
- package/dist/utils/validation.d.mts +2 -1
- package/dist/utils/validation.d.mts.map +1 -1
- package/dist/utils/validation.mjs +1 -0
- package/dist/utils/validation.mjs.map +1 -1
- package/package.json +3 -3
|
@@ -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;AAY1C,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;IAyBvB;;;;;;;;;;;;OAYG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,SAAS,EACT,aAAa,GAYd;;QAzDD,QAAG,GAAiB,IAAI,gBAAY,EAAE,CAAC;QAE9B,6DAEwB;QAExB,4CAA2B;QAE3B,6DAEiB;QAEjB,4CAA6D;Q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D,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;AA1FD,4DA0FC","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 { TransactionControllerMessenger } from '../TransactionController';\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 readonly #messenger: TransactionControllerMessenger;\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.messenger - The TransactionControllerMessenger instance.\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 messenger,\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 messenger: TransactionControllerMessenger;\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 this.#messenger = messenger;\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(\n transactionMeta,\n this.#gasFeeFlows,\n this.#messenger,\n );\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 messenger: this.#messenger,\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 messenger: this.#messenger,\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.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport function updateTransactionGasFees({\n txMeta,\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n}: {\n txMeta: TransactionMeta;\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;\n layer1GasFee?: Hex;\n}): void {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const { type: gasEstimateType } = gasFeeEstimates ?? {};\n const shouldUpdateTxParamsGasFees = isTxParamsGasFeeUpdatesEnabled(txMeta);\n\n if (shouldUpdateTxParamsGasFees && isUsingGasFeeEstimateLevel) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\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"]}
|
|
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;AAa1C,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;IAyBvB;;;;;;;;;;;;OAYG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,SAAS,EACT,aAAa,GAYd;;QAzDD,QAAG,GAAiB,IAAI,gBAAY,EAAE,CAAC;QAE9B,6DAEwB;QAExB,4CAA2B;QAE3B,6DAEiB;QAEjB,4CAA6D;QAE7D,gDAA0C;QAE1C,kDAAuC;QAEvC,0CAA2C;QAEpD,wCAAoD;QAEpD,gCAAW,KAAK,EAAC;QAoCf,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,6BAA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AAC,YAAY,CAAC,EAAE,YAAY,CAAC;SAC1E;QAED,IAAI,eAAe,KAAK,0BAAkB,CAAC,QAAQ,EAAE;YACnD,MAAM,uBAAuB,GAC3B,eAA0C,CAAC;YAC7C,QAAQ,CAAC,QAAQ,GAAG,uBAAuB,CAAC,QAAQ,CAAC;SACtD;QAED,IAAI,eAAe,KAAK,0BAAkB,CAAC,MAAM,EAAE;YACjD,MAAM,qBAAqB,GAAG,eAAwC,CAAC;YACvE,QAAQ,CAAC,QAAQ,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;SACzD;QAED,8DAA8D;QAC9D,OAAO,QAAQ,CAAC,YAAY,CAAC;QAC7B,OAAO,QAAQ,CAAC,oBAAoB,CAAC;KACtC;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 { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n TransactionParams,\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 readonly #messenger: TransactionControllerMessenger;\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.messenger - The TransactionControllerMessenger instance.\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 messenger,\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 messenger: TransactionControllerMessenger;\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 this.#messenger = messenger;\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(\n transactionMeta,\n this.#gasFeeFlows,\n this.#messenger,\n );\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 messenger: this.#messenger,\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 messenger: this.#messenger,\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 * Updates gas properties for 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.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport function updateTransactionGasProperties({\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n txMeta,\n}: {\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;\n layer1GasFee?: Hex;\n txMeta: TransactionMeta;\n}): void {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const shouldUpdateTxParamsGasFees = isTxParamsGasFeeUpdatesEnabled(txMeta);\n\n if (\n shouldUpdateTxParamsGasFees &&\n isUsingGasFeeEstimateLevel &&\n gasFeeEstimates\n ) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txMeta.txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\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\n/**\n * Updates `txParams` gas values accordingly with given `userFeeLevel` from `txMeta.gasFeeEstimates`.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.userFeeLevel - The user fee level.\n */\nexport function updateTransactionGasEstimates({\n txMeta,\n userFeeLevel,\n}: {\n txMeta: TransactionMeta;\n userFeeLevel: GasFeeEstimateLevel;\n}): void {\n const { txParams, gasFeeEstimates } = txMeta;\n\n if (!gasFeeEstimates) {\n return;\n }\n\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\n );\n}\n\n/**\n * Updates gas fee parameters based on transaction type and gas estimate type\n *\n * @param txParams - The transaction parameters to update\n * @param gasFeeEstimates - The gas fee estimates\n * @param userFeeLevel - The user fee level\n * @param isEIP1559Compatible - Whether the transaction is EIP-1559 compatible\n */\nfunction updateGasFeeParameters(\n txParams: TransactionParams,\n gasFeeEstimates: GasFeeEstimates,\n userFeeLevel: GasFeeEstimateLevel,\n isEIP1559Compatible: boolean,\n): void {\n const { type: gasEstimateType } = gasFeeEstimates;\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n txParams.maxFeePerGas = gasPrice;\n txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.gasPrice = feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txParams.maxFeePerGas;\n delete txParams.maxPriorityFeePerGas;\n }\n}\n"]}
|
|
@@ -5,6 +5,7 @@ import type { Hex } from "@metamask/utils";
|
|
|
5
5
|
import EventEmitter from "events";
|
|
6
6
|
import type { TransactionControllerMessenger } from "../TransactionController.cjs";
|
|
7
7
|
import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta } from "../types.cjs";
|
|
8
|
+
import { GasFeeEstimateLevel } from "../types.cjs";
|
|
8
9
|
/**
|
|
9
10
|
* Automatically polls and updates suggested gas fees on unapproved transactions.
|
|
10
11
|
*/
|
|
@@ -36,7 +37,7 @@ export declare class GasFeePoller {
|
|
|
36
37
|
});
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
|
-
*
|
|
40
|
+
* Updates gas properties for transaction.
|
|
40
41
|
*
|
|
41
42
|
* @param args - Argument bag.
|
|
42
43
|
* @param args.txMeta - The transaction meta.
|
|
@@ -45,11 +46,22 @@ export declare class GasFeePoller {
|
|
|
45
46
|
* @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
|
|
46
47
|
* @param args.layer1GasFee - The layer 1 gas fee.
|
|
47
48
|
*/
|
|
48
|
-
export declare function
|
|
49
|
-
txMeta: TransactionMeta;
|
|
49
|
+
export declare function updateTransactionGasProperties({ gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, txMeta, }: {
|
|
50
50
|
gasFeeEstimates?: GasFeeEstimates;
|
|
51
51
|
gasFeeEstimatesLoaded?: boolean;
|
|
52
52
|
isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;
|
|
53
53
|
layer1GasFee?: Hex;
|
|
54
|
+
txMeta: TransactionMeta;
|
|
55
|
+
}): void;
|
|
56
|
+
/**
|
|
57
|
+
* Updates `txParams` gas values accordingly with given `userFeeLevel` from `txMeta.gasFeeEstimates`.
|
|
58
|
+
*
|
|
59
|
+
* @param args - Argument bag.
|
|
60
|
+
* @param args.txMeta - The transaction meta.
|
|
61
|
+
* @param args.userFeeLevel - The user fee level.
|
|
62
|
+
*/
|
|
63
|
+
export declare function updateTransactionGasEstimates({ txMeta, userFeeLevel, }: {
|
|
64
|
+
txMeta: TransactionMeta;
|
|
65
|
+
userFeeLevel: GasFeeEstimateLevel;
|
|
54
66
|
}): void;
|
|
55
67
|
//# 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,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EACV,eAAe,EACf,UAAU,EAIV,gBAAgB,EAEhB,eAAe,
|
|
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,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EACV,eAAe,EACf,UAAU,EAIV,gBAAgB,EAEhB,eAAe,EAEhB,qBAAiB;AAClB,OAAO,EACL,mBAAmB,EAIpB,qBAAiB;AAQlB;;GAEG;AACH,qBAAa,YAAY;;IACvB,GAAG,EAAE,YAAY,CAAsB;IAwBvC;;;;;;;;;;;;OAYG;gBACS,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,SAAS,EACT,aAAa,GACd,EAAE;QACD,4BAA4B,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,eAAe,GAAG,SAAS,CAAC;QAC5E,WAAW,EAAE,UAAU,EAAE,CAAC;QAC1B,4BAA4B,EAAE,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,SAAS,EAAE,8BAA8B,CAAC;QAC1C,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;KAC/C;CA6NF;AAED;;;;;;;;;GASG;AACH,wBAAgB,8BAA8B,CAAC,EAC7C,eAAe,EACf,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,EACZ,MAAM,GACP,EAAE;IACD,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8BAA8B,EAAE,CAAC,eAAe,EAAE,eAAe,KAAK,OAAO,CAAC;IAC9E,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,MAAM,EAAE,eAAe,CAAC;CACzB,GAAG,IAAI,CAiCP;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,EAC5C,MAAM,EACN,YAAY,GACb,EAAE;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,YAAY,EAAE,mBAAmB,CAAC;CACnC,GAAG,IAAI,CAgBP"}
|
|
@@ -5,6 +5,7 @@ import type { Hex } from "@metamask/utils";
|
|
|
5
5
|
import EventEmitter from "events";
|
|
6
6
|
import type { TransactionControllerMessenger } from "../TransactionController.mjs";
|
|
7
7
|
import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta } from "../types.mjs";
|
|
8
|
+
import { GasFeeEstimateLevel } from "../types.mjs";
|
|
8
9
|
/**
|
|
9
10
|
* Automatically polls and updates suggested gas fees on unapproved transactions.
|
|
10
11
|
*/
|
|
@@ -36,7 +37,7 @@ export declare class GasFeePoller {
|
|
|
36
37
|
});
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
|
-
*
|
|
40
|
+
* Updates gas properties for transaction.
|
|
40
41
|
*
|
|
41
42
|
* @param args - Argument bag.
|
|
42
43
|
* @param args.txMeta - The transaction meta.
|
|
@@ -45,11 +46,22 @@ export declare class GasFeePoller {
|
|
|
45
46
|
* @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
|
|
46
47
|
* @param args.layer1GasFee - The layer 1 gas fee.
|
|
47
48
|
*/
|
|
48
|
-
export declare function
|
|
49
|
-
txMeta: TransactionMeta;
|
|
49
|
+
export declare function updateTransactionGasProperties({ gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, txMeta, }: {
|
|
50
50
|
gasFeeEstimates?: GasFeeEstimates;
|
|
51
51
|
gasFeeEstimatesLoaded?: boolean;
|
|
52
52
|
isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;
|
|
53
53
|
layer1GasFee?: Hex;
|
|
54
|
+
txMeta: TransactionMeta;
|
|
55
|
+
}): void;
|
|
56
|
+
/**
|
|
57
|
+
* Updates `txParams` gas values accordingly with given `userFeeLevel` from `txMeta.gasFeeEstimates`.
|
|
58
|
+
*
|
|
59
|
+
* @param args - Argument bag.
|
|
60
|
+
* @param args.txMeta - The transaction meta.
|
|
61
|
+
* @param args.userFeeLevel - The user fee level.
|
|
62
|
+
*/
|
|
63
|
+
export declare function updateTransactionGasEstimates({ txMeta, userFeeLevel, }: {
|
|
64
|
+
txMeta: TransactionMeta;
|
|
65
|
+
userFeeLevel: GasFeeEstimateLevel;
|
|
54
66
|
}): void;
|
|
55
67
|
//# 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,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EACV,eAAe,EACf,UAAU,EAIV,gBAAgB,EAEhB,eAAe,
|
|
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,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EACV,eAAe,EACf,UAAU,EAIV,gBAAgB,EAEhB,eAAe,EAEhB,qBAAiB;AAClB,OAAO,EACL,mBAAmB,EAIpB,qBAAiB;AAQlB;;GAEG;AACH,qBAAa,YAAY;;IACvB,GAAG,EAAE,YAAY,CAAsB;IAwBvC;;;;;;;;;;;;OAYG;gBACS,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,SAAS,EACT,aAAa,GACd,EAAE;QACD,4BAA4B,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,eAAe,GAAG,SAAS,CAAC;QAC5E,WAAW,EAAE,UAAU,EAAE,CAAC;QAC1B,4BAA4B,EAAE,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,SAAS,EAAE,8BAA8B,CAAC;QAC1C,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;KAC/C;CA6NF;AAED;;;;;;;;;GASG;AACH,wBAAgB,8BAA8B,CAAC,EAC7C,eAAe,EACf,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,EACZ,MAAM,GACP,EAAE;IACD,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8BAA8B,EAAE,CAAC,eAAe,EAAE,eAAe,KAAK,OAAO,CAAC;IAC9E,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,MAAM,EAAE,eAAe,CAAC;CACzB,GAAG,IAAI,CAiCP;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,EAC5C,MAAM,EACN,YAAY,GACb,EAAE;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,YAAY,EAAE,mBAAmB,CAAC;CACnC,GAAG,IAAI,CAgBP"}
|
|
@@ -191,7 +191,7 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
191
191
|
return new Map(await Promise.all(entryPromises));
|
|
192
192
|
};
|
|
193
193
|
/**
|
|
194
|
-
*
|
|
194
|
+
* Updates gas properties for transaction.
|
|
195
195
|
*
|
|
196
196
|
* @param args - Argument bag.
|
|
197
197
|
* @param args.txMeta - The transaction meta.
|
|
@@ -200,55 +200,15 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
200
200
|
* @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
|
|
201
201
|
* @param args.layer1GasFee - The layer 1 gas fee.
|
|
202
202
|
*/
|
|
203
|
-
export function
|
|
203
|
+
export function updateTransactionGasProperties({ gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, txMeta, }) {
|
|
204
204
|
const userFeeLevel = txMeta.userFeeLevel;
|
|
205
205
|
const isUsingGasFeeEstimateLevel = Object.values(GasFeeEstimateLevel).includes(userFeeLevel);
|
|
206
|
-
const { type: gasEstimateType } = gasFeeEstimates ?? {};
|
|
207
206
|
const shouldUpdateTxParamsGasFees = isTxParamsGasFeeUpdatesEnabled(txMeta);
|
|
208
|
-
if (shouldUpdateTxParamsGasFees &&
|
|
207
|
+
if (shouldUpdateTxParamsGasFees &&
|
|
208
|
+
isUsingGasFeeEstimateLevel &&
|
|
209
|
+
gasFeeEstimates) {
|
|
209
210
|
const isEIP1559Compatible = txMeta.txParams.type !== TransactionEnvelopeType.legacy;
|
|
210
|
-
|
|
211
|
-
// Handle EIP-1559 compatible transactions
|
|
212
|
-
if (gasEstimateType === GasFeeEstimateType.FeeMarket) {
|
|
213
|
-
const feeMarketGasFeeEstimates = gasFeeEstimates;
|
|
214
|
-
txMeta.txParams.maxFeePerGas =
|
|
215
|
-
feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;
|
|
216
|
-
txMeta.txParams.maxPriorityFeePerGas =
|
|
217
|
-
feeMarketGasFeeEstimates[userFeeLevel].maxPriorityFeePerGas;
|
|
218
|
-
}
|
|
219
|
-
if (gasEstimateType === GasFeeEstimateType.GasPrice) {
|
|
220
|
-
const gasPriceGasFeeEstimates = gasFeeEstimates;
|
|
221
|
-
txMeta.txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;
|
|
222
|
-
txMeta.txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;
|
|
223
|
-
}
|
|
224
|
-
if (gasEstimateType === GasFeeEstimateType.Legacy) {
|
|
225
|
-
const legacyGasFeeEstimates = gasFeeEstimates;
|
|
226
|
-
const gasPrice = legacyGasFeeEstimates[userFeeLevel];
|
|
227
|
-
txMeta.txParams.maxFeePerGas = gasPrice;
|
|
228
|
-
txMeta.txParams.maxPriorityFeePerGas = gasPrice;
|
|
229
|
-
}
|
|
230
|
-
// Remove gasPrice for EIP-1559 transactions
|
|
231
|
-
delete txMeta.txParams.gasPrice;
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
// Handle non-EIP-1559 transactions
|
|
235
|
-
if (gasEstimateType === GasFeeEstimateType.FeeMarket) {
|
|
236
|
-
const feeMarketGasFeeEstimates = gasFeeEstimates;
|
|
237
|
-
txMeta.txParams.gasPrice =
|
|
238
|
-
feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;
|
|
239
|
-
}
|
|
240
|
-
if (gasEstimateType === GasFeeEstimateType.GasPrice) {
|
|
241
|
-
const gasPriceGasFeeEstimates = gasFeeEstimates;
|
|
242
|
-
txMeta.txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;
|
|
243
|
-
}
|
|
244
|
-
if (gasEstimateType === GasFeeEstimateType.Legacy) {
|
|
245
|
-
const legacyGasFeeEstimates = gasFeeEstimates;
|
|
246
|
-
txMeta.txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];
|
|
247
|
-
}
|
|
248
|
-
// Remove EIP-1559 specific parameters for legacy transactions
|
|
249
|
-
delete txMeta.txParams.maxFeePerGas;
|
|
250
|
-
delete txMeta.txParams.maxPriorityFeePerGas;
|
|
251
|
-
}
|
|
211
|
+
updateGasFeeParameters(txMeta.txParams, gasFeeEstimates, userFeeLevel, isEIP1559Compatible);
|
|
252
212
|
}
|
|
253
213
|
if (gasFeeEstimates) {
|
|
254
214
|
txMeta.gasFeeEstimates = gasFeeEstimates;
|
|
@@ -260,4 +220,71 @@ export function updateTransactionGasFees({ txMeta, gasFeeEstimates, gasFeeEstima
|
|
|
260
220
|
txMeta.layer1GasFee = layer1GasFee;
|
|
261
221
|
}
|
|
262
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Updates `txParams` gas values accordingly with given `userFeeLevel` from `txMeta.gasFeeEstimates`.
|
|
225
|
+
*
|
|
226
|
+
* @param args - Argument bag.
|
|
227
|
+
* @param args.txMeta - The transaction meta.
|
|
228
|
+
* @param args.userFeeLevel - The user fee level.
|
|
229
|
+
*/
|
|
230
|
+
export function updateTransactionGasEstimates({ txMeta, userFeeLevel, }) {
|
|
231
|
+
const { txParams, gasFeeEstimates } = txMeta;
|
|
232
|
+
if (!gasFeeEstimates) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const isEIP1559Compatible = txMeta.txParams.type !== TransactionEnvelopeType.legacy;
|
|
236
|
+
updateGasFeeParameters(txParams, gasFeeEstimates, userFeeLevel, isEIP1559Compatible);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Updates gas fee parameters based on transaction type and gas estimate type
|
|
240
|
+
*
|
|
241
|
+
* @param txParams - The transaction parameters to update
|
|
242
|
+
* @param gasFeeEstimates - The gas fee estimates
|
|
243
|
+
* @param userFeeLevel - The user fee level
|
|
244
|
+
* @param isEIP1559Compatible - Whether the transaction is EIP-1559 compatible
|
|
245
|
+
*/
|
|
246
|
+
function updateGasFeeParameters(txParams, gasFeeEstimates, userFeeLevel, isEIP1559Compatible) {
|
|
247
|
+
const { type: gasEstimateType } = gasFeeEstimates;
|
|
248
|
+
if (isEIP1559Compatible) {
|
|
249
|
+
// Handle EIP-1559 compatible transactions
|
|
250
|
+
if (gasEstimateType === GasFeeEstimateType.FeeMarket) {
|
|
251
|
+
const feeMarketGasFeeEstimates = gasFeeEstimates;
|
|
252
|
+
txParams.maxFeePerGas =
|
|
253
|
+
feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;
|
|
254
|
+
txParams.maxPriorityFeePerGas =
|
|
255
|
+
feeMarketGasFeeEstimates[userFeeLevel]?.maxPriorityFeePerGas;
|
|
256
|
+
}
|
|
257
|
+
if (gasEstimateType === GasFeeEstimateType.GasPrice) {
|
|
258
|
+
const gasPriceGasFeeEstimates = gasFeeEstimates;
|
|
259
|
+
txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;
|
|
260
|
+
txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;
|
|
261
|
+
}
|
|
262
|
+
if (gasEstimateType === GasFeeEstimateType.Legacy) {
|
|
263
|
+
const legacyGasFeeEstimates = gasFeeEstimates;
|
|
264
|
+
const gasPrice = legacyGasFeeEstimates[userFeeLevel];
|
|
265
|
+
txParams.maxFeePerGas = gasPrice;
|
|
266
|
+
txParams.maxPriorityFeePerGas = gasPrice;
|
|
267
|
+
}
|
|
268
|
+
// Remove gasPrice for EIP-1559 transactions
|
|
269
|
+
delete txParams.gasPrice;
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
// Handle non-EIP-1559 transactions
|
|
273
|
+
if (gasEstimateType === GasFeeEstimateType.FeeMarket) {
|
|
274
|
+
const feeMarketGasFeeEstimates = gasFeeEstimates;
|
|
275
|
+
txParams.gasPrice = feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;
|
|
276
|
+
}
|
|
277
|
+
if (gasEstimateType === GasFeeEstimateType.GasPrice) {
|
|
278
|
+
const gasPriceGasFeeEstimates = gasFeeEstimates;
|
|
279
|
+
txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;
|
|
280
|
+
}
|
|
281
|
+
if (gasEstimateType === GasFeeEstimateType.Legacy) {
|
|
282
|
+
const legacyGasFeeEstimates = gasFeeEstimates;
|
|
283
|
+
txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];
|
|
284
|
+
}
|
|
285
|
+
// Remove EIP-1559 specific parameters for legacy transactions
|
|
286
|
+
delete txParams.maxFeePerGas;
|
|
287
|
+
delete txParams.maxPriorityFeePerGas;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
263
290
|
//# sourceMappingURL=GasFeePoller.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GasFeePoller.mjs","sourceRoot":"","sources":["../../src/helpers/GasF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,CAAC,YAAY,GAAG,QAAQ,CAAC;gBACxC,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,kBAAkB,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,kBAAkB,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,kBAAkB,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","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 { TransactionControllerMessenger } from '../TransactionController';\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 readonly #messenger: TransactionControllerMessenger;\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.messenger - The TransactionControllerMessenger instance.\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 messenger,\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 messenger: TransactionControllerMessenger;\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 this.#messenger = messenger;\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(\n transactionMeta,\n this.#gasFeeFlows,\n this.#messenger,\n );\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 messenger: this.#messenger,\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 messenger: this.#messenger,\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.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport function updateTransactionGasFees({\n txMeta,\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n}: {\n txMeta: TransactionMeta;\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;\n layer1GasFee?: Hex;\n}): void {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const { type: gasEstimateType } = gasFeeEstimates ?? {};\n const shouldUpdateTxParamsGasFees = isTxParamsGasFeeUpdatesEnabled(txMeta);\n\n if (shouldUpdateTxParamsGasFees && isUsingGasFeeEstimateLevel) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\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"]}
|
|
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,OAA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QAED,8DAA8D;QAC9D,OAAO,QAAQ,CAAC,YAAY,CAAC;QAC7B,OAAO,QAAQ,CAAC,oBAAoB,CAAC;KACtC;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 { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n TransactionParams,\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 readonly #messenger: TransactionControllerMessenger;\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.messenger - The TransactionControllerMessenger instance.\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 messenger,\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 messenger: TransactionControllerMessenger;\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 this.#messenger = messenger;\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(\n transactionMeta,\n this.#gasFeeFlows,\n this.#messenger,\n );\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 messenger: this.#messenger,\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 messenger: this.#messenger,\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 * Updates gas properties for 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.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport function updateTransactionGasProperties({\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n txMeta,\n}: {\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;\n layer1GasFee?: Hex;\n txMeta: TransactionMeta;\n}): void {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const shouldUpdateTxParamsGasFees = isTxParamsGasFeeUpdatesEnabled(txMeta);\n\n if (\n shouldUpdateTxParamsGasFees &&\n isUsingGasFeeEstimateLevel &&\n gasFeeEstimates\n ) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txMeta.txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\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\n/**\n * Updates `txParams` gas values accordingly with given `userFeeLevel` from `txMeta.gasFeeEstimates`.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.userFeeLevel - The user fee level.\n */\nexport function updateTransactionGasEstimates({\n txMeta,\n userFeeLevel,\n}: {\n txMeta: TransactionMeta;\n userFeeLevel: GasFeeEstimateLevel;\n}): void {\n const { txParams, gasFeeEstimates } = txMeta;\n\n if (!gasFeeEstimates) {\n return;\n }\n\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\n );\n}\n\n/**\n * Updates gas fee parameters based on transaction type and gas estimate type\n *\n * @param txParams - The transaction parameters to update\n * @param gasFeeEstimates - The gas fee estimates\n * @param userFeeLevel - The user fee level\n * @param isEIP1559Compatible - Whether the transaction is EIP-1559 compatible\n */\nfunction updateGasFeeParameters(\n txParams: TransactionParams,\n gasFeeEstimates: GasFeeEstimates,\n userFeeLevel: GasFeeEstimateLevel,\n isEIP1559Compatible: boolean,\n): void {\n const { type: gasEstimateType } = gasFeeEstimates;\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n txParams.maxFeePerGas = gasPrice;\n txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.gasPrice = feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txParams.maxFeePerGas;\n delete txParams.maxPriorityFeePerGas;\n }\n}\n"]}
|
|
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
|
-
var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_getCache, _IncomingTransactionHelper_getCurrentAccount, _IncomingTransactionHelper_getLocalTransactions, _IncomingTransactionHelper_includeTokenTransfers, _IncomingTransactionHelper_isEnabled, _IncomingTransactionHelper_isRunning, _IncomingTransactionHelper_messenger, _IncomingTransactionHelper_queryEntireHistory, _IncomingTransactionHelper_remoteTransactionSource, _IncomingTransactionHelper_timeoutId, _IncomingTransactionHelper_trimTransactions, _IncomingTransactionHelper_updateCache, _IncomingTransactionHelper_updateTransactions, _IncomingTransactionHelper_onInterval, _IncomingTransactionHelper_sortTransactionsByTime, _IncomingTransactionHelper_canStart, _IncomingTransactionHelper_getInterval;
|
|
16
|
+
var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_client, _IncomingTransactionHelper_getCache, _IncomingTransactionHelper_getCurrentAccount, _IncomingTransactionHelper_getLocalTransactions, _IncomingTransactionHelper_includeTokenTransfers, _IncomingTransactionHelper_isEnabled, _IncomingTransactionHelper_isRunning, _IncomingTransactionHelper_messenger, _IncomingTransactionHelper_queryEntireHistory, _IncomingTransactionHelper_remoteTransactionSource, _IncomingTransactionHelper_timeoutId, _IncomingTransactionHelper_trimTransactions, _IncomingTransactionHelper_updateCache, _IncomingTransactionHelper_updateTransactions, _IncomingTransactionHelper_onInterval, _IncomingTransactionHelper_sortTransactionsByTime, _IncomingTransactionHelper_canStart, _IncomingTransactionHelper_getInterval, _IncomingTransactionHelper_getTags;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.IncomingTransactionHelper = void 0;
|
|
19
19
|
// This package purposefully relies on Node's EventEmitter module.
|
|
@@ -21,9 +21,11 @@ exports.IncomingTransactionHelper = void 0;
|
|
|
21
21
|
const events_1 = __importDefault(require("events"));
|
|
22
22
|
const logger_1 = require("../logger.cjs");
|
|
23
23
|
const feature_flags_1 = require("../utils/feature-flags.cjs");
|
|
24
|
+
const TAG_POLLING = 'automatic-polling';
|
|
24
25
|
class IncomingTransactionHelper {
|
|
25
|
-
constructor({ getCache, getCurrentAccount, getLocalTransactions, includeTokenTransfers, isEnabled, messenger, queryEntireHistory, remoteTransactionSource, trimTransactions, updateCache, updateTransactions, }) {
|
|
26
|
+
constructor({ client, getCache, getCurrentAccount, getLocalTransactions, includeTokenTransfers, isEnabled, messenger, queryEntireHistory, remoteTransactionSource, trimTransactions, updateCache, updateTransactions, }) {
|
|
26
27
|
_IncomingTransactionHelper_instances.add(this);
|
|
28
|
+
_IncomingTransactionHelper_client.set(this, void 0);
|
|
27
29
|
_IncomingTransactionHelper_getCache.set(this, void 0);
|
|
28
30
|
_IncomingTransactionHelper_getCurrentAccount.set(this, void 0);
|
|
29
31
|
_IncomingTransactionHelper_getLocalTransactions.set(this, void 0);
|
|
@@ -38,6 +40,7 @@ class IncomingTransactionHelper {
|
|
|
38
40
|
_IncomingTransactionHelper_updateCache.set(this, void 0);
|
|
39
41
|
_IncomingTransactionHelper_updateTransactions.set(this, void 0);
|
|
40
42
|
this.hub = new events_1.default();
|
|
43
|
+
__classPrivateFieldSet(this, _IncomingTransactionHelper_client, client, "f");
|
|
41
44
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_getCache, getCache, "f");
|
|
42
45
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_getCurrentAccount, getCurrentAccount, "f");
|
|
43
46
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_getLocalTransactions, getLocalTransactions, "f");
|
|
@@ -75,9 +78,11 @@ class IncomingTransactionHelper {
|
|
|
75
78
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_isRunning, false, "f");
|
|
76
79
|
(0, logger_1.incomingTransactionsLogger)('Stopped polling');
|
|
77
80
|
}
|
|
78
|
-
async update({ isInterval } = {}) {
|
|
81
|
+
async update({ isInterval, tags, } = {}) {
|
|
82
|
+
const finalTags = __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_getTags).call(this, tags, isInterval);
|
|
79
83
|
(0, logger_1.incomingTransactionsLogger)('Checking for incoming transactions', {
|
|
80
84
|
isInterval: Boolean(isInterval),
|
|
85
|
+
tags: finalTags,
|
|
81
86
|
});
|
|
82
87
|
if (!__classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_canStart).call(this)) {
|
|
83
88
|
return;
|
|
@@ -95,6 +100,7 @@ class IncomingTransactionHelper {
|
|
|
95
100
|
cache,
|
|
96
101
|
includeTokenTransfers,
|
|
97
102
|
queryEntireHistory,
|
|
103
|
+
tags: finalTags,
|
|
98
104
|
updateCache: __classPrivateFieldGet(this, _IncomingTransactionHelper_updateCache, "f"),
|
|
99
105
|
updateTransactions,
|
|
100
106
|
});
|
|
@@ -133,7 +139,7 @@ class IncomingTransactionHelper {
|
|
|
133
139
|
}
|
|
134
140
|
}
|
|
135
141
|
exports.IncomingTransactionHelper = IncomingTransactionHelper;
|
|
136
|
-
_IncomingTransactionHelper_getCache = new WeakMap(), _IncomingTransactionHelper_getCurrentAccount = new WeakMap(), _IncomingTransactionHelper_getLocalTransactions = new WeakMap(), _IncomingTransactionHelper_includeTokenTransfers = new WeakMap(), _IncomingTransactionHelper_isEnabled = new WeakMap(), _IncomingTransactionHelper_isRunning = new WeakMap(), _IncomingTransactionHelper_messenger = new WeakMap(), _IncomingTransactionHelper_queryEntireHistory = new WeakMap(), _IncomingTransactionHelper_remoteTransactionSource = new WeakMap(), _IncomingTransactionHelper_timeoutId = new WeakMap(), _IncomingTransactionHelper_trimTransactions = new WeakMap(), _IncomingTransactionHelper_updateCache = new WeakMap(), _IncomingTransactionHelper_updateTransactions = new WeakMap(), _IncomingTransactionHelper_instances = new WeakSet(), _IncomingTransactionHelper_onInterval = async function _IncomingTransactionHelper_onInterval() {
|
|
142
|
+
_IncomingTransactionHelper_client = new WeakMap(), _IncomingTransactionHelper_getCache = new WeakMap(), _IncomingTransactionHelper_getCurrentAccount = new WeakMap(), _IncomingTransactionHelper_getLocalTransactions = new WeakMap(), _IncomingTransactionHelper_includeTokenTransfers = new WeakMap(), _IncomingTransactionHelper_isEnabled = new WeakMap(), _IncomingTransactionHelper_isRunning = new WeakMap(), _IncomingTransactionHelper_messenger = new WeakMap(), _IncomingTransactionHelper_queryEntireHistory = new WeakMap(), _IncomingTransactionHelper_remoteTransactionSource = new WeakMap(), _IncomingTransactionHelper_timeoutId = new WeakMap(), _IncomingTransactionHelper_trimTransactions = new WeakMap(), _IncomingTransactionHelper_updateCache = new WeakMap(), _IncomingTransactionHelper_updateTransactions = new WeakMap(), _IncomingTransactionHelper_instances = new WeakSet(), _IncomingTransactionHelper_onInterval = async function _IncomingTransactionHelper_onInterval() {
|
|
137
143
|
try {
|
|
138
144
|
await this.update({ isInterval: true });
|
|
139
145
|
}
|
|
@@ -151,5 +157,17 @@ _IncomingTransactionHelper_getCache = new WeakMap(), _IncomingTransactionHelper_
|
|
|
151
157
|
return __classPrivateFieldGet(this, _IncomingTransactionHelper_isEnabled, "f").call(this);
|
|
152
158
|
}, _IncomingTransactionHelper_getInterval = function _IncomingTransactionHelper_getInterval() {
|
|
153
159
|
return (0, feature_flags_1.getIncomingTransactionsPollingInterval)(__classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f"));
|
|
160
|
+
}, _IncomingTransactionHelper_getTags = function _IncomingTransactionHelper_getTags(requestTags, isInterval) {
|
|
161
|
+
const tags = [];
|
|
162
|
+
if (__classPrivateFieldGet(this, _IncomingTransactionHelper_client, "f")) {
|
|
163
|
+
tags.push(__classPrivateFieldGet(this, _IncomingTransactionHelper_client, "f"));
|
|
164
|
+
}
|
|
165
|
+
if (requestTags?.length) {
|
|
166
|
+
tags.push(...requestTags);
|
|
167
|
+
}
|
|
168
|
+
else if (isInterval) {
|
|
169
|
+
tags.push(TAG_POLLING);
|
|
170
|
+
}
|
|
171
|
+
return tags?.length ? tags : undefined;
|
|
154
172
|
};
|
|
155
173
|
//# sourceMappingURL=IncomingTransactionHelper.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAGlC,0CAA8D;AAE9D,8DAAgF;AAShF,MAAa,yBAAyB;IAiCpC,YAAY,EACV,QAAQ,EACR,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAA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CAAC;IAGC,OAAO,uBAAA,IAAI,4CAAW,MAAf,IAAI,CAAa,CAAC;AAC3B,CAAC;IAGC,OAAO,IAAA,sDAAsC,EAAC,uBAAA,IAAI,4CAAW,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type { Hex } 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 type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport { getIncomingTransactionsPollingInterval } from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n queryEntireHistory?: boolean;\n updateTransactions?: boolean;\n};\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #getCache: () => Record<string, unknown>;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #queryEntireHistory?: boolean;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateCache: (fn: (cache: Record<string, unknown>) => void) => void;\n\n readonly #updateTransactions?: boolean;\n\n constructor({\n getCache,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n queryEntireHistory,\n remoteTransactionSource,\n trimTransactions,\n updateCache,\n updateTransactions,\n }: {\n getCache: () => Record<string, unknown>;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n queryEntireHistory?: boolean;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateCache: (fn: (cache: Record<string, unknown>) => void) => void;\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#getCache = getCache;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? (() => true);\n this.#isRunning = false;\n this.#messenger = messenger;\n this.#queryEntireHistory = queryEntireHistory;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateCache = updateCache;\n this.#updateTransactions = updateTransactions;\n }\n\n start() {\n if (this.#isRunning) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Starting polling', { interval });\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeoutId = setTimeout(() => this.#onInterval(), interval);\n this.#isRunning = true;\n\n log('Started polling');\n }\n\n stop() {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n async #onInterval() {\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n if (this.#isRunning) {\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({ isInterval }: { isInterval?: boolean } = {}): Promise<void> {\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const cache = this.#getCache();\n const includeTokenTransfers = this.#includeTokenTransfers ?? true;\n const queryEntireHistory = this.#queryEntireHistory ?? true;\n const updateTransactions = this.#updateTransactions ?? false;\n\n let remoteTransactions: TransactionMeta[] = [];\n\n try {\n remoteTransactions =\n await this.#remoteTransactionSource.fetchTransactions({\n address: account.address as Hex,\n cache,\n includeTokenTransfers,\n queryEntireHistory,\n updateCache: this.#updateCache,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]) {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;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AC,CAAC;KACpE;IAED,IAAI,uBAAA,IAAI,4CAAW,EAAE;QACnB,uBAAA,IAAI,wCAAc,UAAU;QAC1B,kEAAkE;QAClE,GAAG,EAAE,CAAC,uBAAA,IAAI,mFAAY,MAAhB,IAAI,CAAc,EACxB,uBAAA,IAAI,oFAAa,MAAjB,IAAI,CAAe,CACpB,MAAA,CAAC;KACH;AACH,CAAC,iHAkGuB,YAA+B;IACrD,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1D,CAAC;IAGC,OAAO,uBAAA,IAAI,4CAAW,MAAf,IAAI,CAAa,CAAC;AAC3B,CAAC;IAGC,OAAO,IAAA,sDAAsC,EAAC,uBAAA,IAAI,4CAAW,CAAC,CAAC;AACjE,CAAC,mFAGC,WAAiC,EACjC,UAA+B;IAE/B,MAAM,IAAI,GAAG,EAAE,CAAC;IAEhB,IAAI,uBAAA,IAAI,yCAAQ,EAAE;QAChB,IAAI,CAAC,IAAI,CAAC,uBAAA,IAAI,yCAAQ,CAAC,CAAC;KACzB;IAED,IAAI,WAAW,EAAE,MAAM,EAAE;QACvB,IAAI,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;KAC3B;SAAM,IAAI,UAAU,EAAE;QACrB,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;KACxB;IAED,OAAO,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACzC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type { Hex } 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 type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport { getIncomingTransactionsPollingInterval } from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n client?: string;\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n queryEntireHistory?: boolean;\n updateTransactions?: boolean;\n};\n\nconst TAG_POLLING = 'automatic-polling';\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #client?: string;\n\n readonly #getCache: () => Record<string, unknown>;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #queryEntireHistory?: boolean;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateCache: (fn: (cache: Record<string, unknown>) => void) => void;\n\n readonly #updateTransactions?: boolean;\n\n constructor({\n client,\n getCache,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n queryEntireHistory,\n remoteTransactionSource,\n trimTransactions,\n updateCache,\n updateTransactions,\n }: {\n client?: string;\n getCache: () => Record<string, unknown>;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n queryEntireHistory?: boolean;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateCache: (fn: (cache: Record<string, unknown>) => void) => void;\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#client = client;\n this.#getCache = getCache;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? (() => true);\n this.#isRunning = false;\n this.#messenger = messenger;\n this.#queryEntireHistory = queryEntireHistory;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateCache = updateCache;\n this.#updateTransactions = updateTransactions;\n }\n\n start() {\n if (this.#isRunning) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Starting polling', { interval });\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeoutId = setTimeout(() => this.#onInterval(), interval);\n this.#isRunning = true;\n\n log('Started polling');\n }\n\n stop() {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n async #onInterval() {\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n if (this.#isRunning) {\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({\n isInterval,\n tags,\n }: { isInterval?: boolean; tags?: string[] } = {}): Promise<void> {\n const finalTags = this.#getTags(tags, isInterval);\n\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n tags: finalTags,\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const cache = this.#getCache();\n const includeTokenTransfers = this.#includeTokenTransfers ?? true;\n const queryEntireHistory = this.#queryEntireHistory ?? true;\n const updateTransactions = this.#updateTransactions ?? false;\n\n let remoteTransactions: TransactionMeta[] = [];\n\n try {\n remoteTransactions =\n await this.#remoteTransactionSource.fetchTransactions({\n address: account.address as Hex,\n cache,\n includeTokenTransfers,\n queryEntireHistory,\n tags: finalTags,\n updateCache: this.#updateCache,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]) {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n\n #getTags(\n requestTags: string[] | undefined,\n isInterval: boolean | undefined,\n ): string[] | undefined {\n const tags = [];\n\n if (this.#client) {\n tags.push(this.#client);\n }\n\n if (requestTags?.length) {\n tags.push(...requestTags);\n } else if (isInterval) {\n tags.push(TAG_POLLING);\n }\n\n return tags?.length ? tags : undefined;\n }\n}\n"]}
|