@metamask-previews/assets-controllers 28.0.0-preview-cb17e99 → 28.0.0-preview-96ea89dc

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 (44) hide show
  1. package/dist/TokenBalancesController.js +2 -2
  2. package/dist/TokenBalancesController.mjs +1 -1
  3. package/dist/TokenDetectionController.js +2 -2
  4. package/dist/TokenDetectionController.mjs +1 -1
  5. package/dist/TokenRatesController.js +2 -2
  6. package/dist/TokenRatesController.mjs +1 -1
  7. package/dist/TokensController.js +2 -2
  8. package/dist/TokensController.mjs +1 -1
  9. package/dist/{chunk-65FXYE6I.mjs → chunk-4LY47RPI.mjs} +5 -11
  10. package/dist/chunk-4LY47RPI.mjs.map +1 -0
  11. package/dist/{chunk-6VBIGDGQ.mjs → chunk-6XOM7KOQ.mjs} +22 -75
  12. package/dist/chunk-6XOM7KOQ.mjs.map +1 -0
  13. package/dist/{chunk-YUKHDFUN.mjs → chunk-D3PCUDTX.mjs} +21 -46
  14. package/dist/chunk-D3PCUDTX.mjs.map +1 -0
  15. package/dist/{chunk-KYJXDVWZ.js → chunk-I53XGBU3.js} +21 -46
  16. package/dist/chunk-I53XGBU3.js.map +1 -0
  17. package/dist/{chunk-JZGMPO5X.js → chunk-IOQX3VTD.js} +13 -25
  18. package/dist/chunk-IOQX3VTD.js.map +1 -0
  19. package/dist/{chunk-N4J3LGOF.js → chunk-LD4GC7OR.js} +5 -11
  20. package/dist/chunk-LD4GC7OR.js.map +1 -0
  21. package/dist/{chunk-W4JCZ6UQ.mjs → chunk-OWTCG2N3.mjs} +13 -25
  22. package/dist/chunk-OWTCG2N3.mjs.map +1 -0
  23. package/dist/{chunk-TDK4U47T.js → chunk-SOK5YX7I.js} +22 -75
  24. package/dist/chunk-SOK5YX7I.js.map +1 -0
  25. package/dist/index.js +5 -5
  26. package/dist/index.mjs +4 -4
  27. package/dist/tsconfig.build.tsbuildinfo +1 -1
  28. package/dist/types/TokenBalancesController.d.ts +2 -2
  29. package/dist/types/TokenBalancesController.d.ts.map +1 -1
  30. package/dist/types/TokenDetectionController.d.ts +5 -5
  31. package/dist/types/TokenDetectionController.d.ts.map +1 -1
  32. package/dist/types/TokenRatesController.d.ts +7 -10
  33. package/dist/types/TokenRatesController.d.ts.map +1 -1
  34. package/dist/types/TokensController.d.ts +5 -5
  35. package/dist/types/TokensController.d.ts.map +1 -1
  36. package/package.json +2 -2
  37. package/dist/chunk-65FXYE6I.mjs.map +0 -1
  38. package/dist/chunk-6VBIGDGQ.mjs.map +0 -1
  39. package/dist/chunk-JZGMPO5X.js.map +0 -1
  40. package/dist/chunk-KYJXDVWZ.js.map +0 -1
  41. package/dist/chunk-N4J3LGOF.js.map +0 -1
  42. package/dist/chunk-TDK4U47T.js.map +0 -1
  43. package/dist/chunk-W4JCZ6UQ.mjs.map +0 -1
  44. package/dist/chunk-YUKHDFUN.mjs.map +0 -1
@@ -2,11 +2,11 @@
2
2
 
3
3
 
4
4
 
5
- var _chunkN4J3LGOFjs = require('./chunk-N4J3LGOF.js');
5
+ var _chunkLD4GC7ORjs = require('./chunk-LD4GC7OR.js');
6
6
  require('./chunk-Z4BLTVTB.js');
7
7
 
8
8
 
9
9
 
10
10
 
11
- exports.TokenBalancesController = _chunkN4J3LGOFjs.TokenBalancesController; exports.default = _chunkN4J3LGOFjs.TokenBalancesController_default; exports.getDefaultTokenBalancesState = _chunkN4J3LGOFjs.getDefaultTokenBalancesState;
11
+ exports.TokenBalancesController = _chunkLD4GC7ORjs.TokenBalancesController; exports.default = _chunkLD4GC7ORjs.TokenBalancesController_default; exports.getDefaultTokenBalancesState = _chunkLD4GC7ORjs.getDefaultTokenBalancesState;
12
12
  //# sourceMappingURL=TokenBalancesController.js.map
@@ -2,7 +2,7 @@ import {
2
2
  TokenBalancesController,
3
3
  TokenBalancesController_default,
4
4
  getDefaultTokenBalancesState
5
- } from "./chunk-65FXYE6I.mjs";
5
+ } from "./chunk-4LY47RPI.mjs";
6
6
  import "./chunk-XUI43LEZ.mjs";
7
7
  export {
8
8
  TokenBalancesController,
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- var _chunkKYJXDVWZjs = require('./chunk-KYJXDVWZ.js');
7
+ var _chunkI53XGBU3js = require('./chunk-I53XGBU3.js');
8
8
  require('./chunk-LS6R3HQL.js');
9
9
  require('./chunk-Z4BLTVTB.js');
10
10
 
@@ -13,5 +13,5 @@ require('./chunk-Z4BLTVTB.js');
13
13
 
14
14
 
15
15
 
16
- exports.STATIC_MAINNET_TOKEN_LIST = _chunkKYJXDVWZjs.STATIC_MAINNET_TOKEN_LIST; exports.TokenDetectionController = _chunkKYJXDVWZjs.TokenDetectionController; exports.controllerName = _chunkKYJXDVWZjs.controllerName; exports.default = _chunkKYJXDVWZjs.TokenDetectionController_default; exports.isEqualCaseInsensitive = _chunkKYJXDVWZjs.isEqualCaseInsensitive;
16
+ exports.STATIC_MAINNET_TOKEN_LIST = _chunkI53XGBU3js.STATIC_MAINNET_TOKEN_LIST; exports.TokenDetectionController = _chunkI53XGBU3js.TokenDetectionController; exports.controllerName = _chunkI53XGBU3js.controllerName; exports.default = _chunkI53XGBU3js.TokenDetectionController_default; exports.isEqualCaseInsensitive = _chunkI53XGBU3js.isEqualCaseInsensitive;
17
17
  //# sourceMappingURL=TokenDetectionController.js.map
@@ -4,7 +4,7 @@ import {
4
4
  TokenDetectionController_default,
5
5
  controllerName,
6
6
  isEqualCaseInsensitive
7
- } from "./chunk-YUKHDFUN.mjs";
7
+ } from "./chunk-D3PCUDTX.mjs";
8
8
  import "./chunk-X5PLVMOQ.mjs";
9
9
  import "./chunk-XUI43LEZ.mjs";
10
10
  export {
@@ -1,12 +1,12 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkJZGMPO5Xjs = require('./chunk-JZGMPO5X.js');
4
+ var _chunkIOQX3VTDjs = require('./chunk-IOQX3VTD.js');
5
5
  require('./chunk-DYH5P3VY.js');
6
6
  require('./chunk-LS6R3HQL.js');
7
7
  require('./chunk-Z4BLTVTB.js');
8
8
 
9
9
 
10
10
 
11
- exports.TokenRatesController = _chunkJZGMPO5Xjs.TokenRatesController; exports.default = _chunkJZGMPO5Xjs.TokenRatesController_default;
11
+ exports.TokenRatesController = _chunkIOQX3VTDjs.TokenRatesController; exports.default = _chunkIOQX3VTDjs.TokenRatesController_default;
12
12
  //# sourceMappingURL=TokenRatesController.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  TokenRatesController,
3
3
  TokenRatesController_default
4
- } from "./chunk-W4JCZ6UQ.mjs";
4
+ } from "./chunk-OWTCG2N3.mjs";
5
5
  import "./chunk-PWZE6KJV.mjs";
6
6
  import "./chunk-X5PLVMOQ.mjs";
7
7
  import "./chunk-XUI43LEZ.mjs";
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- var _chunkTDK4U47Tjs = require('./chunk-TDK4U47T.js');
5
+ var _chunkSOK5YX7Ijs = require('./chunk-SOK5YX7I.js');
6
6
  require('./chunk-3ZHN4GFT.js');
7
7
  require('./chunk-BO2WZDUM.js');
8
8
  require('./chunk-OHSQRYVL.js');
@@ -12,5 +12,5 @@ require('./chunk-Z4BLTVTB.js');
12
12
 
13
13
 
14
14
 
15
- exports.TokensController = _chunkTDK4U47Tjs.TokensController; exports.default = _chunkTDK4U47Tjs.TokensController_default; exports.getDefaultTokensState = _chunkTDK4U47Tjs.getDefaultTokensState;
15
+ exports.TokensController = _chunkSOK5YX7Ijs.TokensController; exports.default = _chunkSOK5YX7Ijs.TokensController_default; exports.getDefaultTokensState = _chunkSOK5YX7Ijs.getDefaultTokensState;
16
16
  //# sourceMappingURL=TokensController.js.map
@@ -2,7 +2,7 @@ import {
2
2
  TokensController,
3
3
  TokensController_default,
4
4
  getDefaultTokensState
5
- } from "./chunk-6VBIGDGQ.mjs";
5
+ } from "./chunk-6XOM7KOQ.mjs";
6
6
  import "./chunk-S7UA2DU7.mjs";
7
7
  import "./chunk-LLYYJY7H.mjs";
8
8
  import "./chunk-NXGX7LZJ.mjs";
@@ -5,9 +5,6 @@ import {
5
5
  } from "./chunk-XUI43LEZ.mjs";
6
6
 
7
7
  // src/TokenBalancesController.ts
8
- import {
9
- isEVMAccount
10
- } from "@metamask/accounts-controller";
11
8
  import {
12
9
  BaseController
13
10
  } from "@metamask/base-controller";
@@ -106,18 +103,15 @@ var TokenBalancesController = class extends BaseController {
106
103
  if (__privateGet(this, _disabled)) {
107
104
  return;
108
105
  }
109
- const selectedInternalAccount = this.messagingSystem.call(
110
- "AccountsController:getSelectedAccount"
111
- );
112
- if (!isEVMAccount(selectedInternalAccount)) {
113
- return;
114
- }
115
106
  const newContractBalances = {};
116
107
  for (const token of __privateGet(this, _tokens)) {
117
108
  const { address } = token;
109
+ const { selectedAddress } = this.messagingSystem.call(
110
+ "PreferencesController:getState"
111
+ );
118
112
  try {
119
113
  newContractBalances[address] = toHex(
120
- await __privateGet(this, _getERC20BalanceOf).call(this, address, selectedInternalAccount.address)
114
+ await __privateGet(this, _getERC20BalanceOf).call(this, address, selectedAddress)
121
115
  );
122
116
  token.balanceError = null;
123
117
  } catch (error) {
@@ -142,4 +136,4 @@ export {
142
136
  TokenBalancesController,
143
137
  TokenBalancesController_default
144
138
  };
145
- //# sourceMappingURL=chunk-65FXYE6I.mjs.map
139
+ //# sourceMappingURL=chunk-4LY47RPI.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TokenBalancesController.ts"],"sourcesContent":["import {\n type RestrictedControllerMessenger,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n BaseController,\n} from '@metamask/base-controller';\nimport { safelyExecute, toHex } from '@metamask/controller-utils';\nimport type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';\n\nimport type { AssetsContractController } from './AssetsContractController';\nimport type { Token } from './TokenRatesController';\nimport type { TokensControllerStateChangeEvent } from './TokensController';\n\nconst DEFAULT_INTERVAL = 180000;\n\nconst controllerName = 'TokenBalancesController';\n\nconst metadata = {\n contractBalances: { persist: true, anonymous: false },\n};\n\n/**\n * Token balances controller options\n * @property interval - Polling interval used to fetch new token balances.\n * @property tokens - List of tokens to track balances for.\n * @property disabled - If set to true, all tracked tokens contract balances updates are blocked.\n * @property getERC20BalanceOf - Gets the balance of the given account at the given contract address.\n */\ntype TokenBalancesControllerOptions = {\n interval?: number;\n tokens?: Token[];\n disabled?: boolean;\n getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];\n messenger: TokenBalancesControllerMessenger;\n state?: Partial<TokenBalancesControllerState>;\n};\n\n/**\n * Represents a mapping of hash token contract addresses to their balances.\n */\ntype ContractBalances = Record<string, string>;\n\n/**\n * Token balances controller state\n * @property contractBalances - Hash of token contract addresses to balances\n */\nexport type TokenBalancesControllerState = {\n contractBalances: ContractBalances;\n};\n\nexport type TokenBalancesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n TokenBalancesControllerState\n>;\n\nexport type TokenBalancesControllerActions =\n TokenBalancesControllerGetStateAction;\n\nexport type AllowedActions = PreferencesControllerGetStateAction;\n\nexport type TokenBalancesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n TokenBalancesControllerState\n >;\n\nexport type TokenBalancesControllerEvents =\n TokenBalancesControllerStateChangeEvent;\n\nexport type AllowedEvents = TokensControllerStateChangeEvent;\n\nexport type TokenBalancesControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n TokenBalancesControllerActions | AllowedActions,\n TokenBalancesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Get the default TokenBalancesController state.\n *\n * @returns The default TokenBalancesController state.\n */\nexport function getDefaultTokenBalancesState(): TokenBalancesControllerState {\n return {\n contractBalances: {},\n };\n}\n\n/**\n * Controller that passively polls on a set interval token balances\n * for tokens stored in the TokensController\n */\nexport class TokenBalancesController extends BaseController<\n typeof controllerName,\n TokenBalancesControllerState,\n TokenBalancesControllerMessenger\n> {\n #handle?: ReturnType<typeof setTimeout>;\n\n #getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];\n\n #interval: number;\n\n #tokens: Token[];\n\n #disabled: boolean;\n\n /**\n * Construct a Token Balances Controller.\n *\n * @param options - The controller options.\n * @param options.interval - Polling interval used to fetch new token balances.\n * @param options.tokens - List of tokens to track balances for.\n * @param options.disabled - If set to true, all tracked tokens contract balances updates are blocked.\n * @param options.getERC20BalanceOf - Gets the balance of the given account at the given contract address.\n * @param options.state - Initial state to set on this controller.\n * @param options.messenger - The controller restricted messenger.\n */\n constructor({\n interval = DEFAULT_INTERVAL,\n tokens = [],\n disabled = false,\n getERC20BalanceOf,\n messenger,\n state = {},\n }: TokenBalancesControllerOptions) {\n super({\n name: controllerName,\n metadata,\n messenger,\n state: {\n ...getDefaultTokenBalancesState(),\n ...state,\n },\n });\n\n this.#disabled = disabled;\n this.#interval = interval;\n this.#tokens = tokens;\n\n this.messagingSystem.subscribe(\n 'TokensController:stateChange',\n ({ tokens: newTokens, detectedTokens }) => {\n this.#tokens = [...newTokens, ...detectedTokens];\n this.updateBalances();\n },\n );\n\n this.#getERC20BalanceOf = getERC20BalanceOf;\n\n this.poll();\n }\n\n /**\n * Allows controller to update tracked tokens contract balances.\n */\n enable() {\n this.#disabled = false;\n }\n\n /**\n * Blocks controller from updating tracked tokens contract balances.\n */\n disable() {\n this.#disabled = true;\n }\n\n /**\n * Starts a new polling interval.\n *\n * @param interval - Polling interval used to fetch new token balances.\n */\n async poll(interval?: number): Promise<void> {\n if (interval) {\n this.#interval = interval;\n }\n\n if (this.#handle) {\n clearTimeout(this.#handle);\n }\n\n await safelyExecute(() => this.updateBalances());\n\n this.#handle = setTimeout(() => {\n this.poll(this.#interval);\n }, this.#interval);\n }\n\n /**\n * Updates balances for all tokens.\n */\n async updateBalances() {\n if (this.#disabled) {\n return;\n }\n\n const newContractBalances: ContractBalances = {};\n for (const token of this.#tokens) {\n const { address } = token;\n const { selectedAddress } = this.messagingSystem.call(\n 'PreferencesController:getState',\n );\n try {\n newContractBalances[address] = toHex(\n await this.#getERC20BalanceOf(address, selectedAddress),\n );\n token.balanceError = null;\n } catch (error) {\n newContractBalances[address] = toHex(0);\n token.balanceError = error;\n }\n }\n\n this.update((state) => {\n state.contractBalances = newContractBalances;\n });\n }\n}\n\nexport default TokenBalancesController;\n"],"mappings":";;;;;;;AAAA;AAAA,EAIE;AAAA,OACK;AACP,SAAS,eAAe,aAAa;AAOrC,IAAM,mBAAmB;AAEzB,IAAM,iBAAiB;AAEvB,IAAM,WAAW;AAAA,EACf,kBAAkB,EAAE,SAAS,MAAM,WAAW,MAAM;AACtD;AAiEO,SAAS,+BAA6D;AAC3E,SAAO;AAAA,IACL,kBAAkB,CAAC;AAAA,EACrB;AACF;AAxFA;AA8FO,IAAM,0BAAN,cAAsC,eAI3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBA,YAAY;AAAA,IACV,WAAW;AAAA,IACX,SAAS,CAAC;AAAA,IACV,WAAW;AAAA,IACX;AAAA,IACA;AAAA,IACA,QAAQ,CAAC;AAAA,EACX,GAAmC;AACjC,UAAM;AAAA,MACJ,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA,OAAO;AAAA,QACL,GAAG,6BAA6B;AAAA,QAChC,GAAG;AAAA,MACL;AAAA,IACF,CAAC;AArCH;AAEA;AAEA;AAEA;AAEA;AA+BE,uBAAK,WAAY;AACjB,uBAAK,WAAY;AACjB,uBAAK,SAAU;AAEf,SAAK,gBAAgB;AAAA,MACnB;AAAA,MACA,CAAC,EAAE,QAAQ,WAAW,eAAe,MAAM;AACzC,2BAAK,SAAU,CAAC,GAAG,WAAW,GAAG,cAAc;AAC/C,aAAK,eAAe;AAAA,MACtB;AAAA,IACF;AAEA,uBAAK,oBAAqB;AAE1B,SAAK,KAAK;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA,EAKA,SAAS;AACP,uBAAK,WAAY;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU;AACR,uBAAK,WAAY;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,KAAK,UAAkC;AAC3C,QAAI,UAAU;AACZ,yBAAK,WAAY;AAAA,IACnB;AAEA,QAAI,mBAAK,UAAS;AAChB,mBAAa,mBAAK,QAAO;AAAA,IAC3B;AAEA,UAAM,cAAc,MAAM,KAAK,eAAe,CAAC;AAE/C,uBAAK,SAAU,WAAW,MAAM;AAC9B,WAAK,KAAK,mBAAK,UAAS;AAAA,IAC1B,GAAG,mBAAK,UAAS;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,iBAAiB;AACrB,QAAI,mBAAK,YAAW;AAClB;AAAA,IACF;AAEA,UAAM,sBAAwC,CAAC;AAC/C,eAAW,SAAS,mBAAK,UAAS;AAChC,YAAM,EAAE,QAAQ,IAAI;AACpB,YAAM,EAAE,gBAAgB,IAAI,KAAK,gBAAgB;AAAA,QAC/C;AAAA,MACF;AACA,UAAI;AACF,4BAAoB,OAAO,IAAI;AAAA,UAC7B,MAAM,mBAAK,oBAAL,WAAwB,SAAS;AAAA,QACzC;AACA,cAAM,eAAe;AAAA,MACvB,SAAS,OAAO;AACd,4BAAoB,OAAO,IAAI,MAAM,CAAC;AACtC,cAAM,eAAe;AAAA,MACvB;AAAA,IACF;AAEA,SAAK,OAAO,CAAC,UAAU;AACrB,YAAM,mBAAmB;AAAA,IAC3B,CAAC;AAAA,EACH;AACF;AAxHE;AAEA;AAEA;AAEA;AAEA;AAkHF,IAAO,kCAAQ;","names":[]}
@@ -16,7 +16,6 @@ import {
16
16
  // src/TokensController.ts
17
17
  import { Contract } from "@ethersproject/contracts";
18
18
  import { Web3Provider } from "@ethersproject/providers";
19
- import { isEVMAccount } from "@metamask/accounts-controller";
20
19
  import { BaseControllerV1 } from "@metamask/base-controller";
21
20
  import contractsMap from "@metamask/contract-metadata";
22
21
  import {
@@ -73,7 +72,7 @@ var TokensController = class extends BaseControllerV1 {
73
72
  */
74
73
  this.name = "TokensController";
75
74
  this.defaultConfig = {
76
- selectedAccountId: "",
75
+ selectedAddress: "",
77
76
  chainId: initialChainId,
78
77
  provider: void 0,
79
78
  ...config
@@ -90,18 +89,15 @@ var TokensController = class extends BaseControllerV1 {
90
89
  this.addDetectedTokens.bind(this)
91
90
  );
92
91
  this.messagingSystem.subscribe(
93
- "AccountsController:selectedAccountChange",
94
- (internalAccount) => {
95
- this.configure({ selectedAccountId: internalAccount.id });
96
- if (!isEVMAccount(internalAccount)) {
97
- return;
98
- }
92
+ "PreferencesController:stateChange",
93
+ ({ selectedAddress }) => {
99
94
  const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
100
95
  const { chainId } = this.config;
96
+ this.configure({ selectedAddress });
101
97
  this.update({
102
- tokens: allTokens[chainId]?.[internalAccount.address] ?? [],
103
- ignoredTokens: allIgnoredTokens[chainId]?.[internalAccount.address] ?? [],
104
- detectedTokens: allDetectedTokens[chainId]?.[internalAccount.address] ?? []
98
+ tokens: allTokens[chainId]?.[selectedAddress] ?? [],
99
+ ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] ?? [],
100
+ detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] ?? []
105
101
  });
106
102
  }
107
103
  );
@@ -109,22 +105,15 @@ var TokensController = class extends BaseControllerV1 {
109
105
  "NetworkController:networkDidChange",
110
106
  ({ providerConfig }) => {
111
107
  const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
112
- const { selectedAccountId } = this.config;
113
- const selectedInternalAccount = this.messagingSystem.call(
114
- "AccountsController:getAccount",
115
- selectedAccountId
116
- );
108
+ const { selectedAddress } = this.config;
117
109
  const { chainId } = providerConfig;
118
110
  this.abortController.abort();
119
111
  this.abortController = new AbortController();
120
112
  this.configure({ chainId });
121
- if (!selectedInternalAccount || !isEVMAccount(selectedInternalAccount)) {
122
- return;
123
- }
124
113
  this.update({
125
- tokens: allTokens[chainId]?.[selectedInternalAccount.address] || [],
126
- ignoredTokens: allIgnoredTokens[chainId]?.[selectedInternalAccount.address] || [],
127
- detectedTokens: allDetectedTokens[chainId]?.[selectedInternalAccount.address] || []
114
+ tokens: allTokens[chainId]?.[selectedAddress] || [],
115
+ ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] || [],
116
+ detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] || []
128
117
  });
129
118
  }
130
119
  );
@@ -181,7 +170,7 @@ var TokensController = class extends BaseControllerV1 {
181
170
  interactingAddress,
182
171
  networkClientId
183
172
  }) {
184
- const { chainId, selectedAccountId } = this.config;
173
+ const { chainId, selectedAddress } = this.config;
185
174
  const releaseLock = await this.mutex.acquire();
186
175
  const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
187
176
  let currentChainId = chainId;
@@ -191,16 +180,8 @@ var TokensController = class extends BaseControllerV1 {
191
180
  networkClientId
192
181
  ).configuration.chainId;
193
182
  }
194
- const internalAccount = this.messagingSystem.call(
195
- "AccountsController:getAccount",
196
- selectedAccountId
197
- );
198
- if (!internalAccount || !isEVMAccount(internalAccount)) {
199
- releaseLock();
200
- return [];
201
- }
202
- const accountAddress = interactingAddress || internalAccount.address;
203
- const isInteractingWithWalletAccount = accountAddress === internalAccount.address;
183
+ const accountAddress = interactingAddress || selectedAddress;
184
+ const isInteractingWithWalletAccount = accountAddress === selectedAddress;
204
185
  try {
205
186
  address = toChecksumHexAddress(address);
206
187
  const tokens = allTokens[currentChainId]?.[accountAddress] || [];
@@ -376,16 +357,8 @@ var TokensController = class extends BaseControllerV1 {
376
357
  */
377
358
  async addDetectedTokens(incomingDetectedTokens, detectionDetails) {
378
359
  const releaseLock = await this.mutex.acquire();
379
- const internalAccount = this.messagingSystem.call(
380
- "AccountsController:getAccount",
381
- this.config.selectedAccountId
382
- );
383
- if (!internalAccount || !isEVMAccount(internalAccount)) {
384
- releaseLock();
385
- return;
386
- }
387
360
  const chainId = detectionDetails?.chainId ?? this.config.chainId;
388
- const accountAddress = detectionDetails?.selectedAddress ?? internalAccount.address;
361
+ const accountAddress = detectionDetails?.selectedAddress ?? this.config.selectedAddress;
389
362
  const { allTokens, allDetectedTokens, allIgnoredTokens } = this.state;
390
363
  let newTokens = [...allTokens?.[chainId]?.[accountAddress] ?? []];
391
364
  let newDetectedTokens = [
@@ -439,17 +412,9 @@ var TokensController = class extends BaseControllerV1 {
439
412
  interactingChainId: chainId
440
413
  }
441
414
  );
442
- const { chainId: currentChain, selectedAccountId: currentAccountId } = this.config;
443
- const currentInternalAccount = this.messagingSystem.call(
444
- "AccountsController:getAccount",
445
- currentAccountId
446
- );
447
- if (!currentInternalAccount || !isEVMAccount(currentInternalAccount)) {
448
- releaseLock();
449
- return;
450
- }
451
- newTokens = newAllTokens?.[currentChain]?.[currentInternalAccount.address] || [];
452
- newDetectedTokens = newAllDetectedTokens?.[currentChain]?.[currentInternalAccount.address] || [];
415
+ const { chainId: currentChain, selectedAddress: currentAddress } = this.config;
416
+ newTokens = newAllTokens?.[currentChain]?.[currentAddress] || [];
417
+ newDetectedTokens = newAllDetectedTokens?.[currentChain]?.[currentAddress] || [];
453
418
  this.update({
454
419
  tokens: newTokens,
455
420
  allTokens: newAllTokens,
@@ -560,13 +525,6 @@ var TokensController = class extends BaseControllerV1 {
560
525
  if (!isValidHexAddress(asset.address)) {
561
526
  throw rpcErrors.invalidParams(`Invalid address "${asset.address}"`);
562
527
  }
563
- const selectedAccount = this.messagingSystem.call(
564
- "AccountsController:getAccount",
565
- this.config.selectedAccountId
566
- );
567
- if (!selectedAccount || !isEVMAccount(selectedAccount)) {
568
- throw new Error(`Account is not an EVM account`);
569
- }
570
528
  if (await this._detectIsERC721(asset.address, networkClientId)) {
571
529
  throw rpcErrors.invalidParams(
572
530
  `Contract ${asset.address} must match type ${type}, but was detected as ${ERC721}`
@@ -632,7 +590,7 @@ var TokensController = class extends BaseControllerV1 {
632
590
  id: this._generateRandomId(),
633
591
  time: Date.now(),
634
592
  type,
635
- interactingAddress: interactingAddress || selectedAccount.address
593
+ interactingAddress: interactingAddress || this.config.selectedAddress
636
594
  };
637
595
  await this._requestApproval(suggestedAssetMeta);
638
596
  const { address, symbol, decimals, name, image } = asset;
@@ -667,19 +625,8 @@ var TokensController = class extends BaseControllerV1 {
667
625
  interactingChainId
668
626
  } = params;
669
627
  const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
670
- const { chainId, selectedAccountId } = this.config;
671
- const selectedInternalAccount = this.messagingSystem.call(
672
- "AccountsController:getAccount",
673
- selectedAccountId
674
- );
675
- if (!selectedInternalAccount || !isEVMAccount(selectedInternalAccount)) {
676
- return {
677
- newAllTokens: allTokens,
678
- newAllIgnoredTokens: allIgnoredTokens,
679
- newAllDetectedTokens: allDetectedTokens
680
- };
681
- }
682
- const userAddressToAddTokens = interactingAddress ?? selectedInternalAccount.address;
628
+ const { chainId, selectedAddress } = this.config;
629
+ const userAddressToAddTokens = interactingAddress ?? selectedAddress;
683
630
  const chainIdToAddTokens = interactingChainId ?? chainId;
684
631
  let newAllTokens = allTokens;
685
632
  if (newTokens?.length || newTokens && allTokens && allTokens[chainIdToAddTokens] && allTokens[chainIdToAddTokens][userAddressToAddTokens]) {
@@ -754,4 +701,4 @@ export {
754
701
  TokensController,
755
702
  TokensController_default
756
703
  };
757
- //# sourceMappingURL=chunk-6VBIGDGQ.mjs.map
704
+ //# sourceMappingURL=chunk-6XOM7KOQ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TokensController.ts"],"sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport type { AddApprovalRequest } from '@metamask/approval-controller';\nimport type {\n BaseConfig,\n BaseState,\n RestrictedControllerMessenger,\n} from '@metamask/base-controller';\nimport { BaseControllerV1 } from '@metamask/base-controller';\nimport contractsMap from '@metamask/contract-metadata';\nimport {\n toChecksumHexAddress,\n ERC721_INTERFACE_ID,\n ORIGIN_METAMASK,\n ApprovalType,\n ERC20,\n ERC721,\n ERC1155,\n isValidHexAddress,\n safelyExecute,\n} from '@metamask/controller-utils';\nimport { abiERC721 } from '@metamask/metamask-eth-abis';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerNetworkDidChangeEvent,\n Provider,\n} from '@metamask/network-controller';\nimport type { PreferencesControllerStateChangeEvent } from '@metamask/preferences-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\nimport { EventEmitter } from 'events';\nimport type { Patch } from 'immer/dist/immer';\nimport { v1 as random } from 'uuid';\n\nimport { formatAggregatorNames, formatIconUrlWithProxy } from './assetsUtil';\nimport { ERC20Standard } from './Standards/ERC20Standard';\nimport { ERC1155Standard } from './Standards/NftStandards/ERC1155/ERC1155Standard';\nimport {\n fetchTokenMetadata,\n TOKEN_METADATA_NO_SUPPORT_ERROR,\n} from './token-service';\nimport type {\n TokenListMap,\n TokenListStateChange,\n TokenListToken,\n} from './TokenListController';\nimport type { Token } from './TokenRatesController';\n\n/**\n * @type TokensConfig\n *\n * Tokens controller configuration\n * @property selectedAddress - Vault selected address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface TokensConfig extends BaseConfig {\n selectedAddress: string;\n chainId: Hex;\n provider: Provider | undefined;\n}\n\n/**\n * @type SuggestedAssetMeta\n *\n * Suggested asset by EIP747 meta data\n * @property id - Generated UUID associated with this suggested asset\n * @property time - Timestamp associated with this this suggested asset\n * @property type - Type type this suggested asset\n * @property asset - Asset suggested object\n * @property interactingAddress - Account address that requested watch asset\n */\ntype SuggestedAssetMeta = {\n id: string;\n time: number;\n type: string;\n asset: Token;\n interactingAddress: string;\n};\n\n/**\n * @type TokensState\n *\n * Assets controller state\n * @property tokens - List of tokens associated with the active network and address pair\n * @property ignoredTokens - List of ignoredTokens associated with the active network and address pair\n * @property detectedTokens - List of detected tokens associated with the active network and address pair\n * @property allTokens - Object containing tokens by network and account\n * @property allIgnoredTokens - Object containing hidden/ignored tokens by network and account\n * @property allDetectedTokens - Object containing tokens detected with non-zero balances\n */\nexport type TokensState = {\n tokens: Token[];\n ignoredTokens: string[];\n detectedTokens: Token[];\n allTokens: { [chainId: Hex]: { [key: string]: Token[] } };\n allIgnoredTokens: { [chainId: Hex]: { [key: string]: string[] } };\n allDetectedTokens: { [chainId: Hex]: { [key: string]: Token[] } };\n};\n\n/**\n * The name of the {@link TokensController}.\n */\nconst controllerName = 'TokensController';\n\nexport type TokensControllerActions =\n | TokensControllerGetStateAction\n | TokensControllerAddDetectedTokensAction;\n\n// TODO: Once `TokensController` is upgraded to V2, rewrite this type using the `ControllerGetStateAction` type, which constrains `TokensState` as `Record<string, Json>`.\nexport type TokensControllerGetStateAction = {\n type: `${typeof controllerName}:getState`;\n handler: () => TokensState;\n};\n\nexport type TokensControllerAddDetectedTokensAction = {\n type: `${typeof controllerName}:addDetectedTokens`;\n handler: TokensController['addDetectedTokens'];\n};\n\n/**\n * The external actions available to the {@link TokensController}.\n */\nexport type AllowedActions =\n | AddApprovalRequest\n | NetworkControllerGetNetworkClientByIdAction;\n\n// TODO: Once `TokensController` is upgraded to V2, rewrite this type using the `ControllerStateChangeEvent` type, which constrains `TokensState` as `Record<string, Json>`.\nexport type TokensControllerStateChangeEvent = {\n type: `${typeof controllerName}:stateChange`;\n payload: [TokensState, Patch[]];\n};\n\nexport type TokensControllerEvents = TokensControllerStateChangeEvent;\n\nexport type AllowedEvents =\n | NetworkControllerNetworkDidChangeEvent\n | PreferencesControllerStateChangeEvent\n | TokenListStateChange;\n\n/**\n * The messenger of the {@link TokensController}.\n */\nexport type TokensControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n TokensControllerActions | AllowedActions,\n TokensControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const getDefaultTokensState = (): TokensState => {\n return {\n tokens: [],\n ignoredTokens: [],\n detectedTokens: [],\n allTokens: {},\n allIgnoredTokens: {},\n allDetectedTokens: {},\n };\n};\n\n/**\n * Controller that stores assets and exposes convenience methods\n */\nexport class TokensController extends BaseControllerV1<\n TokensConfig,\n TokensState & BaseState\n> {\n private readonly mutex = new Mutex();\n\n private abortController: AbortController;\n\n private readonly messagingSystem: TokensControllerMessenger;\n\n /**\n * Fetch metadata for a token.\n *\n * @param tokenAddress - The address of the token.\n * @returns The token metadata.\n */\n private async fetchTokenMetadata(\n tokenAddress: string,\n ): Promise<TokenListToken | undefined> {\n try {\n const token = await fetchTokenMetadata<TokenListToken>(\n this.config.chainId,\n tokenAddress,\n this.abortController.signal,\n );\n return token;\n } catch (error) {\n if (\n error instanceof Error &&\n error.message.includes(TOKEN_METADATA_NO_SUPPORT_ERROR)\n ) {\n return undefined;\n }\n throw error;\n }\n }\n\n /**\n * EventEmitter instance used to listen to specific EIP747 events\n */\n hub = new EventEmitter();\n\n /**\n * Name of this controller used during composition\n */\n override name = 'TokensController';\n\n /**\n * Creates a TokensController instance.\n *\n * @param options - The controller options.\n * @param options.chainId - The chain ID of the current network.\n * @param options.config - Initial options used to configure this controller.\n * @param options.state - Initial state to set on this controller.\n * @param options.messenger - The controller messenger.\n */\n constructor({\n chainId: initialChainId,\n config,\n state,\n messenger,\n }: {\n chainId: Hex;\n config?: Partial<TokensConfig>;\n state?: Partial<TokensState>;\n messenger: TokensControllerMessenger;\n }) {\n super(config, state);\n\n this.defaultConfig = {\n selectedAddress: '',\n chainId: initialChainId,\n provider: undefined,\n ...config,\n };\n\n this.defaultState = {\n ...getDefaultTokensState(),\n ...state,\n };\n\n this.initialize();\n this.abortController = new AbortController();\n\n this.messagingSystem = messenger;\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:addDetectedTokens` as const,\n this.addDetectedTokens.bind(this),\n );\n\n this.messagingSystem.subscribe(\n 'PreferencesController:stateChange',\n ({ selectedAddress }) => {\n const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;\n const { chainId } = this.config;\n this.configure({ selectedAddress });\n this.update({\n tokens: allTokens[chainId]?.[selectedAddress] ?? [],\n ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] ?? [],\n detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] ?? [],\n });\n },\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:networkDidChange',\n ({ providerConfig }) => {\n const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;\n const { selectedAddress } = this.config;\n const { chainId } = providerConfig;\n this.abortController.abort();\n this.abortController = new AbortController();\n this.configure({ chainId });\n this.update({\n tokens: allTokens[chainId]?.[selectedAddress] || [],\n ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] || [],\n detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] || [],\n });\n },\n );\n\n this.messagingSystem.subscribe(\n 'TokenListController:stateChange',\n ({ tokenList }) => {\n const { tokens } = this.state;\n if (tokens.length && !tokens[0].name) {\n this.updateTokensAttribute(tokenList, 'name');\n }\n },\n );\n }\n\n /**\n * Adds a token to the stored token list.\n *\n * @param options - The method argument object.\n * @param options.address - Hex address of the token contract.\n * @param options.symbol - Symbol of the token.\n * @param options.decimals - Number of decimals the token uses.\n * @param options.name - Name of the token.\n * @param options.image - Image of the token.\n * @param options.interactingAddress - The address of the account to add a token to.\n * @param options.networkClientId - Network Client ID.\n * @returns Current token list.\n */\n async addToken({\n address,\n symbol,\n decimals,\n name,\n image,\n interactingAddress,\n networkClientId,\n }: {\n address: string;\n symbol: string;\n decimals: number;\n name?: string;\n image?: string;\n interactingAddress?: string;\n networkClientId?: NetworkClientId;\n }): Promise<Token[]> {\n const { chainId, selectedAddress } = this.config;\n const releaseLock = await this.mutex.acquire();\n const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;\n let currentChainId = chainId;\n if (networkClientId) {\n currentChainId = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n ).configuration.chainId;\n }\n\n const accountAddress = interactingAddress || selectedAddress;\n const isInteractingWithWalletAccount = accountAddress === selectedAddress;\n\n try {\n address = toChecksumHexAddress(address);\n const tokens = allTokens[currentChainId]?.[accountAddress] || [];\n const ignoredTokens =\n allIgnoredTokens[currentChainId]?.[accountAddress] || [];\n const detectedTokens =\n allDetectedTokens[currentChainId]?.[accountAddress] || [];\n const newTokens: Token[] = [...tokens];\n const [isERC721, tokenMetadata] = await Promise.all([\n this._detectIsERC721(address, networkClientId),\n // TODO parameterize the token metadata fetch by networkClientId\n this.fetchTokenMetadata(address),\n ]);\n // TODO remove this once this method is fully parameterized by networkClientId\n if (!networkClientId && currentChainId !== this.config.chainId) {\n throw new Error(\n 'TokensController Error: Switched networks while adding token',\n );\n }\n const newEntry: Token = {\n address,\n symbol,\n decimals,\n image:\n image ||\n formatIconUrlWithProxy({\n chainId: currentChainId,\n tokenAddress: address,\n }),\n isERC721,\n aggregators: formatAggregatorNames(tokenMetadata?.aggregators || []),\n name,\n };\n const previousIndex = newTokens.findIndex(\n (token) => token.address.toLowerCase() === address.toLowerCase(),\n );\n if (previousIndex !== -1) {\n newTokens[previousIndex] = newEntry;\n } else {\n newTokens.push(newEntry);\n }\n\n const newIgnoredTokens = ignoredTokens.filter(\n (tokenAddress) => tokenAddress.toLowerCase() !== address.toLowerCase(),\n );\n const newDetectedTokens = detectedTokens.filter(\n (token) => token.address.toLowerCase() !== address.toLowerCase(),\n );\n\n const { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens } =\n this._getNewAllTokensState({\n newTokens,\n newIgnoredTokens,\n newDetectedTokens,\n interactingAddress: accountAddress,\n interactingChainId: currentChainId,\n });\n\n let newState: Partial<TokensState> = {\n allTokens: newAllTokens,\n allIgnoredTokens: newAllIgnoredTokens,\n allDetectedTokens: newAllDetectedTokens,\n };\n\n // Only update active tokens if user is interacting with their active wallet account.\n if (isInteractingWithWalletAccount) {\n newState = {\n ...newState,\n tokens: newTokens,\n ignoredTokens: newIgnoredTokens,\n detectedTokens: newDetectedTokens,\n };\n }\n\n this.update(newState);\n return newTokens;\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Add a batch of tokens.\n *\n * @param tokensToImport - Array of tokens to import.\n * @param networkClientId - Optional network client ID used to determine interacting chain ID.\n */\n async addTokens(tokensToImport: Token[], networkClientId?: NetworkClientId) {\n const releaseLock = await this.mutex.acquire();\n const { tokens, detectedTokens, ignoredTokens } = this.state;\n const importedTokensMap: { [key: string]: true } = {};\n // Used later to dedupe imported tokens\n const newTokensMap = tokens.reduce((output, current) => {\n output[current.address] = current;\n return output;\n }, {} as { [address: string]: Token });\n try {\n tokensToImport.forEach((tokenToAdd) => {\n const { address, symbol, decimals, image, aggregators, name } =\n tokenToAdd;\n const checksumAddress = toChecksumHexAddress(address);\n const formattedToken: Token = {\n address: checksumAddress,\n symbol,\n decimals,\n image,\n aggregators,\n name,\n };\n newTokensMap[address] = formattedToken;\n importedTokensMap[address.toLowerCase()] = true;\n return formattedToken;\n });\n const newTokens = Object.values(newTokensMap);\n\n const newDetectedTokens = detectedTokens.filter(\n (token) => !importedTokensMap[token.address.toLowerCase()],\n );\n const newIgnoredTokens = ignoredTokens.filter(\n (tokenAddress) => !newTokensMap[tokenAddress.toLowerCase()],\n );\n\n let interactingChainId;\n if (networkClientId) {\n interactingChainId = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n ).configuration.chainId;\n }\n\n const { newAllTokens, newAllDetectedTokens, newAllIgnoredTokens } =\n this._getNewAllTokensState({\n newTokens,\n newDetectedTokens,\n newIgnoredTokens,\n interactingChainId,\n });\n\n this.update({\n tokens: newTokens,\n allTokens: newAllTokens,\n detectedTokens: newDetectedTokens,\n allDetectedTokens: newAllDetectedTokens,\n ignoredTokens: newIgnoredTokens,\n allIgnoredTokens: newAllIgnoredTokens,\n });\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Ignore a batch of tokens.\n *\n * @param tokenAddressesToIgnore - Array of token addresses to ignore.\n */\n ignoreTokens(tokenAddressesToIgnore: string[]) {\n const { ignoredTokens, detectedTokens, tokens } = this.state;\n const ignoredTokensMap: { [key: string]: true } = {};\n let newIgnoredTokens: string[] = [...ignoredTokens];\n\n const checksummedTokenAddresses = tokenAddressesToIgnore.map((address) => {\n const checksumAddress = toChecksumHexAddress(address);\n ignoredTokensMap[address.toLowerCase()] = true;\n return checksumAddress;\n });\n newIgnoredTokens = [...ignoredTokens, ...checksummedTokenAddresses];\n const newDetectedTokens = detectedTokens.filter(\n (token) => !ignoredTokensMap[token.address.toLowerCase()],\n );\n const newTokens = tokens.filter(\n (token) => !ignoredTokensMap[token.address.toLowerCase()],\n );\n\n const { newAllIgnoredTokens, newAllDetectedTokens, newAllTokens } =\n this._getNewAllTokensState({\n newIgnoredTokens,\n newDetectedTokens,\n newTokens,\n });\n\n this.update({\n ignoredTokens: newIgnoredTokens,\n tokens: newTokens,\n detectedTokens: newDetectedTokens,\n allIgnoredTokens: newAllIgnoredTokens,\n allDetectedTokens: newAllDetectedTokens,\n allTokens: newAllTokens,\n });\n }\n\n /**\n * Adds a batch of detected tokens to the stored token list.\n *\n * @param incomingDetectedTokens - Array of detected tokens to be added or updated.\n * @param detectionDetails - An object containing the chain ID and address of the currently selected network on which the incomingDetectedTokens were detected.\n * @param detectionDetails.selectedAddress - the account address on which the incomingDetectedTokens were detected.\n * @param detectionDetails.chainId - the chainId on which the incomingDetectedTokens were detected.\n */\n async addDetectedTokens(\n incomingDetectedTokens: Token[],\n detectionDetails?: { selectedAddress: string; chainId: Hex },\n ) {\n const releaseLock = await this.mutex.acquire();\n\n // Get existing tokens for the chain + account\n const chainId = detectionDetails?.chainId ?? this.config.chainId;\n const accountAddress =\n detectionDetails?.selectedAddress ?? this.config.selectedAddress;\n\n const { allTokens, allDetectedTokens, allIgnoredTokens } = this.state;\n let newTokens = [...(allTokens?.[chainId]?.[accountAddress] ?? [])];\n let newDetectedTokens = [\n ...(allDetectedTokens?.[chainId]?.[accountAddress] ?? []),\n ];\n\n try {\n incomingDetectedTokens.forEach((tokenToAdd) => {\n const {\n address,\n symbol,\n decimals,\n image,\n aggregators,\n isERC721,\n name,\n } = tokenToAdd;\n const checksumAddress = toChecksumHexAddress(address);\n const newEntry: Token = {\n address: checksumAddress,\n symbol,\n decimals,\n image,\n isERC721,\n aggregators,\n name,\n };\n const previousImportedIndex = newTokens.findIndex(\n (token) =>\n token.address.toLowerCase() === checksumAddress.toLowerCase(),\n );\n if (previousImportedIndex !== -1) {\n // Update existing data of imported token\n newTokens[previousImportedIndex] = newEntry;\n } else {\n const ignoredTokenIndex =\n allIgnoredTokens?.[chainId]?.[accountAddress]?.indexOf(address) ??\n -1;\n\n if (ignoredTokenIndex === -1) {\n // Add detected token\n const previousDetectedIndex = newDetectedTokens.findIndex(\n (token) =>\n token.address.toLowerCase() === checksumAddress.toLowerCase(),\n );\n if (previousDetectedIndex !== -1) {\n newDetectedTokens[previousDetectedIndex] = newEntry;\n } else {\n newDetectedTokens.push(newEntry);\n }\n }\n }\n });\n\n const { newAllTokens, newAllDetectedTokens } = this._getNewAllTokensState(\n {\n newTokens,\n newDetectedTokens,\n interactingAddress: accountAddress,\n interactingChainId: chainId,\n },\n );\n\n // We may be detecting tokens on a different chain/account pair than are currently configured.\n // Re-point `tokens` and `detectedTokens` to keep them referencing the current chain/account.\n const { chainId: currentChain, selectedAddress: currentAddress } =\n this.config;\n\n newTokens = newAllTokens?.[currentChain]?.[currentAddress] || [];\n newDetectedTokens =\n newAllDetectedTokens?.[currentChain]?.[currentAddress] || [];\n\n this.update({\n tokens: newTokens,\n allTokens: newAllTokens,\n detectedTokens: newDetectedTokens,\n allDetectedTokens: newAllDetectedTokens,\n });\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Adds isERC721 field to token object. This is called when a user attempts to add tokens that\n * were previously added which do not yet had isERC721 field.\n *\n * @param tokenAddress - The contract address of the token requiring the isERC721 field added.\n * @returns The new token object with the added isERC721 field.\n */\n async updateTokenType(tokenAddress: string) {\n const isERC721 = await this._detectIsERC721(tokenAddress);\n const { tokens } = this.state;\n const tokenIndex = tokens.findIndex((token) => {\n return token.address.toLowerCase() === tokenAddress.toLowerCase();\n });\n tokens[tokenIndex].isERC721 = isERC721;\n this.update({ tokens });\n return tokens[tokenIndex];\n }\n\n /**\n * This is a function that updates the tokens name for the tokens name if it is not defined.\n *\n * @param tokenList - Represents the fetched token list from service API\n * @param tokenAttribute - Represents the token attribute that we want to update on the token list\n */\n private updateTokensAttribute(\n tokenList: TokenListMap,\n tokenAttribute: keyof Token & keyof TokenListToken,\n ) {\n const { tokens } = this.state;\n\n const newTokens = tokens.map((token) => {\n const newToken = tokenList[token.address.toLowerCase()];\n\n return !token[tokenAttribute] && newToken?.[tokenAttribute]\n ? { ...token, [tokenAttribute]: newToken[tokenAttribute] }\n : { ...token };\n });\n\n this.update({ tokens: newTokens });\n }\n\n /**\n * Detects whether or not a token is ERC-721 compatible.\n *\n * @param tokenAddress - The token contract address.\n * @param networkClientId - Optional network client ID to fetch contract info with.\n * @returns A boolean indicating whether the token address passed in supports the EIP-721\n * interface.\n */\n async _detectIsERC721(\n tokenAddress: string,\n networkClientId?: NetworkClientId,\n ) {\n const checksumAddress = toChecksumHexAddress(tokenAddress);\n // if this token is already in our contract metadata map we don't need\n // to check against the contract\n if (contractsMap[checksumAddress]?.erc721 === true) {\n return Promise.resolve(true);\n } else if (contractsMap[checksumAddress]?.erc20 === true) {\n return Promise.resolve(false);\n }\n\n const tokenContract = this._createEthersContract(\n tokenAddress,\n abiERC721,\n networkClientId,\n );\n try {\n return await tokenContract.supportsInterface(ERC721_INTERFACE_ID);\n } catch (error) {\n // currently we see a variety of errors across different networks when\n // token contracts are not ERC721 compatible. We need to figure out a better\n // way of differentiating token interface types but for now if we get an error\n // we have to assume the token is not ERC721 compatible.\n return false;\n }\n }\n\n _getProvider(networkClientId?: NetworkClientId): Web3Provider {\n return new Web3Provider(\n // @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released\n networkClientId\n ? this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n ).provider\n : this.config.provider,\n );\n }\n\n _createEthersContract(\n tokenAddress: string,\n abi: string,\n networkClientId?: NetworkClientId,\n ): Contract {\n const web3provider = this._getProvider(networkClientId);\n const tokenContract = new Contract(tokenAddress, abi, web3provider);\n return tokenContract;\n }\n\n _generateRandomId(): string {\n return random();\n }\n\n /**\n * Adds a new suggestedAsset to the list of watched assets.\n * Parameters will be validated according to the asset type being watched.\n *\n * @param options - The method options.\n * @param options.asset - The asset to be watched. For now only ERC20 tokens are accepted.\n * @param options.type - The asset type.\n * @param options.interactingAddress - The address of the account that is requesting to watch the asset.\n * @param options.networkClientId - Network Client ID.\n * @returns A promise that resolves if the asset was watched successfully, and rejects otherwise.\n */\n async watchAsset({\n asset,\n type,\n interactingAddress,\n networkClientId,\n }: {\n asset: Token;\n type: string;\n interactingAddress?: string;\n networkClientId?: NetworkClientId;\n }): Promise<void> {\n if (type !== ERC20) {\n throw new Error(`Asset of type ${type} not supported`);\n }\n\n if (!asset.address) {\n throw rpcErrors.invalidParams('Address must be specified');\n }\n\n if (!isValidHexAddress(asset.address)) {\n throw rpcErrors.invalidParams(`Invalid address \"${asset.address}\"`);\n }\n\n // Validate contract\n\n if (await this._detectIsERC721(asset.address, networkClientId)) {\n throw rpcErrors.invalidParams(\n `Contract ${asset.address} must match type ${type}, but was detected as ${ERC721}`,\n );\n }\n\n const provider = this._getProvider(networkClientId);\n const isErc1155 = await safelyExecute(() =>\n new ERC1155Standard(provider).contractSupportsBase1155Interface(\n asset.address,\n ),\n );\n if (isErc1155) {\n throw rpcErrors.invalidParams(\n `Contract ${asset.address} must match type ${type}, but was detected as ${ERC1155}`,\n );\n }\n\n const erc20 = new ERC20Standard(provider);\n const [contractName, contractSymbol, contractDecimals] = await Promise.all([\n safelyExecute(() => erc20.getTokenName(asset.address)),\n safelyExecute(() => erc20.getTokenSymbol(asset.address)),\n safelyExecute(async () => erc20.getTokenDecimals(asset.address)),\n ]);\n\n asset.name = contractName;\n\n // Validate symbol\n\n if (!asset.symbol && !contractSymbol) {\n throw rpcErrors.invalidParams(\n 'A symbol is required, but was not found in either the request or contract',\n );\n }\n\n if (\n contractSymbol !== undefined &&\n asset.symbol !== undefined &&\n asset.symbol.toUpperCase() !== contractSymbol.toUpperCase()\n ) {\n throw rpcErrors.invalidParams(\n `The symbol in the request (${asset.symbol}) does not match the symbol in the contract (${contractSymbol})`,\n );\n }\n\n asset.symbol = contractSymbol ?? asset.symbol;\n if (typeof asset.symbol !== 'string') {\n throw rpcErrors.invalidParams(`Invalid symbol: not a string`);\n }\n\n if (asset.symbol.length > 11) {\n throw rpcErrors.invalidParams(\n `Invalid symbol \"${asset.symbol}\": longer than 11 characters`,\n );\n }\n\n // Validate decimals\n\n if (asset.decimals === undefined && contractDecimals === undefined) {\n throw rpcErrors.invalidParams(\n 'Decimals are required, but were not found in either the request or contract',\n );\n }\n\n if (\n contractDecimals !== undefined &&\n asset.decimals !== undefined &&\n String(asset.decimals) !== contractDecimals\n ) {\n throw rpcErrors.invalidParams(\n `The decimals in the request (${asset.decimals}) do not match the decimals in the contract (${contractDecimals})`,\n );\n }\n\n const decimalsStr = contractDecimals ?? asset.decimals;\n const decimalsNum = parseInt(decimalsStr as unknown as string, 10);\n if (!Number.isInteger(decimalsNum) || decimalsNum > 36 || decimalsNum < 0) {\n throw rpcErrors.invalidParams(\n `Invalid decimals \"${decimalsStr}\": must be an integer 0 <= 36`,\n );\n }\n asset.decimals = decimalsNum;\n\n const suggestedAssetMeta: SuggestedAssetMeta = {\n asset,\n id: this._generateRandomId(),\n time: Date.now(),\n type,\n interactingAddress: interactingAddress || this.config.selectedAddress,\n };\n\n await this._requestApproval(suggestedAssetMeta);\n\n const { address, symbol, decimals, name, image } = asset;\n await this.addToken({\n address,\n symbol,\n decimals,\n name,\n image,\n interactingAddress: suggestedAssetMeta.interactingAddress,\n networkClientId,\n });\n }\n\n /**\n * Takes a new tokens and ignoredTokens array for the current network/account combination\n * and returns new allTokens and allIgnoredTokens state to update to.\n *\n * @param params - Object that holds token params.\n * @param params.newTokens - The new tokens to set for the current network and selected account.\n * @param params.newIgnoredTokens - The new ignored tokens to set for the current network and selected account.\n * @param params.newDetectedTokens - The new detected tokens to set for the current network and selected account.\n * @param params.interactingAddress - The account address to use to store the tokens.\n * @param params.interactingChainId - The chainId to use to store the tokens.\n * @returns The updated `allTokens` and `allIgnoredTokens` state.\n */\n _getNewAllTokensState(params: {\n newTokens?: Token[];\n newIgnoredTokens?: string[];\n newDetectedTokens?: Token[];\n interactingAddress?: string;\n interactingChainId?: Hex;\n }) {\n const {\n newTokens,\n newIgnoredTokens,\n newDetectedTokens,\n interactingAddress,\n interactingChainId,\n } = params;\n const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;\n const { chainId, selectedAddress } = this.config;\n\n const userAddressToAddTokens = interactingAddress ?? selectedAddress;\n const chainIdToAddTokens = interactingChainId ?? chainId;\n\n let newAllTokens = allTokens;\n if (\n newTokens?.length ||\n (newTokens &&\n allTokens &&\n allTokens[chainIdToAddTokens] &&\n allTokens[chainIdToAddTokens][userAddressToAddTokens])\n ) {\n const networkTokens = allTokens[chainIdToAddTokens];\n const newNetworkTokens = {\n ...networkTokens,\n ...{ [userAddressToAddTokens]: newTokens },\n };\n newAllTokens = {\n ...allTokens,\n ...{ [chainIdToAddTokens]: newNetworkTokens },\n };\n }\n\n let newAllIgnoredTokens = allIgnoredTokens;\n if (\n newIgnoredTokens?.length ||\n (newIgnoredTokens &&\n allIgnoredTokens &&\n allIgnoredTokens[chainIdToAddTokens] &&\n allIgnoredTokens[chainIdToAddTokens][userAddressToAddTokens])\n ) {\n const networkIgnoredTokens = allIgnoredTokens[chainIdToAddTokens];\n const newIgnoredNetworkTokens = {\n ...networkIgnoredTokens,\n ...{ [userAddressToAddTokens]: newIgnoredTokens },\n };\n newAllIgnoredTokens = {\n ...allIgnoredTokens,\n ...{ [chainIdToAddTokens]: newIgnoredNetworkTokens },\n };\n }\n\n let newAllDetectedTokens = allDetectedTokens;\n if (\n newDetectedTokens?.length ||\n (newDetectedTokens &&\n allDetectedTokens &&\n allDetectedTokens[chainIdToAddTokens] &&\n allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens])\n ) {\n const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens];\n const newDetectedNetworkTokens = {\n ...networkDetectedTokens,\n ...{ [userAddressToAddTokens]: newDetectedTokens },\n };\n newAllDetectedTokens = {\n ...allDetectedTokens,\n ...{ [chainIdToAddTokens]: newDetectedNetworkTokens },\n };\n }\n return { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens };\n }\n\n /**\n * Removes all tokens from the ignored list.\n */\n clearIgnoredTokens() {\n this.update({ ignoredTokens: [], allIgnoredTokens: {} });\n }\n\n async _requestApproval(suggestedAssetMeta: SuggestedAssetMeta) {\n return this.messagingSystem.call(\n 'ApprovalController:addRequest',\n {\n id: suggestedAssetMeta.id,\n origin: ORIGIN_METAMASK,\n type: ApprovalType.WatchAsset,\n requestData: {\n id: suggestedAssetMeta.id,\n interactingAddress: suggestedAssetMeta.interactingAddress,\n asset: {\n address: suggestedAssetMeta.asset.address,\n decimals: suggestedAssetMeta.asset.decimals,\n symbol: suggestedAssetMeta.asset.symbol,\n image: suggestedAssetMeta.asset.image || null,\n },\n },\n },\n true,\n );\n }\n}\n\nexport default TokensController;\n"],"mappings":";;;;;;;;;;;;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,oBAAoB;AAO7B,SAAS,wBAAwB;AACjC,OAAO,kBAAkB;AACzB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB;AAQ1B,SAAS,iBAAiB;AAE1B,SAAS,aAAa;AACtB,SAAS,oBAAoB;AAE7B,SAAS,MAAM,cAAc;AAwE7B,IAAM,iBAAiB;AAgDhB,IA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QAAI,uBAAuB;AAC3B,QACE,mBAAmB,UAClB,qBACC,qBACA,kBAAkB,kBAAkB,KACpC,kBAAkB,kBAAkB,EAAE,sBAAsB,GAC9D;AACA,YAAM,wBAAwB,kBAAkB,kBAAkB;AAClE,YAAM,2BAA2B;AAAA,QAC/B,GAAG;AAAA,QACH,GAAG,EAAE,CAAC,sBAAsB,GAAG,kBAAkB;AAAA,MACnD;AACA,6BAAuB;AAAA,QACrB,GAAG;AAAA,QACH,GAAG,EAAE,CAAC,kBAAkB,GAAG,yBAAyB;AAAA,MACtD;AAAA,IACF;AACA,WAAO,EAAE,cAAc,qBAAqB,qBAAqB;AAAA,EACnE;AAAA;AAAA;AAAA;AAAA,EAKA,qBAAqB;AACnB,SAAK,OAAO,EAAE,eAAe,CAAC,GAAG,kBAAkB,CAAC,EAAE,CAAC;AAAA,EACzD;AAAA,EAEA,MAAM,iBAAiB,oBAAwC;AAC7D,WAAO,KAAK,gBAAgB;AAAA,MAC1B;AAAA,MACA;AAAA,QACE,IAAI,mBAAmB;AAAA,QACvB,QAAQ;AAAA,QACR,MAAM,aAAa;AAAA,QACnB,aAAa;AAAA,UACX,IAAI,mBAAmB;AAAA,UACvB,oBAAoB,mBAAmB;AAAA,UACvC,OAAO;AAAA,YACL,SAAS,mBAAmB,MAAM;AAAA,YAClC,UAAU,mBAAmB,MAAM;AAAA,YACnC,QAAQ,mBAAmB,MAAM;AAAA,YACjC,OAAO,mBAAmB,MAAM,SAAS;AAAA,UAC3C;AAAA,QACF;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAO,2BAAQ;","names":[]}
@@ -9,9 +9,6 @@ import {
9
9
  } from "./chunk-XUI43LEZ.mjs";
10
10
 
11
11
  // src/TokenDetectionController.ts
12
- import {
13
- isEVMAccount
14
- } from "@metamask/accounts-controller";
15
12
  import contractMap from "@metamask/contract-metadata";
16
13
  import { ChainId, safelyExecute } from "@metamask/controller-utils";
17
14
  import { StaticIntervalPollingController } from "@metamask/polling-controller";
@@ -37,7 +34,7 @@ var STATIC_MAINNET_TOKEN_LIST = Object.entries(
37
34
  };
38
35
  }, {});
39
36
  var controllerName = "TokenDetectionController";
40
- var _intervalId, _selectedAccountId, _networkClientId, _tokenList, _disabled, _isUnlocked, _isDetectionEnabledFromPreferences, _isDetectionEnabledForNetwork, _getBalancesInSingleCall, _trackMetaMetricsEvent, _registerEventListeners, registerEventListeners_fn, _stopPolling, stopPolling_fn, _startPolling, startPolling_fn, _getCorrectChainIdAndNetworkClientId, getCorrectChainIdAndNetworkClientId_fn, _restartTokenDetection, restartTokenDetection_fn, _getSlicesOfTokensToDetect, getSlicesOfTokensToDetect_fn, _addDetectedTokens, addDetectedTokens_fn;
37
+ var _intervalId, _selectedAddress, _networkClientId, _tokenList, _disabled, _isUnlocked, _isDetectionEnabledFromPreferences, _isDetectionEnabledForNetwork, _getBalancesInSingleCall, _trackMetaMetricsEvent, _registerEventListeners, registerEventListeners_fn, _stopPolling, stopPolling_fn, _startPolling, startPolling_fn, _getCorrectChainIdAndNetworkClientId, getCorrectChainIdAndNetworkClientId_fn, _restartTokenDetection, restartTokenDetection_fn, _getSlicesOfTokensToDetect, getSlicesOfTokensToDetect_fn, _addDetectedTokens, addDetectedTokens_fn;
41
38
  var TokenDetectionController = class extends StaticIntervalPollingController {
42
39
  /**
43
40
  * Creates a TokenDetectionController instance.
@@ -46,12 +43,12 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
46
43
  * @param options.messenger - The controller messaging system.
47
44
  * @param options.disabled - If set to true, all network requests are blocked.
48
45
  * @param options.interval - Polling interval used to fetch new token rates
49
- * @param options.selectedAccountId - Vault selected address
46
+ * @param options.selectedAddress - Vault selected address
50
47
  * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
51
48
  * @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
52
49
  */
53
50
  constructor({
54
- selectedAccountId,
51
+ selectedAddress,
55
52
  interval = DEFAULT_INTERVAL,
56
53
  disabled = true,
57
54
  getBalancesInSingleCall,
@@ -79,14 +76,14 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
79
76
  * in case of address change or user session initialization.
80
77
  *
81
78
  * @param options - Options for restart token detection.
82
- * @param options.selectedAccountId - the id of the InternalAccount against which to detect for token balances
79
+ * @param options.selectedAddress - the selectedAddress against which to detect for token balances
83
80
  * @param options.networkClientId - The ID of the network client to use.
84
81
  */
85
82
  __privateAdd(this, _restartTokenDetection);
86
83
  __privateAdd(this, _getSlicesOfTokensToDetect);
87
84
  __privateAdd(this, _addDetectedTokens);
88
85
  __privateAdd(this, _intervalId, void 0);
89
- __privateAdd(this, _selectedAccountId, void 0);
86
+ __privateAdd(this, _selectedAddress, void 0);
90
87
  __privateAdd(this, _networkClientId, void 0);
91
88
  __privateAdd(this, _tokenList, {});
92
89
  __privateAdd(this, _disabled, void 0);
@@ -97,14 +94,7 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
97
94
  __privateAdd(this, _trackMetaMetricsEvent, void 0);
98
95
  __privateSet(this, _disabled, disabled);
99
96
  this.setIntervalLength(interval);
100
- if (selectedAccountId) {
101
- __privateSet(this, _selectedAccountId, selectedAccountId);
102
- } else {
103
- const selectedInternalAccount = this.messagingSystem.call(
104
- "AccountsController:getSelectedAccount"
105
- );
106
- __privateSet(this, _selectedAccountId, selectedInternalAccount.id);
107
- }
97
+ __privateSet(this, _selectedAddress, selectedAddress ?? this.messagingSystem.call("AccountsController:getSelectedAccount").address);
108
98
  const { chainId, networkClientId } = __privateMethod(this, _getCorrectChainIdAndNetworkClientId, getCorrectChainIdAndNetworkClientId_fn).call(this);
109
99
  __privateSet(this, _networkClientId, networkClientId);
110
100
  const { useTokenDetection: defaultUseTokenDetection } = this.messagingSystem.call("PreferencesController:getState");
@@ -175,14 +165,7 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
175
165
  if (!this.isActive) {
176
166
  return;
177
167
  }
178
- const selectedInternalAccount = this.messagingSystem.call(
179
- "AccountsController:getAccount",
180
- __privateGet(this, _selectedAccountId)
181
- );
182
- if (!selectedInternalAccount || !isEVMAccount(selectedInternalAccount)) {
183
- return;
184
- }
185
- const addressAgainstWhichToDetect = selectedAddress ?? selectedInternalAccount.address;
168
+ const addressAgainstWhichToDetect = selectedAddress ?? __privateGet(this, _selectedAddress);
186
169
  const { chainId, networkClientId: selectedNetworkClientId } = __privateMethod(this, _getCorrectChainIdAndNetworkClientId, getCorrectChainIdAndNetworkClientId_fn).call(this, networkClientId);
187
170
  const chainIdAgainstWhichToDetect = chainId;
188
171
  const networkClientIdAgainstWhichToDetect = selectedNetworkClientId;
@@ -211,7 +194,7 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
211
194
  }
212
195
  };
213
196
  _intervalId = new WeakMap();
214
- _selectedAccountId = new WeakMap();
197
+ _selectedAddress = new WeakMap();
215
198
  _networkClientId = new WeakMap();
216
199
  _tokenList = new WeakMap();
217
200
  _disabled = new WeakMap();
@@ -241,27 +224,26 @@ registerEventListeners_fn = function() {
241
224
  );
242
225
  this.messagingSystem.subscribe(
243
226
  "PreferencesController:stateChange",
244
- async ({ useTokenDetection }) => {
245
- const selectedAccount = this.messagingSystem.call(
246
- "AccountsController:getSelectedAccount"
247
- );
227
+ async ({ selectedAddress: newSelectedAddress, useTokenDetection }) => {
228
+ const isSelectedAddressChanged = __privateGet(this, _selectedAddress) !== newSelectedAddress;
248
229
  const isDetectionChangedFromPreferences = __privateGet(this, _isDetectionEnabledFromPreferences) !== useTokenDetection;
230
+ __privateSet(this, _selectedAddress, newSelectedAddress);
249
231
  __privateSet(this, _isDetectionEnabledFromPreferences, useTokenDetection);
250
- if (isDetectionChangedFromPreferences) {
232
+ if (isSelectedAddressChanged || isDetectionChangedFromPreferences) {
251
233
  await __privateMethod(this, _restartTokenDetection, restartTokenDetection_fn).call(this, {
252
- selectedAccountId: selectedAccount.id
234
+ selectedAddress: __privateGet(this, _selectedAddress)
253
235
  });
254
236
  }
255
237
  }
256
238
  );
257
239
  this.messagingSystem.subscribe(
258
240
  "AccountsController:selectedAccountChange",
259
- async (internalAccount) => {
260
- const didSelectedAccountIdChanged = __privateGet(this, _selectedAccountId) !== internalAccount.id;
261
- if (didSelectedAccountIdChanged) {
262
- __privateSet(this, _selectedAccountId, internalAccount.id);
241
+ async ({ address: newSelectedAddress }) => {
242
+ const isSelectedAddressChanged = __privateGet(this, _selectedAddress) !== newSelectedAddress;
243
+ if (isSelectedAddressChanged) {
244
+ __privateSet(this, _selectedAddress, newSelectedAddress);
263
245
  await __privateMethod(this, _restartTokenDetection, restartTokenDetection_fn).call(this, {
264
- selectedAccountId: __privateGet(this, _selectedAccountId)
246
+ selectedAddress: __privateGet(this, _selectedAddress)
265
247
  });
266
248
  }
267
249
  }
@@ -328,19 +310,12 @@ getCorrectChainIdAndNetworkClientId_fn = function(networkClientId) {
328
310
  };
329
311
  _restartTokenDetection = new WeakSet();
330
312
  restartTokenDetection_fn = async function({
331
- selectedAccountId,
313
+ selectedAddress,
332
314
  networkClientId
333
315
  } = {}) {
334
- const internalAccount = this.messagingSystem.call(
335
- "AccountsController:getAccount",
336
- selectedAccountId ?? __privateGet(this, _selectedAccountId)
337
- );
338
- if (!internalAccount || !isEVMAccount(internalAccount)) {
339
- return;
340
- }
341
316
  await this.detectTokens({
342
317
  networkClientId,
343
- selectedAddress: internalAccount.address
318
+ selectedAddress
344
319
  });
345
320
  this.setIntervalLength(DEFAULT_INTERVAL);
346
321
  };
@@ -433,4 +408,4 @@ export {
433
408
  TokenDetectionController,
434
409
  TokenDetectionController_default
435
410
  };
436
- //# sourceMappingURL=chunk-YUKHDFUN.mjs.map
411
+ //# sourceMappingURL=chunk-D3PCUDTX.mjs.map