@metamask/transaction-controller 58.0.0 → 58.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -1
- package/dist/TransactionController.cjs +27 -12
- package/dist/TransactionController.cjs.map +1 -1
- package/dist/TransactionController.d.cts +5 -3
- package/dist/TransactionController.d.cts.map +1 -1
- package/dist/TransactionController.d.mts +5 -3
- package/dist/TransactionController.d.mts.map +1 -1
- package/dist/TransactionController.mjs +27 -12
- package/dist/TransactionController.mjs.map +1 -1
- package/dist/api/accounts-api.cjs.map +1 -1
- package/dist/api/accounts-api.d.cts +1 -1
- package/dist/api/accounts-api.d.cts.map +1 -1
- package/dist/api/accounts-api.d.mts +1 -1
- package/dist/api/accounts-api.d.mts.map +1 -1
- package/dist/api/accounts-api.mjs.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.cjs +39 -105
- package/dist/helpers/AccountsApiRemoteTransactionSource.cjs.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.cts.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.mts.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.mjs +39 -105
- package/dist/helpers/AccountsApiRemoteTransactionSource.mjs.map +1 -1
- package/dist/helpers/GasFeePoller.cjs +60 -4
- package/dist/helpers/GasFeePoller.cjs.map +1 -1
- package/dist/helpers/GasFeePoller.d.cts +4 -2
- package/dist/helpers/GasFeePoller.d.cts.map +1 -1
- package/dist/helpers/GasFeePoller.d.mts +4 -2
- package/dist/helpers/GasFeePoller.d.mts.map +1 -1
- package/dist/helpers/GasFeePoller.mjs +60 -4
- package/dist/helpers/GasFeePoller.mjs.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.cjs +7 -18
- package/dist/helpers/IncomingTransactionHelper.cjs.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.cts +6 -4
- package/dist/helpers/IncomingTransactionHelper.d.cts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.mts +6 -4
- package/dist/helpers/IncomingTransactionHelper.d.mts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.mjs +7 -18
- package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +2 -13
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +2 -13
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/batch.cjs +17 -7
- package/dist/utils/batch.cjs.map +1 -1
- package/dist/utils/batch.d.cts.map +1 -1
- package/dist/utils/batch.d.mts.map +1 -1
- package/dist/utils/batch.mjs +18 -8
- package/dist/utils/batch.mjs.map +1 -1
- package/dist/utils/gas.cjs +46 -7
- package/dist/utils/gas.cjs.map +1 -1
- package/dist/utils/gas.d.cts.map +1 -1
- package/dist/utils/gas.d.mts.map +1 -1
- package/dist/utils/gas.mjs +46 -7
- package/dist/utils/gas.mjs.map +1 -1
- package/dist/utils/transaction-type.cjs +23 -27
- package/dist/utils/transaction-type.cjs.map +1 -1
- package/dist/utils/transaction-type.d.cts +1 -1
- package/dist/utils/transaction-type.d.cts.map +1 -1
- package/dist/utils/transaction-type.d.mts +1 -1
- package/dist/utils/transaction-type.d.mts.map +1 -1
- package/dist/utils/transaction-type.mjs +23 -27
- package/dist/utils/transaction-type.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@ import type { NetworkClientId, Provider } from "@metamask/network-controller";
|
|
|
4
4
|
import type { Hex } from "@metamask/utils";
|
|
5
5
|
import EventEmitter from "events";
|
|
6
6
|
import type { TransactionControllerMessenger } from "../TransactionController.mjs";
|
|
7
|
-
import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta } from "../types.mjs";
|
|
7
|
+
import type { GasFeeEstimates, GasFeeFlow, Layer1GasFeeFlow, TransactionMeta, TransactionBatchMeta } from "../types.mjs";
|
|
8
8
|
import { GasFeeEstimateLevel } from "../types.mjs";
|
|
9
9
|
/**
|
|
10
10
|
* Automatically polls and updates suggested gas fees on unapproved transactions.
|
|
@@ -21,16 +21,18 @@ export declare class GasFeePoller {
|
|
|
21
21
|
* @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.
|
|
22
22
|
* @param options.getProvider - Callback to obtain a provider instance.
|
|
23
23
|
* @param options.getTransactions - Callback to obtain the transaction data.
|
|
24
|
+
* @param options.getTransactionBatches - Callback to obtain the transaction batch data.
|
|
24
25
|
* @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.
|
|
25
26
|
* @param options.messenger - The TransactionControllerMessenger instance.
|
|
26
27
|
* @param options.onStateChange - Callback to register a listener for controller state changes.
|
|
27
28
|
*/
|
|
28
|
-
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, messenger, onStateChange, }: {
|
|
29
|
+
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, getTransactionBatches, layer1GasFeeFlows, messenger, onStateChange, }: {
|
|
29
30
|
findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;
|
|
30
31
|
gasFeeFlows: GasFeeFlow[];
|
|
31
32
|
getGasFeeControllerEstimates: (options: FetchGasFeeEstimateOptions) => Promise<GasFeeState>;
|
|
32
33
|
getProvider: (networkClientId: NetworkClientId) => Provider;
|
|
33
34
|
getTransactions: () => TransactionMeta[];
|
|
35
|
+
getTransactionBatches: () => TransactionBatchMeta[];
|
|
34
36
|
layer1GasFeeFlows: Layer1GasFeeFlow[];
|
|
35
37
|
messenger: TransactionControllerMessenger;
|
|
36
38
|
onStateChange: (listener: () => void) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GasFeePoller.d.mts","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,E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AAgB,EAAE,CAAC;QACtC,SAAS,EAAE,8BAA8B,CAAC;QAC1C,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;KAC/C;CA2TF;AAED;;;;;;;;;GASG;AACH,wBAAgB,8BAA8B,CAAC,EAC7C,eAAe,EACf,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,EACZ,MAAM,GACP,EAAE;IACD,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8BAA8B,EAAE,CAAC,eAAe,EAAE,eAAe,KAAK,OAAO,CAAC;IAC9E,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,MAAM,EAAE,eAAe,CAAC;CACzB,GAAG,IAAI,CAiCP;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,EAC5C,MAAM,EACN,YAAY,GACb,EAAE;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,YAAY,EAAE,mBAAmB,CAAC;CACnC,GAAG,IAAI,CAgBP"}
|
|
@@ -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_messenger, _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_getTransactionBatches, _GasFeePoller_layer1GasFeeFlows, _GasFeePoller_messenger, _GasFeePoller_timeout, _GasFeePoller_running, _GasFeePoller_start, _GasFeePoller_stop, _GasFeePoller_onTimeout, _GasFeePoller_updateUnapprovedTransactions, _GasFeePoller_updateUnapprovedTransactionBatches, _GasFeePoller_updateUnapprovedTransaction, _GasFeePoller_updateUnapprovedTransactionBatch, _GasFeePoller_updateTransactionGasFeeEstimates, _GasFeePoller_updateTransactionLayer1GasFee, _GasFeePoller_getUnapprovedTransactions, _GasFeePoller_getUnapprovedTransactionBatches, _GasFeePoller_getGasFeeControllerData;
|
|
13
13
|
function $importDefault(module) {
|
|
14
14
|
if (module?.__esModule) {
|
|
15
15
|
return module.default;
|
|
@@ -22,6 +22,7 @@ import { createModuleLogger } from "@metamask/utils";
|
|
|
22
22
|
// This package purposefully relies on Node's EventEmitter module.
|
|
23
23
|
// eslint-disable-next-line import-x/no-nodejs-modules
|
|
24
24
|
import EventEmitter from "events";
|
|
25
|
+
import { DefaultGasFeeFlow } from "../gas-flows/DefaultGasFeeFlow.mjs";
|
|
25
26
|
import { projectLogger } from "../logger.mjs";
|
|
26
27
|
import { GasFeeEstimateLevel, GasFeeEstimateType, TransactionStatus, TransactionEnvelopeType } from "../types.mjs";
|
|
27
28
|
import { getGasFeeFlow } from "../utils/gas-flow.mjs";
|
|
@@ -41,11 +42,12 @@ export class GasFeePoller {
|
|
|
41
42
|
* @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.
|
|
42
43
|
* @param options.getProvider - Callback to obtain a provider instance.
|
|
43
44
|
* @param options.getTransactions - Callback to obtain the transaction data.
|
|
45
|
+
* @param options.getTransactionBatches - Callback to obtain the transaction batch data.
|
|
44
46
|
* @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.
|
|
45
47
|
* @param options.messenger - The TransactionControllerMessenger instance.
|
|
46
48
|
* @param options.onStateChange - Callback to register a listener for controller state changes.
|
|
47
49
|
*/
|
|
48
|
-
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, layer1GasFeeFlows, messenger, onStateChange, }) {
|
|
50
|
+
constructor({ findNetworkClientIdByChainId, gasFeeFlows, getGasFeeControllerEstimates, getProvider, getTransactions, getTransactionBatches, layer1GasFeeFlows, messenger, onStateChange, }) {
|
|
49
51
|
_GasFeePoller_instances.add(this);
|
|
50
52
|
this.hub = new EventEmitter();
|
|
51
53
|
_GasFeePoller_findNetworkClientIdByChainId.set(this, void 0);
|
|
@@ -53,6 +55,7 @@ export class GasFeePoller {
|
|
|
53
55
|
_GasFeePoller_getGasFeeControllerEstimates.set(this, void 0);
|
|
54
56
|
_GasFeePoller_getProvider.set(this, void 0);
|
|
55
57
|
_GasFeePoller_getTransactions.set(this, void 0);
|
|
58
|
+
_GasFeePoller_getTransactionBatches.set(this, void 0);
|
|
56
59
|
_GasFeePoller_layer1GasFeeFlows.set(this, void 0);
|
|
57
60
|
_GasFeePoller_messenger.set(this, void 0);
|
|
58
61
|
_GasFeePoller_timeout.set(this, void 0);
|
|
@@ -63,10 +66,13 @@ export class GasFeePoller {
|
|
|
63
66
|
__classPrivateFieldSet(this, _GasFeePoller_getGasFeeControllerEstimates, getGasFeeControllerEstimates, "f");
|
|
64
67
|
__classPrivateFieldSet(this, _GasFeePoller_getProvider, getProvider, "f");
|
|
65
68
|
__classPrivateFieldSet(this, _GasFeePoller_getTransactions, getTransactions, "f");
|
|
69
|
+
__classPrivateFieldSet(this, _GasFeePoller_getTransactionBatches, getTransactionBatches, "f");
|
|
66
70
|
__classPrivateFieldSet(this, _GasFeePoller_messenger, messenger, "f");
|
|
67
71
|
onStateChange(() => {
|
|
68
72
|
const unapprovedTransactions = __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_getUnapprovedTransactions).call(this);
|
|
69
|
-
|
|
73
|
+
const unapprovedTransactionBatches = __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_getUnapprovedTransactionBatches).call(this);
|
|
74
|
+
if (unapprovedTransactions.length ||
|
|
75
|
+
unapprovedTransactionBatches.length) {
|
|
70
76
|
__classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_start).call(this);
|
|
71
77
|
}
|
|
72
78
|
else {
|
|
@@ -75,7 +81,7 @@ export class GasFeePoller {
|
|
|
75
81
|
});
|
|
76
82
|
}
|
|
77
83
|
}
|
|
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() {
|
|
84
|
+
_GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFeeFlows = new WeakMap(), _GasFeePoller_getGasFeeControllerEstimates = new WeakMap(), _GasFeePoller_getProvider = new WeakMap(), _GasFeePoller_getTransactions = new WeakMap(), _GasFeePoller_getTransactionBatches = 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() {
|
|
79
85
|
if (__classPrivateFieldGet(this, _GasFeePoller_running, "f")) {
|
|
80
86
|
return;
|
|
81
87
|
}
|
|
@@ -94,6 +100,7 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
94
100
|
log('Stopped polling');
|
|
95
101
|
}, _GasFeePoller_onTimeout = async function _GasFeePoller_onTimeout() {
|
|
96
102
|
await __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_updateUnapprovedTransactions).call(this);
|
|
103
|
+
await __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_updateUnapprovedTransactionBatches).call(this);
|
|
97
104
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
98
105
|
__classPrivateFieldSet(this, _GasFeePoller_timeout, setTimeout(() => __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_onTimeout).call(this), INTERVAL_MILLISECONDS), "f");
|
|
99
106
|
}, _GasFeePoller_updateUnapprovedTransactions = async function _GasFeePoller_updateUnapprovedTransactions() {
|
|
@@ -109,6 +116,19 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
109
116
|
const gasFeeControllerData = gasFeeControllerDataByChainId.get(chainId);
|
|
110
117
|
return __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_updateUnapprovedTransaction).call(this, tx, gasFeeControllerData);
|
|
111
118
|
}));
|
|
119
|
+
}, _GasFeePoller_updateUnapprovedTransactionBatches = async function _GasFeePoller_updateUnapprovedTransactionBatches() {
|
|
120
|
+
const unapprovedTransactionBatches = __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_getUnapprovedTransactionBatches).call(this);
|
|
121
|
+
if (!unapprovedTransactionBatches.length) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
log('Found unapproved transaction batches', unapprovedTransactionBatches.length);
|
|
125
|
+
const gasFeeControllerDataByChainId = await __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_getGasFeeControllerData).call(this, unapprovedTransactionBatches);
|
|
126
|
+
log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);
|
|
127
|
+
await Promise.all(unapprovedTransactionBatches.flatMap((txBatch) => {
|
|
128
|
+
const { chainId } = txBatch;
|
|
129
|
+
const gasFeeControllerData = gasFeeControllerDataByChainId.get(chainId);
|
|
130
|
+
return __classPrivateFieldGet(this, _GasFeePoller_instances, "m", _GasFeePoller_updateUnapprovedTransactionBatch).call(this, txBatch, gasFeeControllerData);
|
|
131
|
+
}));
|
|
112
132
|
}, _GasFeePoller_updateUnapprovedTransaction = async function _GasFeePoller_updateUnapprovedTransaction(transactionMeta, gasFeeControllerData) {
|
|
113
133
|
const { id } = transactionMeta;
|
|
114
134
|
const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([
|
|
@@ -124,6 +144,40 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
124
144
|
gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,
|
|
125
145
|
layer1GasFee,
|
|
126
146
|
});
|
|
147
|
+
}, _GasFeePoller_updateUnapprovedTransactionBatch = async function _GasFeePoller_updateUnapprovedTransactionBatch(txBatchMeta, gasFeeControllerData) {
|
|
148
|
+
const { id } = txBatchMeta;
|
|
149
|
+
const ethQuery = new EthQuery(__classPrivateFieldGet(this, _GasFeePoller_getProvider, "f").call(this, txBatchMeta.networkClientId));
|
|
150
|
+
const defaultGasFeeFlow = new DefaultGasFeeFlow();
|
|
151
|
+
const request = {
|
|
152
|
+
ethQuery,
|
|
153
|
+
gasFeeControllerData,
|
|
154
|
+
messenger: __classPrivateFieldGet(this, _GasFeePoller_messenger, "f"),
|
|
155
|
+
transactionMeta: {
|
|
156
|
+
...txBatchMeta,
|
|
157
|
+
txParams: {
|
|
158
|
+
...txBatchMeta.transactions?.[0],
|
|
159
|
+
from: txBatchMeta.from,
|
|
160
|
+
gas: txBatchMeta.gas,
|
|
161
|
+
},
|
|
162
|
+
time: Date.now(),
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
let gasFeeEstimates;
|
|
166
|
+
try {
|
|
167
|
+
const response = await defaultGasFeeFlow.getGasFees(request);
|
|
168
|
+
gasFeeEstimates = response.estimates;
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
log('Failed to get gas fees for batch', txBatchMeta.id, error);
|
|
172
|
+
}
|
|
173
|
+
if (!gasFeeEstimates) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
this.hub.emit('transaction-batch-updated', {
|
|
177
|
+
transactionBatchId: id,
|
|
178
|
+
gasFeeEstimates,
|
|
179
|
+
gasFeeEstimatesLoaded: true,
|
|
180
|
+
});
|
|
127
181
|
}, _GasFeePoller_updateTransactionGasFeeEstimates = async function _GasFeePoller_updateTransactionGasFeeEstimates(transactionMeta, gasFeeControllerData) {
|
|
128
182
|
const { networkClientId } = transactionMeta;
|
|
129
183
|
const ethQuery = new EthQuery(__classPrivateFieldGet(this, _GasFeePoller_getProvider, "f").call(this, networkClientId));
|
|
@@ -170,6 +224,8 @@ _GasFeePoller_findNetworkClientIdByChainId = new WeakMap(), _GasFeePoller_gasFee
|
|
|
170
224
|
return layer1GasFee;
|
|
171
225
|
}, _GasFeePoller_getUnapprovedTransactions = function _GasFeePoller_getUnapprovedTransactions() {
|
|
172
226
|
return __classPrivateFieldGet(this, _GasFeePoller_getTransactions, "f").call(this).filter((tx) => tx.status === TransactionStatus.unapproved);
|
|
227
|
+
}, _GasFeePoller_getUnapprovedTransactionBatches = function _GasFeePoller_getUnapprovedTransactionBatches() {
|
|
228
|
+
return __classPrivateFieldGet(this, _GasFeePoller_getTransactionBatches, "f").call(this).filter((batch) => batch.status === TransactionStatus.unapproved);
|
|
173
229
|
}, _GasFeePoller_getGasFeeControllerData = async function _GasFeePoller_getGasFeeControllerData(transactions) {
|
|
174
230
|
const networkClientIdsByChainId = new Map();
|
|
175
231
|
for (const transaction of transactions) {
|
|
@@ -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;AAa1C,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;IAyBvB;;;;;;;;;;;;OAYG;IACH,YAAY,EACV,4BAA4B,EAC5B,WAAW,EACX,4BAA4B,EAC5B,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,SAAS,EACT,aAAa,GAYd;;QAzDD,QAAG,GAAiB,IAAI,YAAY,EAAE,CAAC;QAE9B,6DAEwB;QAExB,4CAA2B;QAE3B,6DAEiB;QAEjB,4CAA6D;QAE7D,gDAA0C;QAE1C,kDAAuC;QAEvC,0CAA2C;QAEpD,wCAAoD;QAEpD,gCAAW,KAAK,EAAC;QAoCf,uBAAA,IAAI,8CAAiC,4BAA4B,MAAA,CAAC;QAClE,uBAAA,IAAI,6BAAgB,WAAW,MAAA,CAAC;QAChC,uBAAA,IAAI,mCAAsB,iBAAiB,MAAA,CAAC;QAC5C,uBAAA,IAAI,8CAAiC,4BAA4B,MAAA,CAAC;QAClE,uBAAA,IAAI,6BAAgB,WAAW,MAAA,CAAC;QAChC,uBAAA,IAAI,iCAAoB,eAA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uBAAuB,GAC3B,eAA0C,CAAC;YAC7C,QAAQ,CAAC,QAAQ,GAAG,uBAAuB,CAAC,QAAQ,CAAC;SACtD;QAED,IAAI,eAAe,KAAK,kBAAkB,CAAC,MAAM,EAAE;YACjD,MAAM,qBAAqB,GAAG,eAAwC,CAAC;YACvE,QAAQ,CAAC,QAAQ,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;SACzD;QAED,8DAA8D;QAC9D,OAAO,QAAQ,CAAC,YAAY,CAAC;QAC7B,OAAO,QAAQ,CAAC,oBAAoB,CAAC;KACtC;AACH,CAAC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n TransactionParams,\n} from '../types';\nimport {\n GasFeeEstimateLevel,\n GasFeeEstimateType,\n TransactionStatus,\n TransactionEnvelopeType,\n} from '../types';\nimport { getGasFeeFlow } from '../utils/gas-flow';\nimport { getTransactionLayer1GasFee } from '../utils/layer1-gas-fee-flow';\n\nconst log = createModuleLogger(projectLogger, 'gas-fee-poller');\n\nconst INTERVAL_MILLISECONDS = 10000;\n\n/**\n * Automatically polls and updates suggested gas fees on unapproved transactions.\n */\nexport class GasFeePoller {\n hub: EventEmitter = new EventEmitter();\n\n readonly #findNetworkClientIdByChainId: (\n chainId: Hex,\n ) => NetworkClientId | undefined;\n\n readonly #gasFeeFlows: GasFeeFlow[];\n\n readonly #getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n\n readonly #getProvider: (networkClientId: NetworkClientId) => Provider;\n\n readonly #getTransactions: () => TransactionMeta[];\n\n readonly #layer1GasFeeFlows: Layer1GasFeeFlow[];\n\n readonly #messenger: TransactionControllerMessenger;\n\n #timeout: ReturnType<typeof setTimeout> | undefined;\n\n #running = false;\n\n /**\n * Constructs a new instance of the GasFeePoller.\n *\n * @param options - The options for this instance.\n * @param options.findNetworkClientIdByChainId - Callback to find the network client ID by chain ID.\n * @param options.gasFeeFlows - The gas fee flows to use to obtain suitable gas fees.\n * @param options.getGasFeeControllerEstimates - Callback to obtain the default fee estimates.\n * @param options.getProvider - Callback to obtain a provider instance.\n * @param options.getTransactions - Callback to obtain the transaction data.\n * @param options.layer1GasFeeFlows - The layer 1 gas fee flows to use to obtain suitable layer 1 gas fees.\n * @param options.messenger - The TransactionControllerMessenger instance.\n * @param options.onStateChange - Callback to register a listener for controller state changes.\n */\n constructor({\n findNetworkClientIdByChainId,\n gasFeeFlows,\n getGasFeeControllerEstimates,\n getProvider,\n getTransactions,\n layer1GasFeeFlows,\n messenger,\n onStateChange,\n }: {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeControllerEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getProvider: (networkClientId: NetworkClientId) => Provider;\n getTransactions: () => TransactionMeta[];\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n messenger: TransactionControllerMessenger;\n onStateChange: (listener: () => void) => void;\n }) {\n this.#findNetworkClientIdByChainId = findNetworkClientIdByChainId;\n this.#gasFeeFlows = gasFeeFlows;\n this.#layer1GasFeeFlows = layer1GasFeeFlows;\n this.#getGasFeeControllerEstimates = getGasFeeControllerEstimates;\n this.#getProvider = getProvider;\n this.#getTransactions = getTransactions;\n this.#messenger = messenger;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (unapprovedTransactions.length) {\n this.#start();\n } else {\n this.#stop();\n }\n });\n }\n\n #start() {\n if (this.#running) {\n return;\n }\n\n // Intentionally not awaiting since this starts the timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#onTimeout();\n\n this.#running = true;\n\n log('Started polling');\n }\n\n #stop() {\n if (!this.#running) {\n return;\n }\n\n clearTimeout(this.#timeout);\n\n this.#timeout = undefined;\n this.#running = false;\n\n log('Stopped polling');\n }\n\n async #onTimeout() {\n await this.#updateUnapprovedTransactions();\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeout = setTimeout(() => this.#onTimeout(), INTERVAL_MILLISECONDS);\n }\n\n async #updateUnapprovedTransactions() {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n\n if (!unapprovedTransactions.length) {\n return;\n }\n\n log('Found unapproved transactions', unapprovedTransactions.length);\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactions,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactions.flatMap((tx) => {\n const { chainId } = tx;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransaction(tx, gasFeeControllerData);\n }),\n );\n }\n\n async #updateUnapprovedTransaction(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = transactionMeta;\n\n const [gasFeeEstimatesResponse, layer1GasFee] = await Promise.all([\n this.#updateTransactionGasFeeEstimates(\n transactionMeta,\n gasFeeControllerData,\n ),\n this.#updateTransactionLayer1GasFee(transactionMeta),\n ]);\n\n if (!gasFeeEstimatesResponse && !layer1GasFee) {\n return;\n }\n\n this.hub.emit('transaction-updated', {\n transactionId: id,\n gasFeeEstimates: gasFeeEstimatesResponse?.gasFeeEstimates,\n gasFeeEstimatesLoaded: gasFeeEstimatesResponse?.gasFeeEstimatesLoaded,\n layer1GasFee,\n });\n }\n\n async #updateTransactionGasFeeEstimates(\n transactionMeta: TransactionMeta,\n gasFeeControllerData: GasFeeState,\n ): Promise<\n | { gasFeeEstimates?: GasFeeEstimates; gasFeeEstimatesLoaded: boolean }\n | undefined\n > {\n const { networkClientId } = transactionMeta;\n\n const ethQuery = new EthQuery(this.#getProvider(networkClientId));\n const gasFeeFlow = getGasFeeFlow(\n transactionMeta,\n this.#gasFeeFlows,\n this.#messenger,\n );\n\n if (gasFeeFlow) {\n log(\n 'Found gas fee flow',\n gasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n }\n\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n messenger: this.#messenger,\n transactionMeta,\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n if (gasFeeFlow) {\n try {\n const response = await gasFeeFlow.getGasFees(request);\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get suggested gas fees', transactionMeta.id, error);\n }\n }\n\n if (!gasFeeEstimates && transactionMeta.gasFeeEstimatesLoaded) {\n return undefined;\n }\n\n log('Updated gas fee estimates', {\n gasFeeEstimates,\n transaction: transactionMeta.id,\n });\n\n return { gasFeeEstimates, gasFeeEstimatesLoaded: true };\n }\n\n async #updateTransactionLayer1GasFee(\n transactionMeta: TransactionMeta,\n ): Promise<Hex | undefined> {\n const { networkClientId } = transactionMeta;\n const provider = this.#getProvider(networkClientId);\n\n const layer1GasFee = await getTransactionLayer1GasFee({\n layer1GasFeeFlows: this.#layer1GasFeeFlows,\n messenger: this.#messenger,\n provider,\n transactionMeta,\n });\n\n if (layer1GasFee) {\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n }\n\n return layer1GasFee;\n }\n\n #getUnapprovedTransactions() {\n return this.#getTransactions().filter(\n (tx) => tx.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n\n/**\n * Updates gas properties for transaction.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.gasFeeEstimates - The gas fee estimates.\n * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.\n * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport function updateTransactionGasProperties({\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n txMeta,\n}: {\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;\n layer1GasFee?: Hex;\n txMeta: TransactionMeta;\n}): void {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const shouldUpdateTxParamsGasFees = isTxParamsGasFeeUpdatesEnabled(txMeta);\n\n if (\n shouldUpdateTxParamsGasFees &&\n isUsingGasFeeEstimateLevel &&\n gasFeeEstimates\n ) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txMeta.txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\n );\n }\n\n if (gasFeeEstimates) {\n txMeta.gasFeeEstimates = gasFeeEstimates;\n }\n\n if (gasFeeEstimatesLoaded !== undefined) {\n txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;\n }\n\n if (layer1GasFee) {\n txMeta.layer1GasFee = layer1GasFee;\n }\n}\n\n/**\n * Updates `txParams` gas values accordingly with given `userFeeLevel` from `txMeta.gasFeeEstimates`.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.userFeeLevel - The user fee level.\n */\nexport function updateTransactionGasEstimates({\n txMeta,\n userFeeLevel,\n}: {\n txMeta: TransactionMeta;\n userFeeLevel: GasFeeEstimateLevel;\n}): void {\n const { txParams, gasFeeEstimates } = txMeta;\n\n if (!gasFeeEstimates) {\n return;\n }\n\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\n );\n}\n\n/**\n * Updates gas fee parameters based on transaction type and gas estimate type\n *\n * @param txParams - The transaction parameters to update\n * @param gasFeeEstimates - The gas fee estimates\n * @param userFeeLevel - The user fee level\n * @param isEIP1559Compatible - Whether the transaction is EIP-1559 compatible\n */\nfunction updateGasFeeParameters(\n txParams: TransactionParams,\n gasFeeEstimates: GasFeeEstimates,\n userFeeLevel: GasFeeEstimateLevel,\n isEIP1559Compatible: boolean,\n): void {\n const { type: gasEstimateType } = gasFeeEstimates;\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n txParams.maxFeePerGas = gasPrice;\n txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.gasPrice = feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txParams.maxFeePerGas;\n delete txParams.maxPriorityFeePerGas;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"GasFeePoller.mjs","sourceRoot":"","sources":["../../src/helpers/GasFeePoller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,SAAQ,4BAA4B;;AAO3C,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AACrD,kEAAkE;AAClE,sDAAsD;AACtD,OAA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B,GAC3B,eAA0C,CAAC;YAC7C,QAAQ,CAAC,YAAY,GAAG,uBAAuB,CAAC,QAAQ,CAAC;YACzD,QAAQ,CAAC,oBAAoB,GAAG,uBAAuB,CAAC,QAAQ,CAAC;SAClE;QAED,IAAI,eAAe,KAAK,kBAAkB,CAAC,MAAM,EAAE;YACjD,MAAM,qBAAqB,GAAG,eAAwC,CAAC;YACvE,MAAM,QAAQ,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;YACrD,QAAQ,CAAC,YAAY,GAAG,QAAQ,CAAC;YACjC,QAAQ,CAAC,oBAAoB,GAAG,QAAQ,CAAC;SAC1C;QAED,4CAA4C;QAC5C,OAAO,QAAQ,CAAC,QAAQ,CAAC;KAC1B;SAAM;QACL,mCAAmC;QACnC,IAAI,eAAe,KAAK,kBAAkB,CAAC,SAAS,EAAE;YACpD,MAAM,wBAAwB,GAC5B,eAA2C,CAAC;YAC9C,QAAQ,CAAC,QAAQ,GAAG,wBAAwB,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC;SAC1E;QAED,IAAI,eAAe,KAAK,kBAAkB,CAAC,QAAQ,EAAE;YACnD,MAAM,uBAAuB,GAC3B,eAA0C,CAAC;YAC7C,QAAQ,CAAC,QAAQ,GAAG,uBAAuB,CAAC,QAAQ,CAAC;SACtD;QAED,IAAI,eAAe,KAAK,kBAAkB,CAAC,MAAM,EAAE;YACjD,MAAM,qBAAqB,GAAG,eAAwC,CAAC;YACvE,QAAQ,CAAC,QAAQ,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;SACzD;QAED,8DAA8D;QAC9D,OAAO,QAAQ,CAAC,YAAY,CAAC;QAC7B,OAAO,QAAQ,CAAC,oBAAoB,CAAC;KACtC;AACH,CAAC","sourcesContent":["import EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { NetworkClientId, Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport { DefaultGasFeeFlow } from '../gas-flows/DefaultGasFeeFlow';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n GasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasPriceGasFeeEstimates,\n FeeMarketGasFeeEstimates,\n Layer1GasFeeFlow,\n LegacyGasFeeEstimates,\n TransactionMeta,\n TransactionParams,\n TransactionBatchMeta,\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 #getTransactionBatches: () => TransactionBatchMeta[];\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.getTransactionBatches - Callback to obtain the transaction batch 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 getTransactionBatches,\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 getTransactionBatches: () => TransactionBatchMeta[];\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.#getTransactionBatches = getTransactionBatches;\n this.#messenger = messenger;\n\n onStateChange(() => {\n const unapprovedTransactions = this.#getUnapprovedTransactions();\n const unapprovedTransactionBatches =\n this.#getUnapprovedTransactionBatches();\n\n if (\n unapprovedTransactions.length ||\n unapprovedTransactionBatches.length\n ) {\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 await this.#updateUnapprovedTransactionBatches();\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 #updateUnapprovedTransactionBatches() {\n const unapprovedTransactionBatches =\n this.#getUnapprovedTransactionBatches();\n\n if (!unapprovedTransactionBatches.length) {\n return;\n }\n\n log(\n 'Found unapproved transaction batches',\n unapprovedTransactionBatches.length,\n );\n\n const gasFeeControllerDataByChainId = await this.#getGasFeeControllerData(\n unapprovedTransactionBatches,\n );\n\n log('Retrieved gas fee controller data', gasFeeControllerDataByChainId);\n\n await Promise.all(\n unapprovedTransactionBatches.flatMap((txBatch) => {\n const { chainId } = txBatch;\n\n const gasFeeControllerData = gasFeeControllerDataByChainId.get(\n chainId,\n ) as GasFeeState;\n\n return this.#updateUnapprovedTransactionBatch(\n txBatch,\n gasFeeControllerData,\n );\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 #updateUnapprovedTransactionBatch(\n txBatchMeta: TransactionBatchMeta,\n gasFeeControllerData: GasFeeState,\n ) {\n const { id } = txBatchMeta;\n\n const ethQuery = new EthQuery(\n this.#getProvider(txBatchMeta.networkClientId),\n );\n const defaultGasFeeFlow = new DefaultGasFeeFlow();\n const request: GasFeeFlowRequest = {\n ethQuery,\n gasFeeControllerData,\n messenger: this.#messenger,\n transactionMeta: {\n ...txBatchMeta,\n txParams: {\n ...txBatchMeta.transactions?.[0],\n from: txBatchMeta.from,\n gas: txBatchMeta.gas,\n },\n time: Date.now(),\n },\n };\n\n let gasFeeEstimates: GasFeeEstimates | undefined;\n\n try {\n const response = await defaultGasFeeFlow.getGasFees(request);\n\n gasFeeEstimates = response.estimates;\n } catch (error) {\n log('Failed to get gas fees for batch', txBatchMeta.id, error);\n }\n\n if (!gasFeeEstimates) {\n return;\n }\n\n this.hub.emit('transaction-batch-updated', {\n transactionBatchId: id,\n gasFeeEstimates,\n gasFeeEstimatesLoaded: true,\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 #getUnapprovedTransactionBatches() {\n return this.#getTransactionBatches().filter(\n (batch) => batch.status === TransactionStatus.unapproved,\n );\n }\n\n async #getGasFeeControllerData(\n transactions: TransactionMeta[] | TransactionBatchMeta[],\n ): Promise<Map<string, GasFeeState>> {\n const networkClientIdsByChainId = new Map<Hex, NetworkClientId>();\n\n for (const transaction of transactions) {\n const { chainId, networkClientId: transactionNetworkClientId } =\n transaction;\n\n if (networkClientIdsByChainId.has(chainId)) {\n continue;\n }\n\n const networkClientId =\n transactionNetworkClientId ??\n (this.#findNetworkClientIdByChainId(chainId) as string);\n\n networkClientIdsByChainId.set(chainId, networkClientId);\n }\n\n log('Extracted network client IDs by chain ID', networkClientIdsByChainId);\n\n const entryPromises = Array.from(networkClientIdsByChainId.entries()).map(\n async ([chainId, networkClientId]) => {\n return [\n chainId,\n await this.#getGasFeeControllerEstimates({ networkClientId }),\n ] as const;\n },\n );\n\n return new Map(await Promise.all(entryPromises));\n }\n}\n\n/**\n * Updates gas properties for transaction.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.gasFeeEstimates - The gas fee estimates.\n * @param args.gasFeeEstimatesLoaded - Whether the gas fee estimates are loaded.\n * @param args.isTxParamsGasFeeUpdatesEnabled - Whether to update the gas fee properties in `txParams`.\n * @param args.layer1GasFee - The layer 1 gas fee.\n */\nexport function updateTransactionGasProperties({\n gasFeeEstimates,\n gasFeeEstimatesLoaded,\n isTxParamsGasFeeUpdatesEnabled,\n layer1GasFee,\n txMeta,\n}: {\n gasFeeEstimates?: GasFeeEstimates;\n gasFeeEstimatesLoaded?: boolean;\n isTxParamsGasFeeUpdatesEnabled: (transactionMeta: TransactionMeta) => boolean;\n layer1GasFee?: Hex;\n txMeta: TransactionMeta;\n}): void {\n const userFeeLevel = txMeta.userFeeLevel as GasFeeEstimateLevel;\n const isUsingGasFeeEstimateLevel =\n Object.values(GasFeeEstimateLevel).includes(userFeeLevel);\n const shouldUpdateTxParamsGasFees = isTxParamsGasFeeUpdatesEnabled(txMeta);\n\n if (\n shouldUpdateTxParamsGasFees &&\n isUsingGasFeeEstimateLevel &&\n gasFeeEstimates\n ) {\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txMeta.txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\n );\n }\n\n if (gasFeeEstimates) {\n txMeta.gasFeeEstimates = gasFeeEstimates;\n }\n\n if (gasFeeEstimatesLoaded !== undefined) {\n txMeta.gasFeeEstimatesLoaded = gasFeeEstimatesLoaded;\n }\n\n if (layer1GasFee) {\n txMeta.layer1GasFee = layer1GasFee;\n }\n}\n\n/**\n * Updates `txParams` gas values accordingly with given `userFeeLevel` from `txMeta.gasFeeEstimates`.\n *\n * @param args - Argument bag.\n * @param args.txMeta - The transaction meta.\n * @param args.userFeeLevel - The user fee level.\n */\nexport function updateTransactionGasEstimates({\n txMeta,\n userFeeLevel,\n}: {\n txMeta: TransactionMeta;\n userFeeLevel: GasFeeEstimateLevel;\n}): void {\n const { txParams, gasFeeEstimates } = txMeta;\n\n if (!gasFeeEstimates) {\n return;\n }\n\n const isEIP1559Compatible =\n txMeta.txParams.type !== TransactionEnvelopeType.legacy;\n\n updateGasFeeParameters(\n txParams,\n gasFeeEstimates,\n userFeeLevel,\n isEIP1559Compatible,\n );\n}\n\n/**\n * Updates gas fee parameters based on transaction type and gas estimate type\n *\n * @param txParams - The transaction parameters to update\n * @param gasFeeEstimates - The gas fee estimates\n * @param userFeeLevel - The user fee level\n * @param isEIP1559Compatible - Whether the transaction is EIP-1559 compatible\n */\nfunction updateGasFeeParameters(\n txParams: TransactionParams,\n gasFeeEstimates: GasFeeEstimates,\n userFeeLevel: GasFeeEstimateLevel,\n isEIP1559Compatible: boolean,\n): void {\n const { type: gasEstimateType } = gasFeeEstimates;\n\n if (isEIP1559Compatible) {\n // Handle EIP-1559 compatible transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.maxFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n txParams.maxPriorityFeePerGas =\n feeMarketGasFeeEstimates[userFeeLevel]?.maxPriorityFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.maxFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n txParams.maxPriorityFeePerGas = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n const gasPrice = legacyGasFeeEstimates[userFeeLevel];\n txParams.maxFeePerGas = gasPrice;\n txParams.maxPriorityFeePerGas = gasPrice;\n }\n\n // Remove gasPrice for EIP-1559 transactions\n delete txParams.gasPrice;\n } else {\n // Handle non-EIP-1559 transactions\n if (gasEstimateType === GasFeeEstimateType.FeeMarket) {\n const feeMarketGasFeeEstimates =\n gasFeeEstimates as FeeMarketGasFeeEstimates;\n txParams.gasPrice = feeMarketGasFeeEstimates[userFeeLevel]?.maxFeePerGas;\n }\n\n if (gasEstimateType === GasFeeEstimateType.GasPrice) {\n const gasPriceGasFeeEstimates =\n gasFeeEstimates as GasPriceGasFeeEstimates;\n txParams.gasPrice = gasPriceGasFeeEstimates.gasPrice;\n }\n\n if (gasEstimateType === GasFeeEstimateType.Legacy) {\n const legacyGasFeeEstimates = gasFeeEstimates as LegacyGasFeeEstimates;\n txParams.gasPrice = legacyGasFeeEstimates[userFeeLevel];\n }\n\n // Remove EIP-1559 specific parameters for legacy transactions\n delete txParams.maxFeePerGas;\n delete txParams.maxPriorityFeePerGas;\n }\n}\n"]}
|
|
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
|
-
var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_client,
|
|
16
|
+
var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_client, _IncomingTransactionHelper_getCurrentAccount, _IncomingTransactionHelper_getLocalTransactions, _IncomingTransactionHelper_includeTokenTransfers, _IncomingTransactionHelper_isEnabled, _IncomingTransactionHelper_isRunning, _IncomingTransactionHelper_messenger, _IncomingTransactionHelper_remoteTransactionSource, _IncomingTransactionHelper_timeoutId, _IncomingTransactionHelper_trimTransactions, _IncomingTransactionHelper_updateTransactions, _IncomingTransactionHelper_onInterval, _IncomingTransactionHelper_sortTransactionsByTime, _IncomingTransactionHelper_canStart, _IncomingTransactionHelper_getInterval, _IncomingTransactionHelper_getTags;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.IncomingTransactionHelper = void 0;
|
|
19
19
|
// This package purposefully relies on Node's EventEmitter module.
|
|
@@ -23,35 +23,29 @@ const logger_1 = require("../logger.cjs");
|
|
|
23
23
|
const feature_flags_1 = require("../utils/feature-flags.cjs");
|
|
24
24
|
const TAG_POLLING = 'automatic-polling';
|
|
25
25
|
class IncomingTransactionHelper {
|
|
26
|
-
constructor({ client,
|
|
26
|
+
constructor({ client, getCurrentAccount, getLocalTransactions, includeTokenTransfers, isEnabled, messenger, remoteTransactionSource, trimTransactions, updateTransactions, }) {
|
|
27
27
|
_IncomingTransactionHelper_instances.add(this);
|
|
28
28
|
_IncomingTransactionHelper_client.set(this, void 0);
|
|
29
|
-
_IncomingTransactionHelper_getCache.set(this, void 0);
|
|
30
29
|
_IncomingTransactionHelper_getCurrentAccount.set(this, void 0);
|
|
31
30
|
_IncomingTransactionHelper_getLocalTransactions.set(this, void 0);
|
|
32
31
|
_IncomingTransactionHelper_includeTokenTransfers.set(this, void 0);
|
|
33
32
|
_IncomingTransactionHelper_isEnabled.set(this, void 0);
|
|
34
33
|
_IncomingTransactionHelper_isRunning.set(this, void 0);
|
|
35
34
|
_IncomingTransactionHelper_messenger.set(this, void 0);
|
|
36
|
-
_IncomingTransactionHelper_queryEntireHistory.set(this, void 0);
|
|
37
35
|
_IncomingTransactionHelper_remoteTransactionSource.set(this, void 0);
|
|
38
36
|
_IncomingTransactionHelper_timeoutId.set(this, void 0);
|
|
39
37
|
_IncomingTransactionHelper_trimTransactions.set(this, void 0);
|
|
40
|
-
_IncomingTransactionHelper_updateCache.set(this, void 0);
|
|
41
38
|
_IncomingTransactionHelper_updateTransactions.set(this, void 0);
|
|
42
39
|
this.hub = new events_1.default();
|
|
43
40
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_client, client, "f");
|
|
44
|
-
__classPrivateFieldSet(this, _IncomingTransactionHelper_getCache, getCache, "f");
|
|
45
41
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_getCurrentAccount, getCurrentAccount, "f");
|
|
46
42
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_getLocalTransactions, getLocalTransactions, "f");
|
|
47
43
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_includeTokenTransfers, includeTokenTransfers, "f");
|
|
48
44
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_isEnabled, isEnabled ?? (() => true), "f");
|
|
49
45
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_isRunning, false, "f");
|
|
50
46
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_messenger, messenger, "f");
|
|
51
|
-
__classPrivateFieldSet(this, _IncomingTransactionHelper_queryEntireHistory, queryEntireHistory, "f");
|
|
52
47
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_remoteTransactionSource, remoteTransactionSource, "f");
|
|
53
48
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_trimTransactions, trimTransactions, "f");
|
|
54
|
-
__classPrivateFieldSet(this, _IncomingTransactionHelper_updateCache, updateCache, "f");
|
|
55
49
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_updateTransactions, updateTransactions, "f");
|
|
56
50
|
}
|
|
57
51
|
start() {
|
|
@@ -62,11 +56,11 @@ class IncomingTransactionHelper {
|
|
|
62
56
|
return;
|
|
63
57
|
}
|
|
64
58
|
const interval = __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_getInterval).call(this);
|
|
65
|
-
(0, logger_1.incomingTransactionsLogger)('
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
67
|
-
__classPrivateFieldSet(this, _IncomingTransactionHelper_timeoutId, setTimeout(() => __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onInterval).call(this), interval), "f");
|
|
59
|
+
(0, logger_1.incomingTransactionsLogger)('Started polling', { interval });
|
|
68
60
|
__classPrivateFieldSet(this, _IncomingTransactionHelper_isRunning, true, "f");
|
|
69
|
-
(
|
|
61
|
+
__classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onInterval).call(this).catch((error) => {
|
|
62
|
+
(0, logger_1.incomingTransactionsLogger)('Initial polling failed', error);
|
|
63
|
+
});
|
|
70
64
|
}
|
|
71
65
|
stop() {
|
|
72
66
|
if (__classPrivateFieldGet(this, _IncomingTransactionHelper_timeoutId, "f")) {
|
|
@@ -88,20 +82,15 @@ class IncomingTransactionHelper {
|
|
|
88
82
|
return;
|
|
89
83
|
}
|
|
90
84
|
const account = __classPrivateFieldGet(this, _IncomingTransactionHelper_getCurrentAccount, "f").call(this);
|
|
91
|
-
const cache = __classPrivateFieldGet(this, _IncomingTransactionHelper_getCache, "f").call(this);
|
|
92
85
|
const includeTokenTransfers = __classPrivateFieldGet(this, _IncomingTransactionHelper_includeTokenTransfers, "f") ?? true;
|
|
93
|
-
const queryEntireHistory = __classPrivateFieldGet(this, _IncomingTransactionHelper_queryEntireHistory, "f") ?? true;
|
|
94
86
|
const updateTransactions = __classPrivateFieldGet(this, _IncomingTransactionHelper_updateTransactions, "f") ?? false;
|
|
95
87
|
let remoteTransactions = [];
|
|
96
88
|
try {
|
|
97
89
|
remoteTransactions =
|
|
98
90
|
await __classPrivateFieldGet(this, _IncomingTransactionHelper_remoteTransactionSource, "f").fetchTransactions({
|
|
99
91
|
address: account.address,
|
|
100
|
-
cache,
|
|
101
92
|
includeTokenTransfers,
|
|
102
|
-
queryEntireHistory,
|
|
103
93
|
tags: finalTags,
|
|
104
|
-
updateCache: __classPrivateFieldGet(this, _IncomingTransactionHelper_updateCache, "f"),
|
|
105
94
|
updateTransactions,
|
|
106
95
|
});
|
|
107
96
|
}
|
|
@@ -139,7 +128,7 @@ class IncomingTransactionHelper {
|
|
|
139
128
|
}
|
|
140
129
|
}
|
|
141
130
|
exports.IncomingTransactionHelper = IncomingTransactionHelper;
|
|
142
|
-
_IncomingTransactionHelper_client = new WeakMap(),
|
|
131
|
+
_IncomingTransactionHelper_client = new WeakMap(), _IncomingTransactionHelper_getCurrentAccount = new WeakMap(), _IncomingTransactionHelper_getLocalTransactions = new WeakMap(), _IncomingTransactionHelper_includeTokenTransfers = new WeakMap(), _IncomingTransactionHelper_isEnabled = new WeakMap(), _IncomingTransactionHelper_isRunning = new WeakMap(), _IncomingTransactionHelper_messenger = new WeakMap(), _IncomingTransactionHelper_remoteTransactionSource = new WeakMap(), _IncomingTransactionHelper_timeoutId = new WeakMap(), _IncomingTransactionHelper_trimTransactions = new WeakMap(), _IncomingTransactionHelper_updateTransactions = new WeakMap(), _IncomingTransactionHelper_instances = new WeakSet(), _IncomingTransactionHelper_onInterval = async function _IncomingTransactionHelper_onInterval() {
|
|
143
132
|
try {
|
|
144
133
|
await this.update({ isInterval: true });
|
|
145
134
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAGlC,0CAA8D;AAE9D,8DAAgF;AAUhF,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAExC,MAAa,yBAAyB;IAmCpC,YAAY,EACV,MAAM,EACN,QAAQ,EACR,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,kBAAkB,EAClB,uBAAuB,EACvB,gBAAgB,EAChB,WAAW,EACX,kBAAkB,GAgBnB;;QA5DQ,oDAAiB;QAEjB,sDAAyC;QAEzC,+DAEP;QAEO,kEAA+C;QAE/C,mEAAiC;QAEjC,uDAA0B;QAEnC,uDAAoB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AAC,CAAC,CAAC,CAAC;AAC1D,CAAC;IAGC,OAAO,uBAAA,IAAI,4CAAW,MAAf,IAAI,CAAa,CAAC;AAC3B,CAAC;IAGC,OAAO,IAAA,sDAAsC,EAAC,uBAAA,IAAI,4CAAW,CAAC,CAAC;AACjE,CAAC,mFAGC,WAAiC,EACjC,UAA+B;IAE/B,MAAM,IAAI,GAAG,EAAE,CAAC;IAEhB,IAAI,uBAAA,IAAI,yCAAQ,EAAE;QAChB,IAAI,CAAC,IAAI,CAAC,uBAAA,IAAI,yCAAQ,CAAC,CAAC;KACzB;IAED,IAAI,WAAW,EAAE,MAAM,EAAE;QACvB,IAAI,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;KAC3B;SAAM,IAAI,UAAU,EAAE;QACrB,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;KACxB;IAED,OAAO,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACzC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type { Hex } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport { getIncomingTransactionsPollingInterval } from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n client?: string;\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n queryEntireHistory?: boolean;\n updateTransactions?: boolean;\n};\n\nconst TAG_POLLING = 'automatic-polling';\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #client?: string;\n\n readonly #getCache: () => Record<string, unknown>;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #queryEntireHistory?: boolean;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateCache: (fn: (cache: Record<string, unknown>) => void) => void;\n\n readonly #updateTransactions?: boolean;\n\n constructor({\n client,\n getCache,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n queryEntireHistory,\n remoteTransactionSource,\n trimTransactions,\n updateCache,\n updateTransactions,\n }: {\n client?: string;\n getCache: () => Record<string, unknown>;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n queryEntireHistory?: boolean;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateCache: (fn: (cache: Record<string, unknown>) => void) => void;\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#client = client;\n this.#getCache = getCache;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? (() => true);\n this.#isRunning = false;\n this.#messenger = messenger;\n this.#queryEntireHistory = queryEntireHistory;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateCache = updateCache;\n this.#updateTransactions = updateTransactions;\n }\n\n start() {\n if (this.#isRunning) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Starting polling', { interval });\n\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#timeoutId = setTimeout(() => this.#onInterval(), interval);\n this.#isRunning = true;\n\n log('Started polling');\n }\n\n stop() {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n async #onInterval() {\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n if (this.#isRunning) {\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({\n isInterval,\n tags,\n }: { isInterval?: boolean; tags?: string[] } = {}): Promise<void> {\n const finalTags = this.#getTags(tags, isInterval);\n\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n tags: finalTags,\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const cache = this.#getCache();\n const includeTokenTransfers = this.#includeTokenTransfers ?? true;\n const queryEntireHistory = this.#queryEntireHistory ?? true;\n const updateTransactions = this.#updateTransactions ?? false;\n\n let remoteTransactions: TransactionMeta[] = [];\n\n try {\n remoteTransactions =\n await this.#remoteTransactionSource.fetchTransactions({\n address: account.address as Hex,\n cache,\n includeTokenTransfers,\n queryEntireHistory,\n tags: finalTags,\n updateCache: this.#updateCache,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]) {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n\n #getTags(\n requestTags: string[] | undefined,\n isInterval: boolean | undefined,\n ): string[] | undefined {\n const tags = [];\n\n if (this.#client) {\n tags.push(this.#client);\n }\n\n if (requestTags?.length) {\n tags.push(...requestTags);\n } else if (isInterval) {\n tags.push(TAG_POLLING);\n }\n\n return tags?.length ? tags : undefined;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAGlC,0CAA8D;AAE9D,8DAAgF;AAmBhF,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAExC,MAAa,yBAAyB;IA6BpC,YAAY,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GAanB;;QAhDQ,oDAAiB;QAEjB,+DAEP;QAEO,kEAA+C;QAE/C,mEAAiC;QAEjC,uDAA0B;QAEnC,uDAAoB;QAEX,uDAA2C;QAE3C,qEAA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AG,WAAW,CAAC,CAAC;KAC3B;SAAM,IAAI,UAAU,EAAE;QACrB,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;KACxB;IAED,OAAO,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACzC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type { Hex } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport { getIncomingTransactionsPollingInterval } from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n /** Name of the client to include in requests. */\n client?: string;\n\n /** Whether to retrieve incoming token transfers. Defaults to false. */\n includeTokenTransfers?: boolean;\n\n /** Callback to determine if incoming transaction polling is enabled. */\n isEnabled?: () => boolean;\n\n /** @deprecated No longer used. */\n queryEntireHistory?: boolean;\n\n /** Whether to retrieve outgoing transactions. Defaults to false. */\n updateTransactions?: boolean;\n};\n\nconst TAG_POLLING = 'automatic-polling';\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #client?: string;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateTransactions?: boolean;\n\n constructor({\n client,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n remoteTransactionSource,\n trimTransactions,\n updateTransactions,\n }: {\n client?: string;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#client = client;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? (() => true);\n this.#isRunning = false;\n this.#messenger = messenger;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateTransactions = updateTransactions;\n }\n\n start() {\n if (this.#isRunning) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Started polling', { interval });\n\n this.#isRunning = true;\n\n this.#onInterval().catch((error) => {\n log('Initial polling failed', error);\n });\n }\n\n stop() {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n async #onInterval() {\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n if (this.#isRunning) {\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({\n isInterval,\n tags,\n }: { isInterval?: boolean; tags?: string[] } = {}): Promise<void> {\n const finalTags = this.#getTags(tags, isInterval);\n\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n tags: finalTags,\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const includeTokenTransfers = this.#includeTokenTransfers ?? 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 includeTokenTransfers,\n tags: finalTags,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]) {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n\n #getTags(\n requestTags: string[] | undefined,\n isInterval: boolean | undefined,\n ): string[] | undefined {\n const tags = [];\n\n if (this.#client) {\n tags.push(this.#client);\n }\n\n if (requestTags?.length) {\n tags.push(...requestTags);\n } else if (isInterval) {\n tags.push(TAG_POLLING);\n }\n\n return tags?.length ? tags : undefined;\n }\n}\n"]}
|
|
@@ -4,27 +4,29 @@ import EventEmitter from "events";
|
|
|
4
4
|
import type { TransactionControllerMessenger } from "../index.cjs";
|
|
5
5
|
import type { RemoteTransactionSource, TransactionMeta } from "../types.cjs";
|
|
6
6
|
export type IncomingTransactionOptions = {
|
|
7
|
+
/** Name of the client to include in requests. */
|
|
7
8
|
client?: string;
|
|
9
|
+
/** Whether to retrieve incoming token transfers. Defaults to false. */
|
|
8
10
|
includeTokenTransfers?: boolean;
|
|
11
|
+
/** Callback to determine if incoming transaction polling is enabled. */
|
|
9
12
|
isEnabled?: () => boolean;
|
|
13
|
+
/** @deprecated No longer used. */
|
|
10
14
|
queryEntireHistory?: boolean;
|
|
15
|
+
/** Whether to retrieve outgoing transactions. Defaults to false. */
|
|
11
16
|
updateTransactions?: boolean;
|
|
12
17
|
};
|
|
13
18
|
export declare class IncomingTransactionHelper {
|
|
14
19
|
#private;
|
|
15
20
|
hub: EventEmitter;
|
|
16
|
-
constructor({ client,
|
|
21
|
+
constructor({ client, getCurrentAccount, getLocalTransactions, includeTokenTransfers, isEnabled, messenger, remoteTransactionSource, trimTransactions, updateTransactions, }: {
|
|
17
22
|
client?: string;
|
|
18
|
-
getCache: () => Record<string, unknown>;
|
|
19
23
|
getCurrentAccount: () => ReturnType<AccountsController['getSelectedAccount']>;
|
|
20
24
|
getLocalTransactions: () => TransactionMeta[];
|
|
21
25
|
includeTokenTransfers?: boolean;
|
|
22
26
|
isEnabled?: () => boolean;
|
|
23
27
|
messenger: TransactionControllerMessenger;
|
|
24
|
-
queryEntireHistory?: boolean;
|
|
25
28
|
remoteTransactionSource: RemoteTransactionSource;
|
|
26
29
|
trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];
|
|
27
|
-
updateCache: (fn: (cache: Record<string, unknown>) => void) => void;
|
|
28
30
|
updateTransactions?: boolean;
|
|
29
31
|
});
|
|
30
32
|
start(): void;
|
|
@@ -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;AAElC,OAAO,KAAK,EAAE,8BAA8B,EAAE,qBAAW;AAEzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAGzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,MAAM,CAAC,EAAE,MAAM,CAAC;
|
|
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;AAElC,OAAO,KAAK,EAAE,8BAA8B,EAAE,qBAAW;AAEzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAGzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,uEAAuE;IACvE,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAEhC,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;IAE1B,kCAAkC;IAClC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B,oEAAoE;IACpE,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAIF,qBAAa,yBAAyB;;IACpC,GAAG,EAAE,YAAY,CAAC;gBA4BN,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GACnB,EAAE;QACD,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,iBAAiB,EAAE,MAAM,UAAU,CACjC,kBAAkB,CAAC,oBAAoB,CAAC,CACzC,CAAC;QACF,oBAAoB,EAAE,MAAM,eAAe,EAAE,CAAC;QAC9C,qBAAqB,CAAC,EAAE,OAAO,CAAC;QAChC,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;QAC1B,SAAS,EAAE,8BAA8B,CAAC;QAC1C,uBAAuB,EAAE,uBAAuB,CAAC;QACjD,gBAAgB,EAAE,CAAC,YAAY,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,CAAC;QACzE,kBAAkB,CAAC,EAAE,OAAO,CAAC;KAC9B;IAeD,KAAK;IAoBL,IAAI;IA8BE,MAAM,CAAC,EACX,UAAU,EACV,IAAI,GACL,GAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAsHlE"}
|
|
@@ -4,27 +4,29 @@ import EventEmitter from "events";
|
|
|
4
4
|
import type { TransactionControllerMessenger } from "../index.mjs";
|
|
5
5
|
import type { RemoteTransactionSource, TransactionMeta } from "../types.mjs";
|
|
6
6
|
export type IncomingTransactionOptions = {
|
|
7
|
+
/** Name of the client to include in requests. */
|
|
7
8
|
client?: string;
|
|
9
|
+
/** Whether to retrieve incoming token transfers. Defaults to false. */
|
|
8
10
|
includeTokenTransfers?: boolean;
|
|
11
|
+
/** Callback to determine if incoming transaction polling is enabled. */
|
|
9
12
|
isEnabled?: () => boolean;
|
|
13
|
+
/** @deprecated No longer used. */
|
|
10
14
|
queryEntireHistory?: boolean;
|
|
15
|
+
/** Whether to retrieve outgoing transactions. Defaults to false. */
|
|
11
16
|
updateTransactions?: boolean;
|
|
12
17
|
};
|
|
13
18
|
export declare class IncomingTransactionHelper {
|
|
14
19
|
#private;
|
|
15
20
|
hub: EventEmitter;
|
|
16
|
-
constructor({ client,
|
|
21
|
+
constructor({ client, getCurrentAccount, getLocalTransactions, includeTokenTransfers, isEnabled, messenger, remoteTransactionSource, trimTransactions, updateTransactions, }: {
|
|
17
22
|
client?: string;
|
|
18
|
-
getCache: () => Record<string, unknown>;
|
|
19
23
|
getCurrentAccount: () => ReturnType<AccountsController['getSelectedAccount']>;
|
|
20
24
|
getLocalTransactions: () => TransactionMeta[];
|
|
21
25
|
includeTokenTransfers?: boolean;
|
|
22
26
|
isEnabled?: () => boolean;
|
|
23
27
|
messenger: TransactionControllerMessenger;
|
|
24
|
-
queryEntireHistory?: boolean;
|
|
25
28
|
remoteTransactionSource: RemoteTransactionSource;
|
|
26
29
|
trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];
|
|
27
|
-
updateCache: (fn: (cache: Record<string, unknown>) => void) => void;
|
|
28
30
|
updateTransactions?: boolean;
|
|
29
31
|
});
|
|
30
32
|
start(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IncomingTransactionHelper.d.mts","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sCAAsC;AAIxE,OAAO,YAAY,eAAe;AAElC,OAAO,KAAK,EAAE,8BAA8B,EAAE,qBAAW;AAEzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAGzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,MAAM,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"IncomingTransactionHelper.d.mts","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sCAAsC;AAIxE,OAAO,YAAY,eAAe;AAElC,OAAO,KAAK,EAAE,8BAA8B,EAAE,qBAAW;AAEzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAGzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,uEAAuE;IACvE,qBAA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