@metamask/assets-controllers 30.0.0 → 32.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/CHANGELOG.md +96 -1
  2. package/dist/AccountTrackerController.js +2 -2
  3. package/dist/AccountTrackerController.mjs +1 -1
  4. package/dist/AssetsContractController.js +2 -2
  5. package/dist/AssetsContractController.mjs +1 -1
  6. package/dist/NftController.js +2 -2
  7. package/dist/NftController.mjs +3 -3
  8. package/dist/NftDetectionController.js +2 -2
  9. package/dist/NftDetectionController.mjs +1 -1
  10. package/dist/TokenBalancesController.js +2 -2
  11. package/dist/TokenBalancesController.mjs +1 -1
  12. package/dist/TokenListController.js +3 -3
  13. package/dist/TokenListController.mjs +2 -2
  14. package/dist/TokenRatesController.js +2 -2
  15. package/dist/TokenRatesController.mjs +1 -1
  16. package/dist/TokensController.js +3 -3
  17. package/dist/TokensController.mjs +2 -2
  18. package/dist/chunk-4AC3X2U5.js +771 -0
  19. package/dist/chunk-4AC3X2U5.js.map +1 -0
  20. package/dist/chunk-543CC74T.mjs +253 -0
  21. package/dist/chunk-543CC74T.mjs.map +1 -0
  22. package/dist/{chunk-Z3OQU4XW.mjs → chunk-5W5OO2Q5.mjs} +2 -2
  23. package/dist/chunk-5W5OO2Q5.mjs.map +1 -0
  24. package/dist/chunk-62T7RKU3.mjs +1265 -0
  25. package/dist/chunk-62T7RKU3.mjs.map +1 -0
  26. package/dist/{chunk-PAJTKWEC.mjs → chunk-65PB33TE.mjs} +1 -1
  27. package/dist/chunk-65PB33TE.mjs.map +1 -0
  28. package/dist/{chunk-7MMEHAKG.mjs → chunk-73F3SN5O.mjs} +332 -265
  29. package/dist/chunk-73F3SN5O.mjs.map +1 -0
  30. package/dist/{chunk-XHMM35YT.mjs → chunk-A2DDWXMS.mjs} +2 -2
  31. package/dist/chunk-CGLUTXI7.js +1265 -0
  32. package/dist/chunk-CGLUTXI7.js.map +1 -0
  33. package/dist/{chunk-X4FFNQHE.mjs → chunk-E4ECCGJV.mjs} +1 -1
  34. package/dist/chunk-E4ECCGJV.mjs.map +1 -0
  35. package/dist/{chunk-V4ZO3F2S.js → chunk-GU53EI7A.js} +1 -1
  36. package/dist/chunk-GU53EI7A.js.map +1 -0
  37. package/dist/{chunk-MHN7CJCZ.mjs → chunk-HLCGZGPA.mjs} +1 -1
  38. package/dist/chunk-HLCGZGPA.mjs.map +1 -0
  39. package/dist/{chunk-LD4GC7OR.js → chunk-IBK6AXPP.js} +8 -9
  40. package/dist/chunk-IBK6AXPP.js.map +1 -0
  41. package/dist/{chunk-5F5EQAX5.js → chunk-JUI3XNEF.js} +3 -3
  42. package/dist/{chunk-3R43XIIX.js → chunk-PYMUBJQX.js} +2 -2
  43. package/dist/chunk-PYMUBJQX.js.map +1 -0
  44. package/dist/chunk-QFDTOEYR.js +253 -0
  45. package/dist/chunk-QFDTOEYR.js.map +1 -0
  46. package/dist/{chunk-4ODKGWYQ.js → chunk-QHWKLZUC.js} +1 -1
  47. package/dist/chunk-QHWKLZUC.js.map +1 -0
  48. package/dist/{chunk-6W5EQ3JQ.js → chunk-UEDNQBJN.js} +1 -1
  49. package/dist/chunk-UEDNQBJN.js.map +1 -0
  50. package/dist/{chunk-4LY47RPI.mjs → chunk-WCCLSUOI.mjs} +8 -9
  51. package/dist/{chunk-4LY47RPI.mjs.map → chunk-WCCLSUOI.mjs.map} +1 -1
  52. package/dist/index.js +10 -12
  53. package/dist/index.mjs +11 -13
  54. package/dist/token-service.js +2 -2
  55. package/dist/token-service.mjs +1 -1
  56. package/dist/tsconfig.build.tsbuildinfo +1 -1
  57. package/dist/types/AccountTrackerController.d.ts +1 -1
  58. package/dist/types/AccountTrackerController.d.ts.map +1 -1
  59. package/dist/types/AssetsContractController.d.ts +1 -1
  60. package/dist/types/AssetsContractController.d.ts.map +1 -1
  61. package/dist/types/NftController.d.ts +65 -184
  62. package/dist/types/NftController.d.ts.map +1 -1
  63. package/dist/types/NftDetectionController.d.ts +34 -75
  64. package/dist/types/NftDetectionController.d.ts.map +1 -1
  65. package/dist/types/TokenRatesController.d.ts +6 -6
  66. package/dist/types/TokenRatesController.d.ts.map +1 -1
  67. package/dist/types/TokensController.d.ts +27 -131
  68. package/dist/types/TokensController.d.ts.map +1 -1
  69. package/dist/types/index.d.ts +5 -3
  70. package/dist/types/index.d.ts.map +1 -1
  71. package/package.json +16 -16
  72. package/dist/chunk-3R43XIIX.js.map +0 -1
  73. package/dist/chunk-46UZDIXW.mjs +0 -1205
  74. package/dist/chunk-46UZDIXW.mjs.map +0 -1
  75. package/dist/chunk-4ODKGWYQ.js.map +0 -1
  76. package/dist/chunk-6W5EQ3JQ.js.map +0 -1
  77. package/dist/chunk-7MMEHAKG.mjs.map +0 -1
  78. package/dist/chunk-FMZML3V5.js +0 -237
  79. package/dist/chunk-FMZML3V5.js.map +0 -1
  80. package/dist/chunk-KOKB6U4Z.mjs +0 -237
  81. package/dist/chunk-KOKB6U4Z.mjs.map +0 -1
  82. package/dist/chunk-LAU6ZDZR.js +0 -1205
  83. package/dist/chunk-LAU6ZDZR.js.map +0 -1
  84. package/dist/chunk-LD4GC7OR.js.map +0 -1
  85. package/dist/chunk-MBCN3MNX.js +0 -704
  86. package/dist/chunk-MBCN3MNX.js.map +0 -1
  87. package/dist/chunk-MHN7CJCZ.mjs.map +0 -1
  88. package/dist/chunk-PAJTKWEC.mjs.map +0 -1
  89. package/dist/chunk-V4ZO3F2S.js.map +0 -1
  90. package/dist/chunk-X4FFNQHE.mjs.map +0 -1
  91. package/dist/chunk-Z3OQU4XW.mjs.map +0 -1
  92. /package/dist/{chunk-XHMM35YT.mjs.map → chunk-A2DDWXMS.mjs.map} +0 -0
  93. /package/dist/{chunk-5F5EQAX5.js.map → chunk-JUI3XNEF.js.map} +0 -0
@@ -7,16 +7,22 @@ import {
7
7
  import {
8
8
  TOKEN_METADATA_NO_SUPPORT_ERROR,
9
9
  fetchTokenMetadata
10
- } from "./chunk-Z3OQU4XW.mjs";
10
+ } from "./chunk-5W5OO2Q5.mjs";
11
11
  import {
12
12
  formatAggregatorNames,
13
13
  formatIconUrlWithProxy
14
14
  } from "./chunk-Q5JRBGWO.mjs";
15
+ import {
16
+ __privateAdd,
17
+ __privateGet,
18
+ __privateMethod,
19
+ __privateSet
20
+ } from "./chunk-XUI43LEZ.mjs";
15
21
 
16
22
  // src/TokensController.ts
17
23
  import { Contract } from "@ethersproject/contracts";
18
24
  import { Web3Provider } from "@ethersproject/providers";
19
- import { BaseControllerV1 } from "@metamask/base-controller";
25
+ import { BaseController } from "@metamask/base-controller";
20
26
  import contractsMap from "@metamask/contract-metadata";
21
27
  import {
22
28
  toChecksumHexAddress,
@@ -32,8 +38,33 @@ import {
32
38
  import { abiERC721 } from "@metamask/metamask-eth-abis";
33
39
  import { rpcErrors } from "@metamask/rpc-errors";
34
40
  import { Mutex } from "async-mutex";
35
- import { EventEmitter } from "events";
36
41
  import { v1 as random } from "uuid";
42
+ var metadata = {
43
+ tokens: {
44
+ persist: true,
45
+ anonymous: false
46
+ },
47
+ ignoredTokens: {
48
+ persist: true,
49
+ anonymous: false
50
+ },
51
+ detectedTokens: {
52
+ persist: true,
53
+ anonymous: false
54
+ },
55
+ allTokens: {
56
+ persist: true,
57
+ anonymous: false
58
+ },
59
+ allIgnoredTokens: {
60
+ persist: true,
61
+ anonymous: false
62
+ },
63
+ allDetectedTokens: {
64
+ persist: true,
65
+ anonymous: false
66
+ }
67
+ };
37
68
  var controllerName = "TokensController";
38
69
  var getDefaultTokensState = () => {
39
70
  return {
@@ -45,109 +76,117 @@ var getDefaultTokensState = () => {
45
76
  allDetectedTokens: {}
46
77
  };
47
78
  };
48
- var TokensController = class extends BaseControllerV1 {
79
+ var _mutex, _chainId, _selectedAddress, _provider, _abortController, _onNetworkDidChange, onNetworkDidChange_fn, _onPreferenceControllerStateChange, onPreferenceControllerStateChange_fn, _fetchTokenMetadata, fetchTokenMetadata_fn, _updateTokensAttribute, updateTokensAttribute_fn, _detectIsERC721, detectIsERC721_fn, _getProvider, getProvider_fn, _createEthersContract, createEthersContract_fn, _generateRandomId, generateRandomId_fn, _getNewAllTokensState, getNewAllTokensState_fn, _requestApproval, requestApproval_fn;
80
+ var TokensController = class extends BaseController {
49
81
  /**
50
- * Creates a TokensController instance.
51
- *
52
- * @param options - The controller options.
82
+ * Tokens controller options
83
+ * @param options - Constructor options.
53
84
  * @param options.chainId - The chain ID of the current network.
54
- * @param options.config - Initial options used to configure this controller.
85
+ * @param options.selectedAddress - Vault selected address
86
+ * @param options.provider - Network provider.
55
87
  * @param options.state - Initial state to set on this controller.
56
88
  * @param options.messenger - The controller messenger.
57
89
  */
58
90
  constructor({
59
91
  chainId: initialChainId,
60
- config,
92
+ selectedAddress,
93
+ provider,
61
94
  state,
62
95
  messenger
63
96
  }) {
64
- super(config, state);
65
- this.mutex = new Mutex();
97
+ super({
98
+ name: controllerName,
99
+ metadata,
100
+ messenger,
101
+ state: {
102
+ ...getDefaultTokensState(),
103
+ ...state
104
+ }
105
+ });
66
106
  /**
67
- * EventEmitter instance used to listen to specific EIP747 events
107
+ * Handles the event when the network changes.
108
+ *
109
+ * @param networkState - The changed network state.
110
+ * @param networkState.providerConfig - RPC URL and network name provider settings of the currently connected network
68
111
  */
69
- this.hub = new EventEmitter();
112
+ __privateAdd(this, _onNetworkDidChange);
70
113
  /**
71
- * Name of this controller used during composition
114
+ * Handles the state change of the preference controller.
115
+ * @param preferencesState - The new state of the preference controller.
116
+ * @param preferencesState.selectedAddress - The current selected address of the preference controller.
72
117
  */
73
- this.name = "TokensController";
74
- this.defaultConfig = {
75
- selectedAddress: "",
76
- chainId: initialChainId,
77
- provider: void 0,
78
- ...config
79
- };
80
- this.defaultState = {
81
- ...getDefaultTokensState(),
82
- ...state
83
- };
84
- this.initialize();
85
- this.abortController = new AbortController();
86
- this.messagingSystem = messenger;
118
+ __privateAdd(this, _onPreferenceControllerStateChange);
119
+ /**
120
+ * Fetch metadata for a token.
121
+ *
122
+ * @param tokenAddress - The address of the token.
123
+ * @returns The token metadata.
124
+ */
125
+ __privateAdd(this, _fetchTokenMetadata);
126
+ /**
127
+ * This is a function that updates the tokens name for the tokens name if it is not defined.
128
+ *
129
+ * @param tokenList - Represents the fetched token list from service API
130
+ * @param tokenAttribute - Represents the token attribute that we want to update on the token list
131
+ */
132
+ __privateAdd(this, _updateTokensAttribute);
133
+ /**
134
+ * Detects whether or not a token is ERC-721 compatible.
135
+ *
136
+ * @param tokenAddress - The token contract address.
137
+ * @param networkClientId - Optional network client ID to fetch contract info with.
138
+ * @returns A boolean indicating whether the token address passed in supports the EIP-721
139
+ * interface.
140
+ */
141
+ __privateAdd(this, _detectIsERC721);
142
+ __privateAdd(this, _getProvider);
143
+ __privateAdd(this, _createEthersContract);
144
+ __privateAdd(this, _generateRandomId);
145
+ /**
146
+ * Takes a new tokens and ignoredTokens array for the current network/account combination
147
+ * and returns new allTokens and allIgnoredTokens state to update to.
148
+ *
149
+ * @param params - Object that holds token params.
150
+ * @param params.newTokens - The new tokens to set for the current network and selected account.
151
+ * @param params.newIgnoredTokens - The new ignored tokens to set for the current network and selected account.
152
+ * @param params.newDetectedTokens - The new detected tokens to set for the current network and selected account.
153
+ * @param params.interactingAddress - The account address to use to store the tokens.
154
+ * @param params.interactingChainId - The chainId to use to store the tokens.
155
+ * @returns The updated `allTokens` and `allIgnoredTokens` state.
156
+ */
157
+ __privateAdd(this, _getNewAllTokensState);
158
+ __privateAdd(this, _requestApproval);
159
+ __privateAdd(this, _mutex, new Mutex());
160
+ __privateAdd(this, _chainId, void 0);
161
+ __privateAdd(this, _selectedAddress, void 0);
162
+ __privateAdd(this, _provider, void 0);
163
+ __privateAdd(this, _abortController, void 0);
164
+ __privateSet(this, _chainId, initialChainId);
165
+ __privateSet(this, _provider, provider);
166
+ __privateSet(this, _selectedAddress, selectedAddress);
167
+ __privateSet(this, _abortController, new AbortController());
87
168
  this.messagingSystem.registerActionHandler(
88
169
  `${controllerName}:addDetectedTokens`,
89
170
  this.addDetectedTokens.bind(this)
90
171
  );
91
172
  this.messagingSystem.subscribe(
92
173
  "PreferencesController:stateChange",
93
- ({ selectedAddress }) => {
94
- const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
95
- const { chainId } = this.config;
96
- this.configure({ selectedAddress });
97
- this.update({
98
- tokens: allTokens[chainId]?.[selectedAddress] ?? [],
99
- ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] ?? [],
100
- detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] ?? []
101
- });
102
- }
174
+ __privateMethod(this, _onPreferenceControllerStateChange, onPreferenceControllerStateChange_fn).bind(this)
103
175
  );
104
176
  this.messagingSystem.subscribe(
105
177
  "NetworkController:networkDidChange",
106
- ({ providerConfig }) => {
107
- const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
108
- const { selectedAddress } = this.config;
109
- const { chainId } = providerConfig;
110
- this.abortController.abort();
111
- this.abortController = new AbortController();
112
- this.configure({ chainId });
113
- this.update({
114
- tokens: allTokens[chainId]?.[selectedAddress] || [],
115
- ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] || [],
116
- detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] || []
117
- });
118
- }
178
+ __privateMethod(this, _onNetworkDidChange, onNetworkDidChange_fn).bind(this)
119
179
  );
120
180
  this.messagingSystem.subscribe(
121
181
  "TokenListController:stateChange",
122
182
  ({ tokenList }) => {
123
183
  const { tokens } = this.state;
124
184
  if (tokens.length && !tokens[0].name) {
125
- this.updateTokensAttribute(tokenList, "name");
185
+ __privateMethod(this, _updateTokensAttribute, updateTokensAttribute_fn).call(this, tokenList, "name");
126
186
  }
127
187
  }
128
188
  );
129
189
  }
130
- /**
131
- * Fetch metadata for a token.
132
- *
133
- * @param tokenAddress - The address of the token.
134
- * @returns The token metadata.
135
- */
136
- async fetchTokenMetadata(tokenAddress) {
137
- try {
138
- const token = await fetchTokenMetadata(
139
- this.config.chainId,
140
- tokenAddress,
141
- this.abortController.signal
142
- );
143
- return token;
144
- } catch (error) {
145
- if (error instanceof Error && error.message.includes(TOKEN_METADATA_NO_SUPPORT_ERROR)) {
146
- return void 0;
147
- }
148
- throw error;
149
- }
150
- }
151
190
  /**
152
191
  * Adds a token to the stored token list.
153
192
  *
@@ -170,8 +209,9 @@ var TokensController = class extends BaseControllerV1 {
170
209
  interactingAddress,
171
210
  networkClientId
172
211
  }) {
173
- const { chainId, selectedAddress } = this.config;
174
- const releaseLock = await this.mutex.acquire();
212
+ const chainId = __privateGet(this, _chainId);
213
+ const selectedAddress = __privateGet(this, _selectedAddress);
214
+ const releaseLock = await __privateGet(this, _mutex).acquire();
175
215
  const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
176
216
  let currentChainId = chainId;
177
217
  if (networkClientId) {
@@ -189,11 +229,11 @@ var TokensController = class extends BaseControllerV1 {
189
229
  const detectedTokens = allDetectedTokens[currentChainId]?.[accountAddress] || [];
190
230
  const newTokens = [...tokens];
191
231
  const [isERC721, tokenMetadata] = await Promise.all([
192
- this._detectIsERC721(address, networkClientId),
232
+ __privateMethod(this, _detectIsERC721, detectIsERC721_fn).call(this, address, networkClientId),
193
233
  // TODO parameterize the token metadata fetch by networkClientId
194
- this.fetchTokenMetadata(address)
234
+ __privateMethod(this, _fetchTokenMetadata, fetchTokenMetadata_fn).call(this, address)
195
235
  ]);
196
- if (!networkClientId && currentChainId !== this.config.chainId) {
236
+ if (!networkClientId && currentChainId !== __privateGet(this, _chainId)) {
197
237
  throw new Error(
198
238
  "TokensController Error: Switched networks while adding token"
199
239
  );
@@ -224,7 +264,7 @@ var TokensController = class extends BaseControllerV1 {
224
264
  const newDetectedTokens = detectedTokens.filter(
225
265
  (token) => token.address.toLowerCase() !== address.toLowerCase()
226
266
  );
227
- const { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens } = this._getNewAllTokensState({
267
+ const { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens } = __privateMethod(this, _getNewAllTokensState, getNewAllTokensState_fn).call(this, {
228
268
  newTokens,
229
269
  newIgnoredTokens,
230
270
  newDetectedTokens,
@@ -244,7 +284,9 @@ var TokensController = class extends BaseControllerV1 {
244
284
  detectedTokens: newDetectedTokens
245
285
  };
246
286
  }
247
- this.update(newState);
287
+ this.update((state) => {
288
+ Object.assign(state, newState);
289
+ });
248
290
  return newTokens;
249
291
  } finally {
250
292
  releaseLock();
@@ -257,7 +299,7 @@ var TokensController = class extends BaseControllerV1 {
257
299
  * @param networkClientId - Optional network client ID used to determine interacting chain ID.
258
300
  */
259
301
  async addTokens(tokensToImport, networkClientId) {
260
- const releaseLock = await this.mutex.acquire();
302
+ const releaseLock = await __privateGet(this, _mutex).acquire();
261
303
  const { tokens, detectedTokens, ignoredTokens } = this.state;
262
304
  const importedTokensMap = {};
263
305
  const newTokensMap = tokens.reduce((output, current) => {
@@ -294,19 +336,19 @@ var TokensController = class extends BaseControllerV1 {
294
336
  networkClientId
295
337
  ).configuration.chainId;
296
338
  }
297
- const { newAllTokens, newAllDetectedTokens, newAllIgnoredTokens } = this._getNewAllTokensState({
339
+ const { newAllTokens, newAllDetectedTokens, newAllIgnoredTokens } = __privateMethod(this, _getNewAllTokensState, getNewAllTokensState_fn).call(this, {
298
340
  newTokens,
299
341
  newDetectedTokens,
300
342
  newIgnoredTokens,
301
343
  interactingChainId
302
344
  });
303
- this.update({
304
- tokens: newTokens,
305
- allTokens: newAllTokens,
306
- detectedTokens: newDetectedTokens,
307
- allDetectedTokens: newAllDetectedTokens,
308
- ignoredTokens: newIgnoredTokens,
309
- allIgnoredTokens: newAllIgnoredTokens
345
+ this.update((state) => {
346
+ state.tokens = newTokens;
347
+ state.allTokens = newAllTokens;
348
+ state.detectedTokens = newDetectedTokens;
349
+ state.allDetectedTokens = newAllDetectedTokens;
350
+ state.ignoredTokens = newIgnoredTokens;
351
+ state.allIgnoredTokens = newAllIgnoredTokens;
310
352
  });
311
353
  } finally {
312
354
  releaseLock();
@@ -333,18 +375,18 @@ var TokensController = class extends BaseControllerV1 {
333
375
  const newTokens = tokens.filter(
334
376
  (token) => !ignoredTokensMap[token.address.toLowerCase()]
335
377
  );
336
- const { newAllIgnoredTokens, newAllDetectedTokens, newAllTokens } = this._getNewAllTokensState({
378
+ const { newAllIgnoredTokens, newAllDetectedTokens, newAllTokens } = __privateMethod(this, _getNewAllTokensState, getNewAllTokensState_fn).call(this, {
337
379
  newIgnoredTokens,
338
380
  newDetectedTokens,
339
381
  newTokens
340
382
  });
341
- this.update({
342
- ignoredTokens: newIgnoredTokens,
343
- tokens: newTokens,
344
- detectedTokens: newDetectedTokens,
345
- allIgnoredTokens: newAllIgnoredTokens,
346
- allDetectedTokens: newAllDetectedTokens,
347
- allTokens: newAllTokens
383
+ this.update((state) => {
384
+ state.ignoredTokens = newIgnoredTokens;
385
+ state.tokens = newTokens;
386
+ state.detectedTokens = newDetectedTokens;
387
+ state.allIgnoredTokens = newAllIgnoredTokens;
388
+ state.allDetectedTokens = newAllDetectedTokens;
389
+ state.allTokens = newAllTokens;
348
390
  });
349
391
  }
350
392
  /**
@@ -356,9 +398,9 @@ var TokensController = class extends BaseControllerV1 {
356
398
  * @param detectionDetails.chainId - the chainId on which the incomingDetectedTokens were detected.
357
399
  */
358
400
  async addDetectedTokens(incomingDetectedTokens, detectionDetails) {
359
- const releaseLock = await this.mutex.acquire();
360
- const chainId = detectionDetails?.chainId ?? this.config.chainId;
361
- const accountAddress = detectionDetails?.selectedAddress ?? this.config.selectedAddress;
401
+ const releaseLock = await __privateGet(this, _mutex).acquire();
402
+ const chainId = detectionDetails?.chainId ?? __privateGet(this, _chainId);
403
+ const accountAddress = detectionDetails?.selectedAddress ?? __privateGet(this, _selectedAddress);
362
404
  const { allTokens, allDetectedTokens, allIgnoredTokens } = this.state;
363
405
  let newTokens = [...allTokens?.[chainId]?.[accountAddress] ?? []];
364
406
  let newDetectedTokens = [
@@ -404,22 +446,19 @@ var TokensController = class extends BaseControllerV1 {
404
446
  }
405
447
  }
406
448
  });
407
- const { newAllTokens, newAllDetectedTokens } = this._getNewAllTokensState(
408
- {
409
- newTokens,
410
- newDetectedTokens,
411
- interactingAddress: accountAddress,
412
- interactingChainId: chainId
413
- }
414
- );
415
- const { chainId: currentChain, selectedAddress: currentAddress } = this.config;
416
- newTokens = newAllTokens?.[currentChain]?.[currentAddress] || [];
417
- newDetectedTokens = newAllDetectedTokens?.[currentChain]?.[currentAddress] || [];
418
- this.update({
419
- tokens: newTokens,
420
- allTokens: newAllTokens,
421
- detectedTokens: newDetectedTokens,
422
- allDetectedTokens: newAllDetectedTokens
449
+ const { newAllTokens, newAllDetectedTokens } = __privateMethod(this, _getNewAllTokensState, getNewAllTokensState_fn).call(this, {
450
+ newTokens,
451
+ newDetectedTokens,
452
+ interactingAddress: accountAddress,
453
+ interactingChainId: chainId
454
+ });
455
+ newTokens = newAllTokens?.[__privateGet(this, _chainId)]?.[__privateGet(this, _selectedAddress)] || [];
456
+ newDetectedTokens = newAllDetectedTokens?.[__privateGet(this, _chainId)]?.[__privateGet(this, _selectedAddress)] || [];
457
+ this.update((state) => {
458
+ state.tokens = newTokens;
459
+ state.allTokens = newAllTokens;
460
+ state.detectedTokens = newDetectedTokens;
461
+ state.allDetectedTokens = newAllDetectedTokens;
423
462
  });
424
463
  } finally {
425
464
  releaseLock();
@@ -433,71 +472,17 @@ var TokensController = class extends BaseControllerV1 {
433
472
  * @returns The new token object with the added isERC721 field.
434
473
  */
435
474
  async updateTokenType(tokenAddress) {
436
- const isERC721 = await this._detectIsERC721(tokenAddress);
437
- const { tokens } = this.state;
475
+ const isERC721 = await __privateMethod(this, _detectIsERC721, detectIsERC721_fn).call(this, tokenAddress);
476
+ const tokens = [...this.state.tokens];
438
477
  const tokenIndex = tokens.findIndex((token) => {
439
478
  return token.address.toLowerCase() === tokenAddress.toLowerCase();
440
479
  });
441
- tokens[tokenIndex].isERC721 = isERC721;
442
- this.update({ tokens });
443
- return tokens[tokenIndex];
444
- }
445
- /**
446
- * This is a function that updates the tokens name for the tokens name if it is not defined.
447
- *
448
- * @param tokenList - Represents the fetched token list from service API
449
- * @param tokenAttribute - Represents the token attribute that we want to update on the token list
450
- */
451
- updateTokensAttribute(tokenList, tokenAttribute) {
452
- const { tokens } = this.state;
453
- const newTokens = tokens.map((token) => {
454
- const newToken = tokenList[token.address.toLowerCase()];
455
- return !token[tokenAttribute] && newToken?.[tokenAttribute] ? { ...token, [tokenAttribute]: newToken[tokenAttribute] } : { ...token };
480
+ const updatedToken = { ...tokens[tokenIndex], isERC721 };
481
+ tokens[tokenIndex] = updatedToken;
482
+ this.update((state) => {
483
+ state.tokens = tokens;
456
484
  });
457
- this.update({ tokens: newTokens });
458
- }
459
- /**
460
- * Detects whether or not a token is ERC-721 compatible.
461
- *
462
- * @param tokenAddress - The token contract address.
463
- * @param networkClientId - Optional network client ID to fetch contract info with.
464
- * @returns A boolean indicating whether the token address passed in supports the EIP-721
465
- * interface.
466
- */
467
- async _detectIsERC721(tokenAddress, networkClientId) {
468
- const checksumAddress = toChecksumHexAddress(tokenAddress);
469
- if (contractsMap[checksumAddress]?.erc721 === true) {
470
- return Promise.resolve(true);
471
- } else if (contractsMap[checksumAddress]?.erc20 === true) {
472
- return Promise.resolve(false);
473
- }
474
- const tokenContract = this._createEthersContract(
475
- tokenAddress,
476
- abiERC721,
477
- networkClientId
478
- );
479
- try {
480
- return await tokenContract.supportsInterface(ERC721_INTERFACE_ID);
481
- } catch (error) {
482
- return false;
483
- }
484
- }
485
- _getProvider(networkClientId) {
486
- return new Web3Provider(
487
- // @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
488
- networkClientId ? this.messagingSystem.call(
489
- "NetworkController:getNetworkClientById",
490
- networkClientId
491
- ).provider : this.config.provider
492
- );
493
- }
494
- _createEthersContract(tokenAddress, abi, networkClientId) {
495
- const web3provider = this._getProvider(networkClientId);
496
- const tokenContract = new Contract(tokenAddress, abi, web3provider);
497
- return tokenContract;
498
- }
499
- _generateRandomId() {
500
- return random();
485
+ return updatedToken;
501
486
  }
502
487
  /**
503
488
  * Adds a new suggestedAsset to the list of watched assets.
@@ -525,12 +510,12 @@ var TokensController = class extends BaseControllerV1 {
525
510
  if (!isValidHexAddress(asset.address)) {
526
511
  throw rpcErrors.invalidParams(`Invalid address "${asset.address}"`);
527
512
  }
528
- if (await this._detectIsERC721(asset.address, networkClientId)) {
513
+ if (await __privateMethod(this, _detectIsERC721, detectIsERC721_fn).call(this, asset.address, networkClientId)) {
529
514
  throw rpcErrors.invalidParams(
530
515
  `Contract ${asset.address} must match type ${type}, but was detected as ${ERC721}`
531
516
  );
532
517
  }
533
- const provider = this._getProvider(networkClientId);
518
+ const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this, networkClientId);
534
519
  const isErc1155 = await safelyExecute(
535
520
  () => new ERC1155Standard(provider).contractSupportsBase1155Interface(
536
521
  asset.address
@@ -587,12 +572,12 @@ var TokensController = class extends BaseControllerV1 {
587
572
  asset.decimals = decimalsNum;
588
573
  const suggestedAssetMeta = {
589
574
  asset,
590
- id: this._generateRandomId(),
575
+ id: __privateMethod(this, _generateRandomId, generateRandomId_fn).call(this),
591
576
  time: Date.now(),
592
577
  type,
593
- interactingAddress: interactingAddress || this.config.selectedAddress
578
+ interactingAddress: interactingAddress || __privateGet(this, _selectedAddress)
594
579
  };
595
- await this._requestApproval(suggestedAssetMeta);
580
+ await __privateMethod(this, _requestApproval, requestApproval_fn).call(this, suggestedAssetMeta);
596
581
  const { address, symbol, decimals, name, image } = asset;
597
582
  await this.addToken({
598
583
  address,
@@ -604,95 +589,177 @@ var TokensController = class extends BaseControllerV1 {
604
589
  networkClientId
605
590
  });
606
591
  }
607
- /**
608
- * Takes a new tokens and ignoredTokens array for the current network/account combination
609
- * and returns new allTokens and allIgnoredTokens state to update to.
610
- *
611
- * @param params - Object that holds token params.
612
- * @param params.newTokens - The new tokens to set for the current network and selected account.
613
- * @param params.newIgnoredTokens - The new ignored tokens to set for the current network and selected account.
614
- * @param params.newDetectedTokens - The new detected tokens to set for the current network and selected account.
615
- * @param params.interactingAddress - The account address to use to store the tokens.
616
- * @param params.interactingChainId - The chainId to use to store the tokens.
617
- * @returns The updated `allTokens` and `allIgnoredTokens` state.
618
- */
619
- _getNewAllTokensState(params) {
620
- const {
621
- newTokens,
622
- newIgnoredTokens,
623
- newDetectedTokens,
624
- interactingAddress,
625
- interactingChainId
626
- } = params;
627
- const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
628
- const { chainId, selectedAddress } = this.config;
629
- const userAddressToAddTokens = interactingAddress ?? selectedAddress;
630
- const chainIdToAddTokens = interactingChainId ?? chainId;
631
- let newAllTokens = allTokens;
632
- if (newTokens?.length || newTokens && allTokens && allTokens[chainIdToAddTokens] && allTokens[chainIdToAddTokens][userAddressToAddTokens]) {
633
- const networkTokens = allTokens[chainIdToAddTokens];
634
- const newNetworkTokens = {
635
- ...networkTokens,
636
- ...{ [userAddressToAddTokens]: newTokens }
637
- };
638
- newAllTokens = {
639
- ...allTokens,
640
- ...{ [chainIdToAddTokens]: newNetworkTokens }
641
- };
642
- }
643
- let newAllIgnoredTokens = allIgnoredTokens;
644
- if (newIgnoredTokens?.length || newIgnoredTokens && allIgnoredTokens && allIgnoredTokens[chainIdToAddTokens] && allIgnoredTokens[chainIdToAddTokens][userAddressToAddTokens]) {
645
- const networkIgnoredTokens = allIgnoredTokens[chainIdToAddTokens];
646
- const newIgnoredNetworkTokens = {
647
- ...networkIgnoredTokens,
648
- ...{ [userAddressToAddTokens]: newIgnoredTokens }
649
- };
650
- newAllIgnoredTokens = {
651
- ...allIgnoredTokens,
652
- ...{ [chainIdToAddTokens]: newIgnoredNetworkTokens }
653
- };
654
- }
655
- let newAllDetectedTokens = allDetectedTokens;
656
- if (newDetectedTokens?.length || newDetectedTokens && allDetectedTokens && allDetectedTokens[chainIdToAddTokens] && allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens]) {
657
- const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens];
658
- const newDetectedNetworkTokens = {
659
- ...networkDetectedTokens,
660
- ...{ [userAddressToAddTokens]: newDetectedTokens }
661
- };
662
- newAllDetectedTokens = {
663
- ...allDetectedTokens,
664
- ...{ [chainIdToAddTokens]: newDetectedNetworkTokens }
665
- };
666
- }
667
- return { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens };
668
- }
669
592
  /**
670
593
  * Removes all tokens from the ignored list.
671
594
  */
672
595
  clearIgnoredTokens() {
673
- this.update({ ignoredTokens: [], allIgnoredTokens: {} });
596
+ this.update((state) => {
597
+ state.ignoredTokens = [];
598
+ state.allIgnoredTokens = {};
599
+ });
674
600
  }
675
- async _requestApproval(suggestedAssetMeta) {
676
- return this.messagingSystem.call(
677
- "ApprovalController:addRequest",
678
- {
679
- id: suggestedAssetMeta.id,
680
- origin: ORIGIN_METAMASK,
681
- type: ApprovalType.WatchAsset,
682
- requestData: {
683
- id: suggestedAssetMeta.id,
684
- interactingAddress: suggestedAssetMeta.interactingAddress,
685
- asset: {
686
- address: suggestedAssetMeta.asset.address,
687
- decimals: suggestedAssetMeta.asset.decimals,
688
- symbol: suggestedAssetMeta.asset.symbol,
689
- image: suggestedAssetMeta.asset.image || null
690
- }
691
- }
692
- },
693
- true
601
+ };
602
+ _mutex = new WeakMap();
603
+ _chainId = new WeakMap();
604
+ _selectedAddress = new WeakMap();
605
+ _provider = new WeakMap();
606
+ _abortController = new WeakMap();
607
+ _onNetworkDidChange = new WeakSet();
608
+ onNetworkDidChange_fn = function({ providerConfig }) {
609
+ const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
610
+ const { chainId } = providerConfig;
611
+ __privateGet(this, _abortController).abort();
612
+ __privateSet(this, _abortController, new AbortController());
613
+ __privateSet(this, _chainId, chainId);
614
+ this.update((state) => {
615
+ state.tokens = allTokens[chainId]?.[__privateGet(this, _selectedAddress)] || [];
616
+ state.ignoredTokens = allIgnoredTokens[chainId]?.[__privateGet(this, _selectedAddress)] || [];
617
+ state.detectedTokens = allDetectedTokens[chainId]?.[__privateGet(this, _selectedAddress)] || [];
618
+ });
619
+ };
620
+ _onPreferenceControllerStateChange = new WeakSet();
621
+ onPreferenceControllerStateChange_fn = function({ selectedAddress }) {
622
+ const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
623
+ __privateSet(this, _selectedAddress, selectedAddress);
624
+ this.update((state) => {
625
+ state.tokens = allTokens[__privateGet(this, _chainId)]?.[selectedAddress] ?? [];
626
+ state.ignoredTokens = allIgnoredTokens[__privateGet(this, _chainId)]?.[selectedAddress] ?? [];
627
+ state.detectedTokens = allDetectedTokens[__privateGet(this, _chainId)]?.[selectedAddress] ?? [];
628
+ });
629
+ };
630
+ _fetchTokenMetadata = new WeakSet();
631
+ fetchTokenMetadata_fn = async function(tokenAddress) {
632
+ try {
633
+ const token = await fetchTokenMetadata(
634
+ __privateGet(this, _chainId),
635
+ tokenAddress,
636
+ __privateGet(this, _abortController).signal
694
637
  );
638
+ return token;
639
+ } catch (error) {
640
+ if (error instanceof Error && error.message.includes(TOKEN_METADATA_NO_SUPPORT_ERROR)) {
641
+ return void 0;
642
+ }
643
+ throw error;
644
+ }
645
+ };
646
+ _updateTokensAttribute = new WeakSet();
647
+ updateTokensAttribute_fn = function(tokenList, tokenAttribute) {
648
+ const { tokens } = this.state;
649
+ const newTokens = tokens.map((token) => {
650
+ const newToken = tokenList[token.address.toLowerCase()];
651
+ return !token[tokenAttribute] && newToken?.[tokenAttribute] ? { ...token, [tokenAttribute]: newToken[tokenAttribute] } : { ...token };
652
+ });
653
+ this.update((state) => {
654
+ state.tokens = newTokens;
655
+ });
656
+ };
657
+ _detectIsERC721 = new WeakSet();
658
+ detectIsERC721_fn = async function(tokenAddress, networkClientId) {
659
+ const checksumAddress = toChecksumHexAddress(tokenAddress);
660
+ if (contractsMap[checksumAddress]?.erc721 === true) {
661
+ return Promise.resolve(true);
662
+ } else if (contractsMap[checksumAddress]?.erc20 === true) {
663
+ return Promise.resolve(false);
664
+ }
665
+ const tokenContract = __privateMethod(this, _createEthersContract, createEthersContract_fn).call(this, tokenAddress, abiERC721, networkClientId);
666
+ try {
667
+ return await tokenContract.supportsInterface(ERC721_INTERFACE_ID);
668
+ } catch (error) {
669
+ return false;
670
+ }
671
+ };
672
+ _getProvider = new WeakSet();
673
+ getProvider_fn = function(networkClientId) {
674
+ return new Web3Provider(
675
+ // @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
676
+ networkClientId ? this.messagingSystem.call(
677
+ "NetworkController:getNetworkClientById",
678
+ networkClientId
679
+ ).provider : __privateGet(this, _provider)
680
+ );
681
+ };
682
+ _createEthersContract = new WeakSet();
683
+ createEthersContract_fn = function(tokenAddress, abi, networkClientId) {
684
+ const web3provider = __privateMethod(this, _getProvider, getProvider_fn).call(this, networkClientId);
685
+ const tokenContract = new Contract(tokenAddress, abi, web3provider);
686
+ return tokenContract;
687
+ };
688
+ _generateRandomId = new WeakSet();
689
+ generateRandomId_fn = function() {
690
+ return random();
691
+ };
692
+ _getNewAllTokensState = new WeakSet();
693
+ getNewAllTokensState_fn = function(params) {
694
+ const {
695
+ newTokens,
696
+ newIgnoredTokens,
697
+ newDetectedTokens,
698
+ interactingAddress,
699
+ interactingChainId
700
+ } = params;
701
+ const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
702
+ const userAddressToAddTokens = interactingAddress ?? __privateGet(this, _selectedAddress);
703
+ const chainIdToAddTokens = interactingChainId ?? __privateGet(this, _chainId);
704
+ let newAllTokens = allTokens;
705
+ if (newTokens?.length || newTokens && allTokens && allTokens[chainIdToAddTokens] && allTokens[chainIdToAddTokens][userAddressToAddTokens]) {
706
+ const networkTokens = allTokens[chainIdToAddTokens];
707
+ const newNetworkTokens = {
708
+ ...networkTokens,
709
+ ...{ [userAddressToAddTokens]: newTokens }
710
+ };
711
+ newAllTokens = {
712
+ ...allTokens,
713
+ ...{ [chainIdToAddTokens]: newNetworkTokens }
714
+ };
695
715
  }
716
+ let newAllIgnoredTokens = allIgnoredTokens;
717
+ if (newIgnoredTokens?.length || newIgnoredTokens && allIgnoredTokens && allIgnoredTokens[chainIdToAddTokens] && allIgnoredTokens[chainIdToAddTokens][userAddressToAddTokens]) {
718
+ const networkIgnoredTokens = allIgnoredTokens[chainIdToAddTokens];
719
+ const newIgnoredNetworkTokens = {
720
+ ...networkIgnoredTokens,
721
+ ...{ [userAddressToAddTokens]: newIgnoredTokens }
722
+ };
723
+ newAllIgnoredTokens = {
724
+ ...allIgnoredTokens,
725
+ ...{ [chainIdToAddTokens]: newIgnoredNetworkTokens }
726
+ };
727
+ }
728
+ let newAllDetectedTokens = allDetectedTokens;
729
+ if (newDetectedTokens?.length || newDetectedTokens && allDetectedTokens && allDetectedTokens[chainIdToAddTokens] && allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens]) {
730
+ const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens];
731
+ const newDetectedNetworkTokens = {
732
+ ...networkDetectedTokens,
733
+ ...{ [userAddressToAddTokens]: newDetectedTokens }
734
+ };
735
+ newAllDetectedTokens = {
736
+ ...allDetectedTokens,
737
+ ...{ [chainIdToAddTokens]: newDetectedNetworkTokens }
738
+ };
739
+ }
740
+ return { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens };
741
+ };
742
+ _requestApproval = new WeakSet();
743
+ requestApproval_fn = async function(suggestedAssetMeta) {
744
+ return this.messagingSystem.call(
745
+ "ApprovalController:addRequest",
746
+ {
747
+ id: suggestedAssetMeta.id,
748
+ origin: ORIGIN_METAMASK,
749
+ type: ApprovalType.WatchAsset,
750
+ requestData: {
751
+ id: suggestedAssetMeta.id,
752
+ interactingAddress: suggestedAssetMeta.interactingAddress,
753
+ asset: {
754
+ address: suggestedAssetMeta.asset.address,
755
+ decimals: suggestedAssetMeta.asset.decimals,
756
+ symbol: suggestedAssetMeta.asset.symbol,
757
+ image: suggestedAssetMeta.asset.image || null
758
+ }
759
+ }
760
+ },
761
+ true
762
+ );
696
763
  };
697
764
  var TokensController_default = TokensController;
698
765
 
@@ -701,4 +768,4 @@ export {
701
768
  TokensController,
702
769
  TokensController_default
703
770
  };
704
- //# sourceMappingURL=chunk-7MMEHAKG.mjs.map
771
+ //# sourceMappingURL=chunk-73F3SN5O.mjs.map