@metamask/smart-transactions-controller 12.0.1 → 13.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.
@@ -1,4 +1,10 @@
1
1
  "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
2
8
  var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
9
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
10
  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");
@@ -7,90 +13,106 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
7
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
8
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
9
15
  };
10
- var _SmartTransactionsController_instances, _SmartTransactionsController_updateSmartTransaction, _SmartTransactionsController_addMetaMetricsPropsToNewSmartTransaction, _SmartTransactionsController_createOrUpdateSmartTransaction, _SmartTransactionsController_doesTransactionNeedConfirmation, _SmartTransactionsController_confirmSmartTransaction, _SmartTransactionsController_getChainId, _SmartTransactionsController_getEthQuery, _SmartTransactionsController_getCurrentSmartTransactions, _SmartTransactionsController_wipeSmartTransactionsPerChainId;
16
+ var _SmartTransactionsController_instances, _SmartTransactionsController_interval, _SmartTransactionsController_clientId, _SmartTransactionsController_chainId, _SmartTransactionsController_supportedChainIds, _SmartTransactionsController_getNonceLock, _SmartTransactionsController_ethQuery, _SmartTransactionsController_confirmExternalTransaction, _SmartTransactionsController_getRegularTransactions, _SmartTransactionsController_trackMetaMetricsEvent, _SmartTransactionsController_getMetaMetricsProps, _SmartTransactionsController_fetch, _SmartTransactionsController_updateSmartTransaction, _SmartTransactionsController_addMetaMetricsPropsToNewSmartTransaction, _SmartTransactionsController_createOrUpdateSmartTransaction, _SmartTransactionsController_doesTransactionNeedConfirmation, _SmartTransactionsController_confirmSmartTransaction, _SmartTransactionsController_getChainId, _SmartTransactionsController_getEthQuery, _SmartTransactionsController_getCurrentSmartTransactions, _SmartTransactionsController_wipeSmartTransactionsPerChainId;
11
17
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DEFAULT_INTERVAL = void 0;
13
- // eslint-disable-next-line import/no-nodejs-modules
18
+ exports.getDefaultSmartTransactionsControllerState = exports.DEFAULT_INTERVAL = void 0;
14
19
  const bytes_1 = require("@ethersproject/bytes");
15
20
  const controller_utils_1 = require("@metamask/controller-utils");
16
21
  const eth_query_1 = __importDefault(require("@metamask/eth-query"));
17
22
  const polling_controller_1 = require("@metamask/polling-controller");
18
23
  const transaction_controller_1 = require("@metamask/transaction-controller");
19
24
  const bignumber_js_1 = require("bignumber.js");
20
- // eslint-disable-next-line import/no-nodejs-modules
21
- const events_1 = __importDefault(require("events"));
22
25
  const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
23
26
  const constants_1 = require("./constants");
24
27
  const types_1 = require("./types");
25
28
  const utils_1 = require("./utils");
26
29
  const SECOND = 1000;
27
30
  exports.DEFAULT_INTERVAL = SECOND * 5;
31
+ const DEFAULT_CLIENT_ID = 'default';
28
32
  const ETH_QUERY_ERROR_MSG = '`ethQuery` is not defined on SmartTransactionsController';
29
- class SmartTransactionsController extends polling_controller_1.StaticIntervalPollingControllerV1 {
30
- constructor({ onNetworkStateChange, getNonceLock, confirmExternalTransaction, getTransactions, trackMetaMetricsEvent, getNetworkClientById, getMetaMetricsProps, }, config, state) {
31
- super(config, state);
32
- _SmartTransactionsController_instances.add(this);
33
- /**
34
- * Name of this controller used during composition
35
- */
36
- this.name = 'SmartTransactionsController';
37
- this.defaultConfig = {
38
- interval: exports.DEFAULT_INTERVAL,
39
- chainId: controller_utils_1.ChainId.mainnet,
40
- clientId: 'default',
41
- supportedChainIds: [controller_utils_1.ChainId.mainnet, controller_utils_1.ChainId.sepolia],
42
- };
43
- this.defaultState = {
44
- smartTransactionsState: {
45
- smartTransactions: {},
46
- userOptIn: undefined,
47
- userOptInV2: undefined,
48
- fees: {
49
- approvalTxFees: undefined,
50
- tradeTxFees: undefined,
51
- },
52
- liveness: true,
53
- livenessByChainId: {
54
- [controller_utils_1.ChainId.mainnet]: true,
55
- [controller_utils_1.ChainId.sepolia]: true,
33
+ /**
34
+ * The name of the {@link SmartTransactionsController}
35
+ */
36
+ const controllerName = 'SmartTransactionsController';
37
+ const controllerMetadata = {
38
+ smartTransactionsState: {
39
+ persist: false,
40
+ anonymous: true,
41
+ },
42
+ };
43
+ /**
44
+ * Get the default {@link SmartTransactionsController} state.
45
+ *
46
+ * @returns The default {@link SmartTransactionsController} state.
47
+ */
48
+ function getDefaultSmartTransactionsControllerState() {
49
+ return {
50
+ smartTransactionsState: {
51
+ smartTransactions: {},
52
+ userOptIn: null,
53
+ userOptInV2: null,
54
+ fees: {
55
+ approvalTxFees: null,
56
+ tradeTxFees: null,
57
+ },
58
+ liveness: true,
59
+ livenessByChainId: {
60
+ [controller_utils_1.ChainId.mainnet]: true,
61
+ [controller_utils_1.ChainId.sepolia]: true,
62
+ },
63
+ feesByChainId: {
64
+ [controller_utils_1.ChainId.mainnet]: {
65
+ approvalTxFees: null,
66
+ tradeTxFees: null,
56
67
  },
57
- feesByChainId: {
58
- [controller_utils_1.ChainId.mainnet]: {
59
- approvalTxFees: undefined,
60
- tradeTxFees: undefined,
61
- },
62
- [controller_utils_1.ChainId.sepolia]: {
63
- approvalTxFees: undefined,
64
- tradeTxFees: undefined,
65
- },
68
+ [controller_utils_1.ChainId.sepolia]: {
69
+ approvalTxFees: null,
70
+ tradeTxFees: null,
66
71
  },
67
72
  },
68
- };
69
- this.initialize();
70
- this.setIntervalLength(this.config.interval);
71
- this.getNonceLock = getNonceLock;
72
- this.ethQuery = undefined;
73
- this.confirmExternalTransaction = confirmExternalTransaction;
74
- this.getRegularTransactions = getTransactions;
75
- this.trackMetaMetricsEvent = trackMetaMetricsEvent;
76
- this.getNetworkClientById = getNetworkClientById;
77
- this.getMetaMetricsProps = getMetaMetricsProps;
73
+ },
74
+ };
75
+ }
76
+ exports.getDefaultSmartTransactionsControllerState = getDefaultSmartTransactionsControllerState;
77
+ class SmartTransactionsController extends polling_controller_1.StaticIntervalPollingController {
78
+ constructor({ interval = exports.DEFAULT_INTERVAL, clientId = DEFAULT_CLIENT_ID, chainId: InitialChainId = controller_utils_1.ChainId.mainnet, supportedChainIds = [controller_utils_1.ChainId.mainnet, controller_utils_1.ChainId.sepolia], getNonceLock, confirmExternalTransaction, trackMetaMetricsEvent, state = {}, messenger, getTransactions, getMetaMetricsProps, }) {
79
+ super({
80
+ name: controllerName,
81
+ metadata: controllerMetadata,
82
+ messenger,
83
+ state: Object.assign(Object.assign({}, getDefaultSmartTransactionsControllerState()), state),
84
+ });
85
+ _SmartTransactionsController_instances.add(this);
86
+ _SmartTransactionsController_interval.set(this, void 0);
87
+ _SmartTransactionsController_clientId.set(this, void 0);
88
+ _SmartTransactionsController_chainId.set(this, void 0);
89
+ _SmartTransactionsController_supportedChainIds.set(this, void 0);
90
+ _SmartTransactionsController_getNonceLock.set(this, void 0);
91
+ _SmartTransactionsController_ethQuery.set(this, void 0);
92
+ _SmartTransactionsController_confirmExternalTransaction.set(this, void 0);
93
+ _SmartTransactionsController_getRegularTransactions.set(this, void 0);
94
+ _SmartTransactionsController_trackMetaMetricsEvent.set(this, void 0);
95
+ _SmartTransactionsController_getMetaMetricsProps.set(this, void 0);
96
+ __classPrivateFieldSet(this, _SmartTransactionsController_interval, interval, "f");
97
+ __classPrivateFieldSet(this, _SmartTransactionsController_clientId, clientId, "f");
98
+ __classPrivateFieldSet(this, _SmartTransactionsController_chainId, InitialChainId, "f");
99
+ __classPrivateFieldSet(this, _SmartTransactionsController_supportedChainIds, supportedChainIds, "f");
100
+ this.setIntervalLength(interval);
101
+ __classPrivateFieldSet(this, _SmartTransactionsController_getNonceLock, getNonceLock, "f");
102
+ __classPrivateFieldSet(this, _SmartTransactionsController_ethQuery, undefined, "f");
103
+ __classPrivateFieldSet(this, _SmartTransactionsController_confirmExternalTransaction, confirmExternalTransaction, "f");
104
+ __classPrivateFieldSet(this, _SmartTransactionsController_getRegularTransactions, getTransactions, "f");
105
+ __classPrivateFieldSet(this, _SmartTransactionsController_trackMetaMetricsEvent, trackMetaMetricsEvent, "f");
106
+ __classPrivateFieldSet(this, _SmartTransactionsController_getMetaMetricsProps, getMetaMetricsProps, "f");
78
107
  this.initializeSmartTransactionsForChainId();
79
- onNetworkStateChange(({ selectedNetworkClientId }) => {
80
- const { configuration: { chainId }, provider, } = this.getNetworkClientById(selectedNetworkClientId);
81
- this.configure({ chainId });
82
- this.ethQuery = new eth_query_1.default(provider);
108
+ this.messagingSystem.subscribe('NetworkController:stateChange', ({ selectedNetworkClientId }) => {
109
+ const { configuration: { chainId }, provider, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
110
+ __classPrivateFieldSet(this, _SmartTransactionsController_chainId, chainId, "f");
111
+ __classPrivateFieldSet(this, _SmartTransactionsController_ethQuery, new eth_query_1.default(provider), "f");
83
112
  this.initializeSmartTransactionsForChainId();
84
113
  this.checkPoll(this.state);
85
114
  });
86
- this.subscribe((currentState) => this.checkPoll(currentState));
87
- this.eventEmitter = new events_1.default();
88
- }
89
- /* istanbul ignore next */
90
- async fetch(request, options) {
91
- const { clientId } = this.config;
92
- const fetchOptions = Object.assign(Object.assign({}, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, (clientId && { 'X-Client-Id': clientId })) });
93
- return (0, utils_1.handleFetch)(request, fetchOptions);
115
+ this.messagingSystem.subscribe(`${controllerName}:stateChange`, (currentState) => this.checkPoll(currentState));
94
116
  }
95
117
  async _executePoll(networkClientId) {
96
118
  // if this is going to be truly UI driven polling we shouldn't really reach here
@@ -98,14 +120,13 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
98
120
  // wondering if we should add some kind of predicate to the polling controller to check whether
99
121
  // we should poll or not
100
122
  const chainId = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getChainId).call(this, { networkClientId });
101
- if (!this.config.supportedChainIds.includes(chainId)) {
123
+ if (!__classPrivateFieldGet(this, _SmartTransactionsController_supportedChainIds, "f").includes(chainId)) {
102
124
  return Promise.resolve();
103
125
  }
104
126
  return this.updateSmartTransactions({ networkClientId });
105
127
  }
106
- checkPoll(state) {
107
- const { smartTransactions } = state.smartTransactionsState;
108
- const currentSmartTransactions = smartTransactions[this.config.chainId];
128
+ checkPoll({ smartTransactionsState: { smartTransactions }, }) {
129
+ const currentSmartTransactions = smartTransactions[__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")];
109
130
  const pendingTransactions = currentSmartTransactions === null || currentSmartTransactions === void 0 ? void 0 : currentSmartTransactions.filter(utils_1.isSmartTransactionPending);
110
131
  if (!this.timeoutHandle && (pendingTransactions === null || pendingTransactions === void 0 ? void 0 : pendingTransactions.length) > 0) {
111
132
  this.poll();
@@ -115,33 +136,34 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
115
136
  }
116
137
  }
117
138
  initializeSmartTransactionsForChainId() {
118
- var _a;
119
- if (this.config.supportedChainIds.includes(this.config.chainId)) {
120
- const { smartTransactionsState } = this.state;
121
- this.update({
122
- smartTransactionsState: Object.assign(Object.assign({}, smartTransactionsState), { smartTransactions: Object.assign(Object.assign({}, smartTransactionsState.smartTransactions), { [this.config.chainId]: (_a = smartTransactionsState.smartTransactions[this.config.chainId]) !== null && _a !== void 0 ? _a : [] }) }),
139
+ if (__classPrivateFieldGet(this, _SmartTransactionsController_supportedChainIds, "f").includes(__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f"))) {
140
+ this.update((state) => {
141
+ var _a;
142
+ state.smartTransactionsState.smartTransactions[__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")] =
143
+ (_a = state.smartTransactionsState.smartTransactions[__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")]) !== null && _a !== void 0 ? _a : [];
123
144
  });
124
145
  }
125
146
  }
126
147
  async poll(interval) {
127
- const { chainId, supportedChainIds } = this.config;
128
- interval && this.configure({ interval }, false, false);
148
+ if (interval) {
149
+ __classPrivateFieldSet(this, _SmartTransactionsController_interval, interval, "f");
150
+ }
129
151
  this.timeoutHandle && clearInterval(this.timeoutHandle);
130
- if (!supportedChainIds.includes(chainId)) {
152
+ if (!__classPrivateFieldGet(this, _SmartTransactionsController_supportedChainIds, "f").includes(__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f"))) {
131
153
  return;
132
154
  }
133
155
  this.timeoutHandle = setInterval(() => {
134
156
  (0, controller_utils_1.safelyExecute)(async () => this.updateSmartTransactions());
135
- }, this.config.interval);
157
+ }, __classPrivateFieldGet(this, _SmartTransactionsController_interval, "f"));
136
158
  await (0, controller_utils_1.safelyExecute)(async () => this.updateSmartTransactions());
137
159
  }
138
160
  async stop() {
139
161
  this.timeoutHandle && clearInterval(this.timeoutHandle);
140
162
  this.timeoutHandle = undefined;
141
163
  }
142
- setOptInState(state) {
143
- this.update({
144
- smartTransactionsState: Object.assign(Object.assign({}, this.state.smartTransactionsState), { userOptInV2: state }),
164
+ setOptInState(optInState) {
165
+ this.update((state) => {
166
+ state.smartTransactionsState.userOptInV2 = optInState;
145
167
  });
146
168
  }
147
169
  trackStxStatusChange(smartTransaction, prevSmartTransaction) {
@@ -150,7 +172,7 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
150
172
  if (updatedSmartTransaction.status === (prevSmartTransaction === null || prevSmartTransaction === void 0 ? void 0 : prevSmartTransaction.status)) {
151
173
  return; // If status hasn't changed, don't track it again.
152
174
  }
153
- this.trackMetaMetricsEvent({
175
+ __classPrivateFieldGet(this, _SmartTransactionsController_trackMetaMetricsEvent, "f").call(this, {
154
176
  event: constants_1.MetaMetricsEventName.StxStatusUpdated,
155
177
  category: constants_1.MetaMetricsEventCategory.Transactions,
156
178
  properties: (0, utils_1.getSmartTransactionMetricsProperties)(updatedSmartTransaction),
@@ -158,19 +180,18 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
158
180
  });
159
181
  }
160
182
  isNewSmartTransaction(smartTransactionUuid) {
161
- const { chainId } = this.config;
162
- const { smartTransactionsState } = this.state;
163
- const { smartTransactions } = smartTransactionsState;
164
- const currentSmartTransactions = smartTransactions[chainId];
183
+ const { smartTransactionsState: { smartTransactions }, } = this.state;
184
+ const currentSmartTransactions = smartTransactions[__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")];
165
185
  const currentIndex = currentSmartTransactions === null || currentSmartTransactions === void 0 ? void 0 : currentSmartTransactions.findIndex((stx) => stx.uuid === smartTransactionUuid);
166
186
  return currentIndex === -1 || currentIndex === undefined;
167
187
  }
168
188
  updateSmartTransaction(smartTransaction, { networkClientId } = {}) {
169
- let { ethQuery, config: { chainId }, } = this;
189
+ let ethQuery = __classPrivateFieldGet(this, _SmartTransactionsController_ethQuery, "f");
190
+ let chainId = __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f");
170
191
  if (networkClientId) {
171
- const networkClient = this.getNetworkClientById(networkClientId);
172
- chainId = networkClient.configuration.chainId;
173
- ethQuery = new eth_query_1.default(networkClient.provider);
192
+ const { configuration, provider } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
193
+ chainId = configuration.chainId;
194
+ ethQuery = new eth_query_1.default(provider);
174
195
  }
175
196
  __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_createOrUpdateSmartTransaction).call(this, smartTransaction, {
176
197
  chainId,
@@ -178,7 +199,7 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
178
199
  });
179
200
  }
180
201
  async updateSmartTransactions({ networkClientId, } = {}) {
181
- const { smartTransactions } = this.state.smartTransactionsState;
202
+ const { smartTransactionsState: { smartTransactions }, } = this.state;
182
203
  const chainId = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getChainId).call(this, { networkClientId });
183
204
  const smartTransactionsForChainId = smartTransactions[chainId];
184
205
  const transactionsToUpdate = smartTransactionsForChainId
@@ -198,7 +219,7 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
198
219
  const chainId = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getChainId).call(this, { networkClientId });
199
220
  const ethQuery = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getEthQuery).call(this, { networkClientId });
200
221
  const url = `${(0, utils_1.getAPIRequestURL)(types_1.APIType.BATCH_STATUS, chainId)}?${params.toString()}`;
201
- const data = (await this.fetch(url));
222
+ const data = (await __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_fetch).call(this, url));
202
223
  for (const [uuid, stxStatus] of Object.entries(data)) {
203
224
  const smartTransaction = {
204
225
  statusMetadata: stxStatus,
@@ -214,18 +235,18 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
214
235
  return data;
215
236
  }
216
237
  async addNonceToTransaction(transaction) {
217
- const nonceLock = await this.getNonceLock(transaction.from);
238
+ const nonceLock = await __classPrivateFieldGet(this, _SmartTransactionsController_getNonceLock, "f").call(this, transaction.from);
218
239
  const nonce = nonceLock.nextNonce;
219
240
  nonceLock.releaseLock();
220
241
  return Object.assign(Object.assign({}, transaction), { nonce: `0x${nonce.toString(16)}` });
221
242
  }
222
243
  clearFees() {
223
244
  const fees = {
224
- approvalTxFees: undefined,
225
- tradeTxFees: undefined,
245
+ approvalTxFees: null,
246
+ tradeTxFees: null,
226
247
  };
227
- this.update({
228
- smartTransactionsState: Object.assign(Object.assign({}, this.state.smartTransactionsState), { fees }),
248
+ this.update((state) => {
249
+ state.smartTransactionsState.fees = fees;
229
250
  });
230
251
  return fees;
231
252
  }
@@ -247,7 +268,7 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
247
268
  unsignedTradeTransactionWithNonce = await this.addNonceToTransaction(tradeTx);
248
269
  }
249
270
  transactions.push(unsignedTradeTransactionWithNonce);
250
- const data = await this.fetch((0, utils_1.getAPIRequestURL)(types_1.APIType.GET_FEES, chainId), {
271
+ const data = await __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_fetch).call(this, (0, utils_1.getAPIRequestURL)(types_1.APIType.GET_FEES, chainId), {
251
272
  method: 'POST',
252
273
  body: JSON.stringify({
253
274
  txs: transactions,
@@ -260,18 +281,20 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
260
281
  tradeTxFees = data === null || data === void 0 ? void 0 : data.txs[1];
261
282
  }
262
283
  else {
284
+ approvalTxFees = null;
263
285
  tradeTxFees = data === null || data === void 0 ? void 0 : data.txs[0];
264
286
  }
265
- this.update({
266
- smartTransactionsState: Object.assign(Object.assign(Object.assign({}, this.state.smartTransactionsState), (chainId === this.config.chainId && {
267
- fees: {
287
+ this.update((state) => {
288
+ if (chainId === __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")) {
289
+ state.smartTransactionsState.fees = {
268
290
  approvalTxFees,
269
291
  tradeTxFees,
270
- },
271
- })), { feesByChainId: Object.assign(Object.assign({}, this.state.smartTransactionsState.feesByChainId), { [chainId]: {
272
- approvalTxFees,
273
- tradeTxFees,
274
- } }) }),
292
+ };
293
+ }
294
+ state.smartTransactionsState.feesByChainId[chainId] = {
295
+ approvalTxFees,
296
+ tradeTxFees,
297
+ };
275
298
  });
276
299
  return {
277
300
  approvalTxFees,
@@ -281,10 +304,10 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
281
304
  // * After this successful call client must add a nonce representative to
282
305
  // * transaction controller external transactions list
283
306
  async submitSignedTransactions({ transactionMeta, txParams, signedTransactions, signedCanceledTransactions, networkClientId, }) {
284
- var _a;
307
+ var _a, _b;
285
308
  const chainId = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getChainId).call(this, { networkClientId });
286
309
  const ethQuery = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getEthQuery).call(this, { networkClientId });
287
- const data = await this.fetch((0, utils_1.getAPIRequestURL)(types_1.APIType.SUBMIT_TRANSACTIONS, chainId), {
310
+ const data = await __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_fetch).call(this, (0, utils_1.getAPIRequestURL)(types_1.APIType.SUBMIT_TRANSACTIONS, chainId), {
288
311
  method: 'POST',
289
312
  body: JSON.stringify({
290
313
  rawTxs: signedTransactions,
@@ -302,17 +325,15 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
302
325
  catch (error) {
303
326
  console.error('provider error', error);
304
327
  }
305
- const requiresNonce = !txParams.nonce;
328
+ const requiresNonce = txParams && !txParams.nonce;
306
329
  let nonce;
307
330
  let nonceLock;
308
331
  let nonceDetails = {};
309
332
  if (requiresNonce) {
310
- nonceLock = await this.getNonceLock(txParams === null || txParams === void 0 ? void 0 : txParams.from);
333
+ nonceLock = await __classPrivateFieldGet(this, _SmartTransactionsController_getNonceLock, "f").call(this, txParams.from);
311
334
  nonce = (0, bytes_1.hexlify)(nonceLock.nextNonce);
312
335
  nonceDetails = nonceLock.nonceDetails;
313
- if (txParams) {
314
- (_a = txParams.nonce) !== null && _a !== void 0 ? _a : (txParams.nonce = nonce);
315
- }
336
+ (_a = txParams.nonce) !== null && _a !== void 0 ? _a : (txParams.nonce = nonce);
316
337
  }
317
338
  const submitTransactionResponse = Object.assign(Object.assign({}, data), { txHash: (0, utils_1.getTxHash)(signedTransactions[0]) });
318
339
  try {
@@ -326,7 +347,7 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
326
347
  uuid: submitTransactionResponse.uuid,
327
348
  txHash: submitTransactionResponse.txHash,
328
349
  cancellable: true,
329
- type: (transactionMeta === null || transactionMeta === void 0 ? void 0 : transactionMeta.type) || 'swap',
350
+ type: (_b = transactionMeta === null || transactionMeta === void 0 ? void 0 : transactionMeta.type) !== null && _b !== void 0 ? _b : 'swap',
330
351
  }, { chainId, ethQuery });
331
352
  }
332
353
  finally {
@@ -339,7 +360,7 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
339
360
  // in transaction controller external transactions list
340
361
  async cancelSmartTransaction(uuid, { networkClientId, } = {}) {
341
362
  const chainId = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getChainId).call(this, { networkClientId });
342
- await this.fetch((0, utils_1.getAPIRequestURL)(types_1.APIType.CANCEL, chainId), {
363
+ await __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_fetch).call(this, (0, utils_1.getAPIRequestURL)(types_1.APIType.CANCEL, chainId), {
343
364
  method: 'POST',
344
365
  body: JSON.stringify({ uuid }),
345
366
  });
@@ -348,20 +369,23 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
348
369
  const chainId = __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_getChainId).call(this, { networkClientId });
349
370
  let liveness = false;
350
371
  try {
351
- const response = await this.fetch((0, utils_1.getAPIRequestURL)(types_1.APIType.LIVENESS, chainId));
352
- liveness = Boolean(response.lastBlock);
372
+ const response = await __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_fetch).call(this, (0, utils_1.getAPIRequestURL)(types_1.APIType.LIVENESS, chainId));
373
+ liveness = Boolean(response.smartTransactions);
353
374
  }
354
375
  catch (error) {
355
376
  console.log('"fetchLiveness" API call failed');
356
377
  }
357
- this.update({
358
- smartTransactionsState: Object.assign(Object.assign(Object.assign({}, this.state.smartTransactionsState), (chainId === this.config.chainId && { liveness })), { livenessByChainId: Object.assign(Object.assign({}, this.state.smartTransactionsState.livenessByChainId), { [chainId]: liveness }) }),
378
+ this.update((state) => {
379
+ if (chainId === __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")) {
380
+ state.smartTransactionsState.liveness = liveness;
381
+ }
382
+ state.smartTransactionsState.livenessByChainId[chainId] = liveness;
359
383
  });
360
384
  return liveness;
361
385
  }
362
386
  async setStatusRefreshInterval(interval) {
363
- if (interval !== this.config.interval) {
364
- this.configure({ interval }, false, false);
387
+ if (interval !== __classPrivateFieldGet(this, _SmartTransactionsController_interval, "f")) {
388
+ __classPrivateFieldSet(this, _SmartTransactionsController_interval, interval, "f");
365
389
  }
366
390
  }
367
391
  getTransactions({ addressFrom, status, }) {
@@ -388,53 +412,55 @@ class SmartTransactionsController extends polling_controller_1.StaticIntervalPol
388
412
  }
389
413
  const addressLowerCase = address.toLowerCase();
390
414
  if (ignoreNetwork) {
391
- const { smartTransactions } = this.state.smartTransactionsState;
415
+ const { smartTransactionsState: { smartTransactions }, } = this.state;
392
416
  Object.keys(smartTransactions).forEach((chainId) => {
393
- const chainIdHex = chainId;
394
417
  __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_wipeSmartTransactionsPerChainId).call(this, {
395
- chainId: chainIdHex,
418
+ chainId,
396
419
  addressLowerCase,
397
420
  });
398
421
  });
399
422
  }
400
423
  else {
401
424
  __classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_wipeSmartTransactionsPerChainId).call(this, {
402
- chainId: this.config.chainId,
425
+ chainId: __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f"),
403
426
  addressLowerCase,
404
427
  });
405
428
  }
406
429
  }
407
430
  }
408
431
  exports.default = SmartTransactionsController;
409
- _SmartTransactionsController_instances = new WeakSet(), _SmartTransactionsController_updateSmartTransaction = function _SmartTransactionsController_updateSmartTransaction(smartTransaction, { chainId = this.config.chainId, }) {
432
+ _SmartTransactionsController_interval = new WeakMap(), _SmartTransactionsController_clientId = new WeakMap(), _SmartTransactionsController_chainId = new WeakMap(), _SmartTransactionsController_supportedChainIds = new WeakMap(), _SmartTransactionsController_getNonceLock = new WeakMap(), _SmartTransactionsController_ethQuery = new WeakMap(), _SmartTransactionsController_confirmExternalTransaction = new WeakMap(), _SmartTransactionsController_getRegularTransactions = new WeakMap(), _SmartTransactionsController_trackMetaMetricsEvent = new WeakMap(), _SmartTransactionsController_getMetaMetricsProps = new WeakMap(), _SmartTransactionsController_instances = new WeakSet(), _SmartTransactionsController_fetch =
433
+ /* istanbul ignore next */
434
+ async function _SmartTransactionsController_fetch(request, options) {
435
+ const fetchOptions = Object.assign(Object.assign({}, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, (__classPrivateFieldGet(this, _SmartTransactionsController_clientId, "f") && { 'X-Client-Id': __classPrivateFieldGet(this, _SmartTransactionsController_clientId, "f") })) });
436
+ return (0, utils_1.handleFetch)(request, fetchOptions);
437
+ }, _SmartTransactionsController_updateSmartTransaction = function _SmartTransactionsController_updateSmartTransaction(smartTransaction, { chainId = __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f"), }) {
410
438
  var _a;
411
- const { smartTransactionsState } = this.state;
412
- const { smartTransactions } = smartTransactionsState;
439
+ const { smartTransactionsState: { smartTransactions }, } = this.state;
413
440
  const currentSmartTransactions = (_a = smartTransactions[chainId]) !== null && _a !== void 0 ? _a : [];
414
441
  const currentIndex = currentSmartTransactions === null || currentSmartTransactions === void 0 ? void 0 : currentSmartTransactions.findIndex((stx) => stx.uuid === smartTransaction.uuid);
415
442
  if (currentIndex === -1) {
416
443
  return; // Smart transaction not found, don't update anything.
417
444
  }
418
- this.update({
419
- smartTransactionsState: Object.assign(Object.assign({}, smartTransactionsState), { smartTransactions: Object.assign(Object.assign({}, smartTransactionsState.smartTransactions), { [chainId]: smartTransactionsState.smartTransactions[chainId].map((existingSmartTransaction, index) => {
420
- return index === currentIndex
421
- ? Object.assign(Object.assign({}, existingSmartTransaction), smartTransaction) : existingSmartTransaction;
422
- }) }) }),
445
+ if (!(0, controller_utils_1.isSafeDynamicKey)(chainId)) {
446
+ return;
447
+ }
448
+ this.update((state) => {
449
+ state.smartTransactionsState.smartTransactions[chainId][currentIndex] = Object.assign(Object.assign({}, state.smartTransactionsState.smartTransactions[chainId][currentIndex]), smartTransaction);
423
450
  });
424
451
  }, _SmartTransactionsController_addMetaMetricsPropsToNewSmartTransaction = async function _SmartTransactionsController_addMetaMetricsPropsToNewSmartTransaction(smartTransaction) {
425
- const metaMetricsProps = await this.getMetaMetricsProps();
452
+ const metaMetricsProps = await __classPrivateFieldGet(this, _SmartTransactionsController_getMetaMetricsProps, "f").call(this);
426
453
  smartTransaction.accountHardwareType =
427
454
  metaMetricsProps === null || metaMetricsProps === void 0 ? void 0 : metaMetricsProps.accountHardwareType;
428
455
  smartTransaction.accountType = metaMetricsProps === null || metaMetricsProps === void 0 ? void 0 : metaMetricsProps.accountType;
429
456
  smartTransaction.deviceModel = metaMetricsProps === null || metaMetricsProps === void 0 ? void 0 : metaMetricsProps.deviceModel;
430
- }, _SmartTransactionsController_createOrUpdateSmartTransaction = async function _SmartTransactionsController_createOrUpdateSmartTransaction(smartTransaction, { chainId = this.config.chainId, ethQuery = this.ethQuery, }) {
457
+ }, _SmartTransactionsController_createOrUpdateSmartTransaction = async function _SmartTransactionsController_createOrUpdateSmartTransaction(smartTransaction, { chainId = __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f"), ethQuery = __classPrivateFieldGet(this, _SmartTransactionsController_ethQuery, "f"), }) {
431
458
  var _a;
432
- const { smartTransactionsState } = this.state;
433
- const { smartTransactions } = smartTransactionsState;
459
+ const { smartTransactionsState: { smartTransactions }, } = this.state;
434
460
  const currentSmartTransactions = (_a = smartTransactions[chainId]) !== null && _a !== void 0 ? _a : [];
435
461
  const currentIndex = currentSmartTransactions === null || currentSmartTransactions === void 0 ? void 0 : currentSmartTransactions.findIndex((stx) => stx.uuid === smartTransaction.uuid);
436
462
  const isNewSmartTransaction = this.isNewSmartTransaction(smartTransaction.uuid);
437
- if (this.ethQuery === undefined) {
463
+ if (__classPrivateFieldGet(this, _SmartTransactionsController_ethQuery, "f") === undefined) {
438
464
  throw new Error(ETH_QUERY_ERROR_MSG);
439
465
  }
440
466
  if (isNewSmartTransaction) {
@@ -459,14 +485,15 @@ _SmartTransactionsController_instances = new WeakSet(), _SmartTransactionsContro
459
485
  .concat(currentSmartTransactions.slice(cancelledNonceIndex + 1))
460
486
  .concat(historifiedSmartTransaction)
461
487
  : currentSmartTransactions.concat(historifiedSmartTransaction);
462
- this.update({
463
- smartTransactionsState: Object.assign(Object.assign({}, smartTransactionsState), { smartTransactions: Object.assign(Object.assign({}, smartTransactionsState.smartTransactions), { [chainId]: nextSmartTransactions }) }),
488
+ this.update((state) => {
489
+ state.smartTransactionsState.smartTransactions[__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")] =
490
+ nextSmartTransactions;
464
491
  });
465
492
  return;
466
493
  }
467
494
  // We have to emit this event here, because then a txHash is returned to the TransactionController once it's available
468
495
  // and the #doesTransactionNeedConfirmation function will work properly, since it will find the txHash in the regular transactions list.
469
- this.eventEmitter.emit(`${smartTransaction.uuid}:smartTransaction`, smartTransaction);
496
+ this.messagingSystem.publish(`SmartTransactionsController:smartTransaction`, smartTransaction);
470
497
  if ((smartTransaction.status === types_1.SmartTransactionStatuses.SUCCESS ||
471
498
  smartTransaction.status === types_1.SmartTransactionStatuses.REVERTED) &&
472
499
  !smartTransaction.confirmed) {
@@ -487,7 +514,7 @@ _SmartTransactionsController_instances = new WeakSet(), _SmartTransactionsContro
487
514
  if (!txHash) {
488
515
  return true;
489
516
  }
490
- const transactions = this.getRegularTransactions();
517
+ const transactions = __classPrivateFieldGet(this, _SmartTransactionsController_getRegularTransactions, "f").call(this);
491
518
  const foundTransaction = transactions === null || transactions === void 0 ? void 0 : transactions.find((tx) => {
492
519
  var _a;
493
520
  return ((_a = tx.hash) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === txHash.toLowerCase();
@@ -499,7 +526,7 @@ _SmartTransactionsController_instances = new WeakSet(), _SmartTransactionsContro
499
526
  // When it's in the submitted state, the TransactionController checks its status and confirms it,
500
527
  // so no need to confirm it again here.
501
528
  return ![transaction_controller_1.TransactionStatus.confirmed, transaction_controller_1.TransactionStatus.submitted].includes(foundTransaction.status);
502
- }, _SmartTransactionsController_confirmSmartTransaction = async function _SmartTransactionsController_confirmSmartTransaction(smartTransaction, { chainId = this.config.chainId, ethQuery = this.ethQuery, }) {
529
+ }, _SmartTransactionsController_confirmSmartTransaction = async function _SmartTransactionsController_confirmSmartTransaction(smartTransaction, { chainId = __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f"), ethQuery = __classPrivateFieldGet(this, _SmartTransactionsController_ethQuery, "f"), }) {
503
530
  var _a;
504
531
  if (ethQuery === undefined) {
505
532
  throw new Error(ETH_QUERY_ERROR_MSG);
@@ -526,9 +553,13 @@ _SmartTransactionsController_instances = new WeakSet(), _SmartTransactionsContro
526
553
  const txMeta = entry.length > 0
527
554
  ? Object.assign(Object.assign({}, originalTxMeta), { history: originalTxMeta.history.concat(entry) }) : originalTxMeta;
528
555
  if (__classPrivateFieldGet(this, _SmartTransactionsController_instances, "m", _SmartTransactionsController_doesTransactionNeedConfirmation).call(this, txHash)) {
529
- this.confirmExternalTransaction(txMeta, transactionReceipt, baseFeePerGas);
556
+ __classPrivateFieldGet(this, _SmartTransactionsController_confirmExternalTransaction, "f").call(this,
557
+ // TODO: Replace 'as' assertion with correct typing for `txMeta`
558
+ txMeta, transactionReceipt,
559
+ // TODO: Replace 'as' assertion with correct typing for `baseFeePerGas`
560
+ baseFeePerGas);
530
561
  }
531
- this.trackMetaMetricsEvent({
562
+ __classPrivateFieldGet(this, _SmartTransactionsController_trackMetaMetricsEvent, "f").call(this, {
532
563
  event: constants_1.MetaMetricsEventName.StxConfirmed,
533
564
  category: constants_1.MetaMetricsEventCategory.Transactions,
534
565
  properties: (0, utils_1.getSmartTransactionMetricsProperties)(smartTransaction),
@@ -540,42 +571,47 @@ _SmartTransactionsController_instances = new WeakSet(), _SmartTransactionsContro
540
571
  }
541
572
  }
542
573
  catch (error) {
543
- this.trackMetaMetricsEvent({
574
+ __classPrivateFieldGet(this, _SmartTransactionsController_trackMetaMetricsEvent, "f").call(this, {
544
575
  event: constants_1.MetaMetricsEventName.StxConfirmationFailed,
545
576
  category: constants_1.MetaMetricsEventCategory.Transactions,
546
577
  });
547
578
  console.error('confirm error', error);
548
579
  }
580
+ finally {
581
+ this.messagingSystem.publish(`SmartTransactionsController:smartTransactionConfirmationDone`, smartTransaction);
582
+ }
549
583
  }, _SmartTransactionsController_getChainId = function _SmartTransactionsController_getChainId({ networkClientId, } = {}) {
550
- return networkClientId
551
- ? this.getNetworkClientById(networkClientId).configuration.chainId
552
- : this.config.chainId;
584
+ if (networkClientId) {
585
+ return this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId).configuration.chainId;
586
+ }
587
+ return __classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f");
553
588
  }, _SmartTransactionsController_getEthQuery = function _SmartTransactionsController_getEthQuery({ networkClientId, } = {}) {
554
589
  if (networkClientId) {
555
- return new eth_query_1.default(this.getNetworkClientById(networkClientId).provider);
590
+ const { provider } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
591
+ return new eth_query_1.default(provider);
556
592
  }
557
- if (this.ethQuery === undefined) {
593
+ if (__classPrivateFieldGet(this, _SmartTransactionsController_ethQuery, "f") === undefined) {
558
594
  throw new Error(ETH_QUERY_ERROR_MSG);
559
595
  }
560
- return this.ethQuery;
596
+ return __classPrivateFieldGet(this, _SmartTransactionsController_ethQuery, "f");
561
597
  }, _SmartTransactionsController_getCurrentSmartTransactions = function _SmartTransactionsController_getCurrentSmartTransactions() {
562
- const { smartTransactions } = this.state.smartTransactionsState;
563
- const { chainId } = this.config;
564
- const currentSmartTransactions = smartTransactions === null || smartTransactions === void 0 ? void 0 : smartTransactions[chainId];
598
+ const { smartTransactionsState: { smartTransactions }, } = this.state;
599
+ const currentSmartTransactions = smartTransactions === null || smartTransactions === void 0 ? void 0 : smartTransactions[__classPrivateFieldGet(this, _SmartTransactionsController_chainId, "f")];
565
600
  if (!currentSmartTransactions || currentSmartTransactions.length === 0) {
566
601
  return [];
567
602
  }
568
603
  return currentSmartTransactions;
569
604
  }, _SmartTransactionsController_wipeSmartTransactionsPerChainId = function _SmartTransactionsController_wipeSmartTransactionsPerChainId({ chainId, addressLowerCase, }) {
570
- const { smartTransactions } = this.state.smartTransactionsState;
605
+ const { smartTransactionsState: { smartTransactions }, } = this.state;
571
606
  const smartTransactionsForSelectedChain = smartTransactions === null || smartTransactions === void 0 ? void 0 : smartTransactions[chainId];
572
607
  if (!smartTransactionsForSelectedChain ||
573
608
  smartTransactionsForSelectedChain.length === 0) {
574
609
  return;
575
610
  }
576
611
  const newSmartTransactionsForSelectedChain = smartTransactionsForSelectedChain.filter((smartTransaction) => { var _a; return ((_a = smartTransaction.txParams) === null || _a === void 0 ? void 0 : _a.from) !== addressLowerCase; });
577
- this.update({
578
- smartTransactionsState: Object.assign(Object.assign({}, this.state.smartTransactionsState), { smartTransactions: Object.assign(Object.assign({}, smartTransactions), { [chainId]: newSmartTransactionsForSelectedChain }) }),
612
+ this.update((state) => {
613
+ state.smartTransactionsState.smartTransactions[chainId] =
614
+ newSmartTransactionsForSelectedChain;
579
615
  });
580
616
  };
581
617
  //# sourceMappingURL=SmartTransactionsController.js.map