@metamask/assets-controllers 43.1.1 → 44.0.1
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 +25 -1
- package/dist/TokenBalancesController.cjs +157 -72
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +50 -30
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts +50 -30
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +159 -73
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/TokenDetectionController.cjs +4 -8
- package/dist/TokenDetectionController.cjs.map +1 -1
- package/dist/TokenDetectionController.d.cts.map +1 -1
- package/dist/TokenDetectionController.d.mts.map +1 -1
- package/dist/TokenDetectionController.mjs +4 -8
- package/dist/TokenDetectionController.mjs.map +1 -1
- package/dist/TokenRatesController.cjs +11 -1
- package/dist/TokenRatesController.cjs.map +1 -1
- package/dist/TokenRatesController.d.cts.map +1 -1
- package/dist/TokenRatesController.d.mts.map +1 -1
- package/dist/TokenRatesController.mjs +11 -1
- package/dist/TokenRatesController.mjs.map +1 -1
- package/dist/TokensController.cjs +20 -6
- package/dist/TokensController.cjs.map +1 -1
- package/dist/TokensController.d.cts +2 -2
- package/dist/TokensController.d.cts.map +1 -1
- package/dist/TokensController.d.mts +2 -2
- package/dist/TokensController.d.mts.map +1 -1
- package/dist/TokensController.mjs +20 -6
- package/dist/TokensController.mjs.map +1 -1
- package/dist/assetsUtil.cjs +2 -2
- package/dist/assetsUtil.cjs.map +1 -1
- package/dist/assetsUtil.d.cts +3 -3
- package/dist/assetsUtil.d.cts.map +1 -1
- package/dist/assetsUtil.d.mts +3 -3
- package/dist/assetsUtil.d.mts.map +1 -1
- package/dist/assetsUtil.mjs +2 -2
- package/dist/assetsUtil.mjs.map +1 -1
- package/dist/multicall.cjs +350 -0
- package/dist/multicall.cjs.map +1 -0
- package/dist/multicall.d.cts +26 -0
- package/dist/multicall.d.cts.map +1 -0
- package/dist/multicall.d.mts +26 -0
- package/dist/multicall.d.mts.map +1 -0
- package/dist/multicall.mjs +346 -0
- package/dist/multicall.mjs.map +1 -0
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [44.0.1]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Fixes an issue where the token detection was unnecessarily falling back to an RPC approach, causing redundant detections. ([#4928](https://github.com/MetaMask/core/pull/4928))
|
|
15
|
+
|
|
16
|
+
- Fixes an issue where `TokensController.addTokens` was not respecting the network client id passed to it. ([#4940](https://github.com/MetaMask/core/pull/4940))
|
|
17
|
+
|
|
18
|
+
## [44.0.0]
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- **BREAKING**: The `TokenBalancesController` state is now across all chains and accounts under the field `tokenBalances`, as a mapping from account address -> chain id -> token address -> balance. ([#4782](https://github.com/MetaMask/core/pull/4782))
|
|
23
|
+
|
|
24
|
+
- **BREAKING**: The `TokenBalancesController` now extends `StaticIntervalPollingController`, and the new polling API `startPolling` must be used to initiate polling (`startPolling`, `stopPollingByPollingToken`). ([#4782](https://github.com/MetaMask/core/pull/4782))
|
|
25
|
+
|
|
26
|
+
- **BREAKING**: `TokenBalancesController` now requires subscriptions to the `PreferencesController:stateChange` and `NetworkController:stateChange` events. And access to the `NetworkController:getNetworkClientById`, `NetworkController:getState`, `TokensController:getState`, and `PreferencesController:getState` actions. ([#4782](https://github.com/MetaMask/core/pull/4782))
|
|
27
|
+
|
|
28
|
+
- **BREAKING**: `TokensController` requires a subscription to the `NetworkController:stateChange` event. It now now removes state for chain IDs when their network is removed. ([#4782](https://github.com/MetaMask/core/pull/4782))
|
|
29
|
+
|
|
30
|
+
- `TokenRatesController` now removes state for chain IDs when their network is removed. ([#4782](https://github.com/MetaMask/core/pull/4782))
|
|
31
|
+
|
|
10
32
|
## [43.1.1]
|
|
11
33
|
|
|
12
34
|
### Changed
|
|
@@ -1216,7 +1238,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
1216
1238
|
|
|
1217
1239
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
|
1218
1240
|
|
|
1219
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@
|
|
1241
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@44.0.1...HEAD
|
|
1242
|
+
[44.0.1]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@44.0.0...@metamask/assets-controllers@44.0.1
|
|
1243
|
+
[44.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.1.1...@metamask/assets-controllers@44.0.0
|
|
1220
1244
|
[43.1.1]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.1.0...@metamask/assets-controllers@43.1.1
|
|
1221
1245
|
[43.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.0.0...@metamask/assets-controllers@43.1.0
|
|
1222
1246
|
[43.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@42.0.0...@metamask/assets-controllers@43.0.0
|
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
2
7
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
8
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
9
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
11
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
12
|
};
|
|
8
|
-
var
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
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");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var _TokenBalancesController_handle, _TokenBalancesController_interval, _TokenBalancesController_tokens, _TokenBalancesController_disabled;
|
|
13
|
+
var _TokenBalancesController_instances, _TokenBalancesController_queryMultipleAccounts, _TokenBalancesController_allTokens, _TokenBalancesController_allDetectedTokens, _TokenBalancesController_calculateQueryMultipleAccounts, _TokenBalancesController_onPreferencesStateChange, _TokenBalancesController_onTokensStateChange, _TokenBalancesController_onNetworkStateChange, _TokenBalancesController_getChainIds, _TokenBalancesController_getNetworkClient;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.TokenBalancesController = exports.getDefaultTokenBalancesState = void 0;
|
|
16
|
-
const
|
|
16
|
+
const contracts_1 = require("@ethersproject/contracts");
|
|
17
|
+
const providers_1 = require("@ethersproject/providers");
|
|
17
18
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
19
|
+
const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
|
|
20
|
+
const polling_controller_1 = require("@metamask/polling-controller");
|
|
21
|
+
const lodash_1 = require("lodash");
|
|
22
|
+
const multicall_1 = require("./multicall.cjs");
|
|
18
23
|
const DEFAULT_INTERVAL = 180000;
|
|
19
24
|
const controllerName = 'TokenBalancesController';
|
|
20
25
|
const metadata = {
|
|
21
|
-
|
|
26
|
+
tokenBalances: { persist: true, anonymous: false },
|
|
22
27
|
};
|
|
23
28
|
/**
|
|
24
29
|
* Get the default TokenBalancesController state.
|
|
@@ -27,7 +32,7 @@ const metadata = {
|
|
|
27
32
|
*/
|
|
28
33
|
function getDefaultTokenBalancesState() {
|
|
29
34
|
return {
|
|
30
|
-
|
|
35
|
+
tokenBalances: {},
|
|
31
36
|
};
|
|
32
37
|
}
|
|
33
38
|
exports.getDefaultTokenBalancesState = getDefaultTokenBalancesState;
|
|
@@ -35,18 +40,17 @@ exports.getDefaultTokenBalancesState = getDefaultTokenBalancesState;
|
|
|
35
40
|
* Controller that passively polls on a set interval token balances
|
|
36
41
|
* for tokens stored in the TokensController
|
|
37
42
|
*/
|
|
38
|
-
class TokenBalancesController extends
|
|
43
|
+
class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPollingController)() {
|
|
39
44
|
/**
|
|
40
45
|
* Construct a Token Balances Controller.
|
|
41
46
|
*
|
|
42
47
|
* @param options - The controller options.
|
|
43
48
|
* @param options.interval - Polling interval used to fetch new token balances.
|
|
44
|
-
* @param options.tokens - List of tokens to track balances for.
|
|
45
|
-
* @param options.disabled - If set to true, all tracked tokens contract balances updates are blocked.
|
|
46
49
|
* @param options.state - Initial state to set on this controller.
|
|
47
50
|
* @param options.messenger - The controller restricted messenger.
|
|
48
51
|
*/
|
|
49
|
-
constructor({ interval = DEFAULT_INTERVAL,
|
|
52
|
+
constructor({ interval = DEFAULT_INTERVAL, messenger, state = {}, }) {
|
|
53
|
+
var _a, _b;
|
|
50
54
|
super({
|
|
51
55
|
name: controllerName,
|
|
52
56
|
metadata,
|
|
@@ -56,77 +60,137 @@ class TokenBalancesController extends base_controller_1.BaseController {
|
|
|
56
60
|
...state,
|
|
57
61
|
},
|
|
58
62
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
_TokenBalancesController_instances.add(this);
|
|
64
|
+
_TokenBalancesController_queryMultipleAccounts.set(this, void 0);
|
|
65
|
+
_TokenBalancesController_allTokens.set(this, void 0);
|
|
66
|
+
_TokenBalancesController_allDetectedTokens.set(this, void 0);
|
|
67
|
+
/**
|
|
68
|
+
* Determines whether to query all accounts, or just the selected account.
|
|
69
|
+
* @param preferences - The preferences state.
|
|
70
|
+
* @param preferences.isMultiAccountBalancesEnabled - whether to query all accounts (mobile).
|
|
71
|
+
* @param preferences.useMultiAccountBalanceChecker - whether to query all accounts (extension).
|
|
72
|
+
* @returns true if all accounts should be queried.
|
|
73
|
+
*/
|
|
74
|
+
_TokenBalancesController_calculateQueryMultipleAccounts.set(this, ({ isMultiAccountBalancesEnabled, useMultiAccountBalanceChecker, }) => {
|
|
75
|
+
return Boolean(
|
|
76
|
+
// Note: These settings have different names on extension vs mobile
|
|
77
|
+
isMultiAccountBalancesEnabled || useMultiAccountBalanceChecker);
|
|
71
78
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Handles the event for preferences state changes.
|
|
81
|
+
* @param preferences - The preferences state.
|
|
82
|
+
*/
|
|
83
|
+
_TokenBalancesController_onPreferencesStateChange.set(this, (preferences) => {
|
|
84
|
+
// Update the user preference for whether to query multiple accounts.
|
|
85
|
+
const queryMultipleAccounts = __classPrivateFieldGet(this, _TokenBalancesController_calculateQueryMultipleAccounts, "f").call(this, preferences);
|
|
86
|
+
// Refresh when flipped off -> on
|
|
87
|
+
const refresh = queryMultipleAccounts && !__classPrivateFieldGet(this, _TokenBalancesController_queryMultipleAccounts, "f");
|
|
88
|
+
__classPrivateFieldSet(this, _TokenBalancesController_queryMultipleAccounts, queryMultipleAccounts, "f");
|
|
89
|
+
if (refresh) {
|
|
90
|
+
this.updateBalances().catch(console.error);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
/**
|
|
94
|
+
* Handles the event for tokens state changes.
|
|
95
|
+
* @param state - The token state.
|
|
96
|
+
* @param state.allTokens - The state for imported tokens across all chains.
|
|
97
|
+
* @param state.allDetectedTokens - The state for detected tokens across all chains.
|
|
98
|
+
*/
|
|
99
|
+
_TokenBalancesController_onTokensStateChange.set(this, ({ allTokens, allDetectedTokens, }) => {
|
|
100
|
+
// Refresh token balances on chains whose tokens have changed.
|
|
101
|
+
const chainIds = __classPrivateFieldGet(this, _TokenBalancesController_getChainIds, "f").call(this, allTokens, allDetectedTokens);
|
|
102
|
+
const chainIdsToUpdate = chainIds.filter((chainId) => !(0, lodash_1.isEqual)(__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[chainId], allTokens[chainId]) ||
|
|
103
|
+
!(0, lodash_1.isEqual)(__classPrivateFieldGet(this, _TokenBalancesController_allDetectedTokens, "f")[chainId], allDetectedTokens[chainId]));
|
|
104
|
+
__classPrivateFieldSet(this, _TokenBalancesController_allTokens, allTokens, "f");
|
|
105
|
+
__classPrivateFieldSet(this, _TokenBalancesController_allDetectedTokens, allDetectedTokens, "f");
|
|
106
|
+
this.updateBalances({ chainIds: chainIdsToUpdate }).catch(console.error);
|
|
107
|
+
});
|
|
108
|
+
/**
|
|
109
|
+
* Returns an array of chain ids that have tokens.
|
|
110
|
+
* @param allTokens - The state for imported tokens across all chains.
|
|
111
|
+
* @param allDetectedTokens - The state for detected tokens across all chains.
|
|
112
|
+
* @returns An array of chain ids that have tokens.
|
|
113
|
+
*/
|
|
114
|
+
_TokenBalancesController_getChainIds.set(this, (allTokens, allDetectedTokens) => [
|
|
115
|
+
...new Set([
|
|
116
|
+
...Object.keys(allTokens),
|
|
117
|
+
...Object.keys(allDetectedTokens),
|
|
118
|
+
]),
|
|
119
|
+
]);
|
|
120
|
+
this.setIntervalLength(interval);
|
|
121
|
+
// Set initial preference for querying multiple accounts, and subscribe to changes
|
|
122
|
+
__classPrivateFieldSet(this, _TokenBalancesController_queryMultipleAccounts, __classPrivateFieldGet(this, _TokenBalancesController_calculateQueryMultipleAccounts, "f").call(this, this.messagingSystem.call('PreferencesController:getState')), "f");
|
|
123
|
+
this.messagingSystem.subscribe('PreferencesController:stateChange', __classPrivateFieldGet(this, _TokenBalancesController_onPreferencesStateChange, "f").bind(this));
|
|
124
|
+
// Set initial tokens, and subscribe to changes
|
|
125
|
+
(_a = this, _b = this, {
|
|
126
|
+
allTokens: ({ set value(_c) { __classPrivateFieldSet(_a, _TokenBalancesController_allTokens, _c, "f"); } }).value,
|
|
127
|
+
allDetectedTokens: ({ set value(_c) { __classPrivateFieldSet(_b, _TokenBalancesController_allDetectedTokens, _c, "f"); } }).value,
|
|
128
|
+
} = this.messagingSystem.call('TokensController:getState'));
|
|
129
|
+
this.messagingSystem.subscribe('TokensController:stateChange', __classPrivateFieldGet(this, _TokenBalancesController_onTokensStateChange, "f").bind(this));
|
|
130
|
+
// Subscribe to network state changes
|
|
131
|
+
this.messagingSystem.subscribe('NetworkController:stateChange', __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_onNetworkStateChange).bind(this));
|
|
81
132
|
}
|
|
82
133
|
/**
|
|
83
|
-
*
|
|
134
|
+
* Polls for erc20 token balances.
|
|
135
|
+
* @param input - The input for the poll.
|
|
136
|
+
* @param input.chainId - The chain id to poll token balances on.
|
|
84
137
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
138
|
+
async _executePoll({ chainId }) {
|
|
139
|
+
await this.updateBalancesByChainId({ chainId });
|
|
87
140
|
}
|
|
88
141
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* @param
|
|
142
|
+
* Updates the token balances for the given chain ids.
|
|
143
|
+
* @param input - The input for the update.
|
|
144
|
+
* @param input.chainIds - The chain ids to update token balances for.
|
|
145
|
+
* Or omitted to update all chains that contain tokens.
|
|
92
146
|
*/
|
|
93
|
-
async
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
if (__classPrivateFieldGet(this, _TokenBalancesController_handle, "f")) {
|
|
98
|
-
clearTimeout(__classPrivateFieldGet(this, _TokenBalancesController_handle, "f"));
|
|
99
|
-
}
|
|
100
|
-
await (0, controller_utils_1.safelyExecute)(() => this.updateBalances());
|
|
101
|
-
__classPrivateFieldSet(this, _TokenBalancesController_handle, setTimeout(() => {
|
|
102
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
103
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
104
|
-
this.poll(__classPrivateFieldGet(this, _TokenBalancesController_interval, "f"));
|
|
105
|
-
}, __classPrivateFieldGet(this, _TokenBalancesController_interval, "f")), "f");
|
|
147
|
+
async updateBalances({ chainIds } = {}) {
|
|
148
|
+
chainIds ?? (chainIds = __classPrivateFieldGet(this, _TokenBalancesController_getChainIds, "f").call(this, __classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f"), __classPrivateFieldGet(this, _TokenBalancesController_allDetectedTokens, "f")));
|
|
149
|
+
await Promise.allSettled(chainIds.map((chainId) => this.updateBalancesByChainId({ chainId })));
|
|
106
150
|
}
|
|
107
151
|
/**
|
|
108
|
-
* Updates balances for
|
|
152
|
+
* Updates token balances for the given chain id.
|
|
153
|
+
* @param input - The input for the update.
|
|
154
|
+
* @param input.chainId - The chain id to update token balances on.
|
|
109
155
|
*/
|
|
110
|
-
async
|
|
111
|
-
|
|
112
|
-
|
|
156
|
+
async updateBalancesByChainId({ chainId }) {
|
|
157
|
+
const { address: selectedAccountAddress } = this.messagingSystem.call('AccountsController:getSelectedAccount');
|
|
158
|
+
const isSelectedAccount = (accountAddress) => (0, controller_utils_1.toChecksumHexAddress)(accountAddress) ===
|
|
159
|
+
(0, controller_utils_1.toChecksumHexAddress)(selectedAccountAddress);
|
|
160
|
+
const accountTokenPairs = [];
|
|
161
|
+
const addTokens = ([accountAddress, tokens]) => __classPrivateFieldGet(this, _TokenBalancesController_queryMultipleAccounts, "f") || isSelectedAccount(accountAddress)
|
|
162
|
+
? tokens.forEach((t) => accountTokenPairs.push({
|
|
163
|
+
accountAddress: accountAddress,
|
|
164
|
+
tokenAddress: t.address,
|
|
165
|
+
}))
|
|
166
|
+
: undefined;
|
|
167
|
+
// Balances will be updated for both imported and detected tokens
|
|
168
|
+
Object.entries(__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[chainId] ?? {}).forEach(addTokens);
|
|
169
|
+
Object.entries(__classPrivateFieldGet(this, _TokenBalancesController_allDetectedTokens, "f")[chainId] ?? {}).forEach(addTokens);
|
|
170
|
+
let results = [];
|
|
171
|
+
if (accountTokenPairs.length > 0) {
|
|
172
|
+
const provider = new providers_1.Web3Provider(__classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_getNetworkClient).call(this, chainId).provider);
|
|
173
|
+
const calls = accountTokenPairs.map(({ accountAddress, tokenAddress }) => ({
|
|
174
|
+
contract: new contracts_1.Contract(tokenAddress, metamask_eth_abis_1.abiERC20, provider),
|
|
175
|
+
functionSignature: 'balanceOf(address)',
|
|
176
|
+
arguments: [accountAddress],
|
|
177
|
+
}));
|
|
178
|
+
results = await (0, multicall_1.multicallOrFallback)(calls, chainId, provider);
|
|
113
179
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
newContractBalances[address] = (0, controller_utils_1.toHex)(balance);
|
|
121
|
-
token.hasBalanceError = false;
|
|
180
|
+
this.update((state) => {
|
|
181
|
+
var _a, _b;
|
|
182
|
+
// Reset so that when accounts or tokens are removed,
|
|
183
|
+
// their balances are removed rather than left stale.
|
|
184
|
+
for (const accountAddress of Object.keys(state.tokenBalances)) {
|
|
185
|
+
state.tokenBalances[accountAddress][chainId] = {};
|
|
122
186
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
187
|
+
for (let i = 0; i < results.length; i++) {
|
|
188
|
+
const { success, value } = results[i];
|
|
189
|
+
const { accountAddress, tokenAddress } = accountTokenPairs[i];
|
|
190
|
+
if (success) {
|
|
191
|
+
((_b = ((_a = state.tokenBalances)[accountAddress] ?? (_a[accountAddress] = {})))[chainId] ?? (_b[chainId] = {}))[tokenAddress] = (0, controller_utils_1.toHex)(value);
|
|
192
|
+
}
|
|
126
193
|
}
|
|
127
|
-
}
|
|
128
|
-
this.update((state) => {
|
|
129
|
-
state.contractBalances = newContractBalances;
|
|
130
194
|
});
|
|
131
195
|
}
|
|
132
196
|
/**
|
|
@@ -139,6 +203,27 @@ class TokenBalancesController extends base_controller_1.BaseController {
|
|
|
139
203
|
}
|
|
140
204
|
}
|
|
141
205
|
exports.TokenBalancesController = TokenBalancesController;
|
|
142
|
-
|
|
206
|
+
_TokenBalancesController_queryMultipleAccounts = new WeakMap(), _TokenBalancesController_allTokens = new WeakMap(), _TokenBalancesController_allDetectedTokens = new WeakMap(), _TokenBalancesController_calculateQueryMultipleAccounts = new WeakMap(), _TokenBalancesController_onPreferencesStateChange = new WeakMap(), _TokenBalancesController_onTokensStateChange = new WeakMap(), _TokenBalancesController_getChainIds = new WeakMap(), _TokenBalancesController_instances = new WeakSet(), _TokenBalancesController_onNetworkStateChange = function _TokenBalancesController_onNetworkStateChange(_, patches) {
|
|
207
|
+
// Remove state for deleted networks
|
|
208
|
+
for (const patch of patches) {
|
|
209
|
+
if (patch.op === 'remove' &&
|
|
210
|
+
patch.path[0] === 'networkConfigurationsByChainId') {
|
|
211
|
+
const removedChainId = patch.path[1];
|
|
212
|
+
this.update((state) => {
|
|
213
|
+
for (const accountAddress of Object.keys(state.tokenBalances)) {
|
|
214
|
+
delete state.tokenBalances[accountAddress][removedChainId];
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}, _TokenBalancesController_getNetworkClient = function _TokenBalancesController_getNetworkClient(chainId) {
|
|
220
|
+
const { networkConfigurationsByChainId } = this.messagingSystem.call('NetworkController:getState');
|
|
221
|
+
const networkConfiguration = networkConfigurationsByChainId[chainId];
|
|
222
|
+
if (!networkConfiguration) {
|
|
223
|
+
throw new Error(`TokenBalancesController: No network configuration found for chainId ${chainId}`);
|
|
224
|
+
}
|
|
225
|
+
const { networkClientId } = networkConfiguration.rpcEndpoints[networkConfiguration.defaultRpcEndpointIndex];
|
|
226
|
+
return this.messagingSystem.call(`NetworkController:getNetworkClientById`, networkClientId);
|
|
227
|
+
};
|
|
143
228
|
exports.default = TokenBalancesController;
|
|
144
229
|
//# sourceMappingURL=TokenBalancesController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenBalancesController.cjs","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,+DAA2D;AAC3D,iEAAkE;AAMlE,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,SAAgB,4BAA4B;IAC1C,OAAO;QACL,gBAAgB,EAAE,EAAE;KACrB,CAAC;AACJ,CAAC;AAJD,oEAIC;AAED;;;GAGG;AACH,MAAa,uBAAwB,SAAQ,gCAI5C;IASC;;;;;;;;;OASG;IACH,YAAY,EACV,QAAQ,GAAG,gBAAgB,EAC3B,MAAM,GAAG,EAAE,EACX,QAAQ,GAAG,KAAK,EAChB,SAAS,EACT,KAAK,GAAG,EAAE,GACqB;QAC/B,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ;YACR,SAAS;YACT,KAAK,EAAE;gBACL,GAAG,4B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QAC5B,CAAC,EAAE,uBAAA,IAAI,yCAAU,CAAC,MAAA,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,cAAc;QAClB,IAAI,uBAAA,IAAI,yCAAU,EAAE;YAClB,OAAO;SACR;QACD,MAAM,uBAAuB,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CACvD,uCAAuC,CACxC,CAAC;QAEF,MAAM,mBAAmB,GAAqB,EAAE,CAAC;QACjD,KAAK,MAAM,KAAK,IAAI,uBAAA,IAAI,uCAAQ,EAAE;YAChC,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC;YAC1B,IAAI;gBACF,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,4CAA4C,EAC5C,OAAO,EACP,uBAAuB,CAAC,OAAO,CAChC,CAAC;gBACF,mBAAmB,CAAC,OAAO,CAAC,GAAG,IAAA,wBAAK,EAAC,OAAO,CAAC,CAAC;gBAC9C,KAAK,CAAC,eAAe,GAAG,KAAK,CAAC;aAC/B;YAAC,OAAO,KAAK,EAAE;gBACd,mBAAmB,CAAC,OAAO,CAAC,GAAG,IAAA,wBAAK,EAAC,CAAC,CAAC,CAAC;gBACxC,KAAK,CAAC,eAAe,GAAG,IAAI,CAAC;aAC9B;SACF;QAED,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,gBAAgB,GAAG,mBAAmB,CAAC;QAC/C,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,UAAU;QACR,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,OAAO,4BAA4B,EAAE,CAAC;QACxC,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAzID,0DAyIC;;AAED,kBAAe,uBAAuB,CAAC","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { safelyExecute, toHex } from '@metamask/controller-utils';\n\nimport type { AssetsContractControllerGetERC20BalanceOfAction } 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 */\ntype TokenBalancesControllerOptions = {\n interval?: number;\n tokens?: Token[];\n disabled?: boolean;\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 =\n | AccountsControllerGetSelectedAccountAction\n | AssetsContractControllerGetERC20BalanceOfAction;\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 #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.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 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 // 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.messagingSystem.call(\n 'AssetsContractController: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 /**\n * Reset the controller state to the default state.\n */\n resetState() {\n this.update(() => {\n return getDefaultTokenBalancesState();\n });\n }\n}\n\nexport default TokenBalancesController;\n"]}
|
|
1
|
+
{"version":3,"file":"TokenBalancesController.cjs","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,wDAAoD;AACpD,wDAAwD;AAOxD,iEAAyE;AACzE,mEAAuD;AAOvD,qEAA+E;AAS/E,mCAAiC;AAGjC,+CAAkD;AAQlD,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAEjD,MAAM,QAAQ,GAAG;IACf,aAAa,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE;CACnD,CAAC;AAgEF;;;;GAIG;AACH,SAAgB,4BAA4B;IAC1C,OAAO;QACL,aAAa,EAAE,EAAE;KAClB,CAAC;AACJ,CAAC;AAJD,oEAIC;AAOD;;;GAGG;AACH,MAAa,uBAAwB,SAAQ,IAAA,oDAA+B,GAI3E;IAOC;;;;;;;OAOG;IACH,YAAY,EACV,QAA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IAAI,CAAC,eAAe,CAAC,IAAI,CAClE,4BAA4B,CAC7B,CAAC;IAEF,MAAM,oBAAoB,GAAG,8BAA8B,CAAC,OAAO,CAAC,CAAC;IACrE,IAAI,CAAC,oBAAoB,EAAE;QACzB,MAAM,IAAI,KAAK,CACb,uEAAuE,OAAO,EAAE,CACjF,CAAC;KACH;IAED,MAAM,EAAE,eAAe,EAAE,GACvB,oBAAoB,CAAC,YAAY,CAC/B,oBAAoB,CAAC,uBAAuB,CAC7C,CAAC;IAEJ,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAC9B,wCAAwC,EACxC,eAAe,CAChB,CAAC;AACJ,CAAC;AAGH,kBAAe,uBAAuB,CAAC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { toChecksumHexAddress, toHex } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkControllerStateChangeEvent,\n NetworkState,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type {\n PreferencesControllerGetStateAction,\n PreferencesControllerStateChangeEvent,\n PreferencesState,\n} from '@metamask/preferences-controller';\nimport type { Hex } from '@metamask/utils';\nimport type BN from 'bn.js';\nimport type { Patch } from 'immer';\nimport { isEqual } from 'lodash';\n\nimport type { MulticallResult } from './multicall';\nimport { multicallOrFallback } from './multicall';\nimport type { Token } from './TokenRatesController';\nimport type {\n TokensControllerGetStateAction,\n TokensControllerState,\n TokensControllerStateChangeEvent,\n} from './TokensController';\n\nconst DEFAULT_INTERVAL = 180000;\n\nconst controllerName = 'TokenBalancesController';\n\nconst metadata = {\n tokenBalances: { 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 messenger - A controller messenger.\n * @property state - Initial state for the controller.\n */\ntype TokenBalancesControllerOptions = {\n interval?: number;\n messenger: TokenBalancesControllerMessenger;\n state?: Partial<TokenBalancesControllerState>;\n};\n\n/**\n * A mapping from account address to chain id to token address to balance.\n */\ntype TokenBalances = Record<Hex, Record<Hex, Record<Hex, Hex>>>;\n\n/**\n * Token balances controller state\n * @property tokenBalances - A mapping from account address to chain id to token address to balance.\n */\nexport type TokenBalancesControllerState = {\n tokenBalances: TokenBalances;\n};\n\nexport type TokenBalancesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n TokenBalancesControllerState\n>;\n\nexport type TokenBalancesControllerActions =\n TokenBalancesControllerGetStateAction;\n\nexport type AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction\n | TokensControllerGetStateAction\n | PreferencesControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction;\n\nexport type TokenBalancesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n TokenBalancesControllerState\n >;\n\nexport type TokenBalancesControllerEvents =\n TokenBalancesControllerStateChangeEvent;\n\nexport type AllowedEvents =\n | TokensControllerStateChangeEvent\n | PreferencesControllerStateChangeEvent\n | NetworkControllerStateChangeEvent;\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 tokenBalances: {},\n };\n}\n\n/** The input to start polling for the {@link TokenBalancesController} */\nexport type TokenBalancesPollingInput = {\n chainId: Hex;\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 StaticIntervalPollingController<TokenBalancesPollingInput>()<\n typeof controllerName,\n TokenBalancesControllerState,\n TokenBalancesControllerMessenger\n> {\n #queryMultipleAccounts: boolean;\n\n #allTokens: TokensControllerState['allTokens'];\n\n #allDetectedTokens: TokensControllerState['allDetectedTokens'];\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.state - Initial state to set on this controller.\n * @param options.messenger - The controller restricted messenger.\n */\n constructor({\n interval = DEFAULT_INTERVAL,\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.setIntervalLength(interval);\n\n // Set initial preference for querying multiple accounts, and subscribe to changes\n this.#queryMultipleAccounts = this.#calculateQueryMultipleAccounts(\n this.messagingSystem.call('PreferencesController:getState'),\n );\n this.messagingSystem.subscribe(\n 'PreferencesController:stateChange',\n this.#onPreferencesStateChange.bind(this),\n );\n\n // Set initial tokens, and subscribe to changes\n ({\n allTokens: this.#allTokens,\n allDetectedTokens: this.#allDetectedTokens,\n } = this.messagingSystem.call('TokensController:getState'));\n\n this.messagingSystem.subscribe(\n 'TokensController:stateChange',\n this.#onTokensStateChange.bind(this),\n );\n\n // Subscribe to network state changes\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n this.#onNetworkStateChange.bind(this),\n );\n }\n\n /**\n * Determines whether to query all accounts, or just the selected account.\n * @param preferences - The preferences state.\n * @param preferences.isMultiAccountBalancesEnabled - whether to query all accounts (mobile).\n * @param preferences.useMultiAccountBalanceChecker - whether to query all accounts (extension).\n * @returns true if all accounts should be queried.\n */\n #calculateQueryMultipleAccounts = ({\n isMultiAccountBalancesEnabled,\n useMultiAccountBalanceChecker,\n }: PreferencesState & { useMultiAccountBalanceChecker?: boolean }) => {\n return Boolean(\n // Note: These settings have different names on extension vs mobile\n isMultiAccountBalancesEnabled || useMultiAccountBalanceChecker,\n );\n };\n\n /**\n * Handles the event for preferences state changes.\n * @param preferences - The preferences state.\n */\n #onPreferencesStateChange = (preferences: PreferencesState) => {\n // Update the user preference for whether to query multiple accounts.\n const queryMultipleAccounts =\n this.#calculateQueryMultipleAccounts(preferences);\n\n // Refresh when flipped off -> on\n const refresh = queryMultipleAccounts && !this.#queryMultipleAccounts;\n this.#queryMultipleAccounts = queryMultipleAccounts;\n\n if (refresh) {\n this.updateBalances().catch(console.error);\n }\n };\n\n /**\n * Handles the event for tokens state changes.\n * @param state - The token state.\n * @param state.allTokens - The state for imported tokens across all chains.\n * @param state.allDetectedTokens - The state for detected tokens across all chains.\n */\n #onTokensStateChange = ({\n allTokens,\n allDetectedTokens,\n }: TokensControllerState) => {\n // Refresh token balances on chains whose tokens have changed.\n const chainIds = this.#getChainIds(allTokens, allDetectedTokens);\n const chainIdsToUpdate = chainIds.filter(\n (chainId) =>\n !isEqual(this.#allTokens[chainId], allTokens[chainId]) ||\n !isEqual(this.#allDetectedTokens[chainId], allDetectedTokens[chainId]),\n );\n\n this.#allTokens = allTokens;\n this.#allDetectedTokens = allDetectedTokens;\n\n this.updateBalances({ chainIds: chainIdsToUpdate }).catch(console.error);\n };\n\n /**\n * Handles the event for network state changes.\n * @param _ - The network state.\n * @param patches - An array of patch operations performed on the network state.\n */\n #onNetworkStateChange(_: NetworkState, patches: Patch[]) {\n // Remove state for deleted networks\n for (const patch of patches) {\n if (\n patch.op === 'remove' &&\n patch.path[0] === 'networkConfigurationsByChainId'\n ) {\n const removedChainId = patch.path[1] as Hex;\n\n this.update((state) => {\n for (const accountAddress of Object.keys(state.tokenBalances)) {\n delete state.tokenBalances[accountAddress as Hex][removedChainId];\n }\n });\n }\n }\n }\n\n /**\n * Returns an array of chain ids that have tokens.\n * @param allTokens - The state for imported tokens across all chains.\n * @param allDetectedTokens - The state for detected tokens across all chains.\n * @returns An array of chain ids that have tokens.\n */\n #getChainIds = (\n allTokens: TokensControllerState['allTokens'],\n allDetectedTokens: TokensControllerState['allDetectedTokens'],\n ) =>\n [\n ...new Set([\n ...Object.keys(allTokens),\n ...Object.keys(allDetectedTokens),\n ]),\n ] as Hex[];\n\n /**\n * Polls for erc20 token balances.\n * @param input - The input for the poll.\n * @param input.chainId - The chain id to poll token balances on.\n */\n async _executePoll({ chainId }: TokenBalancesPollingInput) {\n await this.updateBalancesByChainId({ chainId });\n }\n\n /**\n * Updates the token balances for the given chain ids.\n * @param input - The input for the update.\n * @param input.chainIds - The chain ids to update token balances for.\n * Or omitted to update all chains that contain tokens.\n */\n async updateBalances({ chainIds }: { chainIds?: Hex[] } = {}) {\n chainIds ??= this.#getChainIds(this.#allTokens, this.#allDetectedTokens);\n\n await Promise.allSettled(\n chainIds.map((chainId) => this.updateBalancesByChainId({ chainId })),\n );\n }\n\n /**\n * Updates token balances for the given chain id.\n * @param input - The input for the update.\n * @param input.chainId - The chain id to update token balances on.\n */\n async updateBalancesByChainId({ chainId }: { chainId: Hex }) {\n const { address: selectedAccountAddress } = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n\n const isSelectedAccount = (accountAddress: string) =>\n toChecksumHexAddress(accountAddress) ===\n toChecksumHexAddress(selectedAccountAddress);\n\n const accountTokenPairs: { accountAddress: Hex; tokenAddress: Hex }[] = [];\n\n const addTokens = ([accountAddress, tokens]: [string, Token[]]) =>\n this.#queryMultipleAccounts || isSelectedAccount(accountAddress)\n ? tokens.forEach((t) =>\n accountTokenPairs.push({\n accountAddress: accountAddress as Hex,\n tokenAddress: t.address as Hex,\n }),\n )\n : undefined;\n\n // Balances will be updated for both imported and detected tokens\n Object.entries(this.#allTokens[chainId] ?? {}).forEach(addTokens);\n Object.entries(this.#allDetectedTokens[chainId] ?? {}).forEach(addTokens);\n\n let results: MulticallResult[] = [];\n\n if (accountTokenPairs.length > 0) {\n const provider = new Web3Provider(\n this.#getNetworkClient(chainId).provider,\n );\n\n const calls = accountTokenPairs.map(\n ({ accountAddress, tokenAddress }) => ({\n contract: new Contract(tokenAddress, abiERC20, provider),\n functionSignature: 'balanceOf(address)',\n arguments: [accountAddress],\n }),\n );\n\n results = await multicallOrFallback(calls, chainId, provider);\n }\n\n this.update((state) => {\n // Reset so that when accounts or tokens are removed,\n // their balances are removed rather than left stale.\n for (const accountAddress of Object.keys(state.tokenBalances)) {\n state.tokenBalances[accountAddress as Hex][chainId] = {};\n }\n\n for (let i = 0; i < results.length; i++) {\n const { success, value } = results[i];\n const { accountAddress, tokenAddress } = accountTokenPairs[i];\n\n if (success) {\n ((state.tokenBalances[accountAddress] ??= {})[chainId] ??= {})[\n tokenAddress\n ] = toHex(value as BN);\n }\n }\n });\n }\n\n /**\n * Reset the controller state to the default state.\n */\n resetState() {\n this.update(() => {\n return getDefaultTokenBalancesState();\n });\n }\n\n /**\n * Returns the network client for a given chain id\n * @param chainId - The chain id to get the network client for.\n * @returns The network client for the given chain id.\n */\n #getNetworkClient(chainId: Hex) {\n const { networkConfigurationsByChainId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n\n const networkConfiguration = networkConfigurationsByChainId[chainId];\n if (!networkConfiguration) {\n throw new Error(\n `TokenBalancesController: No network configuration found for chainId ${chainId}`,\n );\n }\n\n const { networkClientId } =\n networkConfiguration.rpcEndpoints[\n networkConfiguration.defaultRpcEndpointIndex\n ];\n\n return this.messagingSystem.call(\n `NetworkController:getNetworkClientById`,\n networkClientId,\n );\n }\n}\n\nexport default TokenBalancesController;\n"]}
|
|
@@ -1,40 +1,38 @@
|
|
|
1
1
|
import type { AccountsControllerGetSelectedAccountAction } from "@metamask/accounts-controller";
|
|
2
2
|
import type { RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import type { TokensControllerStateChangeEvent } from "./TokensController.cjs";
|
|
3
|
+
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
|
|
4
|
+
import type { PreferencesControllerGetStateAction, PreferencesControllerStateChangeEvent } from "@metamask/preferences-controller";
|
|
5
|
+
import type { Hex } from "@metamask/utils";
|
|
6
|
+
import type { TokensControllerGetStateAction, TokensControllerStateChangeEvent } from "./TokensController.cjs";
|
|
7
7
|
declare const controllerName = "TokenBalancesController";
|
|
8
8
|
/**
|
|
9
9
|
* Token balances controller options
|
|
10
10
|
* @property interval - Polling interval used to fetch new token balances.
|
|
11
|
-
* @property
|
|
12
|
-
* @property
|
|
11
|
+
* @property messenger - A controller messenger.
|
|
12
|
+
* @property state - Initial state for the controller.
|
|
13
13
|
*/
|
|
14
14
|
type TokenBalancesControllerOptions = {
|
|
15
15
|
interval?: number;
|
|
16
|
-
tokens?: Token[];
|
|
17
|
-
disabled?: boolean;
|
|
18
16
|
messenger: TokenBalancesControllerMessenger;
|
|
19
17
|
state?: Partial<TokenBalancesControllerState>;
|
|
20
18
|
};
|
|
21
19
|
/**
|
|
22
|
-
*
|
|
20
|
+
* A mapping from account address to chain id to token address to balance.
|
|
23
21
|
*/
|
|
24
|
-
type
|
|
22
|
+
type TokenBalances = Record<Hex, Record<Hex, Record<Hex, Hex>>>;
|
|
25
23
|
/**
|
|
26
24
|
* Token balances controller state
|
|
27
|
-
* @property
|
|
25
|
+
* @property tokenBalances - A mapping from account address to chain id to token address to balance.
|
|
28
26
|
*/
|
|
29
27
|
export type TokenBalancesControllerState = {
|
|
30
|
-
|
|
28
|
+
tokenBalances: TokenBalances;
|
|
31
29
|
};
|
|
32
30
|
export type TokenBalancesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, TokenBalancesControllerState>;
|
|
33
31
|
export type TokenBalancesControllerActions = TokenBalancesControllerGetStateAction;
|
|
34
|
-
export type AllowedActions =
|
|
32
|
+
export type AllowedActions = NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetStateAction | TokensControllerGetStateAction | PreferencesControllerGetStateAction | AccountsControllerGetSelectedAccountAction;
|
|
35
33
|
export type TokenBalancesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, TokenBalancesControllerState>;
|
|
36
34
|
export type TokenBalancesControllerEvents = TokenBalancesControllerStateChangeEvent;
|
|
37
|
-
export type AllowedEvents = TokensControllerStateChangeEvent;
|
|
35
|
+
export type AllowedEvents = TokensControllerStateChangeEvent | PreferencesControllerStateChangeEvent | NetworkControllerStateChangeEvent;
|
|
38
36
|
export type TokenBalancesControllerMessenger = RestrictedControllerMessenger<typeof controllerName, TokenBalancesControllerActions | AllowedActions, TokenBalancesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
39
37
|
/**
|
|
40
38
|
* Get the default TokenBalancesController state.
|
|
@@ -42,41 +40,63 @@ export type TokenBalancesControllerMessenger = RestrictedControllerMessenger<typ
|
|
|
42
40
|
* @returns The default TokenBalancesController state.
|
|
43
41
|
*/
|
|
44
42
|
export declare function getDefaultTokenBalancesState(): TokenBalancesControllerState;
|
|
43
|
+
/** The input to start polling for the {@link TokenBalancesController} */
|
|
44
|
+
export type TokenBalancesPollingInput = {
|
|
45
|
+
chainId: Hex;
|
|
46
|
+
};
|
|
47
|
+
declare const TokenBalancesController_base: (abstract new (...args: any[]) => {
|
|
48
|
+
readonly "__#787887@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
49
|
+
"__#787887@#intervalLength": number | undefined;
|
|
50
|
+
setIntervalLength(intervalLength: number): void;
|
|
51
|
+
getIntervalLength(): number | undefined;
|
|
52
|
+
_startPolling(input: TokenBalancesPollingInput): void;
|
|
53
|
+
_stopPollingByPollingTokenSetId(key: string): void;
|
|
54
|
+
readonly "__#787879@#pollingTokenSets": Map<string, Set<string>>;
|
|
55
|
+
"__#787879@#callbacks": Map<string, Set<(input: TokenBalancesPollingInput) => void>>;
|
|
56
|
+
_executePoll(input: TokenBalancesPollingInput): Promise<void>;
|
|
57
|
+
startPolling(input: TokenBalancesPollingInput): string;
|
|
58
|
+
stopAllPolling(): void;
|
|
59
|
+
stopPollingByPollingToken(pollingToken: string): void;
|
|
60
|
+
onPollingComplete(input: TokenBalancesPollingInput, callback: (input: TokenBalancesPollingInput) => void): void;
|
|
61
|
+
}) & typeof import("@metamask/base-controller").BaseController;
|
|
45
62
|
/**
|
|
46
63
|
* Controller that passively polls on a set interval token balances
|
|
47
64
|
* for tokens stored in the TokensController
|
|
48
65
|
*/
|
|
49
|
-
export declare class TokenBalancesController extends
|
|
66
|
+
export declare class TokenBalancesController extends TokenBalancesController_base<typeof controllerName, TokenBalancesControllerState, TokenBalancesControllerMessenger> {
|
|
50
67
|
#private;
|
|
51
68
|
/**
|
|
52
69
|
* Construct a Token Balances Controller.
|
|
53
70
|
*
|
|
54
71
|
* @param options - The controller options.
|
|
55
72
|
* @param options.interval - Polling interval used to fetch new token balances.
|
|
56
|
-
* @param options.tokens - List of tokens to track balances for.
|
|
57
|
-
* @param options.disabled - If set to true, all tracked tokens contract balances updates are blocked.
|
|
58
73
|
* @param options.state - Initial state to set on this controller.
|
|
59
74
|
* @param options.messenger - The controller restricted messenger.
|
|
60
75
|
*/
|
|
61
|
-
constructor({ interval,
|
|
62
|
-
/**
|
|
63
|
-
* Allows controller to update tracked tokens contract balances.
|
|
64
|
-
*/
|
|
65
|
-
enable(): void;
|
|
76
|
+
constructor({ interval, messenger, state, }: TokenBalancesControllerOptions);
|
|
66
77
|
/**
|
|
67
|
-
*
|
|
78
|
+
* Polls for erc20 token balances.
|
|
79
|
+
* @param input - The input for the poll.
|
|
80
|
+
* @param input.chainId - The chain id to poll token balances on.
|
|
68
81
|
*/
|
|
69
|
-
|
|
82
|
+
_executePoll({ chainId }: TokenBalancesPollingInput): Promise<void>;
|
|
70
83
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* @param
|
|
84
|
+
* Updates the token balances for the given chain ids.
|
|
85
|
+
* @param input - The input for the update.
|
|
86
|
+
* @param input.chainIds - The chain ids to update token balances for.
|
|
87
|
+
* Or omitted to update all chains that contain tokens.
|
|
74
88
|
*/
|
|
75
|
-
|
|
89
|
+
updateBalances({ chainIds }?: {
|
|
90
|
+
chainIds?: Hex[];
|
|
91
|
+
}): Promise<void>;
|
|
76
92
|
/**
|
|
77
|
-
* Updates balances for
|
|
93
|
+
* Updates token balances for the given chain id.
|
|
94
|
+
* @param input - The input for the update.
|
|
95
|
+
* @param input.chainId - The chain id to update token balances on.
|
|
78
96
|
*/
|
|
79
|
-
|
|
97
|
+
updateBalancesByChainId({ chainId }: {
|
|
98
|
+
chainId: Hex;
|
|
99
|
+
}): Promise<void>;
|
|
80
100
|
/**
|
|
81
101
|
* Reset the controller state to the default state.
|
|
82
102
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenBalancesController.d.cts","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"TokenBalancesController.d.cts","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,0CAA0C,EAAE,sCAAsC;AAChG,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAGnC,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAC/B,iCAAiC,EAElC,qCAAqC;AAEtC,OAAO,KAAK,EACV,mCAAmC,EACnC,qCAAqC,EAEtC,yCAAyC;AAC1C,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAQ3C,OAAO,KAAK,EACV,8BAA8B,EAE9B,gCAAgC,EACjC,+BAA2B;AAI5B,QAAA,MAAM,cAAc,4BAA4B,CAAC;AAMjD;;;;;GAKG;AACH,KAAK,8BAA8B,GAAG;IACpC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,gCAAgC,CAAC;IAC5C,KAAK,CAAC,EAAE,OAAO,CAAC,4BAA4B,CAAC,CAAC;CAC/C,CAAC;AAEF;;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