@metamask/assets-controllers 73.0.0 → 73.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 +15 -1
- package/dist/AccountTrackerController.cjs +50 -34
- package/dist/AccountTrackerController.cjs.map +1 -1
- package/dist/AccountTrackerController.d.cts +9 -6
- package/dist/AccountTrackerController.d.cts.map +1 -1
- package/dist/AccountTrackerController.d.mts +9 -6
- package/dist/AccountTrackerController.d.mts.map +1 -1
- package/dist/AccountTrackerController.mjs +52 -35
- package/dist/AccountTrackerController.mjs.map +1 -1
- package/dist/AssetsContractController.cjs +39 -16
- package/dist/AssetsContractController.cjs.map +1 -1
- package/dist/AssetsContractController.d.cts +7 -5
- package/dist/AssetsContractController.d.cts.map +1 -1
- package/dist/AssetsContractController.d.mts +7 -5
- package/dist/AssetsContractController.d.mts.map +1 -1
- package/dist/AssetsContractController.mjs +39 -16
- package/dist/AssetsContractController.mjs.map +1 -1
- package/dist/TokenBalancesController.cjs +15 -1
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +15 -1
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/multicall.cjs.map +1 -1
- package/dist/multicall.d.cts +1 -2
- package/dist/multicall.d.cts.map +1 -1
- package/dist/multicall.d.mts +1 -2
- package/dist/multicall.d.mts.map +1 -1
- package/dist/multicall.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [73.0.1]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Improved `AccountTrackerController` RPC performance by batching addresses using a multicall contract ([#6099](https://github.com/MetaMask/core/pull/6099))
|
|
15
|
+
- Fallbacks to single address RPC calls on chains that do not have a multicall contract.
|
|
16
|
+
- Improved `AssetsContractController` RPC performance by batching addresses using a multicall contract ([#6099](https://github.com/MetaMask/core/pull/6099))
|
|
17
|
+
- Fallbacks to single address RPC calls on chains that do not have a multicall contract.
|
|
18
|
+
|
|
19
|
+
### Fixed
|
|
20
|
+
|
|
21
|
+
- Fix `TokenBalancesController` to force block number update to avoid stale cached balances ([#6197](https://github.com/MetaMask/core/pull/6197))
|
|
22
|
+
|
|
10
23
|
## [73.0.0]
|
|
11
24
|
|
|
12
25
|
### Changed
|
|
@@ -1791,7 +1804,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
1791
1804
|
|
|
1792
1805
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
|
1793
1806
|
|
|
1794
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@73.0.
|
|
1807
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@73.0.1...HEAD
|
|
1808
|
+
[73.0.1]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@73.0.0...@metamask/assets-controllers@73.0.1
|
|
1795
1809
|
[73.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@72.0.0...@metamask/assets-controllers@73.0.0
|
|
1796
1810
|
[72.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@71.0.0...@metamask/assets-controllers@72.0.0
|
|
1797
1811
|
[71.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@70.0.1...@metamask/assets-controllers@71.0.0
|
|
@@ -16,12 +16,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
16
16
|
var _AccountTrackerController_instances, _AccountTrackerController_refreshMutex, _AccountTrackerController_includeStakedAssets, _AccountTrackerController_getStakedBalanceForChain, _AccountTrackerController_getCorrectNetworkClient, _AccountTrackerController_getNetworkClientIds, _AccountTrackerController_getBalanceFromChain;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.AccountTrackerController = void 0;
|
|
19
|
+
const contracts_1 = require("@ethersproject/contracts");
|
|
20
|
+
const providers_1 = require("@ethersproject/providers");
|
|
19
21
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
20
22
|
const eth_query_1 = __importDefault(require("@metamask/eth-query"));
|
|
21
23
|
const polling_controller_1 = require("@metamask/polling-controller");
|
|
22
24
|
const utils_1 = require("@metamask/utils");
|
|
23
25
|
const async_mutex_1 = require("async-mutex");
|
|
24
26
|
const lodash_1 = require("lodash");
|
|
27
|
+
const single_call_balance_checker_abi_1 = __importDefault(require("single-call-balance-checker-abi"));
|
|
28
|
+
const AssetsContractController_1 = require("./AssetsContractController.cjs");
|
|
25
29
|
const assetsUtil_1 = require("./assetsUtil.cjs");
|
|
26
30
|
/**
|
|
27
31
|
* The name of the {@link AccountTrackerController}.
|
|
@@ -144,45 +148,55 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
144
148
|
this.syncAccounts(chainIds);
|
|
145
149
|
// Create an array of promises for each networkClientId
|
|
146
150
|
const updatePromises = networkClientIds.map(async (networkClientId) => {
|
|
147
|
-
const { chainId, ethQuery } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
151
|
+
const { chainId, ethQuery, provider } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
148
152
|
const { accountsByChainId } = this.state;
|
|
149
153
|
const { isMultiAccountBalancesEnabled } = this.messagingSystem.call('PreferencesController:getState');
|
|
150
154
|
const accountsToUpdate = isMultiAccountBalancesEnabled
|
|
151
155
|
? Object.keys(accountsByChainId[chainId])
|
|
152
156
|
: [(0, controller_utils_1.toChecksumHexAddress)(selectedAccount.address)];
|
|
153
157
|
const accountsForChain = { ...accountsByChainId[chainId] };
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
158
|
+
const stakedBalancesPromise = __classPrivateFieldGet(this, _AccountTrackerController_includeStakedAssets, "f")
|
|
159
|
+
? __classPrivateFieldGet(this, _AccountTrackerController_getStakedBalanceForChain, "f").call(this, accountsToUpdate, networkClientId)
|
|
160
|
+
: Promise.resolve({});
|
|
161
|
+
if ((0, utils_1.hasProperty)(AssetsContractController_1.SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID, chainId)) {
|
|
162
|
+
const contractAddress = AssetsContractController_1.SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID[chainId];
|
|
163
|
+
const contract = new contracts_1.Contract(contractAddress, single_call_balance_checker_abi_1.default, new providers_1.Web3Provider(provider));
|
|
164
|
+
const nativeBalances = await contract.balances(accountsToUpdate, [
|
|
165
|
+
'0x0000000000000000000000000000000000000000',
|
|
166
|
+
]);
|
|
167
|
+
accountsToUpdate.forEach((address, index) => {
|
|
168
|
+
accountsForChain[address] = {
|
|
169
|
+
balance: nativeBalances[index].toHexString(),
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
// Process accounts in batches using reduceInBatchesSerially
|
|
175
|
+
await (0, assetsUtil_1.reduceInBatchesSerially)({
|
|
176
|
+
values: accountsToUpdate,
|
|
177
|
+
batchSize: assetsUtil_1.TOKEN_PRICES_BATCH_SIZE,
|
|
178
|
+
initialResult: undefined,
|
|
179
|
+
eachBatch: async (workingResult, batch) => {
|
|
180
|
+
const balancePromises = batch.map(async (address) => {
|
|
181
|
+
const balanceResult = await __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getBalanceFromChain).call(this, address, ethQuery).catch(() => null);
|
|
182
|
+
// Update account balances
|
|
183
|
+
if (balanceResult) {
|
|
184
|
+
accountsForChain[address] = {
|
|
185
|
+
balance: balanceResult,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
await Promise.allSettled(balancePromises);
|
|
190
|
+
return workingResult;
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
const stakedBalanceResult = (await stakedBalancesPromise);
|
|
195
|
+
Object.entries(stakedBalanceResult).forEach(([address, balance]) => {
|
|
196
|
+
accountsForChain[address] = {
|
|
197
|
+
...accountsForChain[address],
|
|
198
|
+
stakedBalance: balance,
|
|
199
|
+
};
|
|
186
200
|
});
|
|
187
201
|
// After all batches are processed, return the updated data
|
|
188
202
|
return { chainId, accountsForChain };
|
|
@@ -222,13 +236,14 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
222
236
|
*/
|
|
223
237
|
async syncBalanceWithAddresses(addresses, networkClientId) {
|
|
224
238
|
const { ethQuery } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
239
|
+
// TODO: This should use multicall when enabled by the user.
|
|
225
240
|
return await Promise.all(addresses.map((address) => {
|
|
226
241
|
return (0, controller_utils_1.safelyExecuteWithTimeout)(async () => {
|
|
227
242
|
(0, utils_1.assert)(ethQuery, 'Provider not set.');
|
|
228
243
|
const balance = await (0, controller_utils_1.query)(ethQuery, 'getBalance', [address]);
|
|
229
244
|
let stakedBalance;
|
|
230
245
|
if (__classPrivateFieldGet(this, _AccountTrackerController_includeStakedAssets, "f")) {
|
|
231
|
-
stakedBalance = await __classPrivateFieldGet(this, _AccountTrackerController_getStakedBalanceForChain, "f").call(this, address, networkClientId);
|
|
246
|
+
stakedBalance = (await __classPrivateFieldGet(this, _AccountTrackerController_getStakedBalanceForChain, "f").call(this, [address], networkClientId))[address];
|
|
232
247
|
}
|
|
233
248
|
return [address, balance, stakedBalance];
|
|
234
249
|
});
|
|
@@ -257,6 +272,7 @@ _AccountTrackerController_refreshMutex = new WeakMap(), _AccountTrackerControlle
|
|
|
257
272
|
const { configuration: { chainId }, provider, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
|
258
273
|
return {
|
|
259
274
|
chainId,
|
|
275
|
+
provider,
|
|
260
276
|
ethQuery: new eth_query_1.default(provider),
|
|
261
277
|
};
|
|
262
278
|
}, _AccountTrackerController_getNetworkClientIds = function _AccountTrackerController_getNetworkClientIds() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTrackerController.cjs","sourceRoot":"","sources":["../src/AccountTrackerController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAWA,iEAIoC;AACpC,oEAA2C;AAM3C,qEAA+E;AAE/E,2CAAyC;AACzC,6CAAoC;AACpC,mCAA4C;AAM5C,iDAAgF;AAEhF;;GAEG;AACH,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAwBlD,MAAM,sBAAsB,GAAG;IAC7B,iBAAiB,EAAE;QACjB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAgEF;;GAEG;AACH,MAAa,wBAAyB,SAAQ,IAAA,oDAA+B,GAI5E;IAOC;;;;;;;;;OASG;IACH,YAAY,EACV,QAAQ,GAAG,KAAK,EAChB,KAAK,EACL,SAAS,EACT,wBAAwB,EACxB,mBAAmB,GAAG,KAAK,GAO5B;QACC,MAAM,EAAE,uBAAuB,EAAE,GAAG,SAAS,CAAC,IAAI,CAChD,4BAA4B,CAC7B,CAAC;QACF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,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;AACL,CAAC;AAsDH,kBAAe,wBAAwB,CAAC","sourcesContent":["import type {\n AccountsControllerSelectedEvmAccountChangeEvent,\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerListAccountsAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerStateChangeEvent,\n ControllerGetStateAction,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport {\n query,\n safelyExecuteWithTimeout,\n toChecksumHexAddress,\n} from '@metamask/controller-utils';\nimport EthQuery from '@metamask/eth-query';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';\nimport { assert } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport type {\n AssetsContractController,\n StakedBalance,\n} from './AssetsContractController';\nimport { reduceInBatchesSerially, TOKEN_PRICES_BATCH_SIZE } from './assetsUtil';\n\n/**\n * The name of the {@link AccountTrackerController}.\n */\nconst controllerName = 'AccountTrackerController';\n\n/**\n * @type AccountInformation\n *\n * Account information object\n * @property balance - Hex string of an account balance in wei\n * @property stakedBalance - Hex string of an account staked balance in wei\n */\nexport type AccountInformation = {\n balance: string;\n stakedBalance?: string;\n};\n\n/**\n * @type AccountTrackerControllerState\n *\n * Account tracker controller state\n * @property accountsByChainId - Map of addresses to account information by chain\n */\nexport type AccountTrackerControllerState = {\n accountsByChainId: Record<string, { [address: string]: AccountInformation }>;\n};\n\nconst accountTrackerMetadata = {\n accountsByChainId: {\n persist: true,\n anonymous: false,\n },\n};\n\n/**\n * The action that can be performed to get the state of the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AccountTrackerControllerState\n>;\n\n/**\n * The actions that can be performed using the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerActions =\n AccountTrackerControllerGetStateAction;\n\n/**\n * The messenger of the {@link AccountTrackerController} for communication.\n */\nexport type AllowedActions =\n | AccountsControllerListAccountsAction\n | PreferencesControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction\n | NetworkControllerGetStateAction\n | NetworkControllerGetNetworkClientByIdAction;\n\n/**\n * The event that {@link AccountTrackerController} can emit.\n */\nexport type AccountTrackerControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n AccountTrackerControllerState\n >;\n\n/**\n * The events that {@link AccountTrackerController} can emit.\n */\nexport type AccountTrackerControllerEvents =\n AccountTrackerControllerStateChangeEvent;\n\n/**\n * The external events available to the {@link AccountTrackerController}.\n */\nexport type AllowedEvents =\n | AccountsControllerSelectedEvmAccountChangeEvent\n | AccountsControllerSelectedAccountChangeEvent;\n\n/**\n * The messenger of the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AccountTrackerControllerActions | AllowedActions,\n AccountTrackerControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/** The input to start polling for the {@link AccountTrackerController} */\ntype AccountTrackerPollingInput = {\n networkClientIds: NetworkClientId[];\n};\n\n/**\n * Controller that tracks the network balances for all user accounts.\n */\nexport class AccountTrackerController extends StaticIntervalPollingController<AccountTrackerPollingInput>()<\n typeof controllerName,\n AccountTrackerControllerState,\n AccountTrackerControllerMessenger\n> {\n readonly #refreshMutex = new Mutex();\n\n readonly #includeStakedAssets: boolean;\n\n readonly #getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n\n /**\n * Creates an AccountTracker instance.\n *\n * @param options - The controller options.\n * @param options.interval - Polling interval used to fetch new account balances.\n * @param options.state - Initial state to set on this controller.\n * @param options.messenger - The controller messaging system.\n * @param options.getStakedBalanceForChain - The function to get the staked native asset balance for a chain.\n * @param options.includeStakedAssets - Whether to include staked assets in the account balances.\n */\n constructor({\n interval = 10000,\n state,\n messenger,\n getStakedBalanceForChain,\n includeStakedAssets = false,\n }: {\n interval?: number;\n state?: Partial<AccountTrackerControllerState>;\n messenger: AccountTrackerControllerMessenger;\n getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n includeStakedAssets?: boolean;\n }) {\n const { selectedNetworkClientId } = messenger.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = messenger.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n super({\n name: controllerName,\n messenger,\n state: {\n accountsByChainId: {\n [chainId]: {},\n },\n ...state,\n },\n metadata: accountTrackerMetadata,\n });\n this.#getStakedBalanceForChain = getStakedBalanceForChain;\n\n this.#includeStakedAssets = includeStakedAssets;\n\n this.setIntervalLength(interval);\n\n this.messagingSystem.subscribe(\n 'AccountsController:selectedEvmAccountChange',\n (newAddress, prevAddress) => {\n if (newAddress !== prevAddress) {\n // Making an async call for this new event\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.refresh(this.#getNetworkClientIds());\n }\n },\n (event): string => event.address,\n );\n }\n\n private syncAccounts(newChainIds: string[]) {\n const accountsByChainId = cloneDeep(this.state.accountsByChainId);\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId: currentChainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n const existing = Object.keys(accountsByChainId?.[currentChainId] ?? {});\n\n // Initialize new chain IDs if they don't exist\n newChainIds.forEach((newChainId) => {\n if (!accountsByChainId[newChainId]) {\n accountsByChainId[newChainId] = {};\n existing.forEach((address) => {\n accountsByChainId[newChainId][address] = { balance: '0x0' };\n });\n }\n });\n\n // Note: The address from the preferences controller are checksummed\n // The addresses from the accounts controller are lowercased\n const addresses = Object.values(\n this.messagingSystem\n .call('AccountsController:listAccounts')\n .map((internalAccount) =>\n toChecksumHexAddress(internalAccount.address),\n ),\n );\n const newAddresses = addresses.filter(\n (address) => !existing.includes(address),\n );\n const oldAddresses = existing.filter(\n (address) => !addresses.includes(address),\n );\n Object.keys(accountsByChainId).forEach((chainId) => {\n newAddresses.forEach((address) => {\n accountsByChainId[chainId][address] = {\n balance: '0x0',\n };\n });\n });\n\n Object.keys(accountsByChainId).forEach((chainId) => {\n oldAddresses.forEach((address) => {\n delete accountsByChainId[chainId][address];\n });\n });\n\n if (!isEqual(this.state.accountsByChainId, accountsByChainId)) {\n this.update((state) => {\n state.accountsByChainId = accountsByChainId;\n });\n }\n }\n\n /**\n * Resolves a networkClientId to a network client config\n * or globally selected network config if not provided\n *\n * @param networkClientId - Optional networkClientId to fetch a network client with\n * @returns network client config\n */\n #getCorrectNetworkClient(networkClientId?: NetworkClientId): {\n chainId: string;\n ethQuery?: EthQuery;\n } {\n const selectedNetworkClientId =\n networkClientId ??\n this.messagingSystem.call('NetworkController:getState')\n .selectedNetworkClientId;\n const {\n configuration: { chainId },\n provider,\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n return {\n chainId,\n ethQuery: new EthQuery(provider),\n };\n }\n\n /**\n * Retrieves the list of network client IDs.\n *\n * @returns An array of network client IDs.\n */\n #getNetworkClientIds(): NetworkClientId[] {\n const { networkConfigurationsByChainId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n return Object.values(networkConfigurationsByChainId).flatMap(\n (networkConfiguration) =>\n networkConfiguration.rpcEndpoints.map(\n (rpcEndpoint) => rpcEndpoint.networkClientId,\n ),\n );\n }\n\n /**\n * Refreshes the balances of the accounts using the networkClientId\n *\n * @param input - The input for the poll.\n * @param input.networkClientIds - The network client IDs used to get balances.\n */\n async _executePoll({\n networkClientIds,\n }: AccountTrackerPollingInput): Promise<void> {\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.refresh(networkClientIds);\n }\n\n /**\n * Refreshes the balances of the accounts depending on the multi-account setting.\n * If multi-account is disabled, only updates the selected account balance.\n * If multi-account is enabled, updates balances for all accounts.\n *\n * @param networkClientIds - Optional network client IDs to fetch a network client with\n */\n async refresh(networkClientIds: NetworkClientId[]) {\n const selectedAccount = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const releaseLock = await this.#refreshMutex.acquire();\n try {\n const chainIds = networkClientIds.map((networkClientId) => {\n const { chainId } = this.#getCorrectNetworkClient(networkClientId);\n return chainId;\n });\n\n this.syncAccounts(chainIds);\n\n // Create an array of promises for each networkClientId\n const updatePromises = networkClientIds.map(async (networkClientId) => {\n const { chainId, ethQuery } =\n this.#getCorrectNetworkClient(networkClientId);\n const { accountsByChainId } = this.state;\n const { isMultiAccountBalancesEnabled } = this.messagingSystem.call(\n 'PreferencesController:getState',\n );\n\n const accountsToUpdate = isMultiAccountBalancesEnabled\n ? Object.keys(accountsByChainId[chainId])\n : [toChecksumHexAddress(selectedAccount.address)];\n\n const accountsForChain = { ...accountsByChainId[chainId] };\n\n // Process accounts in batches using reduceInBatchesSerially\n await reduceInBatchesSerially<string, void>({\n values: accountsToUpdate,\n batchSize: TOKEN_PRICES_BATCH_SIZE,\n initialResult: undefined,\n eachBatch: async (workingResult: void, batch: string[]) => {\n const balancePromises = batch.map(async (address: string) => {\n const balancePromise = this.#getBalanceFromChain(\n address,\n ethQuery,\n );\n const stakedBalancePromise = this.#includeStakedAssets\n ? this.#getStakedBalanceForChain(address, networkClientId)\n : Promise.resolve(null);\n\n const [balanceResult, stakedBalanceResult] =\n await Promise.allSettled([\n balancePromise,\n stakedBalancePromise,\n ]);\n\n // Update account balances\n if (balanceResult.status === 'fulfilled' && balanceResult.value) {\n accountsForChain[address] = {\n balance: balanceResult.value,\n };\n }\n\n if (\n stakedBalanceResult.status === 'fulfilled' &&\n stakedBalanceResult.value\n ) {\n accountsForChain[address] = {\n ...accountsForChain[address],\n stakedBalance: stakedBalanceResult.value,\n };\n }\n });\n\n await Promise.allSettled(balancePromises);\n return workingResult;\n },\n });\n\n // After all batches are processed, return the updated data\n return { chainId, accountsForChain };\n });\n\n // Wait for all networkClientId updates to settle in parallel\n const allResults = await Promise.allSettled(updatePromises);\n\n // Build a _copy_ of the current state and track whether anything changed\n const nextAccountsByChainId: AccountTrackerControllerState['accountsByChainId'] =\n cloneDeep(this.state.accountsByChainId);\n let hasChanges = false;\n\n allResults.forEach((result) => {\n if (result.status === 'fulfilled') {\n const { chainId, accountsForChain } = result.value;\n // Only mark as changed if the incoming data differs\n if (!isEqual(nextAccountsByChainId[chainId], accountsForChain)) {\n nextAccountsByChainId[chainId] = accountsForChain;\n hasChanges = true;\n }\n }\n });\n\n // 👇🏻 call `update` only when something is new / different\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Fetches the balance of a given address from the blockchain.\n *\n * @param address - The account address to fetch the balance for.\n * @param ethQuery - The EthQuery instance to query getBalnce with.\n * @returns A promise that resolves to the balance in a hex string format.\n */\n async #getBalanceFromChain(\n address: string,\n ethQuery?: EthQuery,\n ): Promise<string | undefined> {\n return await safelyExecuteWithTimeout(async () => {\n assert(ethQuery, 'Provider not set.');\n return await query(ethQuery, 'getBalance', [address]);\n });\n }\n\n /**\n * Sync accounts balances with some additional addresses.\n *\n * @param addresses - the additional addresses, may be hardware wallet addresses.\n * @param networkClientId - Optional networkClientId to fetch a network client with.\n * @returns accounts - addresses with synced balance\n */\n async syncBalanceWithAddresses(\n addresses: string[],\n networkClientId?: NetworkClientId,\n ): Promise<\n Record<string, { balance: string; stakedBalance?: StakedBalance }>\n > {\n const { ethQuery } = this.#getCorrectNetworkClient(networkClientId);\n\n return await Promise.all(\n addresses.map(\n (address): Promise<[string, string, StakedBalance] | undefined> => {\n return safelyExecuteWithTimeout(async () => {\n assert(ethQuery, 'Provider not set.');\n const balance = await query(ethQuery, 'getBalance', [address]);\n\n let stakedBalance: StakedBalance;\n if (this.#includeStakedAssets) {\n stakedBalance = await this.#getStakedBalanceForChain(\n address,\n networkClientId,\n );\n }\n return [address, balance, stakedBalance];\n });\n },\n ),\n ).then((value) => {\n return value.reduce((obj, item) => {\n if (!item) {\n return obj;\n }\n\n const [address, balance, stakedBalance] = item;\n return {\n ...obj,\n [address]: {\n balance,\n stakedBalance,\n },\n };\n }, {});\n });\n }\n}\n\nexport default AccountTrackerController;\n"]}
|
|
1
|
+
{"version":3,"file":"AccountTrackerController.cjs","sourceRoot":"","sources":["../src/AccountTrackerController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,wDAAoD;AACpD,wDAAwD;AAYxD,iEAIoC;AAEpC,oEAA2C;AAM3C,qEAA+E;AAE/E,2CAAsD;AACtD,6CAAoC;AACpC,mCAA4C;AAC5C,sGAA4E;AAE5E,6EAIoC;AACpC,iDAAgF;AAEhF;;GAEG;AACH,MAAM,cAAc,GAAG,0BAA0B,CAAC;AA2BlD,MAAM,sBAAsB,GAAG;IAC7B,iBAAiB,EAAE;QACjB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAgEF;;GAEG;AACH,MAAa,wBAAyB,SAAQ,IAAA,oDAA+B,GAI5E;IAOC;;;;;;;;;OASG;IACH,YAAY,EACV,QAAQ,GAAG,KAAK,EAChB,KAAK,EACL,SAAS,EACT,wBAAwB,EACxB,mBAAmB,GAAG,KAAK,GAO5B;QACC,MAAM,EAAE,uBAAuB,EAAE,GAAG,SAAS,CAAC,IAAI,CAChD,4BAA4B,CAC7B,CAAC;QACF,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,SAAS,CAAC,IAAI,CAChB,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;QACF,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,SAAS;YACT,KAAK,EAAE;gBACL,iBAAiB,EAAE;oBACjB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,CAAC,MAAM,CAAC,8BAA8B,CAAC,CAAC,OAAO,CAC1D,CAAC,oBAAoB,EAAE,EAAE,CACvB,oBAAoB,CAAC,YAAY,CAAC,GAAG,CACnC,CAAC,WAAW,EAAE,EAAE,CAAC,WAAW,CAAC,eAAe,CAC7C,CACJ,CAAC;AACJ,CAAC;AAoJD;;;;;;GAMG;AACH,KAAK,wDACH,OAAe,EACf,QAAmB;IAEnB,OAAO,MAAM,IAAA,2CAAwB,EAAC,KAAK,IAAI,EAAE;QAC/C,IAAA,cAAM,EAAC,QAAQ,EAAE,mBAAmB,CAAC,CAAC;QACtC,OAAO,MAAM,IAAA,wBAAK,EAAC,QAAQ,EAAE,YAAY,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;AACL,CAAC;AAsDH,kBAAe,wBAAwB,CAAC","sourcesContent":["import type { BigNumber } from '@ethersproject/bignumber';\nimport { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport type {\n AccountsControllerSelectedEvmAccountChangeEvent,\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerListAccountsAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerStateChangeEvent,\n ControllerGetStateAction,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport {\n query,\n safelyExecuteWithTimeout,\n toChecksumHexAddress,\n} from '@metamask/controller-utils';\nimport type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';\nimport EthQuery from '@metamask/eth-query';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';\nimport { assert, hasProperty } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\nimport { cloneDeep, isEqual } from 'lodash';\nimport abiSingleCallBalancesContract from 'single-call-balance-checker-abi';\n\nimport {\n SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID,\n type AssetsContractController,\n type StakedBalance,\n} from './AssetsContractController';\nimport { reduceInBatchesSerially, TOKEN_PRICES_BATCH_SIZE } from './assetsUtil';\n\n/**\n * The name of the {@link AccountTrackerController}.\n */\nconst controllerName = 'AccountTrackerController';\n\n/**\n * AccountInformation\n *\n * Account information object\n *\n * balance - Hex string of an account balance in wei\n *\n * stakedBalance - Hex string of an account staked balance in wei\n */\nexport type AccountInformation = {\n balance: string;\n stakedBalance?: string;\n};\n\n/**\n * AccountTrackerControllerState\n *\n * Account tracker controller state\n *\n * accountsByChainId - Map of addresses to account information by chain\n */\nexport type AccountTrackerControllerState = {\n accountsByChainId: Record<string, { [address: string]: AccountInformation }>;\n};\n\nconst accountTrackerMetadata = {\n accountsByChainId: {\n persist: true,\n anonymous: false,\n },\n};\n\n/**\n * The action that can be performed to get the state of the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AccountTrackerControllerState\n>;\n\n/**\n * The actions that can be performed using the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerActions =\n AccountTrackerControllerGetStateAction;\n\n/**\n * The messenger of the {@link AccountTrackerController} for communication.\n */\nexport type AllowedActions =\n | AccountsControllerListAccountsAction\n | PreferencesControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction\n | NetworkControllerGetStateAction\n | NetworkControllerGetNetworkClientByIdAction;\n\n/**\n * The event that {@link AccountTrackerController} can emit.\n */\nexport type AccountTrackerControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n AccountTrackerControllerState\n >;\n\n/**\n * The events that {@link AccountTrackerController} can emit.\n */\nexport type AccountTrackerControllerEvents =\n AccountTrackerControllerStateChangeEvent;\n\n/**\n * The external events available to the {@link AccountTrackerController}.\n */\nexport type AllowedEvents =\n | AccountsControllerSelectedEvmAccountChangeEvent\n | AccountsControllerSelectedAccountChangeEvent;\n\n/**\n * The messenger of the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AccountTrackerControllerActions | AllowedActions,\n AccountTrackerControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/** The input to start polling for the {@link AccountTrackerController} */\ntype AccountTrackerPollingInput = {\n networkClientIds: NetworkClientId[];\n};\n\n/**\n * Controller that tracks the network balances for all user accounts.\n */\nexport class AccountTrackerController extends StaticIntervalPollingController<AccountTrackerPollingInput>()<\n typeof controllerName,\n AccountTrackerControllerState,\n AccountTrackerControllerMessenger\n> {\n readonly #refreshMutex = new Mutex();\n\n readonly #includeStakedAssets: boolean;\n\n readonly #getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n\n /**\n * Creates an AccountTracker instance.\n *\n * @param options - The controller options.\n * @param options.interval - Polling interval used to fetch new account balances.\n * @param options.state - Initial state to set on this controller.\n * @param options.messenger - The controller messaging system.\n * @param options.getStakedBalanceForChain - The function to get the staked native asset balance for a chain.\n * @param options.includeStakedAssets - Whether to include staked assets in the account balances.\n */\n constructor({\n interval = 10000,\n state,\n messenger,\n getStakedBalanceForChain,\n includeStakedAssets = false,\n }: {\n interval?: number;\n state?: Partial<AccountTrackerControllerState>;\n messenger: AccountTrackerControllerMessenger;\n getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n includeStakedAssets?: boolean;\n }) {\n const { selectedNetworkClientId } = messenger.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = messenger.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n super({\n name: controllerName,\n messenger,\n state: {\n accountsByChainId: {\n [chainId]: {},\n },\n ...state,\n },\n metadata: accountTrackerMetadata,\n });\n this.#getStakedBalanceForChain = getStakedBalanceForChain;\n\n this.#includeStakedAssets = includeStakedAssets;\n\n this.setIntervalLength(interval);\n\n this.messagingSystem.subscribe(\n 'AccountsController:selectedEvmAccountChange',\n (newAddress, prevAddress) => {\n if (newAddress !== prevAddress) {\n // Making an async call for this new event\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.refresh(this.#getNetworkClientIds());\n }\n },\n (event): string => event.address,\n );\n }\n\n private syncAccounts(newChainIds: string[]) {\n const accountsByChainId = cloneDeep(this.state.accountsByChainId);\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId: currentChainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n const existing = Object.keys(accountsByChainId?.[currentChainId] ?? {});\n\n // Initialize new chain IDs if they don't exist\n newChainIds.forEach((newChainId) => {\n if (!accountsByChainId[newChainId]) {\n accountsByChainId[newChainId] = {};\n existing.forEach((address) => {\n accountsByChainId[newChainId][address] = { balance: '0x0' };\n });\n }\n });\n\n // Note: The address from the preferences controller are checksummed\n // The addresses from the accounts controller are lowercased\n const addresses = Object.values(\n this.messagingSystem\n .call('AccountsController:listAccounts')\n .map((internalAccount) =>\n toChecksumHexAddress(internalAccount.address),\n ),\n );\n const newAddresses = addresses.filter(\n (address) => !existing.includes(address),\n );\n const oldAddresses = existing.filter(\n (address) => !addresses.includes(address),\n );\n Object.keys(accountsByChainId).forEach((chainId) => {\n newAddresses.forEach((address) => {\n accountsByChainId[chainId][address] = {\n balance: '0x0',\n };\n });\n });\n\n Object.keys(accountsByChainId).forEach((chainId) => {\n oldAddresses.forEach((address) => {\n delete accountsByChainId[chainId][address];\n });\n });\n\n if (!isEqual(this.state.accountsByChainId, accountsByChainId)) {\n this.update((state) => {\n state.accountsByChainId = accountsByChainId;\n });\n }\n }\n\n /**\n * Resolves a networkClientId to a network client config\n * or globally selected network config if not provided\n *\n * @param networkClientId - Optional networkClientId to fetch a network client with\n * @returns network client config\n */\n #getCorrectNetworkClient(networkClientId?: NetworkClientId): {\n chainId: string;\n provider: SafeEventEmitterProvider;\n ethQuery?: EthQuery;\n } {\n const selectedNetworkClientId =\n networkClientId ??\n this.messagingSystem.call('NetworkController:getState')\n .selectedNetworkClientId;\n const {\n configuration: { chainId },\n provider,\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n return {\n chainId,\n provider,\n ethQuery: new EthQuery(provider),\n };\n }\n\n /**\n * Retrieves the list of network client IDs.\n *\n * @returns An array of network client IDs.\n */\n #getNetworkClientIds(): NetworkClientId[] {\n const { networkConfigurationsByChainId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n return Object.values(networkConfigurationsByChainId).flatMap(\n (networkConfiguration) =>\n networkConfiguration.rpcEndpoints.map(\n (rpcEndpoint) => rpcEndpoint.networkClientId,\n ),\n );\n }\n\n /**\n * Refreshes the balances of the accounts using the networkClientId\n *\n * @param input - The input for the poll.\n * @param input.networkClientIds - The network client IDs used to get balances.\n */\n async _executePoll({\n networkClientIds,\n }: AccountTrackerPollingInput): Promise<void> {\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.refresh(networkClientIds);\n }\n\n /**\n * Refreshes the balances of the accounts depending on the multi-account setting.\n * If multi-account is disabled, only updates the selected account balance.\n * If multi-account is enabled, updates balances for all accounts.\n *\n * @param networkClientIds - Optional network client IDs to fetch a network client with\n */\n async refresh(networkClientIds: NetworkClientId[]) {\n const selectedAccount = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const releaseLock = await this.#refreshMutex.acquire();\n try {\n const chainIds = networkClientIds.map((networkClientId) => {\n const { chainId } = this.#getCorrectNetworkClient(networkClientId);\n return chainId;\n });\n\n this.syncAccounts(chainIds);\n\n // Create an array of promises for each networkClientId\n const updatePromises = networkClientIds.map(async (networkClientId) => {\n const { chainId, ethQuery, provider } =\n this.#getCorrectNetworkClient(networkClientId);\n const { accountsByChainId } = this.state;\n const { isMultiAccountBalancesEnabled } = this.messagingSystem.call(\n 'PreferencesController:getState',\n );\n\n const accountsToUpdate = isMultiAccountBalancesEnabled\n ? Object.keys(accountsByChainId[chainId])\n : [toChecksumHexAddress(selectedAccount.address)];\n\n const accountsForChain = { ...accountsByChainId[chainId] };\n\n const stakedBalancesPromise = this.#includeStakedAssets\n ? this.#getStakedBalanceForChain(accountsToUpdate, networkClientId)\n : Promise.resolve({});\n\n if (hasProperty(SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID, chainId)) {\n const contractAddress = SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID[\n chainId\n ] as string;\n\n const contract = new Contract(\n contractAddress,\n abiSingleCallBalancesContract,\n new Web3Provider(provider),\n );\n\n const nativeBalances = await contract.balances(accountsToUpdate, [\n '0x0000000000000000000000000000000000000000',\n ]);\n\n accountsToUpdate.forEach((address, index) => {\n accountsForChain[address] = {\n balance: (nativeBalances[index] as BigNumber).toHexString(),\n };\n });\n } else {\n // Process accounts in batches using reduceInBatchesSerially\n await reduceInBatchesSerially<string, void>({\n values: accountsToUpdate,\n batchSize: TOKEN_PRICES_BATCH_SIZE,\n initialResult: undefined,\n eachBatch: async (workingResult: void, batch: string[]) => {\n const balancePromises = batch.map(async (address: string) => {\n const balanceResult = await this.#getBalanceFromChain(\n address,\n ethQuery,\n ).catch(() => null);\n\n // Update account balances\n if (balanceResult) {\n accountsForChain[address] = {\n balance: balanceResult,\n };\n }\n });\n\n await Promise.allSettled(balancePromises);\n return workingResult;\n },\n });\n }\n\n const stakedBalanceResult = (await stakedBalancesPromise) as Record<\n string,\n StakedBalance\n >;\n\n Object.entries(stakedBalanceResult).forEach(([address, balance]) => {\n accountsForChain[address] = {\n ...accountsForChain[address],\n stakedBalance: balance,\n };\n });\n\n // After all batches are processed, return the updated data\n return { chainId, accountsForChain };\n });\n\n // Wait for all networkClientId updates to settle in parallel\n const allResults = await Promise.allSettled(updatePromises);\n\n // Build a _copy_ of the current state and track whether anything changed\n const nextAccountsByChainId: AccountTrackerControllerState['accountsByChainId'] =\n cloneDeep(this.state.accountsByChainId);\n let hasChanges = false;\n\n allResults.forEach((result) => {\n if (result.status === 'fulfilled') {\n const { chainId, accountsForChain } = result.value;\n // Only mark as changed if the incoming data differs\n if (!isEqual(nextAccountsByChainId[chainId], accountsForChain)) {\n nextAccountsByChainId[chainId] = accountsForChain;\n hasChanges = true;\n }\n }\n });\n\n // 👇🏻 call `update` only when something is new / different\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Fetches the balance of a given address from the blockchain.\n *\n * @param address - The account address to fetch the balance for.\n * @param ethQuery - The EthQuery instance to query getBalnce with.\n * @returns A promise that resolves to the balance in a hex string format.\n */\n async #getBalanceFromChain(\n address: string,\n ethQuery?: EthQuery,\n ): Promise<string | undefined> {\n return await safelyExecuteWithTimeout(async () => {\n assert(ethQuery, 'Provider not set.');\n return await query(ethQuery, 'getBalance', [address]);\n });\n }\n\n /**\n * Sync accounts balances with some additional addresses.\n *\n * @param addresses - the additional addresses, may be hardware wallet addresses.\n * @param networkClientId - Optional networkClientId to fetch a network client with.\n * @returns accounts - addresses with synced balance\n */\n async syncBalanceWithAddresses(\n addresses: string[],\n networkClientId?: NetworkClientId,\n ): Promise<\n Record<string, { balance: string; stakedBalance?: StakedBalance }>\n > {\n const { ethQuery } = this.#getCorrectNetworkClient(networkClientId);\n\n // TODO: This should use multicall when enabled by the user.\n return await Promise.all(\n addresses.map(\n (address): Promise<[string, string, StakedBalance] | undefined> => {\n return safelyExecuteWithTimeout(async () => {\n assert(ethQuery, 'Provider not set.');\n const balance = await query(ethQuery, 'getBalance', [address]);\n\n let stakedBalance: StakedBalance;\n if (this.#includeStakedAssets) {\n stakedBalance = (\n await this.#getStakedBalanceForChain([address], networkClientId)\n )[address];\n }\n return [address, balance, stakedBalance];\n });\n },\n ),\n ).then((value) => {\n return value.reduce((obj, item) => {\n if (!item) {\n return obj;\n }\n\n const [address, balance, stakedBalance] = item;\n return {\n ...obj,\n [address]: {\n balance,\n stakedBalance,\n },\n };\n }, {});\n });\n }\n}\n\nexport default AccountTrackerController;\n"]}
|
|
@@ -2,27 +2,30 @@ import type { AccountsControllerSelectedEvmAccountChangeEvent, AccountsControlle
|
|
|
2
2
|
import type { ControllerStateChangeEvent, ControllerGetStateAction, RestrictedMessenger } from "@metamask/base-controller";
|
|
3
3
|
import type { NetworkClientId, NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
|
|
4
4
|
import type { PreferencesControllerGetStateAction } from "@metamask/preferences-controller";
|
|
5
|
-
import type
|
|
5
|
+
import { type AssetsContractController, type StakedBalance } from "./AssetsContractController.cjs";
|
|
6
6
|
/**
|
|
7
7
|
* The name of the {@link AccountTrackerController}.
|
|
8
8
|
*/
|
|
9
9
|
declare const controllerName = "AccountTrackerController";
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* AccountInformation
|
|
12
12
|
*
|
|
13
13
|
* Account information object
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
*
|
|
15
|
+
* balance - Hex string of an account balance in wei
|
|
16
|
+
*
|
|
17
|
+
* stakedBalance - Hex string of an account staked balance in wei
|
|
16
18
|
*/
|
|
17
19
|
export type AccountInformation = {
|
|
18
20
|
balance: string;
|
|
19
21
|
stakedBalance?: string;
|
|
20
22
|
};
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
24
|
+
* AccountTrackerControllerState
|
|
23
25
|
*
|
|
24
26
|
* Account tracker controller state
|
|
25
|
-
*
|
|
27
|
+
*
|
|
28
|
+
* accountsByChainId - Map of addresses to account information by chain
|
|
26
29
|
*/
|
|
27
30
|
export type AccountTrackerControllerState = {
|
|
28
31
|
accountsByChainId: Record<string, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTrackerController.d.cts","sourceRoot":"","sources":["../src/AccountTrackerController.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AccountTrackerController.d.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|
@@ -2,27 +2,30 @@ import type { AccountsControllerSelectedEvmAccountChangeEvent, AccountsControlle
|
|
|
2
2
|
import type { ControllerStateChangeEvent, ControllerGetStateAction, RestrictedMessenger } from "@metamask/base-controller";
|
|
3
3
|
import type { NetworkClientId, NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
|
|
4
4
|
import type { PreferencesControllerGetStateAction } from "@metamask/preferences-controller";
|
|
5
|
-
import type
|
|
5
|
+
import { type AssetsContractController, type StakedBalance } from "./AssetsContractController.mjs";
|
|
6
6
|
/**
|
|
7
7
|
* The name of the {@link AccountTrackerController}.
|
|
8
8
|
*/
|
|
9
9
|
declare const controllerName = "AccountTrackerController";
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* AccountInformation
|
|
12
12
|
*
|
|
13
13
|
* Account information object
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
*
|
|
15
|
+
* balance - Hex string of an account balance in wei
|
|
16
|
+
*
|
|
17
|
+
* stakedBalance - Hex string of an account staked balance in wei
|
|
16
18
|
*/
|
|
17
19
|
export type AccountInformation = {
|
|
18
20
|
balance: string;
|
|
19
21
|
stakedBalance?: string;
|
|
20
22
|
};
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
24
|
+
* AccountTrackerControllerState
|
|
23
25
|
*
|
|
24
26
|
* Account tracker controller state
|
|
25
|
-
*
|
|
27
|
+
*
|
|
28
|
+
* accountsByChainId - Map of addresses to account information by chain
|
|
26
29
|
*/
|
|
27
30
|
export type AccountTrackerControllerState = {
|
|
28
31
|
accountsByChainId: Record<string, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTrackerController.d.mts","sourceRoot":"","sources":["../src/AccountTrackerController.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AccountTrackerController.d.mts","sourceRoot":"","sources":["../src/AccountTrackerController.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,+CAA+C,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|
|
19
|
+
import { Contract } from "@ethersproject/contracts";
|
|
20
|
+
import { Web3Provider } from "@ethersproject/providers";
|
|
19
21
|
import { query, safelyExecuteWithTimeout, toChecksumHexAddress } from "@metamask/controller-utils";
|
|
20
22
|
import $EthQuery from "@metamask/eth-query";
|
|
21
23
|
const EthQuery = $importDefault($EthQuery);
|
|
22
24
|
import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
|
23
|
-
import { assert } from "@metamask/utils";
|
|
25
|
+
import { assert, hasProperty } from "@metamask/utils";
|
|
24
26
|
import { Mutex } from "async-mutex";
|
|
25
27
|
import $lodash from "lodash";
|
|
26
28
|
const { cloneDeep, isEqual } = $lodash;
|
|
29
|
+
import $abiSingleCallBalancesContract from "single-call-balance-checker-abi";
|
|
30
|
+
const abiSingleCallBalancesContract = $importDefault($abiSingleCallBalancesContract);
|
|
31
|
+
import { SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID } from "./AssetsContractController.mjs";
|
|
27
32
|
import { reduceInBatchesSerially, TOKEN_PRICES_BATCH_SIZE } from "./assetsUtil.mjs";
|
|
28
33
|
/**
|
|
29
34
|
* The name of the {@link AccountTrackerController}.
|
|
@@ -146,45 +151,55 @@ export class AccountTrackerController extends StaticIntervalPollingController()
|
|
|
146
151
|
this.syncAccounts(chainIds);
|
|
147
152
|
// Create an array of promises for each networkClientId
|
|
148
153
|
const updatePromises = networkClientIds.map(async (networkClientId) => {
|
|
149
|
-
const { chainId, ethQuery } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
154
|
+
const { chainId, ethQuery, provider } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
150
155
|
const { accountsByChainId } = this.state;
|
|
151
156
|
const { isMultiAccountBalancesEnabled } = this.messagingSystem.call('PreferencesController:getState');
|
|
152
157
|
const accountsToUpdate = isMultiAccountBalancesEnabled
|
|
153
158
|
? Object.keys(accountsByChainId[chainId])
|
|
154
159
|
: [toChecksumHexAddress(selectedAccount.address)];
|
|
155
160
|
const accountsForChain = { ...accountsByChainId[chainId] };
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
161
|
+
const stakedBalancesPromise = __classPrivateFieldGet(this, _AccountTrackerController_includeStakedAssets, "f")
|
|
162
|
+
? __classPrivateFieldGet(this, _AccountTrackerController_getStakedBalanceForChain, "f").call(this, accountsToUpdate, networkClientId)
|
|
163
|
+
: Promise.resolve({});
|
|
164
|
+
if (hasProperty(SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID, chainId)) {
|
|
165
|
+
const contractAddress = SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID[chainId];
|
|
166
|
+
const contract = new Contract(contractAddress, abiSingleCallBalancesContract, new Web3Provider(provider));
|
|
167
|
+
const nativeBalances = await contract.balances(accountsToUpdate, [
|
|
168
|
+
'0x0000000000000000000000000000000000000000',
|
|
169
|
+
]);
|
|
170
|
+
accountsToUpdate.forEach((address, index) => {
|
|
171
|
+
accountsForChain[address] = {
|
|
172
|
+
balance: nativeBalances[index].toHexString(),
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// Process accounts in batches using reduceInBatchesSerially
|
|
178
|
+
await reduceInBatchesSerially({
|
|
179
|
+
values: accountsToUpdate,
|
|
180
|
+
batchSize: TOKEN_PRICES_BATCH_SIZE,
|
|
181
|
+
initialResult: undefined,
|
|
182
|
+
eachBatch: async (workingResult, batch) => {
|
|
183
|
+
const balancePromises = batch.map(async (address) => {
|
|
184
|
+
const balanceResult = await __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getBalanceFromChain).call(this, address, ethQuery).catch(() => null);
|
|
185
|
+
// Update account balances
|
|
186
|
+
if (balanceResult) {
|
|
187
|
+
accountsForChain[address] = {
|
|
188
|
+
balance: balanceResult,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
await Promise.allSettled(balancePromises);
|
|
193
|
+
return workingResult;
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
const stakedBalanceResult = (await stakedBalancesPromise);
|
|
198
|
+
Object.entries(stakedBalanceResult).forEach(([address, balance]) => {
|
|
199
|
+
accountsForChain[address] = {
|
|
200
|
+
...accountsForChain[address],
|
|
201
|
+
stakedBalance: balance,
|
|
202
|
+
};
|
|
188
203
|
});
|
|
189
204
|
// After all batches are processed, return the updated data
|
|
190
205
|
return { chainId, accountsForChain };
|
|
@@ -224,13 +239,14 @@ export class AccountTrackerController extends StaticIntervalPollingController()
|
|
|
224
239
|
*/
|
|
225
240
|
async syncBalanceWithAddresses(addresses, networkClientId) {
|
|
226
241
|
const { ethQuery } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
242
|
+
// TODO: This should use multicall when enabled by the user.
|
|
227
243
|
return await Promise.all(addresses.map((address) => {
|
|
228
244
|
return safelyExecuteWithTimeout(async () => {
|
|
229
245
|
assert(ethQuery, 'Provider not set.');
|
|
230
246
|
const balance = await query(ethQuery, 'getBalance', [address]);
|
|
231
247
|
let stakedBalance;
|
|
232
248
|
if (__classPrivateFieldGet(this, _AccountTrackerController_includeStakedAssets, "f")) {
|
|
233
|
-
stakedBalance = await __classPrivateFieldGet(this, _AccountTrackerController_getStakedBalanceForChain, "f").call(this, address, networkClientId);
|
|
249
|
+
stakedBalance = (await __classPrivateFieldGet(this, _AccountTrackerController_getStakedBalanceForChain, "f").call(this, [address], networkClientId))[address];
|
|
234
250
|
}
|
|
235
251
|
return [address, balance, stakedBalance];
|
|
236
252
|
});
|
|
@@ -258,6 +274,7 @@ _AccountTrackerController_refreshMutex = new WeakMap(), _AccountTrackerControlle
|
|
|
258
274
|
const { configuration: { chainId }, provider, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
|
259
275
|
return {
|
|
260
276
|
chainId,
|
|
277
|
+
provider,
|
|
261
278
|
ethQuery: new EthQuery(provider),
|
|
262
279
|
};
|
|
263
280
|
}, _AccountTrackerController_getNetworkClientIds = function _AccountTrackerController_getNetworkClientIds() {
|