@metamask/assets-controllers 24.0.0 → 26.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 (54) hide show
  1. package/CHANGELOG.md +205 -37
  2. package/dist/AssetsContractController.d.ts +5 -6
  3. package/dist/AssetsContractController.d.ts.map +1 -1
  4. package/dist/AssetsContractController.js +1 -2
  5. package/dist/AssetsContractController.js.map +1 -1
  6. package/dist/NftController.d.ts.map +1 -1
  7. package/dist/NftController.js +18 -16
  8. package/dist/NftController.js.map +1 -1
  9. package/dist/NftDetectionController.d.ts.map +1 -1
  10. package/dist/NftDetectionController.js +0 -2
  11. package/dist/NftDetectionController.js.map +1 -1
  12. package/dist/Standards/ERC20Standard.d.ts +1 -2
  13. package/dist/Standards/ERC20Standard.d.ts.map +1 -1
  14. package/dist/Standards/ERC20Standard.js +6 -8
  15. package/dist/Standards/ERC20Standard.js.map +1 -1
  16. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts +1 -2
  17. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts.map +1 -1
  18. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js.map +1 -1
  19. package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts.map +1 -1
  20. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +2 -3
  21. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -1
  22. package/dist/TokenBalancesController.d.ts +54 -41
  23. package/dist/TokenBalancesController.d.ts.map +1 -1
  24. package/dist/TokenBalancesController.js +84 -36
  25. package/dist/TokenBalancesController.js.map +1 -1
  26. package/dist/TokenDetectionController.d.ts +47 -24
  27. package/dist/TokenDetectionController.d.ts.map +1 -1
  28. package/dist/TokenDetectionController.js +228 -111
  29. package/dist/TokenDetectionController.js.map +1 -1
  30. package/dist/TokenRatesController.d.ts +1 -1
  31. package/dist/TokenRatesController.d.ts.map +1 -1
  32. package/dist/TokenRatesController.js +2 -27
  33. package/dist/TokenRatesController.js.map +1 -1
  34. package/dist/TokensController.d.ts +20 -20
  35. package/dist/TokensController.d.ts.map +1 -1
  36. package/dist/TokensController.js +16 -23
  37. package/dist/TokensController.js.map +1 -1
  38. package/dist/assetsUtil.d.ts +1 -2
  39. package/dist/assetsUtil.d.ts.map +1 -1
  40. package/dist/assetsUtil.js +7 -3
  41. package/dist/assetsUtil.js.map +1 -1
  42. package/dist/index.d.ts +8 -4
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +9 -5
  45. package/dist/index.js.map +1 -1
  46. package/dist/token-prices-service/codefi-v2.d.ts +1 -1
  47. package/dist/token-prices-service/codefi-v2.d.ts.map +1 -1
  48. package/dist/token-prices-service/codefi-v2.js +3 -1
  49. package/dist/token-prices-service/codefi-v2.js.map +1 -1
  50. package/package.json +21 -14
  51. package/dist/Standards/standards-types.d.ts +0 -15
  52. package/dist/Standards/standards-types.d.ts.map +0 -1
  53. package/dist/Standards/standards-types.js +0 -3
  54. package/dist/Standards/standards-types.js.map +0 -1
@@ -1,26 +1,43 @@
1
+ import type { AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedAccountChangeEvent } from '@metamask/accounts-controller';
1
2
  import type { RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from '@metamask/base-controller';
2
- import type { NetworkClientId, NetworkControllerNetworkDidChangeEvent, NetworkControllerStateChangeEvent, NetworkControllerGetNetworkConfigurationByNetworkClientId } from '@metamask/network-controller';
3
+ import type { KeyringControllerGetStateAction, KeyringControllerLockEvent, KeyringControllerUnlockEvent } from '@metamask/keyring-controller';
4
+ import type { NetworkClientId, NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetNetworkConfigurationByNetworkClientId, NetworkControllerGetStateAction, NetworkControllerNetworkDidChangeEvent } from '@metamask/network-controller';
3
5
  import { StaticIntervalPollingController } from '@metamask/polling-controller';
4
- import type { PreferencesState } from '@metamask/preferences-controller';
6
+ import type { PreferencesControllerGetStateAction, PreferencesControllerStateChangeEvent } from '@metamask/preferences-controller';
5
7
  import type { AssetsContractController } from './AssetsContractController';
6
- import type { GetTokenListState, TokenListStateChange } from './TokenListController';
7
- import type { TokensController, TokensState } from './TokensController';
8
+ import type { GetTokenListState, TokenListMap, TokenListStateChange } from './TokenListController';
9
+ import type { TokensControllerAddDetectedTokensAction, TokensControllerGetStateAction } from './TokensController';
10
+ /**
11
+ * Compare 2 given strings and return boolean
12
+ * eg: "foo" and "FOO" => true
13
+ * eg: "foo" and "bar" => false
14
+ * eg: "foo" and 123 => false
15
+ *
16
+ * @param value1 - first string to compare
17
+ * @param value2 - first string to compare
18
+ * @returns true if 2 strings are identical when they are lowercase
19
+ */
20
+ export declare function isEqualCaseInsensitive(value1: string, value2: string): boolean;
21
+ declare type TokenDetectionMap = {
22
+ [P in keyof TokenListMap]: Omit<TokenListMap[P], 'occurrences'>;
23
+ };
24
+ export declare const STATIC_MAINNET_TOKEN_LIST: TokenDetectionMap;
8
25
  export declare const controllerName = "TokenDetectionController";
9
26
  export declare type TokenDetectionState = Record<never, never>;
10
27
  export declare type TokenDetectionControllerGetStateAction = ControllerGetStateAction<typeof controllerName, TokenDetectionState>;
11
28
  export declare type TokenDetectionControllerActions = TokenDetectionControllerGetStateAction;
12
- export declare type AllowedActions = NetworkControllerGetNetworkConfigurationByNetworkClientId | GetTokenListState;
29
+ export declare type AllowedActions = AccountsControllerGetSelectedAccountAction | NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetNetworkConfigurationByNetworkClientId | NetworkControllerGetStateAction | GetTokenListState | KeyringControllerGetStateAction | PreferencesControllerGetStateAction | TokensControllerGetStateAction | TokensControllerAddDetectedTokensAction;
13
30
  export declare type TokenDetectionControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, TokenDetectionState>;
14
31
  export declare type TokenDetectionControllerEvents = TokenDetectionControllerStateChangeEvent;
15
- export declare type AllowedEvents = NetworkControllerStateChangeEvent | NetworkControllerNetworkDidChangeEvent | TokenListStateChange;
32
+ export declare type AllowedEvents = AccountsControllerSelectedAccountChangeEvent | NetworkControllerNetworkDidChangeEvent | TokenListStateChange | KeyringControllerLockEvent | KeyringControllerUnlockEvent | PreferencesControllerStateChangeEvent;
16
33
  export declare type TokenDetectionControllerMessenger = RestrictedControllerMessenger<typeof controllerName, TokenDetectionControllerActions | AllowedActions, TokenDetectionControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
17
34
  /**
18
35
  * Controller that passively polls on a set interval for Tokens auto detection
19
36
  * @property intervalId - Polling interval used to fetch new token rates
20
- * @property chainId - The chain ID of the current network
21
37
  * @property selectedAddress - Vault selected address
22
38
  * @property networkClientId - The network client ID of the current selected network
23
39
  * @property disabled - Boolean to track if network requests are blocked
40
+ * @property isUnlocked - Boolean to track if the keyring state is unlocked
24
41
  * @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
25
42
  * @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
26
43
  */
@@ -33,24 +50,24 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
33
50
  * @param options.messenger - The controller messaging system.
34
51
  * @param options.disabled - If set to true, all network requests are blocked.
35
52
  * @param options.interval - Polling interval used to fetch new token rates
36
- * @param options.networkClientId - The selected network client ID of the current network
37
53
  * @param options.selectedAddress - Vault selected address
38
- * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
39
- * @param options.addDetectedTokens - Add a list of detected tokens.
40
54
  * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
41
- * @param options.getTokensState - Gets the current state of the Tokens controller.
42
- * @param options.getPreferencesState - Gets the state of the preferences controller.
55
+ * @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
43
56
  */
44
- constructor({ networkClientId, selectedAddress, interval, disabled, onPreferencesStateChange, getBalancesInSingleCall, addDetectedTokens, getPreferencesState, getTokensState, messenger, }: {
45
- networkClientId: NetworkClientId;
57
+ constructor({ selectedAddress, interval, disabled, getBalancesInSingleCall, trackMetaMetricsEvent, messenger, }: {
46
58
  selectedAddress?: string;
47
59
  interval?: number;
48
60
  disabled?: boolean;
49
- onPreferencesStateChange: (listener: (preferencesState: PreferencesState) => void) => void;
50
- addDetectedTokens: TokensController['addDetectedTokens'];
51
61
  getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
52
- getTokensState: () => TokensState;
53
- getPreferencesState: () => PreferencesState;
62
+ trackMetaMetricsEvent: (options: {
63
+ event: string;
64
+ category: string;
65
+ properties: {
66
+ tokens: string[];
67
+ token_standard: string;
68
+ asset_type: string;
69
+ };
70
+ }) => void;
54
71
  messenger: TokenDetectionControllerMessenger;
55
72
  });
56
73
  /**
@@ -61,6 +78,11 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
61
78
  * Blocks controller from making network calls
62
79
  */
63
80
  disable(): void;
81
+ /**
82
+ * Internal isActive state
83
+ * @type {boolean}
84
+ */
85
+ get isActive(): boolean;
64
86
  /**
65
87
  * Start polling for detected tokens.
66
88
  */
@@ -69,19 +91,20 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
69
91
  * Stop polling for detected tokens.
70
92
  */
71
93
  stop(): void;
72
- _executePoll(networkClientId: string, options: {
94
+ _executePoll(networkClientId: NetworkClientId, options: {
73
95
  address: string;
74
96
  }): Promise<void>;
75
97
  /**
76
- * Triggers asset ERC20 token auto detection for each contract address in contract metadata on mainnet.
98
+ * For each token in the token list provided by the TokenListController, checks the token's balance for the selected account address on the active network.
99
+ * On mainnet, if token detection is disabled in preferences, ERC20 token auto detection will be triggered for each contract address in the legacy token list from the @metamask/contract-metadata repo.
77
100
  *
78
- * @param options - Options to detect tokens.
101
+ * @param options - Options for token detection.
79
102
  * @param options.networkClientId - The ID of the network client to use.
80
- * @param options.accountAddress - The account address to use.
103
+ * @param options.selectedAddress - the selectedAddress against which to detect for token balances.
81
104
  */
82
- detectTokens({ networkClientId, accountAddress, }?: {
105
+ detectTokens({ networkClientId, selectedAddress, }?: {
83
106
  networkClientId?: NetworkClientId;
84
- accountAddress?: string;
107
+ selectedAddress?: string;
85
108
  }): Promise<void>;
86
109
  }
87
110
  export default TokenDetectionController;
@@ -1 +1 @@
1
- {"version":3,"file":"TokenDetectionController.d.ts","sourceRoot":"","sources":["../src/TokenDetectionController.ts"],"names":[],"mappings":"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"}
1
+ {"version":3,"file":"TokenDetectionController.d.ts","sourceRoot":"","sources":["../src/TokenDetectionController.ts"],"names":[],"mappings":"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"}
@@ -19,21 +19,57 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
19
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
20
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
21
  };
22
- var _TokenDetectionController_instances, _TokenDetectionController_intervalId, _TokenDetectionController_chainId, _TokenDetectionController_selectedAddress, _TokenDetectionController_networkClientId, _TokenDetectionController_disabled, _TokenDetectionController_isDetectionEnabledFromPreferences, _TokenDetectionController_isDetectionEnabledForNetwork, _TokenDetectionController_addDetectedTokens, _TokenDetectionController_getBalancesInSingleCall, _TokenDetectionController_getTokensState, _TokenDetectionController_stopPolling, _TokenDetectionController_startPolling, _TokenDetectionController_getCorrectChainId;
22
+ var __rest = (this && this.__rest) || function (s, e) {
23
+ var t = {};
24
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
25
+ t[p] = s[p];
26
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
27
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
28
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
29
+ t[p[i]] = s[p[i]];
30
+ }
31
+ return t;
32
+ };
33
+ var __importDefault = (this && this.__importDefault) || function (mod) {
34
+ return (mod && mod.__esModule) ? mod : { "default": mod };
35
+ };
36
+ var _TokenDetectionController_instances, _TokenDetectionController_intervalId, _TokenDetectionController_selectedAddress, _TokenDetectionController_networkClientId, _TokenDetectionController_tokenList, _TokenDetectionController_disabled, _TokenDetectionController_isUnlocked, _TokenDetectionController_isDetectionEnabledFromPreferences, _TokenDetectionController_isDetectionEnabledForNetwork, _TokenDetectionController_getBalancesInSingleCall, _TokenDetectionController_trackMetaMetricsEvent, _TokenDetectionController_registerEventListeners, _TokenDetectionController_stopPolling, _TokenDetectionController_startPolling, _TokenDetectionController_getCorrectChainIdAndNetworkClientId, _TokenDetectionController_restartTokenDetection, _TokenDetectionController_getSlicesOfTokensToDetect, _TokenDetectionController_addDetectedTokens;
23
37
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.TokenDetectionController = exports.controllerName = void 0;
38
+ exports.TokenDetectionController = exports.controllerName = exports.STATIC_MAINNET_TOKEN_LIST = exports.isEqualCaseInsensitive = void 0;
39
+ const contract_metadata_1 = __importDefault(require("@metamask/contract-metadata"));
25
40
  const controller_utils_1 = require("@metamask/controller-utils");
26
41
  const polling_controller_1 = require("@metamask/polling-controller");
27
42
  const assetsUtil_1 = require("./assetsUtil");
28
43
  const DEFAULT_INTERVAL = 180000;
44
+ /**
45
+ * Compare 2 given strings and return boolean
46
+ * eg: "foo" and "FOO" => true
47
+ * eg: "foo" and "bar" => false
48
+ * eg: "foo" and 123 => false
49
+ *
50
+ * @param value1 - first string to compare
51
+ * @param value2 - first string to compare
52
+ * @returns true if 2 strings are identical when they are lowercase
53
+ */
54
+ function isEqualCaseInsensitive(value1, value2) {
55
+ if (typeof value1 !== 'string' || typeof value2 !== 'string') {
56
+ return false;
57
+ }
58
+ return value1.toLowerCase() === value2.toLowerCase();
59
+ }
60
+ exports.isEqualCaseInsensitive = isEqualCaseInsensitive;
61
+ exports.STATIC_MAINNET_TOKEN_LIST = Object.entries(contract_metadata_1.default).reduce((acc, [base, contract]) => {
62
+ const { logo, erc20, erc721 } = contract, tokenMetadata = __rest(contract, ["logo", "erc20", "erc721"]);
63
+ return Object.assign(Object.assign({}, acc), { [base.toLowerCase()]: Object.assign(Object.assign({}, tokenMetadata), { address: base.toLowerCase(), iconUrl: `images/contract/${logo}`, aggregators: [] }) });
64
+ }, {});
29
65
  exports.controllerName = 'TokenDetectionController';
30
66
  /**
31
67
  * Controller that passively polls on a set interval for Tokens auto detection
32
68
  * @property intervalId - Polling interval used to fetch new token rates
33
- * @property chainId - The chain ID of the current network
34
69
  * @property selectedAddress - Vault selected address
35
70
  * @property networkClientId - The network client ID of the current selected network
36
71
  * @property disabled - Boolean to track if network requests are blocked
72
+ * @property isUnlocked - Boolean to track if the keyring state is unlocked
37
73
  * @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
38
74
  * @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
39
75
  */
@@ -45,16 +81,11 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
45
81
  * @param options.messenger - The controller messaging system.
46
82
  * @param options.disabled - If set to true, all network requests are blocked.
47
83
  * @param options.interval - Polling interval used to fetch new token rates
48
- * @param options.networkClientId - The selected network client ID of the current network
49
84
  * @param options.selectedAddress - Vault selected address
50
- * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
51
- * @param options.addDetectedTokens - Add a list of detected tokens.
52
85
  * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
53
- * @param options.getTokensState - Gets the current state of the Tokens controller.
54
- * @param options.getPreferencesState - Gets the state of the preferences controller.
86
+ * @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
55
87
  */
56
- constructor({ networkClientId, selectedAddress = '', interval = DEFAULT_INTERVAL, disabled = true, onPreferencesStateChange, getBalancesInSingleCall, addDetectedTokens, getPreferencesState, getTokensState, messenger, }) {
57
- const { useTokenDetection: defaultUseTokenDetection } = getPreferencesState();
88
+ constructor({ selectedAddress, interval = DEFAULT_INTERVAL, disabled = true, getBalancesInSingleCall, trackMetaMetricsEvent, messenger, }) {
58
89
  super({
59
90
  name: exports.controllerName,
60
91
  messenger,
@@ -63,51 +94,29 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
63
94
  });
64
95
  _TokenDetectionController_instances.add(this);
65
96
  _TokenDetectionController_intervalId.set(this, void 0);
66
- _TokenDetectionController_chainId.set(this, void 0);
67
97
  _TokenDetectionController_selectedAddress.set(this, void 0);
68
98
  _TokenDetectionController_networkClientId.set(this, void 0);
99
+ _TokenDetectionController_tokenList.set(this, {});
69
100
  _TokenDetectionController_disabled.set(this, void 0);
101
+ _TokenDetectionController_isUnlocked.set(this, void 0);
70
102
  _TokenDetectionController_isDetectionEnabledFromPreferences.set(this, void 0);
71
103
  _TokenDetectionController_isDetectionEnabledForNetwork.set(this, void 0);
72
- _TokenDetectionController_addDetectedTokens.set(this, void 0);
73
104
  _TokenDetectionController_getBalancesInSingleCall.set(this, void 0);
74
- _TokenDetectionController_getTokensState.set(this, void 0);
105
+ _TokenDetectionController_trackMetaMetricsEvent.set(this, void 0);
75
106
  __classPrivateFieldSet(this, _TokenDetectionController_disabled, disabled, "f");
76
107
  this.setIntervalLength(interval);
108
+ __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, selectedAddress !== null && selectedAddress !== void 0 ? selectedAddress : this.messagingSystem.call('AccountsController:getSelectedAccount')
109
+ .address, "f");
110
+ const { chainId, networkClientId } = __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainIdAndNetworkClientId).call(this);
77
111
  __classPrivateFieldSet(this, _TokenDetectionController_networkClientId, networkClientId, "f");
78
- __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, selectedAddress, "f");
79
- __classPrivateFieldSet(this, _TokenDetectionController_chainId, __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainId).call(this, networkClientId), "f");
112
+ const { useTokenDetection: defaultUseTokenDetection } = this.messagingSystem.call('PreferencesController:getState');
80
113
  __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, defaultUseTokenDetection, "f");
81
- __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledForNetwork, (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(__classPrivateFieldGet(this, _TokenDetectionController_chainId, "f")), "f");
82
- __classPrivateFieldSet(this, _TokenDetectionController_addDetectedTokens, addDetectedTokens, "f");
114
+ __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledForNetwork, (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(chainId), "f");
83
115
  __classPrivateFieldSet(this, _TokenDetectionController_getBalancesInSingleCall, getBalancesInSingleCall, "f");
84
- __classPrivateFieldSet(this, _TokenDetectionController_getTokensState, getTokensState, "f");
85
- this.messagingSystem.subscribe('TokenListController:stateChange', ({ tokenList }) => __awaiter(this, void 0, void 0, function* () {
86
- const hasTokens = Object.keys(tokenList).length;
87
- if (hasTokens) {
88
- yield this.detectTokens();
89
- }
90
- }));
91
- onPreferencesStateChange(({ selectedAddress: newSelectedAddress, useTokenDetection }) => __awaiter(this, void 0, void 0, function* () {
92
- const isSelectedAddressChanged = __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f") !== newSelectedAddress;
93
- const isDetectionChangedFromPreferences = __classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f") !== useTokenDetection;
94
- __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, newSelectedAddress, "f");
95
- __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, useTokenDetection, "f");
96
- if (useTokenDetection &&
97
- (isSelectedAddressChanged || isDetectionChangedFromPreferences)) {
98
- yield this.detectTokens();
99
- }
100
- }));
101
- this.messagingSystem.subscribe('NetworkController:networkDidChange', ({ selectedNetworkClientId }) => __awaiter(this, void 0, void 0, function* () {
102
- __classPrivateFieldSet(this, _TokenDetectionController_networkClientId, selectedNetworkClientId, "f");
103
- const newChainId = __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainId).call(this, selectedNetworkClientId);
104
- const isChainIdChanged = __classPrivateFieldGet(this, _TokenDetectionController_chainId, "f") !== newChainId;
105
- __classPrivateFieldSet(this, _TokenDetectionController_chainId, newChainId, "f");
106
- __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledForNetwork, (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(newChainId), "f");
107
- if (__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledForNetwork, "f") && isChainIdChanged) {
108
- yield this.detectTokens();
109
- }
110
- }));
116
+ __classPrivateFieldSet(this, _TokenDetectionController_trackMetaMetricsEvent, trackMetaMetricsEvent, "f");
117
+ const { isUnlocked } = this.messagingSystem.call('KeyringController:getState');
118
+ __classPrivateFieldSet(this, _TokenDetectionController_isUnlocked, isUnlocked, "f");
119
+ __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_registerEventListeners).call(this);
111
120
  }
112
121
  /**
113
122
  * Allows controller to make active and passive polling requests
@@ -121,6 +130,13 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
121
130
  disable() {
122
131
  __classPrivateFieldSet(this, _TokenDetectionController_disabled, true, "f");
123
132
  }
133
+ /**
134
+ * Internal isActive state
135
+ * @type {boolean}
136
+ */
137
+ get isActive() {
138
+ return !__classPrivateFieldGet(this, _TokenDetectionController_disabled, "f") && __classPrivateFieldGet(this, _TokenDetectionController_isUnlocked, "f");
139
+ }
124
140
  /**
125
141
  * Start polling for detected tokens.
126
142
  */
@@ -139,96 +155,116 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
139
155
  }
140
156
  _executePoll(networkClientId, options) {
141
157
  return __awaiter(this, void 0, void 0, function* () {
142
- if (__classPrivateFieldGet(this, _TokenDetectionController_disabled, "f")) {
158
+ if (!this.isActive) {
143
159
  return;
144
160
  }
145
161
  yield this.detectTokens({
146
162
  networkClientId,
147
- accountAddress: options.address,
163
+ selectedAddress: options.address,
148
164
  });
149
165
  });
150
166
  }
151
167
  /**
152
- * Triggers asset ERC20 token auto detection for each contract address in contract metadata on mainnet.
168
+ * For each token in the token list provided by the TokenListController, checks the token's balance for the selected account address on the active network.
169
+ * On mainnet, if token detection is disabled in preferences, ERC20 token auto detection will be triggered for each contract address in the legacy token list from the @metamask/contract-metadata repo.
153
170
  *
154
- * @param options - Options to detect tokens.
171
+ * @param options - Options for token detection.
155
172
  * @param options.networkClientId - The ID of the network client to use.
156
- * @param options.accountAddress - The account address to use.
173
+ * @param options.selectedAddress - the selectedAddress against which to detect for token balances.
157
174
  */
158
- detectTokens({ networkClientId, accountAddress, } = {}) {
175
+ detectTokens({ networkClientId, selectedAddress, } = {}) {
176
+ var _a, _b;
159
177
  return __awaiter(this, void 0, void 0, function* () {
160
- if (__classPrivateFieldGet(this, _TokenDetectionController_disabled, "f") ||
161
- !__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledForNetwork, "f") ||
162
- !__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f")) {
178
+ if (!this.isActive) {
163
179
  return;
164
180
  }
165
- const { tokens } = __classPrivateFieldGet(this, _TokenDetectionController_getTokensState, "f").call(this);
166
- const selectedAddress = accountAddress || __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f");
167
- const chainId = __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainId).call(this, networkClientId);
168
- const tokensAddresses = tokens.map(
169
- /* istanbul ignore next*/ (token) => token.address.toLowerCase());
170
- const { tokenList } = this.messagingSystem.call('TokenListController:getState');
171
- const tokensToDetect = [];
172
- for (const address of Object.keys(tokenList)) {
173
- if (!tokensAddresses.includes(address)) {
174
- tokensToDetect.push(address);
175
- }
181
+ const addressAgainstWhichToDetect = selectedAddress !== null && selectedAddress !== void 0 ? selectedAddress : __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f");
182
+ const { chainId, networkClientId: selectedNetworkClientId } = __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainIdAndNetworkClientId).call(this, networkClientId);
183
+ const chainIdAgainstWhichToDetect = chainId;
184
+ const networkClientIdAgainstWhichToDetect = selectedNetworkClientId;
185
+ if (!(0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(chainIdAgainstWhichToDetect)) {
186
+ return;
176
187
  }
177
- const sliceOfTokensToDetect = [];
178
- sliceOfTokensToDetect[0] = tokensToDetect.slice(0, 1000);
179
- sliceOfTokensToDetect[1] = tokensToDetect.slice(1000, tokensToDetect.length - 1);
180
- /* istanbul ignore else */
181
- if (!selectedAddress) {
188
+ if (!__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f") &&
189
+ chainIdAgainstWhichToDetect !== controller_utils_1.ChainId.mainnet) {
182
190
  return;
183
191
  }
184
- for (const tokensSlice of sliceOfTokensToDetect) {
185
- if (tokensSlice.length === 0) {
186
- break;
187
- }
188
- yield (0, controller_utils_1.safelyExecute)(() => __awaiter(this, void 0, void 0, function* () {
189
- var _a;
190
- const balances = yield __classPrivateFieldGet(this, _TokenDetectionController_getBalancesInSingleCall, "f").call(this, selectedAddress, tokensSlice);
191
- const tokensToAdd = [];
192
- for (const tokenAddress of Object.keys(balances)) {
193
- let ignored;
194
- /* istanbul ignore else */
195
- const { ignoredTokens } = __classPrivateFieldGet(this, _TokenDetectionController_getTokensState, "f").call(this);
196
- if (ignoredTokens.length) {
197
- ignored = ignoredTokens.find((ignoredTokenAddress) => ignoredTokenAddress === (0, controller_utils_1.toChecksumHexAddress)(tokenAddress));
198
- }
199
- const caseInsensitiveTokenKey = (_a = Object.keys(tokenList).find((i) => i.toLowerCase() === tokenAddress.toLowerCase())) !== null && _a !== void 0 ? _a : '';
200
- if (ignored === undefined) {
201
- const { decimals, symbol, aggregators, iconUrl, name } = tokenList[caseInsensitiveTokenKey];
202
- tokensToAdd.push({
203
- address: tokenAddress,
204
- decimals,
205
- symbol,
206
- aggregators,
207
- image: iconUrl,
208
- isERC721: false,
209
- name,
210
- });
211
- }
212
- }
213
- if (tokensToAdd.length) {
214
- yield __classPrivateFieldGet(this, _TokenDetectionController_addDetectedTokens, "f").call(this, tokensToAdd, {
215
- selectedAddress,
216
- chainId,
217
- });
218
- }
219
- }));
192
+ const isTokenDetectionInactiveInMainnet = !__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f") &&
193
+ chainIdAgainstWhichToDetect === controller_utils_1.ChainId.mainnet;
194
+ const { tokensChainsCache } = this.messagingSystem.call('TokenListController:getState');
195
+ __classPrivateFieldSet(this, _TokenDetectionController_tokenList, isTokenDetectionInactiveInMainnet
196
+ ? exports.STATIC_MAINNET_TOKEN_LIST
197
+ : (_b = (_a = tokensChainsCache[chainIdAgainstWhichToDetect]) === null || _a === void 0 ? void 0 : _a.data) !== null && _b !== void 0 ? _b : {}, "f");
198
+ for (const tokensSlice of __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getSlicesOfTokensToDetect).call(this, {
199
+ chainId: chainIdAgainstWhichToDetect,
200
+ selectedAddress: addressAgainstWhichToDetect,
201
+ })) {
202
+ yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_addDetectedTokens).call(this, {
203
+ tokensSlice,
204
+ selectedAddress: addressAgainstWhichToDetect,
205
+ networkClientId: networkClientIdAgainstWhichToDetect,
206
+ chainId: chainIdAgainstWhichToDetect,
207
+ });
220
208
  }
221
209
  });
222
210
  }
223
211
  }
224
212
  exports.TokenDetectionController = TokenDetectionController;
225
- _TokenDetectionController_intervalId = new WeakMap(), _TokenDetectionController_chainId = new WeakMap(), _TokenDetectionController_selectedAddress = new WeakMap(), _TokenDetectionController_networkClientId = new WeakMap(), _TokenDetectionController_disabled = new WeakMap(), _TokenDetectionController_isDetectionEnabledFromPreferences = new WeakMap(), _TokenDetectionController_isDetectionEnabledForNetwork = new WeakMap(), _TokenDetectionController_addDetectedTokens = new WeakMap(), _TokenDetectionController_getBalancesInSingleCall = new WeakMap(), _TokenDetectionController_getTokensState = new WeakMap(), _TokenDetectionController_instances = new WeakSet(), _TokenDetectionController_stopPolling = function _TokenDetectionController_stopPolling() {
213
+ _TokenDetectionController_intervalId = new WeakMap(), _TokenDetectionController_selectedAddress = new WeakMap(), _TokenDetectionController_networkClientId = new WeakMap(), _TokenDetectionController_tokenList = new WeakMap(), _TokenDetectionController_disabled = new WeakMap(), _TokenDetectionController_isUnlocked = new WeakMap(), _TokenDetectionController_isDetectionEnabledFromPreferences = new WeakMap(), _TokenDetectionController_isDetectionEnabledForNetwork = new WeakMap(), _TokenDetectionController_getBalancesInSingleCall = new WeakMap(), _TokenDetectionController_trackMetaMetricsEvent = new WeakMap(), _TokenDetectionController_instances = new WeakSet(), _TokenDetectionController_registerEventListeners = function _TokenDetectionController_registerEventListeners() {
214
+ this.messagingSystem.subscribe('KeyringController:unlock', () => __awaiter(this, void 0, void 0, function* () {
215
+ __classPrivateFieldSet(this, _TokenDetectionController_isUnlocked, true, "f");
216
+ yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_restartTokenDetection).call(this);
217
+ }));
218
+ this.messagingSystem.subscribe('KeyringController:lock', () => {
219
+ __classPrivateFieldSet(this, _TokenDetectionController_isUnlocked, false, "f");
220
+ __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_stopPolling).call(this);
221
+ });
222
+ this.messagingSystem.subscribe('TokenListController:stateChange', ({ tokenList }) => __awaiter(this, void 0, void 0, function* () {
223
+ const hasTokens = Object.keys(tokenList).length;
224
+ if (hasTokens) {
225
+ yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_restartTokenDetection).call(this);
226
+ }
227
+ }));
228
+ this.messagingSystem.subscribe('PreferencesController:stateChange', ({ selectedAddress: newSelectedAddress, useTokenDetection }) => __awaiter(this, void 0, void 0, function* () {
229
+ const isSelectedAddressChanged = __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f") !== newSelectedAddress;
230
+ const isDetectionChangedFromPreferences = __classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f") !== useTokenDetection;
231
+ __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, newSelectedAddress, "f");
232
+ __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, useTokenDetection, "f");
233
+ if (useTokenDetection &&
234
+ (isSelectedAddressChanged || isDetectionChangedFromPreferences)) {
235
+ yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_restartTokenDetection).call(this, {
236
+ selectedAddress: __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f"),
237
+ });
238
+ }
239
+ }));
240
+ this.messagingSystem.subscribe('AccountsController:selectedAccountChange', ({ address: newSelectedAddress }) => __awaiter(this, void 0, void 0, function* () {
241
+ const isSelectedAddressChanged = __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f") !== newSelectedAddress;
242
+ if (isSelectedAddressChanged &&
243
+ __classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f")) {
244
+ __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, newSelectedAddress, "f");
245
+ yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_restartTokenDetection).call(this, {
246
+ selectedAddress: __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f"),
247
+ });
248
+ }
249
+ }));
250
+ this.messagingSystem.subscribe('NetworkController:networkDidChange', ({ selectedNetworkClientId }) => __awaiter(this, void 0, void 0, function* () {
251
+ const isNetworkClientIdChanged = __classPrivateFieldGet(this, _TokenDetectionController_networkClientId, "f") !== selectedNetworkClientId;
252
+ const { chainId: newChainId } = __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainIdAndNetworkClientId).call(this, selectedNetworkClientId);
253
+ __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledForNetwork, (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(newChainId), "f");
254
+ if (isNetworkClientIdChanged && __classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledForNetwork, "f")) {
255
+ __classPrivateFieldSet(this, _TokenDetectionController_networkClientId, selectedNetworkClientId, "f");
256
+ yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_restartTokenDetection).call(this, {
257
+ networkClientId: __classPrivateFieldGet(this, _TokenDetectionController_networkClientId, "f"),
258
+ });
259
+ }
260
+ }));
261
+ }, _TokenDetectionController_stopPolling = function _TokenDetectionController_stopPolling() {
226
262
  if (__classPrivateFieldGet(this, _TokenDetectionController_intervalId, "f")) {
227
263
  clearInterval(__classPrivateFieldGet(this, _TokenDetectionController_intervalId, "f"));
228
264
  }
229
265
  }, _TokenDetectionController_startPolling = function _TokenDetectionController_startPolling() {
230
266
  return __awaiter(this, void 0, void 0, function* () {
231
- if (__classPrivateFieldGet(this, _TokenDetectionController_disabled, "f")) {
267
+ if (!this.isActive) {
232
268
  return;
233
269
  }
234
270
  __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_stopPolling).call(this);
@@ -237,10 +273,91 @@ _TokenDetectionController_intervalId = new WeakMap(), _TokenDetectionController_
237
273
  yield this.detectTokens();
238
274
  }), this.getIntervalLength()), "f");
239
275
  });
240
- }, _TokenDetectionController_getCorrectChainId = function _TokenDetectionController_getCorrectChainId(networkClientId) {
241
- var _a;
242
- const { chainId } = (_a = this.messagingSystem.call('NetworkController:getNetworkConfigurationByNetworkClientId', networkClientId !== null && networkClientId !== void 0 ? networkClientId : __classPrivateFieldGet(this, _TokenDetectionController_networkClientId, "f"))) !== null && _a !== void 0 ? _a : {};
243
- return chainId !== null && chainId !== void 0 ? chainId : __classPrivateFieldGet(this, _TokenDetectionController_chainId, "f");
276
+ }, _TokenDetectionController_getCorrectChainIdAndNetworkClientId = function _TokenDetectionController_getCorrectChainIdAndNetworkClientId(networkClientId) {
277
+ if (networkClientId) {
278
+ const networkConfiguration = this.messagingSystem.call('NetworkController:getNetworkConfigurationByNetworkClientId', networkClientId);
279
+ if (networkConfiguration) {
280
+ return {
281
+ chainId: networkConfiguration.chainId,
282
+ networkClientId,
283
+ };
284
+ }
285
+ }
286
+ const { selectedNetworkClientId } = this.messagingSystem.call('NetworkController:getState');
287
+ const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
288
+ return {
289
+ chainId,
290
+ networkClientId: selectedNetworkClientId,
291
+ };
292
+ }, _TokenDetectionController_restartTokenDetection = function _TokenDetectionController_restartTokenDetection({ selectedAddress, networkClientId, } = {}) {
293
+ return __awaiter(this, void 0, void 0, function* () {
294
+ yield this.detectTokens({
295
+ networkClientId,
296
+ selectedAddress,
297
+ });
298
+ this.setIntervalLength(DEFAULT_INTERVAL);
299
+ });
300
+ }, _TokenDetectionController_getSlicesOfTokensToDetect = function _TokenDetectionController_getSlicesOfTokensToDetect({ chainId, selectedAddress, }) {
301
+ const { allTokens, allDetectedTokens, allIgnoredTokens } = this.messagingSystem.call('TokensController:getState');
302
+ const [tokensAddresses, detectedTokensAddresses, ignoredTokensAddresses] = [
303
+ allTokens,
304
+ allDetectedTokens,
305
+ allIgnoredTokens,
306
+ ].map((tokens) => {
307
+ var _a, _b;
308
+ return ((_b = (_a = tokens[chainId]) === null || _a === void 0 ? void 0 : _a[selectedAddress]) !== null && _b !== void 0 ? _b : []).map((value) => typeof value === 'string' ? value : value.address);
309
+ });
310
+ const tokensToDetect = [];
311
+ for (const tokenAddress of Object.keys(__classPrivateFieldGet(this, _TokenDetectionController_tokenList, "f"))) {
312
+ if ([
313
+ tokensAddresses,
314
+ detectedTokensAddresses,
315
+ ignoredTokensAddresses,
316
+ ].every((addresses) => !addresses.find((address) => isEqualCaseInsensitive(address, tokenAddress)))) {
317
+ tokensToDetect.push(tokenAddress);
318
+ }
319
+ }
320
+ const slicesOfTokensToDetect = [];
321
+ for (let i = 0, size = 1000; i < tokensToDetect.length; i += size) {
322
+ slicesOfTokensToDetect.push(tokensToDetect.slice(i, i + size));
323
+ }
324
+ return slicesOfTokensToDetect;
325
+ }, _TokenDetectionController_addDetectedTokens = function _TokenDetectionController_addDetectedTokens({ tokensSlice, selectedAddress, networkClientId, chainId, }) {
326
+ return __awaiter(this, void 0, void 0, function* () {
327
+ yield (0, controller_utils_1.safelyExecute)(() => __awaiter(this, void 0, void 0, function* () {
328
+ const balances = yield __classPrivateFieldGet(this, _TokenDetectionController_getBalancesInSingleCall, "f").call(this, selectedAddress, tokensSlice, networkClientId);
329
+ const tokensWithBalance = [];
330
+ const eventTokensDetails = [];
331
+ for (const nonZeroTokenAddress of Object.keys(balances)) {
332
+ const { decimals, symbol, aggregators, iconUrl, name } = __classPrivateFieldGet(this, _TokenDetectionController_tokenList, "f")[nonZeroTokenAddress];
333
+ eventTokensDetails.push(`${symbol} - ${nonZeroTokenAddress}`);
334
+ tokensWithBalance.push({
335
+ address: nonZeroTokenAddress,
336
+ decimals,
337
+ symbol,
338
+ aggregators,
339
+ image: iconUrl,
340
+ isERC721: false,
341
+ name,
342
+ });
343
+ }
344
+ if (tokensWithBalance.length) {
345
+ __classPrivateFieldGet(this, _TokenDetectionController_trackMetaMetricsEvent, "f").call(this, {
346
+ event: 'Token Detected',
347
+ category: 'Wallet',
348
+ properties: {
349
+ tokens: eventTokensDetails,
350
+ token_standard: 'ERC20',
351
+ asset_type: 'TOKEN',
352
+ },
353
+ });
354
+ yield this.messagingSystem.call('TokensController:addDetectedTokens', tokensWithBalance, {
355
+ selectedAddress,
356
+ chainId,
357
+ });
358
+ }
359
+ }));
360
+ });
244
361
  };
245
362
  exports.default = TokenDetectionController;
246
363
  //# sourceMappingURL=TokenDetectionController.js.map