@metamask/assets-controllers 33.0.0 → 34.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.
- package/CHANGELOG.md +43 -1
- package/dist/AccountTrackerController.js +2 -2
- package/dist/AccountTrackerController.mjs +1 -1
- package/dist/TokenBalancesController.js +2 -2
- package/dist/TokenBalancesController.mjs +1 -1
- package/dist/TokenDetectionController.js +2 -2
- package/dist/TokenDetectionController.mjs +1 -1
- package/dist/TokenRatesController.js +2 -2
- package/dist/TokenRatesController.mjs +1 -1
- package/dist/TokensController.js +2 -2
- package/dist/TokensController.mjs +1 -1
- package/dist/{chunk-RJBYLTY5.js → chunk-2NQRWANM.js} +57 -30
- package/dist/chunk-2NQRWANM.js.map +1 -0
- package/dist/{chunk-3K3LE2CZ.mjs → chunk-2TSAUGE7.mjs} +30 -19
- package/dist/chunk-2TSAUGE7.mjs.map +1 -0
- package/dist/{chunk-UVE4XY5Y.mjs → chunk-4JLB5OIJ.mjs} +57 -30
- package/dist/chunk-4JLB5OIJ.mjs.map +1 -0
- package/dist/{chunk-OEEFNXR7.mjs → chunk-6VQJFTNC.mjs} +4 -4
- package/dist/chunk-6VQJFTNC.mjs.map +1 -0
- package/dist/{chunk-VZF43NDM.js → chunk-GHKGU6GK.js} +30 -19
- package/dist/chunk-GHKGU6GK.js.map +1 -0
- package/dist/{chunk-P3O5CVAH.mjs → chunk-GJCTAKK5.mjs} +36 -34
- package/dist/chunk-GJCTAKK5.mjs.map +1 -0
- package/dist/chunk-HVOIBGYN.js +282 -0
- package/dist/chunk-HVOIBGYN.js.map +1 -0
- package/dist/{chunk-NWVMFCSC.js → chunk-NHFZIY2K.js} +4 -4
- package/dist/chunk-NHFZIY2K.js.map +1 -0
- package/dist/{chunk-FGAZXVKS.js → chunk-RPQ737HL.js} +36 -34
- package/dist/chunk-RPQ737HL.js.map +1 -0
- package/dist/chunk-Z7RMCHD4.mjs +282 -0
- package/dist/chunk-Z7RMCHD4.mjs.map +1 -0
- package/dist/index.js +6 -6
- package/dist/index.mjs +5 -5
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/AccountTrackerController.d.ts +50 -66
- package/dist/types/AccountTrackerController.d.ts.map +1 -1
- package/dist/types/TokenBalancesController.d.ts +2 -2
- package/dist/types/TokenBalancesController.d.ts.map +1 -1
- package/dist/types/TokenDetectionController.d.ts +4 -6
- package/dist/types/TokenDetectionController.d.ts.map +1 -1
- package/dist/types/TokenRatesController.d.ts +3 -3
- package/dist/types/TokenRatesController.d.ts.map +1 -1
- package/dist/types/TokensController.d.ts +4 -6
- package/dist/types/TokensController.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-3K3LE2CZ.mjs.map +0 -1
- package/dist/chunk-526TATMH.mjs +0 -254
- package/dist/chunk-526TATMH.mjs.map +0 -1
- package/dist/chunk-AZ6SRJVI.js +0 -254
- package/dist/chunk-AZ6SRJVI.js.map +0 -1
- package/dist/chunk-FGAZXVKS.js.map +0 -1
- package/dist/chunk-NWVMFCSC.js.map +0 -1
- package/dist/chunk-OEEFNXR7.mjs.map +0 -1
- package/dist/chunk-P3O5CVAH.mjs.map +0 -1
- package/dist/chunk-RJBYLTY5.js.map +0 -1
- package/dist/chunk-UVE4XY5Y.mjs.map +0 -1
- package/dist/chunk-VZF43NDM.js.map +0 -1
|
@@ -34,7 +34,7 @@ var STATIC_MAINNET_TOKEN_LIST = Object.entries(
|
|
|
34
34
|
};
|
|
35
35
|
}, {});
|
|
36
36
|
var controllerName = "TokenDetectionController";
|
|
37
|
-
var _intervalId,
|
|
37
|
+
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, _getSelectedAccount, getSelectedAccount_fn, _getSelectedAddress, getSelectedAddress_fn;
|
|
38
38
|
var TokenDetectionController = class extends StaticIntervalPollingController {
|
|
39
39
|
/**
|
|
40
40
|
* Creates a TokenDetectionController instance.
|
|
@@ -43,12 +43,10 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
|
|
|
43
43
|
* @param options.messenger - The controller messaging system.
|
|
44
44
|
* @param options.disabled - If set to true, all network requests are blocked.
|
|
45
45
|
* @param options.interval - Polling interval used to fetch new token rates
|
|
46
|
-
* @param options.selectedAddress - Vault selected address
|
|
47
46
|
* @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
|
|
48
47
|
* @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
|
|
49
48
|
*/
|
|
50
49
|
constructor({
|
|
51
|
-
selectedAddress,
|
|
52
50
|
interval = DEFAULT_INTERVAL,
|
|
53
51
|
disabled = true,
|
|
54
52
|
getBalancesInSingleCall,
|
|
@@ -82,8 +80,10 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
|
|
|
82
80
|
__privateAdd(this, _restartTokenDetection);
|
|
83
81
|
__privateAdd(this, _getSlicesOfTokensToDetect);
|
|
84
82
|
__privateAdd(this, _addDetectedTokens);
|
|
83
|
+
__privateAdd(this, _getSelectedAccount);
|
|
84
|
+
__privateAdd(this, _getSelectedAddress);
|
|
85
85
|
__privateAdd(this, _intervalId, void 0);
|
|
86
|
-
__privateAdd(this,
|
|
86
|
+
__privateAdd(this, _selectedAccountId, void 0);
|
|
87
87
|
__privateAdd(this, _networkClientId, void 0);
|
|
88
88
|
__privateAdd(this, _tokenList, {});
|
|
89
89
|
__privateAdd(this, _disabled, void 0);
|
|
@@ -94,7 +94,7 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
|
|
|
94
94
|
__privateAdd(this, _trackMetaMetricsEvent, void 0);
|
|
95
95
|
__privateSet(this, _disabled, disabled);
|
|
96
96
|
this.setIntervalLength(interval);
|
|
97
|
-
__privateSet(this,
|
|
97
|
+
__privateSet(this, _selectedAccountId, __privateMethod(this, _getSelectedAccount, getSelectedAccount_fn).call(this).id);
|
|
98
98
|
const { chainId, networkClientId } = __privateMethod(this, _getCorrectChainIdAndNetworkClientId, getCorrectChainIdAndNetworkClientId_fn).call(this);
|
|
99
99
|
__privateSet(this, _networkClientId, networkClientId);
|
|
100
100
|
const { useTokenDetection: defaultUseTokenDetection } = this.messagingSystem.call("PreferencesController:getState");
|
|
@@ -165,7 +165,7 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
|
|
|
165
165
|
if (!this.isActive) {
|
|
166
166
|
return;
|
|
167
167
|
}
|
|
168
|
-
const addressAgainstWhichToDetect = selectedAddress ??
|
|
168
|
+
const addressAgainstWhichToDetect = selectedAddress ?? __privateMethod(this, _getSelectedAddress, getSelectedAddress_fn).call(this);
|
|
169
169
|
const { chainId, networkClientId: selectedNetworkClientId } = __privateMethod(this, _getCorrectChainIdAndNetworkClientId, getCorrectChainIdAndNetworkClientId_fn).call(this, networkClientId);
|
|
170
170
|
const chainIdAgainstWhichToDetect = chainId;
|
|
171
171
|
const networkClientIdAgainstWhichToDetect = selectedNetworkClientId;
|
|
@@ -194,7 +194,7 @@ var TokenDetectionController = class extends StaticIntervalPollingController {
|
|
|
194
194
|
}
|
|
195
195
|
};
|
|
196
196
|
_intervalId = new WeakMap();
|
|
197
|
-
|
|
197
|
+
_selectedAccountId = new WeakMap();
|
|
198
198
|
_networkClientId = new WeakMap();
|
|
199
199
|
_tokenList = new WeakMap();
|
|
200
200
|
_disabled = new WeakMap();
|
|
@@ -228,28 +228,27 @@ registerEventListeners_fn = function() {
|
|
|
228
228
|
"PreferencesController:stateChange",
|
|
229
229
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
230
230
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
231
|
-
async ({
|
|
232
|
-
const
|
|
231
|
+
async ({ useTokenDetection }) => {
|
|
232
|
+
const selectedAccount = __privateMethod(this, _getSelectedAccount, getSelectedAccount_fn).call(this);
|
|
233
233
|
const isDetectionChangedFromPreferences = __privateGet(this, _isDetectionEnabledFromPreferences) !== useTokenDetection;
|
|
234
|
-
__privateSet(this, _selectedAddress, newSelectedAddress);
|
|
235
234
|
__privateSet(this, _isDetectionEnabledFromPreferences, useTokenDetection);
|
|
236
|
-
if (
|
|
235
|
+
if (isDetectionChangedFromPreferences) {
|
|
237
236
|
await __privateMethod(this, _restartTokenDetection, restartTokenDetection_fn).call(this, {
|
|
238
|
-
selectedAddress:
|
|
237
|
+
selectedAddress: selectedAccount.address
|
|
239
238
|
});
|
|
240
239
|
}
|
|
241
240
|
}
|
|
242
241
|
);
|
|
243
242
|
this.messagingSystem.subscribe(
|
|
244
|
-
"AccountsController:
|
|
243
|
+
"AccountsController:selectedEvmAccountChange",
|
|
245
244
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
246
245
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
247
|
-
async (
|
|
248
|
-
const
|
|
249
|
-
if (
|
|
250
|
-
__privateSet(this,
|
|
246
|
+
async (selectedAccount) => {
|
|
247
|
+
const isSelectedAccountIdChanged = __privateGet(this, _selectedAccountId) !== selectedAccount.id;
|
|
248
|
+
if (isSelectedAccountIdChanged) {
|
|
249
|
+
__privateSet(this, _selectedAccountId, selectedAccount.id);
|
|
251
250
|
await __privateMethod(this, _restartTokenDetection, restartTokenDetection_fn).call(this, {
|
|
252
|
-
selectedAddress:
|
|
251
|
+
selectedAddress: selectedAccount.address
|
|
253
252
|
});
|
|
254
253
|
}
|
|
255
254
|
}
|
|
@@ -411,6 +410,18 @@ addDetectedTokens_fn = async function({
|
|
|
411
410
|
}
|
|
412
411
|
});
|
|
413
412
|
};
|
|
413
|
+
_getSelectedAccount = new WeakSet();
|
|
414
|
+
getSelectedAccount_fn = function() {
|
|
415
|
+
return this.messagingSystem.call("AccountsController:getSelectedAccount");
|
|
416
|
+
};
|
|
417
|
+
_getSelectedAddress = new WeakSet();
|
|
418
|
+
getSelectedAddress_fn = function() {
|
|
419
|
+
const account = this.messagingSystem.call(
|
|
420
|
+
"AccountsController:getAccount",
|
|
421
|
+
__privateGet(this, _selectedAccountId)
|
|
422
|
+
);
|
|
423
|
+
return account?.address || "";
|
|
424
|
+
};
|
|
414
425
|
var TokenDetectionController_default = TokenDetectionController;
|
|
415
426
|
|
|
416
427
|
export {
|
|
@@ -420,4 +431,4 @@ export {
|
|
|
420
431
|
TokenDetectionController,
|
|
421
432
|
TokenDetectionController_default
|
|
422
433
|
};
|
|
423
|
-
//# sourceMappingURL=chunk-
|
|
434
|
+
//# sourceMappingURL=chunk-2TSAUGE7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/TokenDetectionController.ts"],"sourcesContent":["import type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerGetAccountAction,\n AccountsControllerSelectedEvmAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport contractMap from '@metamask/contract-metadata';\nimport { ChainId, safelyExecute } from '@metamask/controller-utils';\nimport type {\n KeyringControllerGetStateAction,\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetNetworkConfigurationByNetworkClientId,\n NetworkControllerGetStateAction,\n NetworkControllerNetworkDidChangeEvent,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type {\n PreferencesControllerGetStateAction,\n PreferencesControllerStateChangeEvent,\n} from '@metamask/preferences-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { AssetsContractController } from './AssetsContractController';\nimport { isTokenDetectionSupportedForNetwork } from './assetsUtil';\nimport type {\n GetTokenListState,\n TokenListMap,\n TokenListStateChange,\n} from './TokenListController';\nimport type { Token } from './TokenRatesController';\nimport type {\n TokensControllerAddDetectedTokensAction,\n TokensControllerGetStateAction,\n} from './TokensController';\n\nconst DEFAULT_INTERVAL = 180000;\n\n/**\n * Compare 2 given strings and return boolean\n * eg: \"foo\" and \"FOO\" => true\n * eg: \"foo\" and \"bar\" => false\n * eg: \"foo\" and 123 => false\n *\n * @param value1 - first string to compare\n * @param value2 - first string to compare\n * @returns true if 2 strings are identical when they are lowercase\n */\nexport function isEqualCaseInsensitive(\n value1: string,\n value2: string,\n): boolean {\n if (typeof value1 !== 'string' || typeof value2 !== 'string') {\n return false;\n }\n return value1.toLowerCase() === value2.toLowerCase();\n}\n\ntype LegacyToken = {\n name: string;\n logo: `${string}.svg`;\n symbol: string;\n decimals: number;\n erc20?: boolean;\n erc721?: boolean;\n};\n\ntype TokenDetectionMap = {\n [P in keyof TokenListMap]: Omit<TokenListMap[P], 'occurrences'>;\n};\n\nexport const STATIC_MAINNET_TOKEN_LIST = Object.entries<LegacyToken>(\n contractMap,\n).reduce<TokenDetectionMap>((acc, [base, contract]) => {\n const { logo, erc20, erc721, ...tokenMetadata } = contract;\n return {\n ...acc,\n [base.toLowerCase()]: {\n ...tokenMetadata,\n address: base.toLowerCase(),\n iconUrl: `images/contract/${logo}`,\n aggregators: [],\n },\n };\n}, {});\n\nexport const controllerName = 'TokenDetectionController';\n\nexport type TokenDetectionState = Record<never, never>;\n\nexport type TokenDetectionControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n TokenDetectionState\n>;\n\nexport type TokenDetectionControllerActions =\n TokenDetectionControllerGetStateAction;\n\nexport type AllowedActions =\n | AccountsControllerGetSelectedAccountAction\n | AccountsControllerGetAccountAction\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetNetworkConfigurationByNetworkClientId\n | NetworkControllerGetStateAction\n | GetTokenListState\n | KeyringControllerGetStateAction\n | PreferencesControllerGetStateAction\n | TokensControllerGetStateAction\n | TokensControllerAddDetectedTokensAction;\n\nexport type TokenDetectionControllerStateChangeEvent =\n ControllerStateChangeEvent<typeof controllerName, TokenDetectionState>;\n\nexport type TokenDetectionControllerEvents =\n TokenDetectionControllerStateChangeEvent;\n\nexport type AllowedEvents =\n | AccountsControllerSelectedEvmAccountChangeEvent\n | NetworkControllerNetworkDidChangeEvent\n | TokenListStateChange\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent\n | PreferencesControllerStateChangeEvent;\n\nexport type TokenDetectionControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n TokenDetectionControllerActions | AllowedActions,\n TokenDetectionControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Controller that passively polls on a set interval for Tokens auto detection\n * @property intervalId - Polling interval used to fetch new token rates\n * @property selectedAddress - Vault selected address\n * @property networkClientId - The network client ID of the current selected network\n * @property disabled - Boolean to track if network requests are blocked\n * @property isUnlocked - Boolean to track if the keyring state is unlocked\n * @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController\n * @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network\n */\nexport class TokenDetectionController extends StaticIntervalPollingController<\n typeof controllerName,\n TokenDetectionState,\n TokenDetectionControllerMessenger\n> {\n #intervalId?: ReturnType<typeof setTimeout>;\n\n #selectedAccountId: string;\n\n #networkClientId: NetworkClientId;\n\n #tokenList: TokenDetectionMap = {};\n\n #disabled: boolean;\n\n #isUnlocked: boolean;\n\n #isDetectionEnabledFromPreferences: boolean;\n\n #isDetectionEnabledForNetwork: boolean;\n\n readonly #getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];\n\n readonly #trackMetaMetricsEvent: (options: {\n event: string;\n category: string;\n properties: {\n tokens: string[];\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n token_standard: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n asset_type: string;\n };\n }) => void;\n\n /**\n * Creates a TokenDetectionController instance.\n *\n * @param options - The controller options.\n * @param options.messenger - The controller messaging system.\n * @param options.disabled - If set to true, all network requests are blocked.\n * @param options.interval - Polling interval used to fetch new token rates\n * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.\n * @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.\n */\n constructor({\n interval = DEFAULT_INTERVAL,\n disabled = true,\n getBalancesInSingleCall,\n trackMetaMetricsEvent,\n messenger,\n }: {\n interval?: number;\n disabled?: boolean;\n getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];\n trackMetaMetricsEvent: (options: {\n event: string;\n category: string;\n properties: {\n tokens: string[];\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n token_standard: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n asset_type: string;\n };\n }) => void;\n messenger: TokenDetectionControllerMessenger;\n }) {\n super({\n name: controllerName,\n messenger,\n state: {},\n metadata: {},\n });\n\n this.#disabled = disabled;\n this.setIntervalLength(interval);\n\n this.#selectedAccountId = this.#getSelectedAccount().id;\n\n const { chainId, networkClientId } =\n this.#getCorrectChainIdAndNetworkClientId();\n this.#networkClientId = networkClientId;\n\n const { useTokenDetection: defaultUseTokenDetection } =\n this.messagingSystem.call('PreferencesController:getState');\n this.#isDetectionEnabledFromPreferences = defaultUseTokenDetection;\n this.#isDetectionEnabledForNetwork =\n isTokenDetectionSupportedForNetwork(chainId);\n\n this.#getBalancesInSingleCall = getBalancesInSingleCall;\n\n this.#trackMetaMetricsEvent = trackMetaMetricsEvent;\n\n const { isUnlocked } = this.messagingSystem.call(\n 'KeyringController:getState',\n );\n this.#isUnlocked = isUnlocked;\n\n this.#registerEventListeners();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system subscriptions to controller events.\n */\n #registerEventListeners() {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.messagingSystem.subscribe('KeyringController:unlock', async () => {\n this.#isUnlocked = true;\n await this.#restartTokenDetection();\n });\n\n this.messagingSystem.subscribe('KeyringController:lock', () => {\n this.#isUnlocked = false;\n this.#stopPolling();\n });\n\n this.messagingSystem.subscribe(\n 'TokenListController:stateChange',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async ({ tokenList }) => {\n const hasTokens = Object.keys(tokenList).length;\n\n if (hasTokens) {\n await this.#restartTokenDetection();\n }\n },\n );\n\n this.messagingSystem.subscribe(\n 'PreferencesController:stateChange',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async ({ useTokenDetection }) => {\n const selectedAccount = this.#getSelectedAccount();\n const isDetectionChangedFromPreferences =\n this.#isDetectionEnabledFromPreferences !== useTokenDetection;\n\n this.#isDetectionEnabledFromPreferences = useTokenDetection;\n\n if (isDetectionChangedFromPreferences) {\n await this.#restartTokenDetection({\n selectedAddress: selectedAccount.address,\n });\n }\n },\n );\n\n this.messagingSystem.subscribe(\n 'AccountsController:selectedEvmAccountChange',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (selectedAccount) => {\n const isSelectedAccountIdChanged =\n this.#selectedAccountId !== selectedAccount.id;\n if (isSelectedAccountIdChanged) {\n this.#selectedAccountId = selectedAccount.id;\n await this.#restartTokenDetection({\n selectedAddress: selectedAccount.address,\n });\n }\n },\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:networkDidChange',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async ({ selectedNetworkClientId }) => {\n const isNetworkClientIdChanged =\n this.#networkClientId !== selectedNetworkClientId;\n\n const { chainId: newChainId } =\n this.#getCorrectChainIdAndNetworkClientId(selectedNetworkClientId);\n this.#isDetectionEnabledForNetwork =\n isTokenDetectionSupportedForNetwork(newChainId);\n\n if (isNetworkClientIdChanged && this.#isDetectionEnabledForNetwork) {\n this.#networkClientId = selectedNetworkClientId;\n await this.#restartTokenDetection({\n networkClientId: this.#networkClientId,\n });\n }\n },\n );\n }\n\n /**\n * Allows controller to make active and passive polling requests\n */\n enable(): void {\n this.#disabled = false;\n }\n\n /**\n * Blocks controller from making network calls\n */\n disable(): void {\n this.#disabled = true;\n }\n\n /**\n * Internal isActive state\n * @type {boolean}\n */\n get isActive(): boolean {\n return !this.#disabled && this.#isUnlocked;\n }\n\n /**\n * Start polling for detected tokens.\n */\n async start(): Promise<void> {\n this.enable();\n await this.#startPolling();\n }\n\n /**\n * Stop polling for detected tokens.\n */\n stop(): void {\n this.disable();\n this.#stopPolling();\n }\n\n #stopPolling(): void {\n if (this.#intervalId) {\n clearInterval(this.#intervalId);\n }\n }\n\n /**\n * Starts a new polling interval.\n */\n async #startPolling(): Promise<void> {\n if (!this.isActive) {\n return;\n }\n this.#stopPolling();\n await this.detectTokens();\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.#intervalId = setInterval(async () => {\n await this.detectTokens();\n }, this.getIntervalLength());\n }\n\n #getCorrectChainIdAndNetworkClientId(networkClientId?: NetworkClientId): {\n chainId: Hex;\n networkClientId: NetworkClientId;\n } {\n if (networkClientId) {\n const networkConfiguration = this.messagingSystem.call(\n 'NetworkController:getNetworkConfigurationByNetworkClientId',\n networkClientId,\n );\n if (networkConfiguration) {\n return {\n chainId: networkConfiguration.chainId,\n networkClientId,\n };\n }\n }\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return {\n chainId,\n networkClientId: selectedNetworkClientId,\n };\n }\n\n async _executePoll(\n networkClientId: NetworkClientId,\n options: { address: string },\n ): Promise<void> {\n if (!this.isActive) {\n return;\n }\n await this.detectTokens({\n networkClientId,\n selectedAddress: options.address,\n });\n }\n\n /**\n * Restart token detection polling period and call detectNewTokens\n * in case of address change or user session initialization.\n *\n * @param options - Options for restart token detection.\n * @param options.selectedAddress - the selectedAddress against which to detect for token balances\n * @param options.networkClientId - The ID of the network client to use.\n */\n async #restartTokenDetection({\n selectedAddress,\n networkClientId,\n }: {\n selectedAddress?: string;\n networkClientId?: NetworkClientId;\n } = {}): Promise<void> {\n await this.detectTokens({\n networkClientId,\n selectedAddress,\n });\n this.setIntervalLength(DEFAULT_INTERVAL);\n }\n\n /**\n * 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.\n * 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.\n *\n * @param options - Options for token detection.\n * @param options.networkClientId - The ID of the network client to use.\n * @param options.selectedAddress - the selectedAddress against which to detect for token balances.\n */\n async detectTokens({\n networkClientId,\n selectedAddress,\n }: {\n networkClientId?: NetworkClientId;\n selectedAddress?: string;\n } = {}): Promise<void> {\n if (!this.isActive) {\n return;\n }\n\n const addressAgainstWhichToDetect =\n selectedAddress ?? this.#getSelectedAddress();\n const { chainId, networkClientId: selectedNetworkClientId } =\n this.#getCorrectChainIdAndNetworkClientId(networkClientId);\n const chainIdAgainstWhichToDetect = chainId;\n const networkClientIdAgainstWhichToDetect = selectedNetworkClientId;\n\n if (!isTokenDetectionSupportedForNetwork(chainIdAgainstWhichToDetect)) {\n return;\n }\n if (\n !this.#isDetectionEnabledFromPreferences &&\n chainIdAgainstWhichToDetect !== ChainId.mainnet\n ) {\n return;\n }\n const isTokenDetectionInactiveInMainnet =\n !this.#isDetectionEnabledFromPreferences &&\n chainIdAgainstWhichToDetect === ChainId.mainnet;\n const { tokensChainsCache } = this.messagingSystem.call(\n 'TokenListController:getState',\n );\n this.#tokenList = isTokenDetectionInactiveInMainnet\n ? STATIC_MAINNET_TOKEN_LIST\n : tokensChainsCache[chainIdAgainstWhichToDetect]?.data ?? {};\n\n for (const tokensSlice of this.#getSlicesOfTokensToDetect({\n chainId: chainIdAgainstWhichToDetect,\n selectedAddress: addressAgainstWhichToDetect,\n })) {\n await this.#addDetectedTokens({\n tokensSlice,\n selectedAddress: addressAgainstWhichToDetect,\n networkClientId: networkClientIdAgainstWhichToDetect,\n chainId: chainIdAgainstWhichToDetect,\n });\n }\n }\n\n #getSlicesOfTokensToDetect({\n chainId,\n selectedAddress,\n }: {\n chainId: Hex;\n selectedAddress: string;\n }): string[][] {\n const { allTokens, allDetectedTokens, allIgnoredTokens } =\n this.messagingSystem.call('TokensController:getState');\n const [tokensAddresses, detectedTokensAddresses, ignoredTokensAddresses] = [\n allTokens,\n allDetectedTokens,\n allIgnoredTokens,\n ].map((tokens) =>\n (tokens[chainId]?.[selectedAddress] ?? []).map((value) =>\n typeof value === 'string' ? value : value.address,\n ),\n );\n\n const tokensToDetect: string[] = [];\n for (const tokenAddress of Object.keys(this.#tokenList)) {\n if (\n [\n tokensAddresses,\n detectedTokensAddresses,\n ignoredTokensAddresses,\n ].every(\n (addresses) =>\n !addresses.find((address) =>\n isEqualCaseInsensitive(address, tokenAddress),\n ),\n )\n ) {\n tokensToDetect.push(tokenAddress);\n }\n }\n\n const slicesOfTokensToDetect = [];\n for (let i = 0, size = 1000; i < tokensToDetect.length; i += size) {\n slicesOfTokensToDetect.push(tokensToDetect.slice(i, i + size));\n }\n\n return slicesOfTokensToDetect;\n }\n\n async #addDetectedTokens({\n tokensSlice,\n selectedAddress,\n networkClientId,\n chainId,\n }: {\n tokensSlice: string[];\n selectedAddress: string;\n networkClientId: NetworkClientId;\n chainId: Hex;\n }): Promise<void> {\n await safelyExecute(async () => {\n const balances = await this.#getBalancesInSingleCall(\n selectedAddress,\n tokensSlice,\n networkClientId,\n );\n\n const tokensWithBalance: Token[] = [];\n const eventTokensDetails: string[] = [];\n for (const nonZeroTokenAddress of Object.keys(balances)) {\n const { decimals, symbol, aggregators, iconUrl, name } =\n this.#tokenList[nonZeroTokenAddress];\n eventTokensDetails.push(`${symbol} - ${nonZeroTokenAddress}`);\n tokensWithBalance.push({\n address: nonZeroTokenAddress,\n decimals,\n symbol,\n aggregators,\n image: iconUrl,\n isERC721: false,\n name,\n });\n }\n\n if (tokensWithBalance.length) {\n this.#trackMetaMetricsEvent({\n event: 'Token Detected',\n category: 'Wallet',\n properties: {\n tokens: eventTokensDetails,\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n token_standard: 'ERC20',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n asset_type: 'TOKEN',\n },\n });\n\n await this.messagingSystem.call(\n 'TokensController:addDetectedTokens',\n tokensWithBalance,\n {\n selectedAddress,\n chainId,\n },\n );\n }\n });\n }\n\n #getSelectedAccount() {\n return this.messagingSystem.call('AccountsController:getSelectedAccount');\n }\n\n #getSelectedAddress() {\n // If the address is not defined (or empty), we fallback to the currently selected account's address\n const account = this.messagingSystem.call(\n 'AccountsController:getAccount',\n this.#selectedAccountId,\n );\n return account?.address || '';\n }\n}\n\nexport default TokenDetectionController;\n"],"mappings":";;;;;;;;;;;AAUA,OAAO,iBAAiB;AACxB,SAAS,SAAS,qBAAqB;AAavC,SAAS,uCAAuC;AAoBhD,IAAM,mBAAmB;AAYlB,SAAS,uBACd,QACA,QACS;AACT,MAAI,OAAO,WAAW,YAAY,OAAO,WAAW,UAAU;AAC5D,WAAO;AAAA,EACT;AACA,SAAO,OAAO,YAAY,MAAM,OAAO,YAAY;AACrD;AAeO,IAAM,4BAA4B,OAAO;AAA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_mutex, _chainId, _selectedAccountId, _provider, _abortController, _onNetworkDidChange, onNetworkDidChange_fn, _onSelectedAccountChange, onSelectedAccountChange_fn, _fetchTokenMetadata, fetchTokenMetadata_fn, _updateTokensAttribute, updateTokensAttribute_fn, _detectIsERC721, detectIsERC721_fn, _getProvider, getProvider_fn, _createEthersContract, createEthersContract_fn, _generateRandomId, generateRandomId_fn, _getNewAllTokensState, getNewAllTokensState_fn, _getAddressOrSelectedAddress, getAddressOrSelectedAddress_fn, _isInteractingWithWallet, isInteractingWithWallet_fn, _requestApproval, requestApproval_fn, _getSelectedAccount, getSelectedAccount_fn, _getSelectedAddress, getSelectedAddress_fn;
|
|
80
80
|
var TokensController = class extends BaseController {
|
|
81
81
|
/**
|
|
82
82
|
* Tokens controller options
|
|
83
83
|
* @param options - Constructor options.
|
|
84
84
|
* @param options.chainId - The chain ID of the current network.
|
|
85
|
-
* @param options.selectedAddress - Vault selected address
|
|
86
85
|
* @param options.provider - Network provider.
|
|
87
86
|
* @param options.state - Initial state to set on this controller.
|
|
88
87
|
* @param options.messenger - The controller messenger.
|
|
89
88
|
*/
|
|
90
89
|
constructor({
|
|
91
90
|
chainId: initialChainId,
|
|
92
|
-
selectedAddress,
|
|
93
91
|
provider,
|
|
94
92
|
state,
|
|
95
93
|
messenger
|
|
@@ -112,11 +110,10 @@ var TokensController = class extends BaseController {
|
|
|
112
110
|
*/
|
|
113
111
|
__privateAdd(this, _onNetworkDidChange);
|
|
114
112
|
/**
|
|
115
|
-
* Handles the
|
|
116
|
-
* @param
|
|
117
|
-
* @param preferencesState.selectedAddress - The current selected address of the preference controller.
|
|
113
|
+
* Handles the selected account change in the accounts controller.
|
|
114
|
+
* @param selectedAccount - The new selected account
|
|
118
115
|
*/
|
|
119
|
-
__privateAdd(this,
|
|
116
|
+
__privateAdd(this, _onSelectedAccountChange);
|
|
120
117
|
/**
|
|
121
118
|
* Fetch metadata for a token.
|
|
122
119
|
*
|
|
@@ -156,23 +153,27 @@ var TokensController = class extends BaseController {
|
|
|
156
153
|
* @returns The updated `allTokens` and `allIgnoredTokens` state.
|
|
157
154
|
*/
|
|
158
155
|
__privateAdd(this, _getNewAllTokensState);
|
|
156
|
+
__privateAdd(this, _getAddressOrSelectedAddress);
|
|
157
|
+
__privateAdd(this, _isInteractingWithWallet);
|
|
159
158
|
__privateAdd(this, _requestApproval);
|
|
159
|
+
__privateAdd(this, _getSelectedAccount);
|
|
160
|
+
__privateAdd(this, _getSelectedAddress);
|
|
160
161
|
__privateAdd(this, _mutex, new Mutex());
|
|
161
162
|
__privateAdd(this, _chainId, void 0);
|
|
162
|
-
__privateAdd(this,
|
|
163
|
+
__privateAdd(this, _selectedAccountId, void 0);
|
|
163
164
|
__privateAdd(this, _provider, void 0);
|
|
164
165
|
__privateAdd(this, _abortController, void 0);
|
|
165
166
|
__privateSet(this, _chainId, initialChainId);
|
|
166
167
|
__privateSet(this, _provider, provider);
|
|
167
|
-
__privateSet(this,
|
|
168
|
+
__privateSet(this, _selectedAccountId, __privateMethod(this, _getSelectedAccount, getSelectedAccount_fn).call(this).id);
|
|
168
169
|
__privateSet(this, _abortController, new AbortController());
|
|
169
170
|
this.messagingSystem.registerActionHandler(
|
|
170
171
|
`${controllerName}:addDetectedTokens`,
|
|
171
172
|
this.addDetectedTokens.bind(this)
|
|
172
173
|
);
|
|
173
174
|
this.messagingSystem.subscribe(
|
|
174
|
-
"
|
|
175
|
-
__privateMethod(this,
|
|
175
|
+
"AccountsController:selectedEvmAccountChange",
|
|
176
|
+
__privateMethod(this, _onSelectedAccountChange, onSelectedAccountChange_fn).bind(this)
|
|
176
177
|
);
|
|
177
178
|
this.messagingSystem.subscribe(
|
|
178
179
|
"NetworkController:networkDidChange",
|
|
@@ -211,7 +212,6 @@ var TokensController = class extends BaseController {
|
|
|
211
212
|
networkClientId
|
|
212
213
|
}) {
|
|
213
214
|
const chainId = __privateGet(this, _chainId);
|
|
214
|
-
const selectedAddress = __privateGet(this, _selectedAddress);
|
|
215
215
|
const releaseLock = await __privateGet(this, _mutex).acquire();
|
|
216
216
|
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
|
|
217
217
|
let currentChainId = chainId;
|
|
@@ -221,8 +221,8 @@ var TokensController = class extends BaseController {
|
|
|
221
221
|
networkClientId
|
|
222
222
|
).configuration.chainId;
|
|
223
223
|
}
|
|
224
|
-
const accountAddress =
|
|
225
|
-
const isInteractingWithWalletAccount =
|
|
224
|
+
const accountAddress = __privateMethod(this, _getAddressOrSelectedAddress, getAddressOrSelectedAddress_fn).call(this, interactingAddress);
|
|
225
|
+
const isInteractingWithWalletAccount = __privateMethod(this, _isInteractingWithWallet, isInteractingWithWallet_fn).call(this, accountAddress);
|
|
226
226
|
try {
|
|
227
227
|
address = toChecksumHexAddress(address);
|
|
228
228
|
const tokens = allTokens[currentChainId]?.[accountAddress] || [];
|
|
@@ -401,7 +401,7 @@ var TokensController = class extends BaseController {
|
|
|
401
401
|
async addDetectedTokens(incomingDetectedTokens, detectionDetails) {
|
|
402
402
|
const releaseLock = await __privateGet(this, _mutex).acquire();
|
|
403
403
|
const chainId = detectionDetails?.chainId ?? __privateGet(this, _chainId);
|
|
404
|
-
const accountAddress = detectionDetails?.selectedAddress ??
|
|
404
|
+
const accountAddress = detectionDetails?.selectedAddress ?? __privateMethod(this, _getSelectedAddress, getSelectedAddress_fn).call(this);
|
|
405
405
|
const { allTokens, allDetectedTokens, allIgnoredTokens } = this.state;
|
|
406
406
|
let newTokens = [...allTokens?.[chainId]?.[accountAddress] ?? []];
|
|
407
407
|
let newDetectedTokens = [
|
|
@@ -453,8 +453,9 @@ var TokensController = class extends BaseController {
|
|
|
453
453
|
interactingAddress: accountAddress,
|
|
454
454
|
interactingChainId: chainId
|
|
455
455
|
});
|
|
456
|
-
|
|
457
|
-
|
|
456
|
+
const selectedAddress = __privateMethod(this, _getSelectedAddress, getSelectedAddress_fn).call(this);
|
|
457
|
+
newTokens = newAllTokens?.[__privateGet(this, _chainId)]?.[selectedAddress] || [];
|
|
458
|
+
newDetectedTokens = newAllDetectedTokens?.[__privateGet(this, _chainId)]?.[selectedAddress] || [];
|
|
458
459
|
this.update((state) => {
|
|
459
460
|
state.tokens = newTokens;
|
|
460
461
|
state.allTokens = newAllTokens;
|
|
@@ -511,6 +512,7 @@ var TokensController = class extends BaseController {
|
|
|
511
512
|
if (!isValidHexAddress(asset.address)) {
|
|
512
513
|
throw rpcErrors.invalidParams(`Invalid address "${asset.address}"`);
|
|
513
514
|
}
|
|
515
|
+
const selectedAddress = __privateMethod(this, _getAddressOrSelectedAddress, getAddressOrSelectedAddress_fn).call(this, interactingAddress);
|
|
514
516
|
if (await __privateMethod(this, _detectIsERC721, detectIsERC721_fn).call(this, asset.address, networkClientId)) {
|
|
515
517
|
throw rpcErrors.invalidParams(
|
|
516
518
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
@@ -586,7 +588,7 @@ var TokensController = class extends BaseController {
|
|
|
586
588
|
id: __privateMethod(this, _generateRandomId, generateRandomId_fn).call(this),
|
|
587
589
|
time: Date.now(),
|
|
588
590
|
type,
|
|
589
|
-
interactingAddress:
|
|
591
|
+
interactingAddress: selectedAddress
|
|
590
592
|
};
|
|
591
593
|
await __privateMethod(this, _requestApproval, requestApproval_fn).call(this, suggestedAssetMeta);
|
|
592
594
|
const { address, symbol, decimals, name, image } = asset;
|
|
@@ -612,7 +614,7 @@ var TokensController = class extends BaseController {
|
|
|
612
614
|
};
|
|
613
615
|
_mutex = new WeakMap();
|
|
614
616
|
_chainId = new WeakMap();
|
|
615
|
-
|
|
617
|
+
_selectedAccountId = new WeakMap();
|
|
616
618
|
_provider = new WeakMap();
|
|
617
619
|
_abortController = new WeakMap();
|
|
618
620
|
_onNetworkDidChange = new WeakSet();
|
|
@@ -626,20 +628,21 @@ onNetworkDidChange_fn = function({ selectedNetworkClientId }) {
|
|
|
626
628
|
__privateGet(this, _abortController).abort();
|
|
627
629
|
__privateSet(this, _abortController, new AbortController());
|
|
628
630
|
__privateSet(this, _chainId, chainId);
|
|
631
|
+
const selectedAddress = __privateMethod(this, _getSelectedAddress, getSelectedAddress_fn).call(this);
|
|
629
632
|
this.update((state) => {
|
|
630
|
-
state.tokens = allTokens[chainId]?.[
|
|
631
|
-
state.ignoredTokens = allIgnoredTokens[chainId]?.[
|
|
632
|
-
state.detectedTokens = allDetectedTokens[chainId]?.[
|
|
633
|
+
state.tokens = allTokens[chainId]?.[selectedAddress] || [];
|
|
634
|
+
state.ignoredTokens = allIgnoredTokens[chainId]?.[selectedAddress] || [];
|
|
635
|
+
state.detectedTokens = allDetectedTokens[chainId]?.[selectedAddress] || [];
|
|
633
636
|
});
|
|
634
637
|
};
|
|
635
|
-
|
|
636
|
-
|
|
638
|
+
_onSelectedAccountChange = new WeakSet();
|
|
639
|
+
onSelectedAccountChange_fn = function(selectedAccount) {
|
|
637
640
|
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
|
|
638
|
-
__privateSet(this,
|
|
641
|
+
__privateSet(this, _selectedAccountId, selectedAccount.id);
|
|
639
642
|
this.update((state) => {
|
|
640
|
-
state.tokens = allTokens[__privateGet(this, _chainId)]?.[
|
|
641
|
-
state.ignoredTokens = allIgnoredTokens[__privateGet(this, _chainId)]?.[
|
|
642
|
-
state.detectedTokens = allDetectedTokens[__privateGet(this, _chainId)]?.[
|
|
643
|
+
state.tokens = allTokens[__privateGet(this, _chainId)]?.[selectedAccount.address] ?? [];
|
|
644
|
+
state.ignoredTokens = allIgnoredTokens[__privateGet(this, _chainId)]?.[selectedAccount.address] ?? [];
|
|
645
|
+
state.detectedTokens = allDetectedTokens[__privateGet(this, _chainId)]?.[selectedAccount.address] ?? [];
|
|
643
646
|
});
|
|
644
647
|
};
|
|
645
648
|
_fetchTokenMetadata = new WeakSet();
|
|
@@ -714,7 +717,7 @@ getNewAllTokensState_fn = function(params) {
|
|
|
714
717
|
interactingChainId
|
|
715
718
|
} = params;
|
|
716
719
|
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
|
|
717
|
-
const userAddressToAddTokens =
|
|
720
|
+
const userAddressToAddTokens = __privateMethod(this, _getAddressOrSelectedAddress, getAddressOrSelectedAddress_fn).call(this, interactingAddress);
|
|
718
721
|
const chainIdToAddTokens = interactingChainId ?? __privateGet(this, _chainId);
|
|
719
722
|
let newAllTokens = allTokens;
|
|
720
723
|
if (newTokens?.length || newTokens && allTokens && allTokens[chainIdToAddTokens] && allTokens[chainIdToAddTokens][userAddressToAddTokens]) {
|
|
@@ -754,6 +757,18 @@ getNewAllTokensState_fn = function(params) {
|
|
|
754
757
|
}
|
|
755
758
|
return { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens };
|
|
756
759
|
};
|
|
760
|
+
_getAddressOrSelectedAddress = new WeakSet();
|
|
761
|
+
getAddressOrSelectedAddress_fn = function(address) {
|
|
762
|
+
if (address) {
|
|
763
|
+
return address;
|
|
764
|
+
}
|
|
765
|
+
return __privateMethod(this, _getSelectedAddress, getSelectedAddress_fn).call(this);
|
|
766
|
+
};
|
|
767
|
+
_isInteractingWithWallet = new WeakSet();
|
|
768
|
+
isInteractingWithWallet_fn = function(address) {
|
|
769
|
+
const selectedAddress = __privateMethod(this, _getSelectedAddress, getSelectedAddress_fn).call(this);
|
|
770
|
+
return selectedAddress === address;
|
|
771
|
+
};
|
|
757
772
|
_requestApproval = new WeakSet();
|
|
758
773
|
requestApproval_fn = async function(suggestedAssetMeta) {
|
|
759
774
|
return this.messagingSystem.call(
|
|
@@ -776,6 +791,18 @@ requestApproval_fn = async function(suggestedAssetMeta) {
|
|
|
776
791
|
true
|
|
777
792
|
);
|
|
778
793
|
};
|
|
794
|
+
_getSelectedAccount = new WeakSet();
|
|
795
|
+
getSelectedAccount_fn = function() {
|
|
796
|
+
return this.messagingSystem.call("AccountsController:getSelectedAccount");
|
|
797
|
+
};
|
|
798
|
+
_getSelectedAddress = new WeakSet();
|
|
799
|
+
getSelectedAddress_fn = function() {
|
|
800
|
+
const account = this.messagingSystem.call(
|
|
801
|
+
"AccountsController:getAccount",
|
|
802
|
+
__privateGet(this, _selectedAccountId)
|
|
803
|
+
);
|
|
804
|
+
return account?.address || "";
|
|
805
|
+
};
|
|
779
806
|
var TokensController_default = TokensController;
|
|
780
807
|
|
|
781
808
|
export {
|
|
@@ -783,4 +810,4 @@ export {
|
|
|
783
810
|
TokensController,
|
|
784
811
|
TokensController_default
|
|
785
812
|
};
|
|
786
|
-
//# sourceMappingURL=chunk-
|
|
813
|
+
//# sourceMappingURL=chunk-4JLB5OIJ.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 {\n AccountsControllerGetAccountAction,\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedEvmAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type { AddApprovalRequest } from '@metamask/approval-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } 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 type { InternalAccount } from '@metamask/keyring-api';\nimport { abiERC721 } from '@metamask/metamask-eth-abis';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerNetworkDidChangeEvent,\n NetworkState,\n Provider,\n} from '@metamask/network-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\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 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 TokensControllerState\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 TokensControllerState = {\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\nconst metadata = {\n tokens: {\n persist: true,\n anonymous: false,\n },\n ignoredTokens: {\n persist: true,\n anonymous: false,\n },\n detectedTokens: {\n persist: true,\n anonymous: false,\n },\n allTokens: {\n persist: true,\n anonymous: false,\n },\n allIgnoredTokens: {\n persist: true,\n anonymous: false,\n },\n allDetectedTokens: {\n persist: true,\n anonymous: false,\n },\n};\n\nconst controllerName = 'TokensController';\n\nexport type TokensControllerActions =\n | TokensControllerGetStateAction\n | TokensControllerAddDetectedTokensAction;\n\nexport type TokensControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n TokensControllerState\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 | AccountsControllerGetAccountAction\n | AccountsControllerGetSelectedAccountAction;\n\nexport type TokensControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n TokensControllerState\n>;\n\nexport type TokensControllerEvents = TokensControllerStateChangeEvent;\n\nexport type AllowedEvents =\n | NetworkControllerNetworkDidChangeEvent\n | TokenListStateChange\n | AccountsControllerSelectedEvmAccountChangeEvent;\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 = (): TokensControllerState => {\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 BaseController<\n typeof controllerName,\n TokensControllerState,\n TokensControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n #chainId: Hex;\n\n #selectedAccountId: string;\n\n #provider: Provider | undefined;\n\n #abortController: AbortController;\n\n /**\n * Tokens controller options\n * @param options - Constructor options.\n * @param options.chainId - The chain ID of the current network.\n * @param options.provider - Network provider.\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 provider,\n state,\n messenger,\n }: {\n chainId: Hex;\n provider: Provider | undefined;\n state?: Partial<TokensControllerState>;\n messenger: TokensControllerMessenger;\n }) {\n super({\n name: controllerName,\n metadata,\n messenger,\n state: {\n ...getDefaultTokensState(),\n ...state,\n },\n });\n\n this.#chainId = initialChainId;\n\n this.#provider = provider;\n\n this.#selectedAccountId = this.#getSelectedAccount().id;\n\n this.#abortController = new AbortController();\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:addDetectedTokens` as const,\n this.addDetectedTokens.bind(this),\n );\n\n this.messagingSystem.subscribe(\n 'AccountsController:selectedEvmAccountChange',\n this.#onSelectedAccountChange.bind(this),\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:networkDidChange',\n this.#onNetworkDidChange.bind(this),\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 * Handles the event when the network changes.\n *\n * @param networkState - The changed network state.\n * @param networkState.selectedNetworkClientId - The ID of the currently\n * selected network client.\n */\n #onNetworkDidChange({ selectedNetworkClientId }: NetworkState) {\n const selectedNetworkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;\n const { chainId } = selectedNetworkClient.configuration;\n this.#abortController.abort();\n this.#abortController = new AbortController();\n this.#chainId = chainId;\n const selectedAddress = this.#getSelectedAddress();\n this.update((state) => {\n state.tokens = allTokens[chainId]?.[selectedAddress] || [];\n state.ignoredTokens = allIgnoredTokens[chainId]?.[selectedAddress] || [];\n state.detectedTokens =\n allDetectedTokens[chainId]?.[selectedAddress] || [];\n });\n }\n\n /**\n * Handles the selected account change in the accounts controller.\n * @param selectedAccount - The new selected account\n */\n #onSelectedAccountChange(selectedAccount: InternalAccount) {\n const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;\n this.#selectedAccountId = selectedAccount.id;\n this.update((state) => {\n state.tokens = allTokens[this.#chainId]?.[selectedAccount.address] ?? [];\n state.ignoredTokens =\n allIgnoredTokens[this.#chainId]?.[selectedAccount.address] ?? [];\n state.detectedTokens =\n allDetectedTokens[this.#chainId]?.[selectedAccount.address] ?? [];\n });\n }\n\n /**\n * Fetch metadata for a token.\n *\n * @param tokenAddress - The address of the token.\n * @returns The token metadata.\n */\n async #fetchTokenMetadata(\n tokenAddress: string,\n ): Promise<TokenListToken | undefined> {\n try {\n const token = await fetchTokenMetadata<TokenListToken>(\n this.#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 * 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 = this.#chainId;\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 =\n this.#getAddressOrSelectedAddress(interactingAddress);\n const isInteractingWithWalletAccount =\n this.#isInteractingWithWallet(accountAddress);\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.#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<TokensControllerState> = {\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((state) => {\n Object.assign(state, newState);\n });\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((state) => {\n state.tokens = newTokens;\n state.allTokens = newAllTokens;\n state.detectedTokens = newDetectedTokens;\n state.allDetectedTokens = newAllDetectedTokens;\n state.ignoredTokens = newIgnoredTokens;\n state.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((state) => {\n state.ignoredTokens = newIgnoredTokens;\n state.tokens = newTokens;\n state.detectedTokens = newDetectedTokens;\n state.allIgnoredTokens = newAllIgnoredTokens;\n state.allDetectedTokens = newAllDetectedTokens;\n state.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 const chainId = detectionDetails?.chainId ?? this.#chainId;\n // Previously selectedAddress could be an empty string. This is to preserve the behaviour\n const accountAddress =\n detectionDetails?.selectedAddress ?? this.#getSelectedAddress();\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 selectedAddress = this.#getSelectedAddress();\n\n newTokens = newAllTokens?.[this.#chainId]?.[selectedAddress] || [];\n newDetectedTokens =\n newAllDetectedTokens?.[this.#chainId]?.[selectedAddress] || [];\n\n this.update((state) => {\n state.tokens = newTokens;\n state.allTokens = newAllTokens;\n state.detectedTokens = newDetectedTokens;\n state.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.tokens];\n const tokenIndex = tokens.findIndex((token) => {\n return token.address.toLowerCase() === tokenAddress.toLowerCase();\n });\n const updatedToken = { ...tokens[tokenIndex], isERC721 };\n tokens[tokenIndex] = updatedToken;\n this.update((state) => {\n state.tokens = tokens;\n });\n return updatedToken;\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 #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((state) => {\n state.tokens = newTokens;\n });\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.#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 const selectedAddress =\n this.#getAddressOrSelectedAddress(interactingAddress);\n\n // Validate contract\n\n if (await this.#detectIsERC721(asset.address, networkClientId)) {\n throw rpcErrors.invalidParams(\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\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: 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\n const userAddressToAddTokens =\n this.#getAddressOrSelectedAddress(interactingAddress);\n\n const chainIdToAddTokens = interactingChainId ?? this.#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 #getAddressOrSelectedAddress(address: string | undefined): string {\n if (address) {\n return address;\n }\n\n return this.#getSelectedAddress();\n }\n\n #isInteractingWithWallet(address: string | undefined) {\n const selectedAddress = this.#getSelectedAddress();\n\n return selectedAddress === address;\n }\n\n /**\n * Removes all tokens from the ignored list.\n */\n clearIgnoredTokens() {\n this.update((state) => {\n state.ignoredTokens = [];\n state.allIgnoredTokens = {};\n });\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 #getSelectedAccount() {\n return this.messagingSystem.call('AccountsController:getSelectedAccount');\n }\n\n #getSelectedAddress() {\n // If the address is not defined (or empty), we fallback to the currently selected account's address\n const account = this.messagingSystem.call(\n 'AccountsController:getAccount',\n this.#selectedAccountId,\n );\n return account?.address || '';\n }\n}\n\nexport default TokensController;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,oBAAoB;AAY7B,SAAS,sBAAsB;AAC/B,OAAO,kBAAkB;AACzB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,iBAAiB;AAQ1B,SAAS,iBAAiB;AAE1B,SAAS,aAAa;AACtB,SAAS,MAAM,cAAc;AAsD7B,IAAM,WAAW;AAAA,EACf,QAAQ;AAAA,IACN,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AAAA,EACA,eAAe;AAAA,IACb,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AAAA,EACA,gBAAgB;AAAA,IACd,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AAAA,EACA,WAAW;AAAA,IACT,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AAAA,EACA,kBAAkB;AAAA,IAChB,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AAAA,EACA,mBAAmB;AAAA,IACjB,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AACF;AAEA,IAAM,iBAAiB;AAgDhB,IAAM,wBAAwB,MAA6B;AAChE,SAAO;AAAA,IACL,QAAQ,CAAC;AAAA,IACT,eAAe,CAAC;AAAA,IAChB,gBAAgB,CAAC;AAAA,IACjB,WAAW,CAAC;AAAA,IACZ,kBAAkB,CAAC;AAAA,IACnB,mBAAmB,CAAC;AAAA,EACtB;AACF;AAhLA;AAqLO,IAAM,mBAAN,cAA+B,eAIpC;AAAA;AAAA;AAAA;AAA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X,IAAI,mBAAmB;AAAA,QACvB,oBAAoB,mBAAmB;AAAA,QACvC,OAAO;AAAA,UACL,SAAS,mBAAmB,MAAM;AAAA,UAClC,UAAU,mBAAmB,MAAM;AAAA,UACnC,QAAQ,mBAAmB,MAAM;AAAA,UACjC,OAAO,mBAAmB,MAAM,SAAS;AAAA,QAC3C;AAAA,MACF;AAAA,IACF;AAAA,IACA;AAAA,EACF;AACF;AAEA;AAAA,wBAAmB,WAAG;AACpB,SAAO,KAAK,gBAAgB,KAAK,uCAAuC;AAC1E;AAEA;AAAA,wBAAmB,WAAG;AAEpB,QAAM,UAAU,KAAK,gBAAgB;AAAA,IACnC;AAAA,IACA,mBAAK;AAAA,EACP;AACA,SAAO,SAAS,WAAW;AAC7B;AAGF,IAAO,2BAAQ;","names":[]}
|
|
@@ -103,14 +103,14 @@ var TokenBalancesController = class extends BaseController {
|
|
|
103
103
|
if (__privateGet(this, _disabled)) {
|
|
104
104
|
return;
|
|
105
105
|
}
|
|
106
|
-
const
|
|
107
|
-
"
|
|
106
|
+
const selectedInternalAccount = this.messagingSystem.call(
|
|
107
|
+
"AccountsController:getSelectedAccount"
|
|
108
108
|
);
|
|
109
109
|
const newContractBalances = {};
|
|
110
110
|
for (const token of __privateGet(this, _tokens)) {
|
|
111
111
|
const { address } = token;
|
|
112
112
|
try {
|
|
113
|
-
const balance = await __privateGet(this, _getERC20BalanceOf).call(this, address,
|
|
113
|
+
const balance = await __privateGet(this, _getERC20BalanceOf).call(this, address, selectedInternalAccount.address);
|
|
114
114
|
newContractBalances[address] = toHex(balance);
|
|
115
115
|
token.hasBalanceError = false;
|
|
116
116
|
} catch (error) {
|
|
@@ -135,4 +135,4 @@ export {
|
|
|
135
135
|
TokenBalancesController,
|
|
136
136
|
TokenBalancesController_default
|
|
137
137
|
};
|
|
138
|
-
//# sourceMappingURL=chunk-
|
|
138
|
+
//# sourceMappingURL=chunk-6VQJFTNC.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/TokenBalancesController.ts"],"sourcesContent":["import { type AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport {\n type RestrictedControllerMessenger,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n BaseController,\n} from '@metamask/base-controller';\nimport { safelyExecute, toHex } from '@metamask/controller-utils';\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 = AccountsControllerGetSelectedAccountAction;\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.updateBalances();\n },\n );\n\n this.#getERC20BalanceOf = getERC20BalanceOf;\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\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 const selectedInternalAccount = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n\n const newContractBalances: ContractBalances = {};\n for (const token of this.#tokens) {\n const { address } = token;\n try {\n const balance = await this.#getERC20BalanceOf(\n address,\n selectedInternalAccount.address,\n );\n newContractBalances[address] = toHex(balance);\n token.hasBalanceError = false;\n } catch (error) {\n newContractBalances[address] = toHex(0);\n token.hasBalanceError = true;\n }\n }\n\n this.update((state) => {\n state.contractBalances = newContractBalances;\n });\n }\n}\n\nexport default TokenBalancesController;\n"],"mappings":";;;;;;;AACA;AAAA,EAIE;AAAA,OACK;AACP,SAAS,eAAe,aAAa;AAMrC,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,C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