@metamask-previews/assets-controllers 25.0.0-preview.8cdddaf → 25.0.0-preview.91dd28a
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 +163 -38
- package/dist/AssetsContractController.d.ts +4 -4
- package/dist/AssetsContractController.d.ts.map +1 -1
- package/dist/AssetsContractController.js +1 -2
- package/dist/AssetsContractController.js.map +1 -1
- package/dist/NftController.d.ts.map +1 -1
- package/dist/NftController.js +12 -14
- package/dist/NftController.js.map +1 -1
- package/dist/Standards/ERC20Standard.d.ts.map +1 -1
- package/dist/Standards/ERC20Standard.js +4 -6
- package/dist/Standards/ERC20Standard.js.map +1 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts.map +1 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +2 -3
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -1
- package/dist/TokenBalancesController.d.ts +12 -10
- package/dist/TokenBalancesController.d.ts.map +1 -1
- package/dist/TokenBalancesController.js +13 -16
- package/dist/TokenBalancesController.js.map +1 -1
- package/dist/TokenDetectionController.d.ts +32 -18
- package/dist/TokenDetectionController.d.ts.map +1 -1
- package/dist/TokenDetectionController.js +141 -60
- package/dist/TokenDetectionController.js.map +1 -1
- package/dist/TokensController.d.ts +20 -20
- package/dist/TokensController.d.ts.map +1 -1
- package/dist/TokensController.js +16 -23
- package/dist/TokensController.js.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -4
- package/dist/index.js.map +1 -1
- package/package.json +7 -2
- package/dist/Standards/standards-types.d.ts +0 -15
- package/dist/Standards/standards-types.d.ts.map +0 -1
- package/dist/Standards/standards-types.js +0 -3
- package/dist/Standards/standards-types.js.map +0 -1
|
@@ -1,24 +1,20 @@
|
|
|
1
1
|
import { type RestrictedControllerMessenger, type ControllerGetStateAction, type ControllerStateChangeEvent, BaseController } from '@metamask/base-controller';
|
|
2
|
-
import type {
|
|
2
|
+
import type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';
|
|
3
3
|
import type { AssetsContractController } from './AssetsContractController';
|
|
4
4
|
import type { Token } from './TokenRatesController';
|
|
5
|
-
import type {
|
|
5
|
+
import type { TokensControllerStateChangeEvent } from './TokensController';
|
|
6
6
|
declare const controllerName = "TokenBalancesController";
|
|
7
7
|
/**
|
|
8
8
|
* Token balances controller options
|
|
9
9
|
* @property interval - Polling interval used to fetch new token balances.
|
|
10
10
|
* @property tokens - List of tokens to track balances for.
|
|
11
11
|
* @property disabled - If set to true, all tracked tokens contract balances updates are blocked.
|
|
12
|
-
* @property onTokensStateChange - Allows subscribing to assets controller state changes.
|
|
13
|
-
* @property getSelectedAddress - Gets the current selected address.
|
|
14
12
|
* @property getERC20BalanceOf - Gets the balance of the given account at the given contract address.
|
|
15
13
|
*/
|
|
16
14
|
declare type TokenBalancesControllerOptions = {
|
|
17
15
|
interval?: number;
|
|
18
16
|
tokens?: Token[];
|
|
19
17
|
disabled?: boolean;
|
|
20
|
-
onTokensStateChange: (listener: (tokenState: TokensState) => void) => void;
|
|
21
|
-
getSelectedAddress: () => PreferencesState['selectedAddress'];
|
|
22
18
|
getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];
|
|
23
19
|
messenger: TokenBalancesControllerMessenger;
|
|
24
20
|
state?: Partial<TokenBalancesControllerState>;
|
|
@@ -36,9 +32,17 @@ export declare type TokenBalancesControllerState = {
|
|
|
36
32
|
};
|
|
37
33
|
export declare type TokenBalancesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, TokenBalancesControllerState>;
|
|
38
34
|
export declare type TokenBalancesControllerActions = TokenBalancesControllerGetStateAction;
|
|
35
|
+
export declare type AllowedActions = PreferencesControllerGetStateAction;
|
|
39
36
|
export declare type TokenBalancesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, TokenBalancesControllerState>;
|
|
40
37
|
export declare type TokenBalancesControllerEvents = TokenBalancesControllerStateChangeEvent;
|
|
41
|
-
export declare type
|
|
38
|
+
export declare type AllowedEvents = TokensControllerStateChangeEvent;
|
|
39
|
+
export declare type TokenBalancesControllerMessenger = RestrictedControllerMessenger<typeof controllerName, TokenBalancesControllerActions | AllowedActions, TokenBalancesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
40
|
+
/**
|
|
41
|
+
* Get the default TokenBalancesController state.
|
|
42
|
+
*
|
|
43
|
+
* @returns The default TokenBalancesController state.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getDefaultTokenBalancesState(): TokenBalancesControllerState;
|
|
42
46
|
/**
|
|
43
47
|
* Controller that passively polls on a set interval token balances
|
|
44
48
|
* for tokens stored in the TokensController
|
|
@@ -52,13 +56,11 @@ export declare class TokenBalancesController extends BaseController<typeof contr
|
|
|
52
56
|
* @param options.interval - Polling interval used to fetch new token balances.
|
|
53
57
|
* @param options.tokens - List of tokens to track balances for.
|
|
54
58
|
* @param options.disabled - If set to true, all tracked tokens contract balances updates are blocked.
|
|
55
|
-
* @param options.onTokensStateChange - Allows subscribing to assets controller state changes.
|
|
56
|
-
* @param options.getSelectedAddress - Gets the current selected address.
|
|
57
59
|
* @param options.getERC20BalanceOf - Gets the balance of the given account at the given contract address.
|
|
58
60
|
* @param options.state - Initial state to set on this controller.
|
|
59
61
|
* @param options.messenger - The controller restricted messenger.
|
|
60
62
|
*/
|
|
61
|
-
constructor({ interval, tokens, disabled,
|
|
63
|
+
constructor({ interval, tokens, disabled, getERC20BalanceOf, messenger, state, }: TokenBalancesControllerOptions);
|
|
62
64
|
/**
|
|
63
65
|
* Allows controller to update tracked tokens contract balances.
|
|
64
66
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenBalancesController.d.ts","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,6BAA6B,EAClC,KAAK,wBAAwB,EAC7B,KAAK,0BAA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AA0B,CACxB,OAAO,cAAc,EACrB,4BAA4B,CAC7B,CAAC;AAEJ,oBAAY,6BAA6B,GACvC,uCAAuC,CAAC;AAE1C,oBAAY,aAAa,GAAG,gCAAgC,CAAC;AAE7D,oBAAY,gCAAgC,GAAG,6BAA6B,CAC1E,OAAO,cAAc,EACrB,8BAA8B,GAAG,cAAc,EAC/C,6BAA6B,GAAG,aAAa,EAC7C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,4BAA4B,IAAI,4BAA4B,CAI3E;AAED;;;GAGG;AACH,qBAAa,uBAAwB,SAAQ,cAAc,CACzD,OAAO,cAAc,EACrB,4BAA4B,EAC5B,gCAAgC,CACjC;;IAWC;;;;;;;;;;OAUG;gBACS,EACV,QAA2B,EAC3B,MAAW,EACX,QAAgB,EAChB,iBAAiB,EACjB,SAAS,EACT,KAAU,GACX,EAAE,8BAA8B;IA4BjC;;OAEG;IACH,MAAM;IAIN;;OAEG;IACH,OAAO;IAIP;;;;OAIG;IACG,IAAI,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgB5C;;OAEG;IACG,cAAc;CA0BrB;AAED,eAAe,uBAAuB,CAAC"}
|
|
@@ -19,9 +19,9 @@ 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
|
|
22
|
+
var _TokenBalancesController_handle, _TokenBalancesController_getERC20BalanceOf, _TokenBalancesController_interval, _TokenBalancesController_tokens, _TokenBalancesController_disabled;
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.TokenBalancesController = void 0;
|
|
24
|
+
exports.TokenBalancesController = exports.getDefaultTokenBalancesState = void 0;
|
|
25
25
|
const base_controller_1 = require("@metamask/base-controller");
|
|
26
26
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
27
27
|
const DEFAULT_INTERVAL = 180000;
|
|
@@ -34,11 +34,12 @@ const metadata = {
|
|
|
34
34
|
*
|
|
35
35
|
* @returns The default TokenBalancesController state.
|
|
36
36
|
*/
|
|
37
|
-
function
|
|
37
|
+
function getDefaultTokenBalancesState() {
|
|
38
38
|
return {
|
|
39
39
|
contractBalances: {},
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
+
exports.getDefaultTokenBalancesState = getDefaultTokenBalancesState;
|
|
42
43
|
/**
|
|
43
44
|
* Controller that passively polls on a set interval token balances
|
|
44
45
|
* for tokens stored in the TokensController
|
|
@@ -51,22 +52,18 @@ class TokenBalancesController extends base_controller_1.BaseController {
|
|
|
51
52
|
* @param options.interval - Polling interval used to fetch new token balances.
|
|
52
53
|
* @param options.tokens - List of tokens to track balances for.
|
|
53
54
|
* @param options.disabled - If set to true, all tracked tokens contract balances updates are blocked.
|
|
54
|
-
* @param options.onTokensStateChange - Allows subscribing to assets controller state changes.
|
|
55
|
-
* @param options.getSelectedAddress - Gets the current selected address.
|
|
56
55
|
* @param options.getERC20BalanceOf - Gets the balance of the given account at the given contract address.
|
|
57
56
|
* @param options.state - Initial state to set on this controller.
|
|
58
57
|
* @param options.messenger - The controller restricted messenger.
|
|
59
58
|
*/
|
|
60
|
-
constructor({ interval = DEFAULT_INTERVAL, tokens = [], disabled = false,
|
|
59
|
+
constructor({ interval = DEFAULT_INTERVAL, tokens = [], disabled = false, getERC20BalanceOf, messenger, state = {}, }) {
|
|
61
60
|
super({
|
|
62
61
|
name: controllerName,
|
|
63
62
|
metadata,
|
|
64
63
|
messenger,
|
|
65
|
-
state: Object.assign(Object.assign({},
|
|
64
|
+
state: Object.assign(Object.assign({}, getDefaultTokenBalancesState()), state),
|
|
66
65
|
});
|
|
67
|
-
_TokenBalancesController_instances.add(this);
|
|
68
66
|
_TokenBalancesController_handle.set(this, void 0);
|
|
69
|
-
_TokenBalancesController_getSelectedAddress.set(this, void 0);
|
|
70
67
|
_TokenBalancesController_getERC20BalanceOf.set(this, void 0);
|
|
71
68
|
_TokenBalancesController_interval.set(this, void 0);
|
|
72
69
|
_TokenBalancesController_tokens.set(this, void 0);
|
|
@@ -74,8 +71,10 @@ class TokenBalancesController extends base_controller_1.BaseController {
|
|
|
74
71
|
__classPrivateFieldSet(this, _TokenBalancesController_disabled, disabled, "f");
|
|
75
72
|
__classPrivateFieldSet(this, _TokenBalancesController_interval, interval, "f");
|
|
76
73
|
__classPrivateFieldSet(this, _TokenBalancesController_tokens, tokens, "f");
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
this.messagingSystem.subscribe('TokensController:stateChange', ({ tokens: newTokens, detectedTokens }) => {
|
|
75
|
+
__classPrivateFieldSet(this, _TokenBalancesController_tokens, [...newTokens, ...detectedTokens], "f");
|
|
76
|
+
this.updateBalances();
|
|
77
|
+
});
|
|
79
78
|
__classPrivateFieldSet(this, _TokenBalancesController_getERC20BalanceOf, getERC20BalanceOf, "f");
|
|
80
79
|
this.poll();
|
|
81
80
|
}
|
|
@@ -121,8 +120,9 @@ class TokenBalancesController extends base_controller_1.BaseController {
|
|
|
121
120
|
const newContractBalances = {};
|
|
122
121
|
for (const token of __classPrivateFieldGet(this, _TokenBalancesController_tokens, "f")) {
|
|
123
122
|
const { address } = token;
|
|
123
|
+
const { selectedAddress } = this.messagingSystem.call('PreferencesController:getState');
|
|
124
124
|
try {
|
|
125
|
-
newContractBalances[address] = (0, controller_utils_1.toHex)(yield __classPrivateFieldGet(this, _TokenBalancesController_getERC20BalanceOf, "f").call(this, address,
|
|
125
|
+
newContractBalances[address] = (0, controller_utils_1.toHex)(yield __classPrivateFieldGet(this, _TokenBalancesController_getERC20BalanceOf, "f").call(this, address, selectedAddress));
|
|
126
126
|
token.balanceError = null;
|
|
127
127
|
}
|
|
128
128
|
catch (error) {
|
|
@@ -137,9 +137,6 @@ class TokenBalancesController extends base_controller_1.BaseController {
|
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
exports.TokenBalancesController = TokenBalancesController;
|
|
140
|
-
_TokenBalancesController_handle = new WeakMap(),
|
|
141
|
-
__classPrivateFieldSet(this, _TokenBalancesController_tokens, [...tokens, ...detectedTokens], "f");
|
|
142
|
-
this.updateBalances();
|
|
143
|
-
};
|
|
140
|
+
_TokenBalancesController_handle = new WeakMap(), _TokenBalancesController_getERC20BalanceOf = new WeakMap(), _TokenBalancesController_interval = new WeakMap(), _TokenBalancesController_tokens = new WeakMap(), _TokenBalancesController_disabled = new WeakMap();
|
|
144
141
|
exports.default = TokenBalancesController;
|
|
145
142
|
//# sourceMappingURL=TokenBalancesController.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenBalancesController.js","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAKmC;AACnC,iEAAkE;AAOlE,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAEjD,MAAM,QAAQ,GAAG;IACf,gBAAgB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE;CACtD,CAAC;AA4DF;;;;GAIG;AACH,
|
|
1
|
+
{"version":3,"file":"TokenBalancesController.js","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAKmC;AACnC,iEAAkE;AAOlE,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAEjD,MAAM,QAAQ,GAAG;IACf,gBAAgB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE;CACtD,CAAC;AA4D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AC/C,CAAC,CAAC,CAAC;QACL,CAAC;KAAA;CACF;AA7HD,0DA6HC;;AAED,kBAAe,uBAAuB,CAAC","sourcesContent":["import {\n type RestrictedControllerMessenger,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n BaseController,\n} from '@metamask/base-controller';\nimport { safelyExecute, toHex } from '@metamask/controller-utils';\nimport type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';\n\nimport type { AssetsContractController } from './AssetsContractController';\nimport type { Token } from './TokenRatesController';\nimport type { TokensControllerStateChangeEvent } from './TokensController';\n\nconst DEFAULT_INTERVAL = 180000;\n\nconst controllerName = 'TokenBalancesController';\n\nconst metadata = {\n contractBalances: { persist: true, anonymous: false },\n};\n\n/**\n * Token balances controller options\n * @property interval - Polling interval used to fetch new token balances.\n * @property tokens - List of tokens to track balances for.\n * @property disabled - If set to true, all tracked tokens contract balances updates are blocked.\n * @property getERC20BalanceOf - Gets the balance of the given account at the given contract address.\n */\ntype TokenBalancesControllerOptions = {\n interval?: number;\n tokens?: Token[];\n disabled?: boolean;\n getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];\n messenger: TokenBalancesControllerMessenger;\n state?: Partial<TokenBalancesControllerState>;\n};\n\n/**\n * Represents a mapping of hash token contract addresses to their balances.\n */\ntype ContractBalances = Record<string, string>;\n\n/**\n * Token balances controller state\n * @property contractBalances - Hash of token contract addresses to balances\n */\nexport type TokenBalancesControllerState = {\n contractBalances: ContractBalances;\n};\n\nexport type TokenBalancesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n TokenBalancesControllerState\n>;\n\nexport type TokenBalancesControllerActions =\n TokenBalancesControllerGetStateAction;\n\nexport type AllowedActions = PreferencesControllerGetStateAction;\n\nexport type TokenBalancesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n TokenBalancesControllerState\n >;\n\nexport type TokenBalancesControllerEvents =\n TokenBalancesControllerStateChangeEvent;\n\nexport type AllowedEvents = TokensControllerStateChangeEvent;\n\nexport type TokenBalancesControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n TokenBalancesControllerActions | AllowedActions,\n TokenBalancesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Get the default TokenBalancesController state.\n *\n * @returns The default TokenBalancesController state.\n */\nexport function getDefaultTokenBalancesState(): TokenBalancesControllerState {\n return {\n contractBalances: {},\n };\n}\n\n/**\n * Controller that passively polls on a set interval token balances\n * for tokens stored in the TokensController\n */\nexport class TokenBalancesController extends BaseController<\n typeof controllerName,\n TokenBalancesControllerState,\n TokenBalancesControllerMessenger\n> {\n #handle?: ReturnType<typeof setTimeout>;\n\n #getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];\n\n #interval: number;\n\n #tokens: Token[];\n\n #disabled: boolean;\n\n /**\n * Construct a Token Balances Controller.\n *\n * @param options - The controller options.\n * @param options.interval - Polling interval used to fetch new token balances.\n * @param options.tokens - List of tokens to track balances for.\n * @param options.disabled - If set to true, all tracked tokens contract balances updates are blocked.\n * @param options.getERC20BalanceOf - Gets the balance of the given account at the given contract address.\n * @param options.state - Initial state to set on this controller.\n * @param options.messenger - The controller restricted messenger.\n */\n constructor({\n interval = DEFAULT_INTERVAL,\n tokens = [],\n disabled = false,\n getERC20BalanceOf,\n messenger,\n state = {},\n }: TokenBalancesControllerOptions) {\n super({\n name: controllerName,\n metadata,\n messenger,\n state: {\n ...getDefaultTokenBalancesState(),\n ...state,\n },\n });\n\n this.#disabled = disabled;\n this.#interval = interval;\n this.#tokens = tokens;\n\n this.messagingSystem.subscribe(\n 'TokensController:stateChange',\n ({ tokens: newTokens, detectedTokens }) => {\n this.#tokens = [...newTokens, ...detectedTokens];\n this.updateBalances();\n },\n );\n\n this.#getERC20BalanceOf = getERC20BalanceOf;\n\n this.poll();\n }\n\n /**\n * Allows controller to update tracked tokens contract balances.\n */\n enable() {\n this.#disabled = false;\n }\n\n /**\n * Blocks controller from updating tracked tokens contract balances.\n */\n disable() {\n this.#disabled = true;\n }\n\n /**\n * Starts a new polling interval.\n *\n * @param interval - Polling interval used to fetch new token balances.\n */\n async poll(interval?: number): Promise<void> {\n if (interval) {\n this.#interval = interval;\n }\n\n if (this.#handle) {\n clearTimeout(this.#handle);\n }\n\n await safelyExecute(() => this.updateBalances());\n\n this.#handle = setTimeout(() => {\n this.poll(this.#interval);\n }, this.#interval);\n }\n\n /**\n * Updates balances for all tokens.\n */\n async updateBalances() {\n if (this.#disabled) {\n return;\n }\n\n const newContractBalances: ContractBalances = {};\n for (const token of this.#tokens) {\n const { address } = token;\n const { selectedAddress } = this.messagingSystem.call(\n 'PreferencesController:getState',\n );\n try {\n newContractBalances[address] = toHex(\n await this.#getERC20BalanceOf(address, selectedAddress),\n );\n token.balanceError = null;\n } catch (error) {\n newContractBalances[address] = toHex(0);\n token.balanceError = error;\n }\n }\n\n this.update((state) => {\n state.contractBalances = newContractBalances;\n });\n }\n}\n\nexport default TokenBalancesController;\n"]}
|
|
@@ -1,18 +1,22 @@
|
|
|
1
|
+
import type { AccountsControllerSelectedAccountChangeEvent } from '@metamask/accounts-controller';
|
|
1
2
|
import type { RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from '@metamask/base-controller';
|
|
2
|
-
import type {
|
|
3
|
+
import type { KeyringControllerGetStateAction, KeyringControllerLockEvent, KeyringControllerUnlockEvent } from '@metamask/keyring-controller';
|
|
4
|
+
import type { NetworkClientId, NetworkControllerNetworkDidChangeEvent, NetworkControllerGetNetworkConfigurationByNetworkClientId } from '@metamask/network-controller';
|
|
3
5
|
import { StaticIntervalPollingController } from '@metamask/polling-controller';
|
|
4
|
-
import type {
|
|
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 {
|
|
8
|
+
import type { GetTokenListState, TokenListStateChange, TokenListToken } from './TokenListController';
|
|
9
|
+
import type { Token } from './TokenRatesController';
|
|
10
|
+
import type { TokensControllerAddDetectedTokensAction, TokensControllerGetStateAction } from './TokensController';
|
|
11
|
+
export declare const STATIC_MAINNET_TOKEN_LIST: Record<string, Partial<TokenListToken> & Pick<Token, "symbol" | "address" | "decimals">>;
|
|
8
12
|
export declare const controllerName = "TokenDetectionController";
|
|
9
13
|
export declare type TokenDetectionState = Record<never, never>;
|
|
10
14
|
export declare type TokenDetectionControllerGetStateAction = ControllerGetStateAction<typeof controllerName, TokenDetectionState>;
|
|
11
15
|
export declare type TokenDetectionControllerActions = TokenDetectionControllerGetStateAction;
|
|
12
|
-
export declare type AllowedActions = NetworkControllerGetNetworkConfigurationByNetworkClientId | GetTokenListState;
|
|
16
|
+
export declare type AllowedActions = NetworkControllerGetNetworkConfigurationByNetworkClientId | GetTokenListState | KeyringControllerGetStateAction | PreferencesControllerGetStateAction | TokensControllerGetStateAction | TokensControllerAddDetectedTokensAction;
|
|
13
17
|
export declare type TokenDetectionControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, TokenDetectionState>;
|
|
14
18
|
export declare type TokenDetectionControllerEvents = TokenDetectionControllerStateChangeEvent;
|
|
15
|
-
export declare type AllowedEvents =
|
|
19
|
+
export declare type AllowedEvents = AccountsControllerSelectedAccountChangeEvent | NetworkControllerNetworkDidChangeEvent | TokenListStateChange | KeyringControllerLockEvent | KeyringControllerUnlockEvent | PreferencesControllerStateChangeEvent;
|
|
16
20
|
export declare type TokenDetectionControllerMessenger = RestrictedControllerMessenger<typeof controllerName, TokenDetectionControllerActions | AllowedActions, TokenDetectionControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
17
21
|
/**
|
|
18
22
|
* Controller that passively polls on a set interval for Tokens auto detection
|
|
@@ -21,6 +25,7 @@ export declare type TokenDetectionControllerMessenger = RestrictedControllerMess
|
|
|
21
25
|
* @property selectedAddress - Vault selected address
|
|
22
26
|
* @property networkClientId - The network client ID of the current selected network
|
|
23
27
|
* @property disabled - Boolean to track if network requests are blocked
|
|
28
|
+
* @property isUnlocked - Boolean to track if the keyring state is unlocked
|
|
24
29
|
* @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
|
|
25
30
|
* @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
|
|
26
31
|
*/
|
|
@@ -35,22 +40,24 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
|
|
|
35
40
|
* @param options.interval - Polling interval used to fetch new token rates
|
|
36
41
|
* @param options.networkClientId - The selected network client ID of the current network
|
|
37
42
|
* @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
43
|
* @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
|
|
41
|
-
* @param options.
|
|
42
|
-
* @param options.getPreferencesState - Gets the state of the preferences controller.
|
|
44
|
+
* @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
|
|
43
45
|
*/
|
|
44
|
-
constructor({ networkClientId, selectedAddress, interval, disabled,
|
|
46
|
+
constructor({ networkClientId, selectedAddress, interval, disabled, getBalancesInSingleCall, trackMetaMetricsEvent, messenger, }: {
|
|
45
47
|
networkClientId: NetworkClientId;
|
|
46
48
|
selectedAddress?: string;
|
|
47
49
|
interval?: number;
|
|
48
50
|
disabled?: boolean;
|
|
49
|
-
onPreferencesStateChange: (listener: (preferencesState: PreferencesState) => void) => void;
|
|
50
|
-
addDetectedTokens: TokensController['addDetectedTokens'];
|
|
51
51
|
getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
trackMetaMetricsEvent: (options: {
|
|
53
|
+
event: string;
|
|
54
|
+
category: string;
|
|
55
|
+
properties: {
|
|
56
|
+
tokens: string[];
|
|
57
|
+
token_standard: string;
|
|
58
|
+
asset_type: string;
|
|
59
|
+
};
|
|
60
|
+
}) => void;
|
|
54
61
|
messenger: TokenDetectionControllerMessenger;
|
|
55
62
|
});
|
|
56
63
|
/**
|
|
@@ -61,6 +68,12 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
|
|
|
61
68
|
* Blocks controller from making network calls
|
|
62
69
|
*/
|
|
63
70
|
disable(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Internal isActive state
|
|
73
|
+
*
|
|
74
|
+
* @type {object}
|
|
75
|
+
*/
|
|
76
|
+
get isActive(): boolean;
|
|
64
77
|
/**
|
|
65
78
|
* Start polling for detected tokens.
|
|
66
79
|
*/
|
|
@@ -73,11 +86,12 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
|
|
|
73
86
|
address: string;
|
|
74
87
|
}): Promise<void>;
|
|
75
88
|
/**
|
|
76
|
-
*
|
|
89
|
+
* 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.
|
|
90
|
+
* 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
91
|
*
|
|
78
|
-
* @param options - Options
|
|
92
|
+
* @param options - Options for token detection.
|
|
79
93
|
* @param options.networkClientId - The ID of the network client to use.
|
|
80
|
-
* @param options.accountAddress -
|
|
94
|
+
* @param options.accountAddress - the selectedAddress against which to detect for token balances.
|
|
81
95
|
*/
|
|
82
96
|
detectTokens({ networkClientId, accountAddress, }?: {
|
|
83
97
|
networkClientId?: NetworkClientId;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenDetectionController.d.ts","sourceRoot":"","sources":["../src/TokenDetectionController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"TokenDetectionController.d.ts","sourceRoot":"","sources":["../src/TokenDetectionController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