@metamask/transaction-controller 52.2.0 → 52.3.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 +17 -1
- package/dist/TransactionController.cjs +22 -4
- package/dist/TransactionController.cjs.map +1 -1
- package/dist/TransactionController.d.cts +0 -2
- package/dist/TransactionController.d.cts.map +1 -1
- package/dist/TransactionController.d.mts +0 -2
- package/dist/TransactionController.d.mts.map +1 -1
- package/dist/TransactionController.mjs +25 -7
- package/dist/TransactionController.mjs.map +1 -1
- package/dist/constants.cjs +1 -0
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +1 -0
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +1 -0
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +1 -0
- package/dist/constants.mjs.map +1 -1
- package/dist/gas-flows/DefaultGasFeeFlow.cjs +1 -3
- package/dist/gas-flows/DefaultGasFeeFlow.cjs.map +1 -1
- package/dist/gas-flows/DefaultGasFeeFlow.d.cts +2 -2
- package/dist/gas-flows/DefaultGasFeeFlow.d.cts.map +1 -1
- package/dist/gas-flows/DefaultGasFeeFlow.d.mts +2 -2
- package/dist/gas-flows/DefaultGasFeeFlow.d.mts.map +1 -1
- package/dist/gas-flows/DefaultGasFeeFlow.mjs +1 -3
- package/dist/gas-flows/DefaultGasFeeFlow.mjs.map +1 -1
- package/dist/gas-flows/LineaGasFeeFlow.cjs +1 -1
- package/dist/gas-flows/LineaGasFeeFlow.cjs.map +1 -1
- package/dist/gas-flows/LineaGasFeeFlow.d.cts +5 -1
- package/dist/gas-flows/LineaGasFeeFlow.d.cts.map +1 -1
- package/dist/gas-flows/LineaGasFeeFlow.d.mts +5 -1
- package/dist/gas-flows/LineaGasFeeFlow.d.mts.map +1 -1
- package/dist/gas-flows/LineaGasFeeFlow.mjs +1 -1
- package/dist/gas-flows/LineaGasFeeFlow.mjs.map +1 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.cjs +1 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.cjs.map +1 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.cts +5 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.cts.map +1 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.mts +5 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.mts.map +1 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.mjs +1 -1
- package/dist/gas-flows/OptimismLayer1GasFeeFlow.mjs.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.cjs.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.d.cts +5 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.d.cts.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.d.mts +5 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.d.mts.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.mjs.map +1 -1
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.cjs +123 -0
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.cjs.map +1 -0
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.cts +28 -0
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.cts.map +1 -0
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.mts +28 -0
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.mts.map +1 -0
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.mjs +118 -0
- package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.mjs.map +1 -0
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.cjs +1 -1
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.cjs.map +1 -1
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.cts +5 -1
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.cts.map +1 -1
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.mts +5 -1
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.mts.map +1 -1
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.mjs +1 -1
- package/dist/gas-flows/ScrollLayer1GasFeeFlow.mjs.map +1 -1
- package/dist/gas-flows/TestGasFeeFlow.cjs +1 -1
- package/dist/gas-flows/TestGasFeeFlow.cjs.map +1 -1
- package/dist/gas-flows/TestGasFeeFlow.d.cts +2 -2
- package/dist/gas-flows/TestGasFeeFlow.d.cts.map +1 -1
- package/dist/gas-flows/TestGasFeeFlow.d.mts +2 -2
- package/dist/gas-flows/TestGasFeeFlow.d.mts.map +1 -1
- package/dist/gas-flows/TestGasFeeFlow.mjs +1 -1
- package/dist/gas-flows/TestGasFeeFlow.mjs.map +1 -1
- package/dist/helpers/GasFeePoller.cjs +11 -8
- package/dist/helpers/GasFeePoller.cjs.map +1 -1
- package/dist/helpers/GasFeePoller.d.cts +6 -5
- package/dist/helpers/GasFeePoller.d.cts.map +1 -1
- package/dist/helpers/GasFeePoller.d.mts +6 -5
- package/dist/helpers/GasFeePoller.d.mts.map +1 -1
- package/dist/helpers/GasFeePoller.mjs +11 -8
- package/dist/helpers/GasFeePoller.mjs.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.cjs +2 -1
- package/dist/helpers/IncomingTransactionHelper.cjs.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.cts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.mts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.mjs +2 -1
- package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -1
- package/dist/helpers/PendingTransactionTracker.cjs +6 -2
- package/dist/helpers/PendingTransactionTracker.cjs.map +1 -1
- package/dist/helpers/PendingTransactionTracker.d.cts +5 -2
- package/dist/helpers/PendingTransactionTracker.d.cts.map +1 -1
- package/dist/helpers/PendingTransactionTracker.d.mts +5 -2
- package/dist/helpers/PendingTransactionTracker.d.mts.map +1 -1
- package/dist/helpers/PendingTransactionTracker.mjs +6 -2
- package/dist/helpers/PendingTransactionTracker.mjs.map +1 -1
- package/dist/helpers/TransactionPoller.cjs +12 -8
- package/dist/helpers/TransactionPoller.cjs.map +1 -1
- package/dist/helpers/TransactionPoller.d.cts +7 -3
- package/dist/helpers/TransactionPoller.d.cts.map +1 -1
- package/dist/helpers/TransactionPoller.d.mts +7 -3
- package/dist/helpers/TransactionPoller.d.mts.map +1 -1
- package/dist/helpers/TransactionPoller.mjs +11 -7
- package/dist/helpers/TransactionPoller.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +18 -5
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +18 -5
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/feature-flags.cjs +39 -2
- package/dist/utils/feature-flags.cjs.map +1 -1
- package/dist/utils/feature-flags.d.cts +49 -0
- package/dist/utils/feature-flags.d.cts.map +1 -1
- package/dist/utils/feature-flags.d.mts +49 -0
- package/dist/utils/feature-flags.d.mts.map +1 -1
- package/dist/utils/feature-flags.mjs +36 -1
- package/dist/utils/feature-flags.mjs.map +1 -1
- package/dist/utils/gas-fees.cjs +21 -3
- package/dist/utils/gas-fees.cjs.map +1 -1
- package/dist/utils/gas-fees.d.cts +14 -0
- package/dist/utils/gas-fees.d.cts.map +1 -1
- package/dist/utils/gas-fees.d.mts +14 -0
- package/dist/utils/gas-fees.d.mts.map +1 -1
- package/dist/utils/gas-fees.mjs +19 -2
- package/dist/utils/gas-fees.mjs.map +1 -1
- package/dist/utils/gas-flow.cjs +3 -2
- package/dist/utils/gas-flow.cjs.map +1 -1
- package/dist/utils/gas-flow.d.cts +3 -1
- package/dist/utils/gas-flow.d.cts.map +1 -1
- package/dist/utils/gas-flow.d.mts +3 -1
- package/dist/utils/gas-flow.d.mts.map +1 -1
- package/dist/utils/gas-flow.mjs +3 -2
- package/dist/utils/gas-flow.mjs.map +1 -1
- package/dist/utils/layer1-gas-fee-flow.cjs +9 -4
- package/dist/utils/layer1-gas-fee-flow.cjs.map +1 -1
- package/dist/utils/layer1-gas-fee-flow.d.cts +4 -1
- package/dist/utils/layer1-gas-fee-flow.d.cts.map +1 -1
- package/dist/utils/layer1-gas-fee-flow.d.mts +4 -1
- package/dist/utils/layer1-gas-fee-flow.d.mts.map +1 -1
- package/dist/utils/layer1-gas-fee-flow.mjs +9 -4
- package/dist/utils/layer1-gas-fee-flow.mjs.map +1 -1
- package/dist/utils/simulation.cjs +17 -2
- package/dist/utils/simulation.cjs.map +1 -1
- package/dist/utils/simulation.d.cts +1 -0
- package/dist/utils/simulation.d.cts.map +1 -1
- package/dist/utils/simulation.d.mts +1 -0
- package/dist/utils/simulation.d.mts.map +1 -1
- package/dist/utils/simulation.mjs +17 -2
- package/dist/utils/simulation.mjs.map +1 -1
- package/dist/utils/utils.cjs +23 -1
- package/dist/utils/utils.cjs.map +1 -1
- package/dist/utils/utils.d.cts +8 -0
- package/dist/utils/utils.d.cts.map +1 -1
- package/dist/utils/utils.d.mts +8 -0
- package/dist/utils/utils.d.mts.map +1 -1
- package/dist/utils/utils.mjs +22 -1
- package/dist/utils/utils.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -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 _GasFeePoller_instances, _GasFeePoller_findNetworkClientIdByChainId, _GasFeePoller_gasFeeFlows, _GasFeePoller_getGasFeeControllerEstimates, _GasFeePoller_getProvider, _GasFeePoller_getTransactions, _GasFeePoller_layer1GasFeeFlows, _GasFeePoller_timeout, _GasFeePoller_running, _GasFeePoller_start, _GasFeePoller_stop, _GasFeePoller_onTimeout, _GasFeePoller_updateUnapprovedTransactions, _GasFeePoller_updateUnapprovedTransaction, _GasFeePoller_updateTransactionGasFeeEstimates, _GasFeePoller_updateTransactionLayer1GasFee, _GasFeePoller_getUnapprovedTransactions, _GasFeePoller_getGasFeeControllerData;
|
|
16
|
+
var _GasFeePoller_instances, _GasFeePoller_findNetworkClientIdByChainId, _GasFeePoller_gasFeeFlows, _GasFeePoller_getGasFeeControllerEstimates, _GasFeePoller_getProvider, _GasFeePoller_getTransactions, _GasFeePoller_layer1GasFeeFlows, _GasFeePoller_messenger, _GasFeePoller_timeout, _GasFeePoller_running, _GasFeePoller_start, _GasFeePoller_stop, _GasFeePoller_onTimeout, _GasFeePoller_updateUnapprovedTransactions, _GasFeePoller_updateUnapprovedTransaction, _GasFeePoller_updateTransactionGasFeeEstimates, _GasFeePoller_updateTransactionLayer1GasFee, _GasFeePoller_getUnapprovedTransactions, _GasFeePoller_getGasFeeControllerData;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.updateTransactionGasFees = exports.GasFeePoller = void 0;
|
|
19
19
|
const eth_query_1 = __importDefault(require("@metamask/eth-query"));
|
|
@@ -41,9 +41,10 @@ class GasFeePoller {
|
|
|
41
41
|
* @param options.getProvider - Callback to obtain a provider instance.
|
|
42
42
|
* @param options.getTransactions - Callback to obtain the transaction data.
|
|
43
43
|
* @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.
|
|
44
|
+
* @param options.messenger - The TransactionControllerMessenger instance.
|
|
44
45
|
* @param options.onStateChange - Callback to register a listener for controller state changes.
|
|
45
46
|
*/
|
|
46
|
-
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, onStateChange, }) {
|
|
47
|
+
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, messenger, onStateChange, }) {
|
|
47
48
|
_GasFeePoller_instances.add(this);
|
|
48
49
|
this.hub = new events_1.default();
|
|
49
50
|
_GasFeePoller_findNetworkClientIdByChainId.set(this, void 0);
|
|
@@ -52,6 +53,7 @@ class GasFeePoller {
|
|
|
52
53
|
_GasFeePoller_getProvider.set(this, void 0);
|
|
53
54
|
_GasFeePoller_getTransactions.set(this, void 0);
|
|
54
55
|
_GasFeePoller_layer1GasFeeFlows.set(this, void 0);
|
|
56
|
+
_GasFeePoller_messenger.set(this, void 0);
|
|
55
57
|
_GasFeePoller_timeout.set(this, void 0);
|
|
56
58
|
_GasFeePoller_running.set(this, false);
|
|
57
59
|
__classPrivateFieldSet(this, _GasFeePoller_findNetworkClientIdByChainId, findNetworkClientIdByChainId, "f");
|
|
@@ -60,6 +62,7 @@ class GasFeePoller {
|
|
|
60
62
|
__classPrivateFieldSet(this, _GasFeePoller_getGasFeeControllerEstimates, getGasFeeControllerEstimates, "f");
|
|
61
63
|
__classPrivateFieldSet(this, _GasFeePoller_getProvider, getProvider, "f");
|
|
62
64
|
__classPrivateFieldSet(this, _GasFeePoller_getTransactions, getTransactions, "f");
|
|
65
|
+
__classPrivateFieldSet(this, _GasFeePoller_messenger, messenger, "f");
|
|
63
66
|
onStateChange(() => {
|
|
64
67
|
const unapprovedTransactions = __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_getUnapprovedTransactions).call(this);
|
|
65
68
|
if (unapprovedTransactions.length) {
|
|
@@ -72,7 +75,7 @@ class GasFeePoller {
|
|
|
72
75
|
}
|
|
73
76
|
}
|
|
74
77
|
exports.GasFeePoller = GasFeePoller;
|
|
75
|
-
_GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFeeFlows = new WeakMap(), _GasFeePoller_getGasFeeControllerEstimates = new WeakMap(), _GasFeePoller_getProvider = new WeakMap(), _GasFeePoller_getTransactions = new WeakMap(), _GasFeePoller_layer1GasFeeFlows = new WeakMap(), _GasFeePoller_timeout = new WeakMap(), _GasFeePoller_running = new WeakMap(), _GasFeePoller_instances = new WeakSet(), _GasFeePoller_start = function _GasFeePoller_start() {
|
|
78
|
+
_GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFeeFlows = new WeakMap(), _GasFeePoller_getGasFeeControllerEstimates = new WeakMap(), _GasFeePoller_getProvider = new WeakMap(), _GasFeePoller_getTransactions = new WeakMap(), _GasFeePoller_layer1GasFeeFlows = new WeakMap(), _GasFeePoller_messenger = new WeakMap(), _GasFeePoller_timeout = new WeakMap(), _GasFeePoller_running = new WeakMap(), _GasFeePoller_instances = new WeakSet(), _GasFeePoller_start = function _GasFeePoller_start() {
|
|
76
79
|
if (__classPrivateFieldGet(this, _GasFeePoller_running, "f")) {
|
|
77
80
|
return;
|
|
78
81
|
}
|
|
@@ -124,13 +127,14 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
124
127
|
}, _GasFeePoller_updateTransactionGasFeeEstimates = async function _GasFeePoller_updateTransactionGasFeeEstimates(transactionMeta, gasFeeControllerData) {
|
|
125
128
|
const { networkClientId } = transactionMeta;
|
|
126
129
|
const ethQuery = new eth_query_1.default(__classPrivateFieldGet(this, _GasFeePoller_getProvider, "f").call(this, networkClientId));
|
|
127
|
-
const gasFeeFlow = (0, gas_flow_1.getGasFeeFlow)(transactionMeta, __classPrivateFieldGet(this, _GasFeePoller_gasFeeFlows, "f"));
|
|
130
|
+
const gasFeeFlow = (0, gas_flow_1.getGasFeeFlow)(transactionMeta, __classPrivateFieldGet(this, _GasFeePoller_gasFeeFlows, "f"), __classPrivateFieldGet(this, _GasFeePoller_messenger, "f"));
|
|
128
131
|
if (gasFeeFlow) {
|
|
129
132
|
log('Found gas fee flow', gasFeeFlow.constructor.name, transactionMeta.id);
|
|
130
133
|
}
|
|
131
134
|
const request = {
|
|
132
135
|
ethQuery,
|
|
133
136
|
gasFeeControllerData,
|
|
137
|
+
messenger: __classPrivateFieldGet(this, _GasFeePoller_messenger, "f"),
|
|
134
138
|
transactionMeta,
|
|
135
139
|
};
|
|
136
140
|
let gasFeeEstimates;
|
|
@@ -156,6 +160,7 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
156
160
|
const provider = __classPrivateFieldGet(this, _GasFeePoller_getProvider, "f").call(this, networkClientId);
|
|
157
161
|
const layer1GasFee = await (0, layer1_gas_fee_flow_1.getTransactionLayer1GasFee)({
|
|
158
162
|
layer1GasFeeFlows: __classPrivateFieldGet(this, _GasFeePoller_layer1GasFeeFlows, "f"),
|
|
163
|
+
messenger: __classPrivateFieldGet(this, _GasFeePoller_messenger, "f"),
|
|
159
164
|
provider,
|
|
160
165
|
transactionMeta,
|
|
161
166
|
});
|
|
@@ -192,17 +197,15 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
192
197
|
* @param args.txMeta - The transaction meta.
|
|
193
198
|
* @param args.gasFeeEstimates - The gas fee estimates.
|
|
194
199
|
* @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
|
|
195
|
-
* @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
|
|
196
200
|
* @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
|
|
197
201
|
* @param args.layer1GasFee - The layer 1 gas fee.
|
|
198
202
|
*/
|
|
199
|
-
|
|
203
|
+
function updateTransactionGasFees({ txMeta, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }) {
|
|
200
204
|
const userFeeLevel = txMeta.userFeeLevel;
|
|
201
205
|
const isUsingGasFeeEstimateLevel = Object.values(types_1.GasFeeEstimateLevel).includes(userFeeLevel);
|
|
202
206
|
const { type: gasEstimateType } = gasFeeEstimates ?? {};
|
|
203
207
|
if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {
|
|
204
|
-
const isEIP1559Compatible = txMeta.txParams.type !== types_1.TransactionEnvelopeType.legacy
|
|
205
|
-
(await getEIP1559Compatibility(txMeta.networkClientId));
|
|
208
|
+
const isEIP1559Compatible = txMeta.txParams.type !== types_1.TransactionEnvelopeType.legacy;
|
|
206
209
|
if (isEIP1559Compatible) {
|
|
207
210
|
// Handle EIP-1559 compatible transactions
|
|
208
211
|
if (gasEstimateType === types_1.GasFeeEstimateType.FeeMarket) {
|
|
@@ -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;AAW1C,wCAKkB;AAClB,oDAAkD;AAClD,0EAA0E;AAE1E,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC;;GAEG;AACH,MAAa,YAAY;IAuBvB;;;;;;;;;;;OAWG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,aAAa,GAWd;;QApDD,QAAG,GAAiB,IAAI,gBAAY,EAAE,CAAC;QAE9B,6DAEwB;QAExB,4CAA2B;QAE3B,6DAEiB;QAEjB,4CAA6D;QAE7D,gDAA0C;QAE1C,kDAAuC;QAEhD,wCAAoD;QAEpD,gCAAW,KAAK,EAAC;QAiCf,uBAAA,IAAI,8CAAiC,4BAA4B,MAAA,CAAC;QAClE,uBAAA,IAAI,6BAAgB,WAAW,MAAA,CAAC;QAChC,uBAAA,IAAI,mCAAsB,iBAAiB,MAAA,CAAC;QAC5C,uBAAA,IAAI,8CAAiC,4BAA4B,MAAA,CAAC;QAClE,uBAAA,IAAI,6BAAgB,WAAW,MAAA,CAAC;QAChC,uBAAA,IAAI,iCAAoB,eAAe,MAAA,CAAC;QAEx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,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,0BAAkB,CAAC,SAAS,EAAE;gBACpD,MAAM,wBAAwB,GAC5B,eAA2C,CAAC;gBAC9C,MAAM,CAAC,QAAQ,CAAC,QAAQ;oBACtB,wBAAwB,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC;aACvD;YAED,IAAI,eAAe,KAAK,0BAAkB,CAAC,QAAQ,EAAE;gBACnD,MAAM,uBAAuB,GAC3B,eAA0C,CAAC;gBAC7C,MAAM,CAAC,QAAQ,CAAC,QAAQ,GAAG,uBAAuB,CAAC,QAAQ,CAAC;aAC7D;YAED,IAAI,eAAe,KAAK,0BAAkB,CAAC,MAAM,EAAE;gBACjD,MAAM,qBAAqB,GAAG,eAAwC,CAAC;gBACvE,MAAM,CAAC,QAAQ,CAAC,QAAQ,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;aAChE;YAED,8DAA8D;YAC9D,OAAO,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC;YACpC,OAAO,MAAM,CAAC,QAAQ,CAAC,oBAAoB,CAAC;SAC7C;KACF;IAED,IAAI,eAAe,EAAE;QACnB,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC;KAC1C;IAED,IAAI,qBAAqB,KAAK,SAAS,EAAE;QACvC,MAAM,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;KACtD;IAED,IAAI,YAAY,EAAE;QAChB,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;KACpC;AACH,CAAC;AA9FD,4DA8FC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { projectLogger } from '../logger';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n} from '../types';\nimport {\n GasFeeEstimateLevel,\n GasFeeEstimateType,\n TransactionStatus,\n TransactionEnvelopeType,\n} from '../types';\nimport { getGasFeeFlow } from '../utils/gas-flow';\nimport { getTransactionLayer1GasFee } from '../utils/layer1-gas-fee-flow';\n\nconst log = createModuleLogger(projectLogger, 'gas-fee-poller');\n\nconst INTERVAL_MILLISECONDS = 10000;\n\n/**\n * Automatically polls and updates suggested gas fees on unapproved transactions.\n */\nexport class GasFeePoller {\n hub: EventEmitter = new EventEmitter();\n\n readonly #findNetworkClientIdByChainId: (\n chainId: Hex,\n ) => NetworkClientId | undefined;\n\n readonly #gasFeeFlows: GasFeeFlow[];\n\n readonly #getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n\n readonly #getProvider: (networkClientId: NetworkClientId) => Provider;\n\n readonly #getTransactions: () => TransactionMeta[];\n\n readonly #layer1GasFeeFlows: Layer1GasFeeFlow[];\n\n #timeout: ReturnType<typeof setTimeout> | undefined;\n\n #running = false;\n\n /**\n * Constructs a new instance of the GasFeePoller.\n *\n * @param options - The options for this instance.\n * @param options.findNetworkClientIdByChainId - Callback to find the network client ID by chain ID.\n * @param options.gasFeeFlows - The gas fee flows to use to obtain suitable gas fees.\n * @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.\n * @param options.getProvider - Callback to obtain a provider instance.\n * @param options.getTransactions - Callback to obtain the transaction data.\n * @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.\n * @param options.onStateChange - Callback to register a listener for controller state changes.\n */\n constructor({\n findNetworkClientIdByChainId,\n gasFeeFlows,\n getGasFeeControllerEstimates,\n getProvider,\n getTransactions,\n layer1GasFeeFlows,\n onStateChange,\n }: {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getProvider: (networkClientId: NetworkClientId) => Provider;\n getTransactions: () => TransactionMeta[];\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n onStateChange: (listener: () => void) => void;\n }) {\n this.#findNetworkClientIdByChainId = findNetworkClientIdByChainId;\n this.#gasFeeFlows = gasFeeFlows;\n this.#layer1GasFeeFlows = layer1GasFeeFlows;\n this.#getGasFeeControllerEstimates = getGasFeeControllerEstimates;\n this.#getProvider = getProvider;\n this.#getTransactions = getTransactions;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (unapprovedTransactions.length) {\n this.#start();\n } else {\n this.#stop();\n }\n });\n }\n\n #start() {\n if (this.#running) {\n return;\n }\n\n // Intentionally not awaiting since this starts the timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#onTimeout();\n\n this.#running = true;\n\n log('Started polling');\n }\n\n #stop() {\n if (!this.#running) {\n return;\n }\n\n clearTimeout(this.#timeout);\n\n this.#timeout = undefined;\n this.#running = false;\n\n log('Stopped polling');\n }\n\n async #onTimeout() {\n await this.#updateUnapprovedTransactions();\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeout = setTimeout(() => this.#onTimeout(), INTERVAL_MILLISECONDS);\n }\n\n async #updateUnapprovedTransactions() {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (!unapprovedTransactions.length) {\n return;\n }\n\n log('Found unapproved transactions', unapprovedTransactions.length);\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactions,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactions.flatMap((tx) => {\n const { chainId } = tx;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransaction(tx, gasFeeControllerData);\n }),\n );\n }\n\n async #updateUnapprovedTransaction(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = transactionMeta;\n\n const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([\n this.#updateTransactionGasFeeEstimates(\n transactionMeta,\n gasFeeControllerData,\n ),\n this.#updateTransactionLayer1GasFee(transactionMeta),\n ]);\n\n if (!gasFeeEstimatesResponse && !layer1GasFee) {\n return;\n }\n\n this.hub.emit('transaction-updated', {\n transactionId: id,\n gasFeeEstimates: gasFeeEstimatesResponse?.gasFeeEstimates,\n gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,\n layer1GasFee,\n });\n }\n\n async #updateTransactionGasFeeEstimates(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ): Promise<\n | { gasFeeEstimates?: GasFeeEstimates; gasFeeEstimatesLoaded: boolean }\n | undefined\n > {\n const { networkClientId } = transactionMeta;\n\n const ethQuery = new EthQuery(this.#getProvider(networkClientId));\n const gasFeeFlow = getGasFeeFlow(transactionMeta, this.#gasFeeFlows);\n\n if (gasFeeFlow) {\n log(\n 'Found gas fee flow',\n gasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n }\n\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n transactionMeta,\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n if (gasFeeFlow) {\n try {\n const response = await gasFeeFlow.getGasFees(request);\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get suggested gas fees', transactionMeta.id, error);\n }\n }\n\n if (!gasFeeEstimates && transactionMeta.gasFeeEstimatesLoaded) {\n return undefined;\n }\n\n log('Updated gas fee estimates', {\n gasFeeEstimates,\n transaction: transactionMeta.id,\n });\n\n return { gasFeeEstimates, gasFeeEstimatesLoaded: true };\n }\n\n async #updateTransactionLayer1GasFee(\n transactionMeta: TransactionMeta,\n ): Promise<Hex | undefined> {\n const { networkClientId } = transactionMeta;\n const provider = this.#getProvider(networkClientId);\n\n const layer1GasFee = await getTransactionLayer1GasFee({\n layer1GasFeeFlows: this.#layer1GasFeeFlows,\n provider,\n transactionMeta,\n });\n\n if (layer1GasFee) {\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n }\n\n return layer1GasFee;\n }\n\n #getUnapprovedTransactions() {\n return this.#getTransactions().filter(\n (tx) => tx.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n\n/**\n * Update the gas fees for a transaction.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.gasFeeEstimates - The gas fee estimates.\n * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.\n * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.\n * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport async function updateTransactionGasFees({\n txMeta,\n getEIP1559Compatibility,\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n}: {\n txMeta: TransactionMeta;\n getEIP1559Compatibility: (\n networkClientId: NetworkClientId,\n ) => Promise<boolean>;\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: boolean;\n layer1GasFee?: Hex;\n}): Promise<void> {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const { type: gasEstimateType } = gasFeeEstimates ?? {};\n\n if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy &&\n (await getEIP1559Compatibility(txMeta.networkClientId));\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n txMeta.txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n\n txMeta.txParams.maxFeePerGas = gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txMeta.txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txMeta.txParams.gasPrice =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txMeta.txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txMeta.txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txMeta.txParams.maxFeePerGas;\n delete txMeta.txParams.maxPriorityFeePerGas;\n }\n }\n\n if (gasFeeEstimates) {\n txMeta.gasFeeEstimates = gasFeeEstimates;\n }\n\n if (gasFeeEstimatesLoaded !== undefined) {\n txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;\n }\n\n if (layer1GasFee) {\n txMeta.layer1GasFee = layer1GasFee;\n }\n}\n"]}
|
|
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;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AM,CAAC,QAAQ,CAAC,QAAQ,GAAG,uBAAuB,CAAC,QAAQ,CAAC;aAC7D;YAED,IAAI,eAAe,KAAK,0BAAkB,CAAC,MAAM,EAAE;gBACjD,MAAM,qBAAqB,GAAG,eAAwC,CAAC;gBACvE,MAAM,CAAC,QAAQ,CAAC,QAAQ,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;aAChE;YAED,8DAA8D;YAC9D,OAAO,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC;YACpC,OAAO,MAAM,CAAC,QAAQ,CAAC,oBAAoB,CAAC;SAC7C;KACF;IAED,IAAI,eAAe,EAAE;QACnB,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC;KAC1C;IAED,IAAI,qBAAqB,KAAK,SAAS,EAAE;QACvC,MAAM,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;KACtD;IAED,IAAI,YAAY,EAAE;QAChB,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;KACpC;AACH,CAAC;AAzFD,4DAyFC","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: 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\n if (isTxParamsGasFeeUpdatesEnabled && 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"]}
|
|
@@ -3,6 +3,7 @@ import type { FetchGasFeeEstimateOptions, GasFeeState } from "@metamask/gas-fee-
|
|
|
3
3
|
import type { NetworkClientId, Provider } from "@metamask/network-controller";
|
|
4
4
|
import type { Hex } from "@metamask/utils";
|
|
5
5
|
import EventEmitter from "events";
|
|
6
|
+
import type { TransactionControllerMessenger } from "../TransactionController.cjs";
|
|
6
7
|
import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta } from "../types.cjs";
|
|
7
8
|
/**
|
|
8
9
|
* Automatically polls and updates suggested gas fees on unapproved transactions.
|
|
@@ -20,15 +21,17 @@ export declare class GasFeePoller {
|
|
|
20
21
|
* @param options.getProvider - Callback to obtain a provider instance.
|
|
21
22
|
* @param options.getTransactions - Callback to obtain the transaction data.
|
|
22
23
|
* @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.
|
|
24
|
+
* @param options.messenger - The TransactionControllerMessenger instance.
|
|
23
25
|
* @param options.onStateChange - Callback to register a listener for controller state changes.
|
|
24
26
|
*/
|
|
25
|
-
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, onStateChange, }: {
|
|
27
|
+
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, messenger, onStateChange, }: {
|
|
26
28
|
findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;
|
|
27
29
|
gasFeeFlows: GasFeeFlow[];
|
|
28
30
|
getGasFeeControllerEstimates: (options: FetchGasFeeEstimateOptions) => Promise<GasFeeState>;
|
|
29
31
|
getProvider: (networkClientId: NetworkClientId) => Provider;
|
|
30
32
|
getTransactions: () => TransactionMeta[];
|
|
31
33
|
layer1GasFeeFlows: Layer1GasFeeFlow[];
|
|
34
|
+
messenger: TransactionControllerMessenger;
|
|
32
35
|
onStateChange: (listener: () => void) => void;
|
|
33
36
|
});
|
|
34
37
|
}
|
|
@@ -39,16 +42,14 @@ export declare class GasFeePoller {
|
|
|
39
42
|
* @param args.txMeta - The transaction meta.
|
|
40
43
|
* @param args.gasFeeEstimates - The gas fee estimates.
|
|
41
44
|
* @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
|
|
42
|
-
* @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
|
|
43
45
|
* @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
|
|
44
46
|
* @param args.layer1GasFee - The layer 1 gas fee.
|
|
45
47
|
*/
|
|
46
|
-
export declare function updateTransactionGasFees({ txMeta,
|
|
48
|
+
export declare function updateTransactionGasFees({ txMeta, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }: {
|
|
47
49
|
txMeta: TransactionMeta;
|
|
48
|
-
getEIP1559Compatibility: (networkClientId: NetworkClientId) => Promise<boolean>;
|
|
49
50
|
gasFeeEstimates?: GasFeeEstimates;
|
|
50
51
|
gasFeeEstimatesLoaded?: boolean;
|
|
51
52
|
isTxParamsGasFeeUpdatesEnabled: boolean;
|
|
52
53
|
layer1GasFee?: Hex;
|
|
53
|
-
}):
|
|
54
|
+
}): void;
|
|
54
55
|
//# 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,EACV,eAAe,EACf,UAAU,EAIV,gBAAgB,EAEhB,eAAe,EAChB,qBAAiB;AAclB;;GAEG;AACH,qBAAa,YAAY;;IACvB,GAAG,EAAE,YAAY,CAAsB;
|
|
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,EAChB,qBAAiB;AAclB;;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,wBAAwB,CAAC,EACvC,MAAM,EACN,eAAe,EACf,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,GACb,EAAE;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8BAA8B,EAAE,OAAO,CAAC;IACxC,YAAY,CAAC,EAAE,GAAG,CAAC;CACpB,GAAG,IAAI,CA6EP"}
|
|
@@ -3,6 +3,7 @@ import type { FetchGasFeeEstimateOptions, GasFeeState } from "@metamask/gas-fee-
|
|
|
3
3
|
import type { NetworkClientId, Provider } from "@metamask/network-controller";
|
|
4
4
|
import type { Hex } from "@metamask/utils";
|
|
5
5
|
import EventEmitter from "events";
|
|
6
|
+
import type { TransactionControllerMessenger } from "../TransactionController.mjs";
|
|
6
7
|
import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta } from "../types.mjs";
|
|
7
8
|
/**
|
|
8
9
|
* Automatically polls and updates suggested gas fees on unapproved transactions.
|
|
@@ -20,15 +21,17 @@ export declare class GasFeePoller {
|
|
|
20
21
|
* @param options.getProvider - Callback to obtain a provider instance.
|
|
21
22
|
* @param options.getTransactions - Callback to obtain the transaction data.
|
|
22
23
|
* @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.
|
|
24
|
+
* @param options.messenger - The TransactionControllerMessenger instance.
|
|
23
25
|
* @param options.onStateChange - Callback to register a listener for controller state changes.
|
|
24
26
|
*/
|
|
25
|
-
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, onStateChange, }: {
|
|
27
|
+
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, messenger, onStateChange, }: {
|
|
26
28
|
findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;
|
|
27
29
|
gasFeeFlows: GasFeeFlow[];
|
|
28
30
|
getGasFeeControllerEstimates: (options: FetchGasFeeEstimateOptions) => Promise<GasFeeState>;
|
|
29
31
|
getProvider: (networkClientId: NetworkClientId) => Provider;
|
|
30
32
|
getTransactions: () => TransactionMeta[];
|
|
31
33
|
layer1GasFeeFlows: Layer1GasFeeFlow[];
|
|
34
|
+
messenger: TransactionControllerMessenger;
|
|
32
35
|
onStateChange: (listener: () => void) => void;
|
|
33
36
|
});
|
|
34
37
|
}
|
|
@@ -39,16 +42,14 @@ export declare class GasFeePoller {
|
|
|
39
42
|
* @param args.txMeta - The transaction meta.
|
|
40
43
|
* @param args.gasFeeEstimates - The gas fee estimates.
|
|
41
44
|
* @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
|
|
42
|
-
* @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
|
|
43
45
|
* @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
|
|
44
46
|
* @param args.layer1GasFee - The layer 1 gas fee.
|
|
45
47
|
*/
|
|
46
|
-
export declare function updateTransactionGasFees({ txMeta,
|
|
48
|
+
export declare function updateTransactionGasFees({ txMeta, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }: {
|
|
47
49
|
txMeta: TransactionMeta;
|
|
48
|
-
getEIP1559Compatibility: (networkClientId: NetworkClientId) => Promise<boolean>;
|
|
49
50
|
gasFeeEstimates?: GasFeeEstimates;
|
|
50
51
|
gasFeeEstimatesLoaded?: boolean;
|
|
51
52
|
isTxParamsGasFeeUpdatesEnabled: boolean;
|
|
52
53
|
layer1GasFee?: Hex;
|
|
53
|
-
}):
|
|
54
|
+
}): void;
|
|
54
55
|
//# 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,EACV,eAAe,EACf,UAAU,EAIV,gBAAgB,EAEhB,eAAe,EAChB,qBAAiB;AAclB;;GAEG;AACH,qBAAa,YAAY;;IACvB,GAAG,EAAE,YAAY,CAAsB;
|
|
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,EAChB,qBAAiB;AAclB;;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,wBAAwB,CAAC,EACvC,MAAM,EACN,eAAe,EACf,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,GACb,EAAE;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8BAA8B,EAAE,OAAO,CAAC;IACxC,YAAY,CAAC,EAAE,GAAG,CAAC;CACpB,GAAG,IAAI,CA6EP"}
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _GasFeePoller_instances, _GasFeePoller_findNetworkClientIdByChainId, _GasFeePoller_gasFeeFlows, _GasFeePoller_getGasFeeControllerEstimates, _GasFeePoller_getProvider, _GasFeePoller_getTransactions, _GasFeePoller_layer1GasFeeFlows, _GasFeePoller_timeout, _GasFeePoller_running, _GasFeePoller_start, _GasFeePoller_stop, _GasFeePoller_onTimeout, _GasFeePoller_updateUnapprovedTransactions, _GasFeePoller_updateUnapprovedTransaction, _GasFeePoller_updateTransactionGasFeeEstimates, _GasFeePoller_updateTransactionLayer1GasFee, _GasFeePoller_getUnapprovedTransactions, _GasFeePoller_getGasFeeControllerData;
|
|
12
|
+
var _GasFeePoller_instances, _GasFeePoller_findNetworkClientIdByChainId, _GasFeePoller_gasFeeFlows, _GasFeePoller_getGasFeeControllerEstimates, _GasFeePoller_getProvider, _GasFeePoller_getTransactions, _GasFeePoller_layer1GasFeeFlows, _GasFeePoller_messenger, _GasFeePoller_timeout, _GasFeePoller_running, _GasFeePoller_start, _GasFeePoller_stop, _GasFeePoller_onTimeout, _GasFeePoller_updateUnapprovedTransactions, _GasFeePoller_updateUnapprovedTransaction, _GasFeePoller_updateTransactionGasFeeEstimates, _GasFeePoller_updateTransactionLayer1GasFee, _GasFeePoller_getUnapprovedTransactions, _GasFeePoller_getGasFeeControllerData;
|
|
13
13
|
function $importDefault(module) {
|
|
14
14
|
if (module?.__esModule) {
|
|
15
15
|
return module.default;
|
|
@@ -42,9 +42,10 @@ export class GasFeePoller {
|
|
|
42
42
|
* @param options.getProvider - Callback to obtain a provider instance.
|
|
43
43
|
* @param options.getTransactions - Callback to obtain the transaction data.
|
|
44
44
|
* @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.
|
|
45
|
+
* @param options.messenger - The TransactionControllerMessenger instance.
|
|
45
46
|
* @param options.onStateChange - Callback to register a listener for controller state changes.
|
|
46
47
|
*/
|
|
47
|
-
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, onStateChange, }) {
|
|
48
|
+
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, messenger, onStateChange, }) {
|
|
48
49
|
_GasFeePoller_instances.add(this);
|
|
49
50
|
this.hub = new EventEmitter();
|
|
50
51
|
_GasFeePoller_findNetworkClientIdByChainId.set(this, void 0);
|
|
@@ -53,6 +54,7 @@ export class GasFeePoller {
|
|
|
53
54
|
_GasFeePoller_getProvider.set(this, void 0);
|
|
54
55
|
_GasFeePoller_getTransactions.set(this, void 0);
|
|
55
56
|
_GasFeePoller_layer1GasFeeFlows.set(this, void 0);
|
|
57
|
+
_GasFeePoller_messenger.set(this, void 0);
|
|
56
58
|
_GasFeePoller_timeout.set(this, void 0);
|
|
57
59
|
_GasFeePoller_running.set(this, false);
|
|
58
60
|
__classPrivateFieldSet(this, _GasFeePoller_findNetworkClientIdByChainId, findNetworkClientIdByChainId, "f");
|
|
@@ -61,6 +63,7 @@ export class GasFeePoller {
|
|
|
61
63
|
__classPrivateFieldSet(this, _GasFeePoller_getGasFeeControllerEstimates, getGasFeeControllerEstimates, "f");
|
|
62
64
|
__classPrivateFieldSet(this, _GasFeePoller_getProvider, getProvider, "f");
|
|
63
65
|
__classPrivateFieldSet(this, _GasFeePoller_getTransactions, getTransactions, "f");
|
|
66
|
+
__classPrivateFieldSet(this, _GasFeePoller_messenger, messenger, "f");
|
|
64
67
|
onStateChange(() => {
|
|
65
68
|
const unapprovedTransactions = __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_getUnapprovedTransactions).call(this);
|
|
66
69
|
if (unapprovedTransactions.length) {
|
|
@@ -72,7 +75,7 @@ export class GasFeePoller {
|
|
|
72
75
|
});
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
|
-
_GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFeeFlows = new WeakMap(), _GasFeePoller_getGasFeeControllerEstimates = new WeakMap(), _GasFeePoller_getProvider = new WeakMap(), _GasFeePoller_getTransactions = new WeakMap(), _GasFeePoller_layer1GasFeeFlows = new WeakMap(), _GasFeePoller_timeout = new WeakMap(), _GasFeePoller_running = new WeakMap(), _GasFeePoller_instances = new WeakSet(), _GasFeePoller_start = function _GasFeePoller_start() {
|
|
78
|
+
_GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFeeFlows = new WeakMap(), _GasFeePoller_getGasFeeControllerEstimates = new WeakMap(), _GasFeePoller_getProvider = new WeakMap(), _GasFeePoller_getTransactions = new WeakMap(), _GasFeePoller_layer1GasFeeFlows = new WeakMap(), _GasFeePoller_messenger = new WeakMap(), _GasFeePoller_timeout = new WeakMap(), _GasFeePoller_running = new WeakMap(), _GasFeePoller_instances = new WeakSet(), _GasFeePoller_start = function _GasFeePoller_start() {
|
|
76
79
|
if (__classPrivateFieldGet(this, _GasFeePoller_running, "f")) {
|
|
77
80
|
return;
|
|
78
81
|
}
|
|
@@ -124,13 +127,14 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
124
127
|
}, _GasFeePoller_updateTransactionGasFeeEstimates = async function _GasFeePoller_updateTransactionGasFeeEstimates(transactionMeta, gasFeeControllerData) {
|
|
125
128
|
const { networkClientId } = transactionMeta;
|
|
126
129
|
const ethQuery = new EthQuery(__classPrivateFieldGet(this, _GasFeePoller_getProvider, "f").call(this, networkClientId));
|
|
127
|
-
const gasFeeFlow = getGasFeeFlow(transactionMeta, __classPrivateFieldGet(this, _GasFeePoller_gasFeeFlows, "f"));
|
|
130
|
+
const gasFeeFlow = getGasFeeFlow(transactionMeta, __classPrivateFieldGet(this, _GasFeePoller_gasFeeFlows, "f"), __classPrivateFieldGet(this, _GasFeePoller_messenger, "f"));
|
|
128
131
|
if (gasFeeFlow) {
|
|
129
132
|
log('Found gas fee flow', gasFeeFlow.constructor.name, transactionMeta.id);
|
|
130
133
|
}
|
|
131
134
|
const request = {
|
|
132
135
|
ethQuery,
|
|
133
136
|
gasFeeControllerData,
|
|
137
|
+
messenger: __classPrivateFieldGet(this, _GasFeePoller_messenger, "f"),
|
|
134
138
|
transactionMeta,
|
|
135
139
|
};
|
|
136
140
|
let gasFeeEstimates;
|
|
@@ -156,6 +160,7 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
156
160
|
const provider = __classPrivateFieldGet(this, _GasFeePoller_getProvider, "f").call(this, networkClientId);
|
|
157
161
|
const layer1GasFee = await getTransactionLayer1GasFee({
|
|
158
162
|
layer1GasFeeFlows: __classPrivateFieldGet(this, _GasFeePoller_layer1GasFeeFlows, "f"),
|
|
163
|
+
messenger: __classPrivateFieldGet(this, _GasFeePoller_messenger, "f"),
|
|
159
164
|
provider,
|
|
160
165
|
transactionMeta,
|
|
161
166
|
});
|
|
@@ -192,17 +197,15 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
192
197
|
* @param args.txMeta - The transaction meta.
|
|
193
198
|
* @param args.gasFeeEstimates - The gas fee estimates.
|
|
194
199
|
* @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.
|
|
195
|
-
* @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.
|
|
196
200
|
* @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.
|
|
197
201
|
* @param args.layer1GasFee - The layer 1 gas fee.
|
|
198
202
|
*/
|
|
199
|
-
export
|
|
203
|
+
export function updateTransactionGasFees({ txMeta, gasFeeEstimates, gasFeeEstimatesLoaded, isTxParamsGasFeeUpdatesEnabled, layer1GasFee, }) {
|
|
200
204
|
const userFeeLevel = txMeta.userFeeLevel;
|
|
201
205
|
const isUsingGasFeeEstimateLevel = Object.values(GasFeeEstimateLevel).includes(userFeeLevel);
|
|
202
206
|
const { type: gasEstimateType } = gasFeeEstimates ?? {};
|
|
203
207
|
if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {
|
|
204
|
-
const isEIP1559Compatible = txMeta.txParams.type !== TransactionEnvelopeType.legacy
|
|
205
|
-
(await getEIP1559Compatibility(txMeta.networkClientId));
|
|
208
|
+
const isEIP1559Compatible = txMeta.txParams.type !== TransactionEnvelopeType.legacy;
|
|
206
209
|
if (isEIP1559Compatible) {
|
|
207
210
|
// Handle EIP-1559 compatible transactions
|
|
208
211
|
if (gasEstimateType === GasFeeEstimateType.FeeMarket) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GasFeePoller.mjs","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,SAAQ,4BAA4B;;AAO3C,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AACrD,kEAAkE;AAClE,sDAAsD;AACtD,OAAO,YAAY,eAAe;AAElC,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAW1C,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,iBAAiB,EACjB,uBAAuB,EACxB,qBAAiB;AAClB,OAAO,EAAE,aAAa,EAAE,8BAA0B;AAClD,OAAO,EAAE,0BAA0B,EAAE,yCAAqC;AAE1E,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,YAAY;IAuBvB;;;;;;;;;;;OAWG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,aAAa,GAWd;;QApDD,QAAG,GAAiB,IAAI,YAAY,EAAE,CAAC;QAE9B,6DAEwB;QAExB,4CAA2B;QAE3B,6DAEiB;QAEjB,4CAA6D;QAE7D,gDAA0C;QAE1C,kDAAuC;QAEhD,wCAAoD;QAEpD,gCAAW,KAAK,EAAC;QAiCf,uBAAA,IAAI,8CAAiC,4BAA4B,MAAA,CAAC;QAClE,uBAAA,I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ChB,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;KACpC;AACH,CAAC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { projectLogger } from '../logger';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n} from '../types';\nimport {\n GasFeeEstimateLevel,\n GasFeeEstimateType,\n TransactionStatus,\n TransactionEnvelopeType,\n} from '../types';\nimport { getGasFeeFlow } from '../utils/gas-flow';\nimport { getTransactionLayer1GasFee } from '../utils/layer1-gas-fee-flow';\n\nconst log = createModuleLogger(projectLogger, 'gas-fee-poller');\n\nconst INTERVAL_MILLISECONDS = 10000;\n\n/**\n * Automatically polls and updates suggested gas fees on unapproved transactions.\n */\nexport class GasFeePoller {\n hub: EventEmitter = new EventEmitter();\n\n readonly #findNetworkClientIdByChainId: (\n chainId: Hex,\n ) => NetworkClientId | undefined;\n\n readonly #gasFeeFlows: GasFeeFlow[];\n\n readonly #getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n\n readonly #getProvider: (networkClientId: NetworkClientId) => Provider;\n\n readonly #getTransactions: () => TransactionMeta[];\n\n readonly #layer1GasFeeFlows: Layer1GasFeeFlow[];\n\n #timeout: ReturnType<typeof setTimeout> | undefined;\n\n #running = false;\n\n /**\n * Constructs a new instance of the GasFeePoller.\n *\n * @param options - The options for this instance.\n * @param options.findNetworkClientIdByChainId - Callback to find the network client ID by chain ID.\n * @param options.gasFeeFlows - The gas fee flows to use to obtain suitable gas fees.\n * @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.\n * @param options.getProvider - Callback to obtain a provider instance.\n * @param options.getTransactions - Callback to obtain the transaction data.\n * @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.\n * @param options.onStateChange - Callback to register a listener for controller state changes.\n */\n constructor({\n findNetworkClientIdByChainId,\n gasFeeFlows,\n getGasFeeControllerEstimates,\n getProvider,\n getTransactions,\n layer1GasFeeFlows,\n onStateChange,\n }: {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getProvider: (networkClientId: NetworkClientId) => Provider;\n getTransactions: () => TransactionMeta[];\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n onStateChange: (listener: () => void) => void;\n }) {\n this.#findNetworkClientIdByChainId = findNetworkClientIdByChainId;\n this.#gasFeeFlows = gasFeeFlows;\n this.#layer1GasFeeFlows = layer1GasFeeFlows;\n this.#getGasFeeControllerEstimates = getGasFeeControllerEstimates;\n this.#getProvider = getProvider;\n this.#getTransactions = getTransactions;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (unapprovedTransactions.length) {\n this.#start();\n } else {\n this.#stop();\n }\n });\n }\n\n #start() {\n if (this.#running) {\n return;\n }\n\n // Intentionally not awaiting since this starts the timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#onTimeout();\n\n this.#running = true;\n\n log('Started polling');\n }\n\n #stop() {\n if (!this.#running) {\n return;\n }\n\n clearTimeout(this.#timeout);\n\n this.#timeout = undefined;\n this.#running = false;\n\n log('Stopped polling');\n }\n\n async #onTimeout() {\n await this.#updateUnapprovedTransactions();\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeout = setTimeout(() => this.#onTimeout(), INTERVAL_MILLISECONDS);\n }\n\n async #updateUnapprovedTransactions() {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (!unapprovedTransactions.length) {\n return;\n }\n\n log('Found unapproved transactions', unapprovedTransactions.length);\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactions,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactions.flatMap((tx) => {\n const { chainId } = tx;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransaction(tx, gasFeeControllerData);\n }),\n );\n }\n\n async #updateUnapprovedTransaction(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = transactionMeta;\n\n const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([\n this.#updateTransactionGasFeeEstimates(\n transactionMeta,\n gasFeeControllerData,\n ),\n this.#updateTransactionLayer1GasFee(transactionMeta),\n ]);\n\n if (!gasFeeEstimatesResponse && !layer1GasFee) {\n return;\n }\n\n this.hub.emit('transaction-updated', {\n transactionId: id,\n gasFeeEstimates: gasFeeEstimatesResponse?.gasFeeEstimates,\n gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,\n layer1GasFee,\n });\n }\n\n async #updateTransactionGasFeeEstimates(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ): Promise<\n | { gasFeeEstimates?: GasFeeEstimates; gasFeeEstimatesLoaded: boolean }\n | undefined\n > {\n const { networkClientId } = transactionMeta;\n\n const ethQuery = new EthQuery(this.#getProvider(networkClientId));\n const gasFeeFlow = getGasFeeFlow(transactionMeta, this.#gasFeeFlows);\n\n if (gasFeeFlow) {\n log(\n 'Found gas fee flow',\n gasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n }\n\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n transactionMeta,\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n if (gasFeeFlow) {\n try {\n const response = await gasFeeFlow.getGasFees(request);\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get suggested gas fees', transactionMeta.id, error);\n }\n }\n\n if (!gasFeeEstimates && transactionMeta.gasFeeEstimatesLoaded) {\n return undefined;\n }\n\n log('Updated gas fee estimates', {\n gasFeeEstimates,\n transaction: transactionMeta.id,\n });\n\n return { gasFeeEstimates, gasFeeEstimatesLoaded: true };\n }\n\n async #updateTransactionLayer1GasFee(\n transactionMeta: TransactionMeta,\n ): Promise<Hex | undefined> {\n const { networkClientId } = transactionMeta;\n const provider = this.#getProvider(networkClientId);\n\n const layer1GasFee = await getTransactionLayer1GasFee({\n layer1GasFeeFlows: this.#layer1GasFeeFlows,\n provider,\n transactionMeta,\n });\n\n if (layer1GasFee) {\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n }\n\n return layer1GasFee;\n }\n\n #getUnapprovedTransactions() {\n return this.#getTransactions().filter(\n (tx) => tx.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n\n/**\n * Update the gas fees for a transaction.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.gasFeeEstimates - The gas fee estimates.\n * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.\n * @param args.getEIP1559Compatibility - A function for verifying a network is EIP-1559 compatible.\n * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport async function updateTransactionGasFees({\n txMeta,\n getEIP1559Compatibility,\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n}: {\n txMeta: TransactionMeta;\n getEIP1559Compatibility: (\n networkClientId: NetworkClientId,\n ) => Promise<boolean>;\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: boolean;\n layer1GasFee?: Hex;\n}): Promise<void> {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const { type: gasEstimateType } = gasFeeEstimates ?? {};\n\n if (isTxParamsGasFeeUpdatesEnabled && isUsingGasFeeEstimateLevel) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy &&\n (await getEIP1559Compatibility(txMeta.networkClientId));\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n txMeta.txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel].maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n\n txMeta.txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n\n txMeta.txParams.maxFeePerGas = gasPrice;\n txMeta.txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txMeta.txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txMeta.txParams.gasPrice =\n feeMarketGasFeeEstimates[userFeeLevel].maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txMeta.txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txMeta.txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txMeta.txParams.maxFeePerGas;\n delete txMeta.txParams.maxPriorityFeePerGas;\n }\n }\n\n if (gasFeeEstimates) {\n txMeta.gasFeeEstimates = gasFeeEstimates;\n }\n\n if (gasFeeEstimatesLoaded !== undefined) {\n txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;\n }\n\n if (layer1GasFee) {\n txMeta.layer1GasFee = layer1GasFee;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"GasFeePoller.mjs","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,SAAQ,4B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AM,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: 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\n if (isTxParamsGasFeeUpdatesEnabled && 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"]}
|
|
@@ -108,7 +108,8 @@ class IncomingTransactionHelper {
|
|
|
108
108
|
const localTransactions = __classPrivateFieldGet(this, _IncomingTransactionHelper_getLocalTransactions, "f").call(this);
|
|
109
109
|
const uniqueTransactions = remoteTransactions.filter((tx) => !localTransactions.some((currentTx) => currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&
|
|
110
110
|
currentTx.txParams.from?.toLowerCase() ===
|
|
111
|
-
tx.txParams.from?.toLowerCase()
|
|
111
|
+
tx.txParams.from?.toLowerCase() &&
|
|
112
|
+
currentTx.type === tx.type));
|
|
112
113
|
if (!uniqueTransactions.length) {
|
|
113
114
|
(0, logger_1.incomingTransactionsLogger)('All transactions are already known');
|
|
114
115
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAElC,0CAA8D;AAU9D,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC,CAAC,aAAa;AAEzC,MAAa,yBAAyB;IA+BpC,YAAY,EACV,QAAQ,EACR,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,kBAAkB,EAClB,uBAAuB,EACvB,gBAAgB,EAChB,WAAW,EACX,kBAAkB,GAcnB;;QApDQ,sDAAyC;QAEzC,+DAEP;QAEO,kEAA+C;QAE/C,mEAAiC;QAEjC,uDAA0B;QAEnC,uDAAoB;QAEX,gEAA8B;QAE9B,qEAAkD;QAE3D,uDAAqB;QAEZ,8DAEc;QAEd,yDAAqE;QAErE,gEAA8B;QA2BrC,IAAI,CAAC,GAAG,GAAG,IAAI,gBAAY,EAAE,CAAC;QAE9B,uBAAA,IAAI,uCAAa,QAAQ,MAAA,CAAC;QAC1B,uBAAA,IAAI,gDAAsB,iBAAiB,MAAA,CAAC;QAC5C,uBAAA,IAAI,mDAAyB,oBAAoB,MAAA,CAAC;QAClD,uBAAA,IAAI,oDAA0B,qBAAqB,MAAA,CAAC;QACpD,uBAAA,IAAI,wCAAc,SAAS,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAA,CAAC;QAC5C,uBAAA,IAAI,wCAAc,KAAK,MAAA,CAAC;QACxB,uBAAA,IAAI,iDAAuB,kBAAkB,MAAA,CAAC;QAC9C,uBAAA,IAAI,sDAA4B,uBAAuB,MAAA,CAAC;QACxD,uBAAA,IAAI,+CAAqB,gBAAgB,MAAA,CAAC;QAC1C,uBAAA,IAAI,0CAAgB,WAAW,MAAA,CAAC;QAChC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AC,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","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 { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\n\nexport type IncomingTransactionOptions = {\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n queryEntireHistory?: boolean;\n updateTransactions?: boolean;\n};\n\nconst INTERVAL = 1000 * 30; // 30 Seconds\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 #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 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 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.#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 log('Starting polling');\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 // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeoutId = setTimeout(() => this.#onInterval(), INTERVAL);\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 ),\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"]}
|
|
1
|
+
{"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAElC,0CAA8D;AAU9D,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC,CAAC,aAAa;AAE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@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 { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\n\nexport type IncomingTransactionOptions = {\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n queryEntireHistory?: boolean;\n updateTransactions?: boolean;\n};\n\nconst INTERVAL = 1000 * 30; // 30 Seconds\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 #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 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 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.#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 log('Starting polling');\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 // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeoutId = setTimeout(() => this.#onInterval(), INTERVAL);\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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IncomingTransactionHelper.d.cts","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sCAAsC;AAIxE,OAAO,YAAY,eAAe;AAGlC,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAEzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;IAC1B,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAIF,qBAAa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E,OAAO,CAAC;KAC9B;IAgBD,KAAK;IAkBL,IAAI;IA2BE,MAAM,CAAC,EAAE,UAAU,EAAE,GAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAgG3E"}
|
|
@@ -102,7 +102,8 @@ export class IncomingTransactionHelper {
|
|
|
102
102
|
const localTransactions = __classPrivateFieldGet(this, _IncomingTransactionHelper_getLocalTransactions, "f").call(this);
|
|
103
103
|
const uniqueTransactions = remoteTransactions.filter((tx) => !localTransactions.some((currentTx) => currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&
|
|
104
104
|
currentTx.txParams.from?.toLowerCase() ===
|
|
105
|
-
tx.txParams.from?.toLowerCase()
|
|
105
|
+
tx.txParams.from?.toLowerCase() &&
|
|
106
|
+
currentTx.type === tx.type));
|
|
106
107
|
if (!uniqueTransactions.length) {
|
|
107
108
|
log('All transactions are already known');
|
|
108
109
|
return;
|