@metamask/assets-controllers 94.0.0 → 95.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +71 -1
- package/dist/AccountTrackerController.cjs +85 -56
- package/dist/AccountTrackerController.cjs.map +1 -1
- package/dist/AccountTrackerController.d.cts +15 -7
- package/dist/AccountTrackerController.d.cts.map +1 -1
- package/dist/AccountTrackerController.d.mts +15 -7
- package/dist/AccountTrackerController.d.mts.map +1 -1
- package/dist/AccountTrackerController.mjs +85 -56
- package/dist/AccountTrackerController.mjs.map +1 -1
- package/dist/CurrencyRateController.d.cts +2 -2
- package/dist/CurrencyRateController.d.mts +2 -2
- package/dist/DeFiPositionsController/DeFiPositionsController.d.cts +2 -2
- package/dist/DeFiPositionsController/DeFiPositionsController.d.mts +2 -2
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.cjs +8 -2
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.cjs.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts +2 -2
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts +2 -2
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.mjs +8 -2
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.mjs.map +1 -1
- package/dist/TokenBalancesController.cjs +351 -382
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +22 -41
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts +22 -41
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +351 -382
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/TokenDetectionController.cjs +139 -207
- package/dist/TokenDetectionController.cjs.map +1 -1
- package/dist/TokenDetectionController.d.cts +40 -14
- package/dist/TokenDetectionController.d.cts.map +1 -1
- package/dist/TokenDetectionController.d.mts +40 -14
- package/dist/TokenDetectionController.d.mts.map +1 -1
- package/dist/TokenDetectionController.mjs +140 -208
- package/dist/TokenDetectionController.mjs.map +1 -1
- package/dist/TokenListController.d.cts +2 -2
- package/dist/TokenListController.d.mts +2 -2
- package/dist/TokenRatesController.d.cts +2 -2
- package/dist/TokenRatesController.d.mts +2 -2
- package/dist/index.cjs +2 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs +13 -2
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs +13 -2
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/types.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/types.d.cts +2 -1
- package/dist/multi-chain-accounts-service/types.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/types.d.mts +2 -1
- package/dist/multi-chain-accounts-service/types.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/types.mjs.map +1 -1
- package/dist/multicall.cjs +5 -0
- package/dist/multicall.cjs.map +1 -1
- package/dist/multicall.d.cts.map +1 -1
- package/dist/multicall.d.mts.map +1 -1
- package/dist/multicall.mjs +5 -0
- package/dist/multicall.mjs.map +1 -1
- package/dist/selectors/token-selectors.cjs +3 -3
- package/dist/selectors/token-selectors.cjs.map +1 -1
- package/dist/selectors/token-selectors.d.cts +711 -0
- package/dist/selectors/token-selectors.d.cts.map +1 -1
- package/dist/selectors/token-selectors.d.mts +711 -0
- package/dist/selectors/token-selectors.d.mts.map +1 -1
- package/dist/selectors/token-selectors.mjs +1 -1
- package/dist/selectors/token-selectors.mjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.cjs +79 -91
- package/dist/token-prices-service/codefi-v2.cjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.cts +40 -17
- package/dist/token-prices-service/codefi-v2.d.cts.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.mts +40 -17
- package/dist/token-prices-service/codefi-v2.d.mts.map +1 -1
- package/dist/token-prices-service/codefi-v2.mjs +79 -91
- package/dist/token-prices-service/codefi-v2.mjs.map +1 -1
- package/dist/token-service.cjs +43 -14
- package/dist/token-service.cjs.map +1 -1
- package/dist/token-service.d.cts +39 -9
- package/dist/token-service.d.cts.map +1 -1
- package/dist/token-service.d.mts +39 -9
- package/dist/token-service.d.mts.map +1 -1
- package/dist/token-service.mjs +43 -14
- package/dist/token-service.mjs.map +1 -1
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,64 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [95.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add RWA (Real World Assets) data support to token service ([#7560](https://github.com/MetaMask/core/pull/7560))
|
|
15
|
+
- Add `includeRwaData` optional parameter to `getTrendingTokens` function to include RWA data in results
|
|
16
|
+
- Add `TokenRwaData` type for RWA token metadata (market hours, pause times, ticker, instrument type)
|
|
17
|
+
- Add `TokenSearchItem` type with optional `rwaData` field for token search results
|
|
18
|
+
- Add `rwaData` optional field to `TrendingAsset` type
|
|
19
|
+
- Update spot-prices endpoint list of supported chains and assets ([#7543](https://github.com/MetaMask/core/pull/7543))
|
|
20
|
+
- Export `selectAllAssets` ([#7496](https://github.com/MetaMask/core/pull/7496))
|
|
21
|
+
- Add multicall address for `MSU (MapleStory Universe)` ([#7453](https://github.com/MetaMask/core/pull/7453))
|
|
22
|
+
- Add MSU (MapleStory Universe) and Etherlink in Spot Prices support ([#7453](https://github.com/MetaMask/core/pull/7453))
|
|
23
|
+
- Set MSU native token address to `eip155:68414/erc20:0xf2b51cc1850fed939658317a22d73d3482767591` instead of `eip155:68414/erc20:0x0000000000000000000000000000000000000000` - particular case where the native token doesn't have the zero address ([#7578](https://github.com/MetaMask/core/pull/7578))
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
|
|
27
|
+
- Bump `@metamask/snaps-controllers` from `^14.0.1` to `^17.2.0` ([#7550](https://github.com/MetaMask/core/pull/7550))
|
|
28
|
+
- Bump `@metamask/snaps-sdk` from `^9.0.0` to `^10.3.0` ([#7550](https://github.com/MetaMask/core/pull/7550))
|
|
29
|
+
- Bump `@metamask/snaps-utils` from `^11.0.0` to `^11.7.0` ([#7550](https://github.com/MetaMask/core/pull/7550))
|
|
30
|
+
- Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
|
|
31
|
+
- Bump `@metamask/permission-controller` from `^12.1.1` to `^12.2.0` ([#7559](https://github.com/MetaMask/core/pull/7559))
|
|
32
|
+
- **BREAKING:** `AccountTrackerController` now requires `KeyringController:getState` action and `KeyringController:lock` event in addition to existing allowed actions and events ([#7492](https://github.com/MetaMask/core/pull/7492))
|
|
33
|
+
- Added `#isLocked` property to track keyring lock state, initialized from `KeyringController:getState`
|
|
34
|
+
- Added `isActive` getter that returns `true` when keyring is unlocked and user is onboarded
|
|
35
|
+
- Balance updates are now skipped when controller is not active (locked or not onboarded)
|
|
36
|
+
- `KeyringController:unlock` event now only updates lock state without triggering immediate refresh
|
|
37
|
+
- `KeyringController:lock` event sets controller to inactive state
|
|
38
|
+
- `AccountTrackerController` now only refreshes balances for the newly added network on `NetworkController:networkAdded` event instead of all networks ([#7492](https://github.com/MetaMask/core/pull/7492))
|
|
39
|
+
- Bump `@metamask/transaction-controller` from `^62.5.0` to `^62.7.0` ([#7494](https://github.com/MetaMask/core/pull/7494))
|
|
40
|
+
- Bump `@metamask/multichain-account-service` from `^4.0.1` to `^4.1.0` ([#7515](https://github.com/MetaMask/core/pull/7515)
|
|
41
|
+
- Bump `@metamask/controller-utils` from `^11.16.0` to `^11.18.0` ([#7534](https://github.com/MetaMask/core/pull/7534), [#7583](https://github.com/MetaMask/core/pull/7583))
|
|
42
|
+
- Bump `@metamask/network-controller` from `^27.0.0` to `^27.2.0` ([#7534](https://github.com/MetaMask/core/pull/7534), [#7583](https://github.com/MetaMask/core/pull/7583))
|
|
43
|
+
|
|
44
|
+
## [94.1.0]
|
|
45
|
+
|
|
46
|
+
### Added
|
|
47
|
+
|
|
48
|
+
- Add `excludeLabels` parameter to `getTrendingTokens` to filter out tokens by labels (e.g., 'stable_coin', 'blue_chip') ([#7466](https://github.com/MetaMask/core/pull/7466))
|
|
49
|
+
- Add support for spot prices on multiple new chains: Apechain, Lens, Plume, Flow EVM, Berachain, xrpl-evm, Fraxtal, Lukso, xdc-network, and Plasma ([#7465](https://github.com/MetaMask/core/pull/7465))
|
|
50
|
+
- Add `ape` to `SUPPORTED_CURRENCIES` for ApeCoin/Apechain native token ([#7465](https://github.com/MetaMask/core/pull/7465))
|
|
51
|
+
- Add `isOnboarded` constructor option to `TokenBalancesController` and `AccountTrackerController` ([#7469](https://github.com/MetaMask/core/pull/7469))
|
|
52
|
+
- When `isOnboarded()` returns `false`, balance updates are skipped to prevent unnecessary API calls during onboarding
|
|
53
|
+
- `TokenBalancesController.isActive` now also checks `isOnboarded()` in addition to `isUnlocked`
|
|
54
|
+
- `AccountTrackerController.#refreshAccounts` now checks `isOnboarded()` before fetching balances
|
|
55
|
+
|
|
56
|
+
### Fixed
|
|
57
|
+
|
|
58
|
+
- Fix `TokenBalancesController` to evaluate `allowExternalServices` dynamically instead of only at constructor time ([#7469](https://github.com/MetaMask/core/pull/7469))
|
|
59
|
+
- Previously, the `#balanceFetchers` array was built once in the constructor, so changes to `allowExternalServices()` after initialization were not reflected
|
|
60
|
+
- Now `allowExternalServices()` is stored as a function and evaluated dynamically in the fetcher's `supports` method
|
|
61
|
+
- Fix `TokenDetectionController` methods `addDetectedTokensViaPolling` and `addDetectedTokensViaWs` to refresh token metadata cache before use ([#7469](https://github.com/MetaMask/core/pull/7469))
|
|
62
|
+
- Previously, these methods used a potentially stale/empty `#tokensChainsCache` from construction time
|
|
63
|
+
- Now they fetch the latest `tokensChainsCache` from `TokenListController:getState` before looking up token metadata
|
|
64
|
+
- Fix `AccountTrackerController.syncBalanceWithAddresses` to also check `isOnboarded()` before fetching balances ([#7469](https://github.com/MetaMask/core/pull/7469))
|
|
65
|
+
- Previously, only `#refreshAccounts` checked `isOnboarded()`, but `syncBalanceWithAddresses` would still make RPC calls during onboarding
|
|
66
|
+
- Now `syncBalanceWithAddresses` returns an empty object when `isOnboarded()` returns `false`
|
|
67
|
+
|
|
10
68
|
## [94.0.0]
|
|
11
69
|
|
|
12
70
|
### Added
|
|
@@ -16,6 +74,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
16
74
|
|
|
17
75
|
### Changed
|
|
18
76
|
|
|
77
|
+
- Bump `@metamask/transaction-controller` from `^62.4.0` to `^62.5.0` ([#7325](https://github.com/MetaMask/core/pull/7325))
|
|
78
|
+
- **BREAKING:** Replace Account API v2 with Account API v4 for token auto-detection ([#7408](https://github.com/MetaMask/core/pull/7408))
|
|
79
|
+
- `TokenDetectionController` now delegates token detection for Account API v4 supported chains to `TokenBalancesController`
|
|
80
|
+
- RPC-based detection continues to be used for chains not supported by Account API v4
|
|
81
|
+
- Added `forceRpc` parameter to `TokenDetectionController.detectTokens()` to force RPC-based detection
|
|
82
|
+
- `TokenDetectionController:detectTokens` action is now registered for cross-controller communication
|
|
83
|
+
- `TokenBalancesController` now triggers RPC-based token detection as fallback when Account API v4 fails or returns unprocessed chains ([#7408](https://github.com/MetaMask/core/pull/7408))
|
|
84
|
+
- Calls `TokenDetectionController:detectTokens` with `forceRpc: true` when fetcher fails
|
|
85
|
+
- Calls `TokenDetectionController:detectTokens` with `forceRpc: true` for any unprocessed chain IDs returned by the API
|
|
86
|
+
- Refactored `TokenBalancesController` for improved code organization and maintainability ([#7408](https://github.com/MetaMask/core/pull/7408))
|
|
19
87
|
- Remove warning logs for failed chain balance fetches in RPC balance fetcher ([#7429](https://github.com/MetaMask/core/pull/7429))
|
|
20
88
|
- Reduce severity of ERC721 metadata interface log from `console.error` to `console.warn` ([#7412](https://github.com/MetaMask/core/pull/7412))
|
|
21
89
|
- Fixes [#24988](https://github.com/MetaMask/metamask-extension/issues/24988)
|
|
@@ -2405,7 +2473,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
2405
2473
|
|
|
2406
2474
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
|
2407
2475
|
|
|
2408
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@
|
|
2476
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@95.0.0...HEAD
|
|
2477
|
+
[95.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@94.1.0...@metamask/assets-controllers@95.0.0
|
|
2478
|
+
[94.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@94.0.0...@metamask/assets-controllers@94.1.0
|
|
2409
2479
|
[94.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@93.1.0...@metamask/assets-controllers@94.0.0
|
|
2410
2480
|
[93.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@93.0.0...@metamask/assets-controllers@93.1.0
|
|
2411
2481
|
[93.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@92.0.0...@metamask/assets-controllers@93.0.0
|
|
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
|
-
var _AccountTrackerController_instances, _AccountTrackerController_refreshMutex, _AccountTrackerController_includeStakedAssets, _AccountTrackerController_accountsApiChainIds, _AccountTrackerController_getStakedBalanceForChain, _AccountTrackerController_balanceFetchers, _AccountTrackerController_fetchingEnabled, _AccountTrackerController_getProvider, _AccountTrackerController_getNetworkClient, _AccountTrackerController_createAccountsApiFetcher, _AccountTrackerController_getCorrectNetworkClient, _AccountTrackerController_getNetworkClientIds, _AccountTrackerController_refreshAccounts, _AccountTrackerController_registerMessageHandlers;
|
|
16
|
+
var _AccountTrackerController_instances, _AccountTrackerController_refreshMutex, _AccountTrackerController_includeStakedAssets, _AccountTrackerController_accountsApiChainIds, _AccountTrackerController_getStakedBalanceForChain, _AccountTrackerController_balanceFetchers, _AccountTrackerController_fetchingEnabled, _AccountTrackerController_isOnboarded, _AccountTrackerController_isLocked, _AccountTrackerController_syncAccounts, _AccountTrackerController_getProvider, _AccountTrackerController_getNetworkClient, _AccountTrackerController_createAccountsApiFetcher, _AccountTrackerController_getCorrectNetworkClient, _AccountTrackerController_getNetworkClientIds, _AccountTrackerController_refreshAccounts, _AccountTrackerController_registerMessageHandlers;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.AccountTrackerController = void 0;
|
|
19
19
|
const providers_1 = require("@ethersproject/providers");
|
|
@@ -89,8 +89,9 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
89
89
|
* @param options.accountsApiChainIds - Function that returns array of chainIds that should use Accounts-API strategy (if supported by API).
|
|
90
90
|
* @param options.allowExternalServices - Disable external HTTP calls (privacy / offline mode).
|
|
91
91
|
* @param options.fetchingEnabled - Function that returns whether the controller is fetching enabled.
|
|
92
|
+
* @param options.isOnboarded - Whether the user has completed onboarding. If false, balance updates are skipped.
|
|
92
93
|
*/
|
|
93
|
-
constructor({ interval = 10000, state, messenger, getStakedBalanceForChain, includeStakedAssets = false, accountsApiChainIds = () => [], allowExternalServices = () => true, fetchingEnabled = () => true, }) {
|
|
94
|
+
constructor({ interval = 10000, state, messenger, getStakedBalanceForChain, includeStakedAssets = false, accountsApiChainIds = () => [], allowExternalServices = () => true, fetchingEnabled = () => true, isOnboarded = () => true, }) {
|
|
94
95
|
const { selectedNetworkClientId } = messenger.call('NetworkController:getState');
|
|
95
96
|
const { configuration: { chainId }, } = messenger.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
|
96
97
|
super({
|
|
@@ -111,17 +112,20 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
111
112
|
_AccountTrackerController_getStakedBalanceForChain.set(this, void 0);
|
|
112
113
|
_AccountTrackerController_balanceFetchers.set(this, void 0);
|
|
113
114
|
_AccountTrackerController_fetchingEnabled.set(this, void 0);
|
|
115
|
+
_AccountTrackerController_isOnboarded.set(this, void 0);
|
|
116
|
+
/** Track if the keyring is locked */
|
|
117
|
+
_AccountTrackerController_isLocked.set(this, true);
|
|
114
118
|
_AccountTrackerController_getProvider.set(this, (chainId) => {
|
|
115
119
|
const { networkConfigurationsByChainId } = this.messenger.call('NetworkController:getState');
|
|
116
|
-
const
|
|
117
|
-
const { networkClientId } =
|
|
120
|
+
const networkConfig = networkConfigurationsByChainId[chainId];
|
|
121
|
+
const { networkClientId } = networkConfig.rpcEndpoints[networkConfig.defaultRpcEndpointIndex];
|
|
118
122
|
const client = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
|
|
119
123
|
return new providers_1.Web3Provider(client.provider);
|
|
120
124
|
});
|
|
121
125
|
_AccountTrackerController_getNetworkClient.set(this, (chainId) => {
|
|
122
126
|
const { networkConfigurationsByChainId } = this.messenger.call('NetworkController:getState');
|
|
123
|
-
const
|
|
124
|
-
const { networkClientId } =
|
|
127
|
+
const networkConfig = networkConfigurationsByChainId[chainId];
|
|
128
|
+
const { networkClientId } = networkConfig.rpcEndpoints[networkConfig.defaultRpcEndpointIndex];
|
|
125
129
|
return this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
|
|
126
130
|
});
|
|
127
131
|
/**
|
|
@@ -153,6 +157,9 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
153
157
|
createAccountTrackerRpcBalanceFetcher(__classPrivateFieldGet(this, _AccountTrackerController_getProvider, "f"), __classPrivateFieldGet(this, _AccountTrackerController_getNetworkClient, "f"), __classPrivateFieldGet(this, _AccountTrackerController_includeStakedAssets, "f")),
|
|
154
158
|
], "f");
|
|
155
159
|
__classPrivateFieldSet(this, _AccountTrackerController_fetchingEnabled, fetchingEnabled, "f");
|
|
160
|
+
__classPrivateFieldSet(this, _AccountTrackerController_isOnboarded, isOnboarded, "f");
|
|
161
|
+
const { isUnlocked } = this.messenger.call('KeyringController:getState');
|
|
162
|
+
__classPrivateFieldSet(this, _AccountTrackerController_isLocked, !isUnlocked, "f");
|
|
156
163
|
this.setIntervalLength(interval);
|
|
157
164
|
this.messenger.subscribe('AccountsController:selectedEvmAccountChange', (newAddress, prevAddress) => {
|
|
158
165
|
if (newAddress !== prevAddress) {
|
|
@@ -161,72 +168,52 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
161
168
|
this.refresh(__classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getNetworkClientIds).call(this));
|
|
162
169
|
}
|
|
163
170
|
}, (event) => event.address);
|
|
164
|
-
this.messenger.subscribe('NetworkController:networkAdded',
|
|
165
|
-
|
|
171
|
+
this.messenger.subscribe('NetworkController:networkAdded', (networkConfiguration) => {
|
|
172
|
+
const { networkClientId } = networkConfiguration.rpcEndpoints[networkConfiguration.defaultRpcEndpointIndex];
|
|
173
|
+
this.refresh([networkClientId]).catch(() => {
|
|
174
|
+
// Silently handle refresh errors
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
this.messenger.subscribe('KeyringController:unlock', () => {
|
|
178
|
+
__classPrivateFieldSet(this, _AccountTrackerController_isLocked, false, "f");
|
|
166
179
|
});
|
|
167
|
-
this.messenger.subscribe('KeyringController:
|
|
168
|
-
|
|
180
|
+
this.messenger.subscribe('KeyringController:lock', () => {
|
|
181
|
+
__classPrivateFieldSet(this, _AccountTrackerController_isLocked, true, "f");
|
|
169
182
|
});
|
|
170
|
-
this.messenger.subscribe('TransactionController:unapprovedTransactionAdded',
|
|
183
|
+
this.messenger.subscribe('TransactionController:unapprovedTransactionAdded', (transactionMeta) => {
|
|
171
184
|
const addresses = [transactionMeta.txParams.from];
|
|
172
185
|
if (transactionMeta.txParams.to) {
|
|
173
186
|
addresses.push(transactionMeta.txParams.to);
|
|
174
187
|
}
|
|
175
|
-
|
|
188
|
+
this.refreshAddresses({
|
|
176
189
|
networkClientIds: [transactionMeta.networkClientId],
|
|
177
190
|
addresses,
|
|
191
|
+
}).catch(() => {
|
|
192
|
+
// Silently handle refresh errors
|
|
178
193
|
});
|
|
179
194
|
});
|
|
180
|
-
this.messenger.subscribe('TransactionController:transactionConfirmed',
|
|
195
|
+
this.messenger.subscribe('TransactionController:transactionConfirmed', (transactionMeta) => {
|
|
181
196
|
const addresses = [transactionMeta.txParams.from];
|
|
182
197
|
if (transactionMeta.txParams.to) {
|
|
183
198
|
addresses.push(transactionMeta.txParams.to);
|
|
184
199
|
}
|
|
185
|
-
|
|
200
|
+
this.refreshAddresses({
|
|
186
201
|
networkClientIds: [transactionMeta.networkClientId],
|
|
187
202
|
addresses,
|
|
203
|
+
}).catch(() => {
|
|
204
|
+
// Silently handle refresh errors
|
|
188
205
|
});
|
|
189
206
|
});
|
|
190
207
|
__classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_registerMessageHandlers).call(this);
|
|
191
208
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
accountsByChainId[newChainId] = {};
|
|
201
|
-
existing.forEach((address) => {
|
|
202
|
-
accountsByChainId[newChainId][address] = { balance: '0x0' };
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
// Note: The address from the preferences controller are checksummed
|
|
207
|
-
// The addresses from the accounts controller are lowercased
|
|
208
|
-
const addresses = Object.values(this.messenger
|
|
209
|
-
.call('AccountsController:listAccounts')
|
|
210
|
-
.map((internalAccount) => (0, controller_utils_1.toChecksumHexAddress)(internalAccount.address)));
|
|
211
|
-
const newAddresses = addresses.filter((address) => !existing.includes(address));
|
|
212
|
-
const oldAddresses = existing.filter((address) => !addresses.includes(address));
|
|
213
|
-
Object.keys(accountsByChainId).forEach((chainId) => {
|
|
214
|
-
newAddresses.forEach((address) => {
|
|
215
|
-
accountsByChainId[chainId][address] = {
|
|
216
|
-
balance: '0x0',
|
|
217
|
-
};
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
Object.keys(accountsByChainId).forEach((chainId) => {
|
|
221
|
-
oldAddresses.forEach((address) => {
|
|
222
|
-
delete accountsByChainId[chainId][address];
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
if (!(0, lodash_1.isEqual)(this.state.accountsByChainId, accountsByChainId)) {
|
|
226
|
-
this.update((state) => {
|
|
227
|
-
state.accountsByChainId = accountsByChainId;
|
|
228
|
-
});
|
|
229
|
-
}
|
|
209
|
+
/**
|
|
210
|
+
* Whether the controller is active (keyring is unlocked and user is onboarded).
|
|
211
|
+
* When locked or not onboarded, balance updates should be skipped.
|
|
212
|
+
*
|
|
213
|
+
* @returns Whether the controller should perform balance updates.
|
|
214
|
+
*/
|
|
215
|
+
get isActive() {
|
|
216
|
+
return !__classPrivateFieldGet(this, _AccountTrackerController_isLocked, "f") && __classPrivateFieldGet(this, _AccountTrackerController_isOnboarded, "f").call(this);
|
|
230
217
|
}
|
|
231
218
|
/**
|
|
232
219
|
* Refreshes the balances of the accounts using the networkClientId
|
|
@@ -279,6 +266,10 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
279
266
|
* @returns accounts - addresses with synced balance
|
|
280
267
|
*/
|
|
281
268
|
async syncBalanceWithAddresses(addresses, networkClientId) {
|
|
269
|
+
// Skip balance fetching if locked or not onboarded to avoid unnecessary RPC calls
|
|
270
|
+
if (!this.isActive) {
|
|
271
|
+
return {};
|
|
272
|
+
}
|
|
282
273
|
const { ethQuery } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
283
274
|
// TODO: This should use multicall when enabled by the user.
|
|
284
275
|
return await Promise.all(addresses.map((address) => {
|
|
@@ -390,7 +381,45 @@ class AccountTrackerController extends (0, polling_controller_1.StaticIntervalPo
|
|
|
390
381
|
}
|
|
391
382
|
}
|
|
392
383
|
exports.AccountTrackerController = AccountTrackerController;
|
|
393
|
-
_AccountTrackerController_refreshMutex = new WeakMap(), _AccountTrackerController_includeStakedAssets = new WeakMap(), _AccountTrackerController_accountsApiChainIds = new WeakMap(), _AccountTrackerController_getStakedBalanceForChain = new WeakMap(), _AccountTrackerController_balanceFetchers = new WeakMap(), _AccountTrackerController_fetchingEnabled = new WeakMap(), _AccountTrackerController_getProvider = new WeakMap(), _AccountTrackerController_getNetworkClient = new WeakMap(), _AccountTrackerController_createAccountsApiFetcher = new WeakMap(), _AccountTrackerController_instances = new WeakSet(),
|
|
384
|
+
_AccountTrackerController_refreshMutex = new WeakMap(), _AccountTrackerController_includeStakedAssets = new WeakMap(), _AccountTrackerController_accountsApiChainIds = new WeakMap(), _AccountTrackerController_getStakedBalanceForChain = new WeakMap(), _AccountTrackerController_balanceFetchers = new WeakMap(), _AccountTrackerController_fetchingEnabled = new WeakMap(), _AccountTrackerController_isOnboarded = new WeakMap(), _AccountTrackerController_isLocked = new WeakMap(), _AccountTrackerController_getProvider = new WeakMap(), _AccountTrackerController_getNetworkClient = new WeakMap(), _AccountTrackerController_createAccountsApiFetcher = new WeakMap(), _AccountTrackerController_instances = new WeakSet(), _AccountTrackerController_syncAccounts = function _AccountTrackerController_syncAccounts(newChainIds) {
|
|
385
|
+
const accountsByChainId = (0, lodash_1.cloneDeep)(this.state.accountsByChainId);
|
|
386
|
+
const { selectedNetworkClientId } = this.messenger.call('NetworkController:getState');
|
|
387
|
+
const { configuration: { chainId: currentChainId }, } = this.messenger.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
|
388
|
+
const existing = Object.keys(accountsByChainId?.[currentChainId] ?? {});
|
|
389
|
+
// Initialize new chain IDs if they don't exist
|
|
390
|
+
newChainIds.forEach((newChainId) => {
|
|
391
|
+
if (!accountsByChainId[newChainId]) {
|
|
392
|
+
accountsByChainId[newChainId] = {};
|
|
393
|
+
existing.forEach((address) => {
|
|
394
|
+
accountsByChainId[newChainId][address] = { balance: '0x0' };
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
// Note: The address from the preferences controller are checksummed
|
|
399
|
+
// The addresses from the accounts controller are lowercased
|
|
400
|
+
const addresses = Object.values(this.messenger
|
|
401
|
+
.call('AccountsController:listAccounts')
|
|
402
|
+
.map((internalAccount) => (0, controller_utils_1.toChecksumHexAddress)(internalAccount.address)));
|
|
403
|
+
const newAddresses = addresses.filter((address) => !existing.includes(address));
|
|
404
|
+
const oldAddresses = existing.filter((address) => !addresses.includes(address));
|
|
405
|
+
Object.keys(accountsByChainId).forEach((chainId) => {
|
|
406
|
+
newAddresses.forEach((address) => {
|
|
407
|
+
accountsByChainId[chainId][address] = {
|
|
408
|
+
balance: '0x0',
|
|
409
|
+
};
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
Object.keys(accountsByChainId).forEach((chainId) => {
|
|
413
|
+
oldAddresses.forEach((address) => {
|
|
414
|
+
delete accountsByChainId[chainId][address];
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
if (!(0, lodash_1.isEqual)(this.state.accountsByChainId, accountsByChainId)) {
|
|
418
|
+
this.update((state) => {
|
|
419
|
+
state.accountsByChainId = accountsByChainId;
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}, _AccountTrackerController_getCorrectNetworkClient = function _AccountTrackerController_getCorrectNetworkClient(networkClientId) {
|
|
394
423
|
const selectedNetworkClientId = networkClientId ??
|
|
395
424
|
this.messenger.call('NetworkController:getState').selectedNetworkClientId;
|
|
396
425
|
const { configuration: { chainId }, provider, blockTracker, } = this.messenger.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
|
@@ -410,8 +439,8 @@ _AccountTrackerController_refreshMutex = new WeakMap(), _AccountTrackerControlle
|
|
|
410
439
|
const { chainId } = __classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_getCorrectNetworkClient).call(this, networkClientId);
|
|
411
440
|
return chainId;
|
|
412
441
|
});
|
|
413
|
-
this.
|
|
414
|
-
if (!__classPrivateFieldGet(this, _AccountTrackerController_fetchingEnabled, "f").call(this)) {
|
|
442
|
+
__classPrivateFieldGet(this, _AccountTrackerController_instances, "m", _AccountTrackerController_syncAccounts).call(this, chainIds);
|
|
443
|
+
if (!__classPrivateFieldGet(this, _AccountTrackerController_fetchingEnabled, "f").call(this) || !this.isActive) {
|
|
415
444
|
return;
|
|
416
445
|
}
|
|
417
446
|
// Use balance fetchers with fallback strategy
|
|
@@ -425,7 +454,7 @@ _AccountTrackerController_refreshMutex = new WeakMap(), _AccountTrackerControlle
|
|
|
425
454
|
}));
|
|
426
455
|
// Try each fetcher in order, removing successfully processed chains
|
|
427
456
|
for (const fetcher of __classPrivateFieldGet(this, _AccountTrackerController_balanceFetchers, "f")) {
|
|
428
|
-
const supportedChains = remainingChains.filter((
|
|
457
|
+
const supportedChains = remainingChains.filter((chainId) => fetcher.supports(chainId));
|
|
429
458
|
if (!supportedChains.length) {
|
|
430
459
|
continue;
|
|
431
460
|
}
|
|
@@ -1 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,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;gBAC/D,CAAC;gBACD,IACE,qBAAqB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,aAAa;oBACrD,aAAa,EACb,CAAC;oBACD,qBAAqB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,aAAa;wBACnD,aAAa,CAAC;oBAChB,UAAU,GAAG,IAAI,CAAC;gBACpB,CAAC;YACH,CAAC,CACF,CAAC;QACJ,CAAC,CACF,CAAC;QAEF,yCAAyC;QACzC,IAAI,UAAU,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,KAAK,CAAC,iBAAiB,GAAG,qBAAqB,CAAC;YAClD,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;YAAS,CAAC;QACT,WAAW,EAAE,CAAC;IAChB,CAAC;AACH,CAAC;IAmKC,IAAI,CAAC,SAAS,CAAC,qBAAqB,CAClC,GAAG,cAAc,uBAAgC,EACjD,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,CACrC,CAAC;IAEF,IAAI,CAAC,SAAS,CAAC,qBAAqB,CAClC,GAAG,cAAc,uBAAgC,EACjD,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,CACrC,CAAC;AACJ,CAAC;AAGH,kBAAe,wBAAwB,CAAC","sourcesContent":["import { Web3Provider } from '@ethersproject/providers';\nimport type {\n AccountsControllerSelectedEvmAccountChangeEvent,\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerListAccountsAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerStateChangeEvent,\n ControllerGetStateAction,\n StateMetadata,\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 { KeyringControllerUnlockEvent } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkClient,\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkControllerNetworkAddedEvent,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type {\n TransactionControllerTransactionConfirmedEvent,\n TransactionControllerUnapprovedTransactionAddedEvent,\n TransactionMeta,\n} from '@metamask/transaction-controller';\nimport { assert } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport { STAKING_CONTRACT_ADDRESS_BY_CHAINID } from './AssetsContractController';\nimport type {\n AssetsContractController,\n StakedBalance,\n} from './AssetsContractController';\nimport { AccountsApiBalanceFetcher } from './multi-chain-accounts-service/api-balance-fetcher';\nimport type {\n BalanceFetcher,\n ProcessedBalance,\n} from './multi-chain-accounts-service/api-balance-fetcher';\nimport { RpcBalanceFetcher } from './rpc-service/rpc-balance-fetcher';\n\n/**\n * The name of the {@link AccountTrackerController}.\n */\nconst controllerName = 'AccountTrackerController';\n\nexport type ChainIdHex = Hex;\nexport type ChecksumAddress = Hex;\n\nconst ZERO_ADDRESS =\n '0x0000000000000000000000000000000000000000' as ChecksumAddress;\n\n/**\n * Creates an RPC balance fetcher configured for AccountTracker use case.\n * Returns only native balances and staked balances (no token balances).\n *\n * @param getProvider - Function to get Web3Provider for a given chain ID\n * @param getNetworkClient - Function to get NetworkClient for a given chain ID\n * @param includeStakedAssets - Whether to include staked assets in the fetch\n * @returns BalanceFetcher configured to fetch only native and optionally staked balances\n */\nfunction createAccountTrackerRpcBalanceFetcher(\n getProvider: (chainId: Hex) => Web3Provider,\n getNetworkClient: (chainId: Hex) => NetworkClient,\n includeStakedAssets: boolean,\n): BalanceFetcher {\n // Provide empty tokens state to ensure only native and staked balances are fetched\n const getEmptyTokensState = () => ({\n allTokens: {},\n allDetectedTokens: {},\n });\n\n const rpcBalanceFetcher = new RpcBalanceFetcher(\n getProvider,\n getNetworkClient,\n getEmptyTokensState,\n );\n\n // Wrap the RpcBalanceFetcher to filter staked balances when not needed\n return {\n supports(_chainId: ChainIdHex): boolean {\n return rpcBalanceFetcher.supports();\n },\n\n async fetch(params) {\n const result = await rpcBalanceFetcher.fetch(params);\n\n if (!includeStakedAssets) {\n // Filter out staked balances from the results\n return {\n balances: result.balances.filter(\n (balance) => balance.token === ZERO_ADDRESS,\n ),\n unprocessedChainIds: result.unprocessedChainIds,\n };\n }\n\n return result;\n },\n };\n}\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: StateMetadata<AccountTrackerControllerState> = {\n accountsByChainId: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\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 action that can be performed to update multiple native token balances in batch.\n */\nexport type AccountTrackerUpdateNativeBalancesAction = {\n type: `${typeof controllerName}:updateNativeBalances`;\n handler: AccountTrackerController['updateNativeBalances'];\n};\n\n/**\n * The action that can be performed to update multiple staked balances in batch.\n */\nexport type AccountTrackerUpdateStakedBalancesAction = {\n type: `${typeof controllerName}:updateStakedBalances`;\n handler: AccountTrackerController['updateStakedBalances'];\n};\n\n/**\n * The actions that can be performed using the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerActions =\n | AccountTrackerControllerGetStateAction\n | AccountTrackerUpdateNativeBalancesAction\n | AccountTrackerUpdateStakedBalancesAction;\n\n/**\n * The messenger of the {@link AccountTrackerController} for communication.\n */\nexport type AllowedActions =\n | AccountsControllerListAccountsAction\n | {\n type: 'PreferencesController:getState';\n handler: () => { isMultiAccountBalancesEnabled: boolean };\n }\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 | TransactionControllerUnapprovedTransactionAddedEvent\n | TransactionControllerTransactionConfirmedEvent\n | NetworkControllerNetworkAddedEvent\n | KeyringControllerUnlockEvent;\n\n/**\n * The messenger of the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerMessenger = Messenger<\n typeof controllerName,\n AccountTrackerControllerActions | AllowedActions,\n AccountTrackerControllerEvents | AllowedEvents\n>;\n\n/** The input to start polling for the {@link AccountTrackerController} */\ntype AccountTrackerPollingInput = {\n networkClientIds: NetworkClientId[];\n queryAllAccounts?: boolean;\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 #accountsApiChainIds: () => ChainIdHex[];\n\n readonly #getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n\n readonly #balanceFetchers: BalanceFetcher[];\n\n readonly #fetchingEnabled: () => boolean;\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 messenger.\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 * @param options.accountsApiChainIds - Function that returns array of chainIds that should use Accounts-API strategy (if supported by API).\n * @param options.allowExternalServices - Disable external HTTP calls (privacy / offline mode).\n * @param options.fetchingEnabled - Function that returns whether the controller is fetching enabled.\n */\n constructor({\n interval = 10000,\n state,\n messenger,\n getStakedBalanceForChain,\n includeStakedAssets = false,\n accountsApiChainIds = () => [],\n allowExternalServices = () => true,\n fetchingEnabled = () => true,\n }: {\n interval?: number;\n state?: Partial<AccountTrackerControllerState>;\n messenger: AccountTrackerControllerMessenger;\n getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n includeStakedAssets?: boolean;\n accountsApiChainIds?: () => ChainIdHex[];\n allowExternalServices?: () => boolean;\n fetchingEnabled?: () => 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 this.#accountsApiChainIds = accountsApiChainIds;\n\n // Initialize balance fetchers - Strategy order: API first, then RPC fallback\n this.#balanceFetchers = [\n ...(accountsApiChainIds().length > 0 && allowExternalServices()\n ? [this.#createAccountsApiFetcher()]\n : []),\n createAccountTrackerRpcBalanceFetcher(\n this.#getProvider,\n this.#getNetworkClient,\n this.#includeStakedAssets,\n ),\n ];\n\n this.#fetchingEnabled = fetchingEnabled;\n\n this.setIntervalLength(interval);\n\n this.messenger.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 this.messenger.subscribe('NetworkController:networkAdded', async () => {\n await this.refresh(this.#getNetworkClientIds());\n });\n\n this.messenger.subscribe('KeyringController:unlock', async () => {\n await this.refresh(this.#getNetworkClientIds());\n });\n\n this.messenger.subscribe(\n 'TransactionController:unapprovedTransactionAdded',\n async (transactionMeta: TransactionMeta) => {\n const addresses = [transactionMeta.txParams.from];\n if (transactionMeta.txParams.to) {\n addresses.push(transactionMeta.txParams.to);\n }\n await this.refreshAddresses({\n networkClientIds: [transactionMeta.networkClientId],\n addresses,\n });\n },\n );\n\n this.messenger.subscribe(\n 'TransactionController:transactionConfirmed',\n async (transactionMeta: TransactionMeta) => {\n const addresses = [transactionMeta.txParams.from];\n if (transactionMeta.txParams.to) {\n addresses.push(transactionMeta.txParams.to);\n }\n await this.refreshAddresses({\n networkClientIds: [transactionMeta.networkClientId],\n addresses,\n });\n },\n );\n\n this.#registerMessageHandlers();\n }\n\n private syncAccounts(newChainIds: string[]) {\n const accountsByChainId = cloneDeep(this.state.accountsByChainId);\n const { selectedNetworkClientId } = this.messenger.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId: currentChainId },\n } = this.messenger.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.messenger\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 readonly #getProvider = (chainId: Hex): Web3Provider => {\n const { networkConfigurationsByChainId } = this.messenger.call(\n 'NetworkController:getState',\n );\n const cfg = networkConfigurationsByChainId[chainId];\n const { networkClientId } = cfg.rpcEndpoints[cfg.defaultRpcEndpointIndex];\n const client = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return new Web3Provider(client.provider);\n };\n\n readonly #getNetworkClient = (chainId: Hex) => {\n const { networkConfigurationsByChainId } = this.messenger.call(\n 'NetworkController:getState',\n );\n const cfg = networkConfigurationsByChainId[chainId];\n const { networkClientId } = cfg.rpcEndpoints[cfg.defaultRpcEndpointIndex];\n return this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n };\n\n /**\n * Creates an AccountsApiBalanceFetcher that only supports chains in the accountsApiChainIds array\n *\n * @returns A BalanceFetcher that wraps AccountsApiBalanceFetcher with chainId filtering\n */\n readonly #createAccountsApiFetcher = (): BalanceFetcher => {\n const originalFetcher = new AccountsApiBalanceFetcher(\n 'extension',\n this.#getProvider,\n );\n\n return {\n supports: (chainId: ChainIdHex): boolean => {\n // Only support chains that are both:\n // 1. In our specified accountsApiChainIds array\n // 2. Actually supported by the AccountsApi\n return (\n this.#accountsApiChainIds().includes(chainId) &&\n originalFetcher.supports(chainId)\n );\n },\n fetch: originalFetcher.fetch.bind(originalFetcher),\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 const selectedNetworkClientId =\n networkClientId ??\n this.messenger.call('NetworkController:getState').selectedNetworkClientId;\n const {\n configuration: { chainId },\n provider,\n blockTracker,\n } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n return {\n chainId,\n provider,\n ethQuery: new EthQuery(provider),\n blockTracker,\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.messenger.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 * @param input.queryAllAccounts - Whether to query all accounts or just the selected account\n */\n async _executePoll({\n networkClientIds,\n queryAllAccounts = false,\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, queryAllAccounts);\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 * @param queryAllAccounts - Whether to query all accounts or just the selected account\n */\n async refresh(\n networkClientIds: NetworkClientId[],\n queryAllAccounts: boolean = false,\n ) {\n const selectedAccount = this.messenger.call(\n 'AccountsController:getSelectedAccount',\n );\n const allAccounts = this.messenger.call('AccountsController:listAccounts');\n const { isMultiAccountBalancesEnabled } = this.messenger.call(\n 'PreferencesController:getState',\n );\n\n await this.#refreshAccounts({\n networkClientIds,\n queryAllAccounts: queryAllAccounts ?? isMultiAccountBalancesEnabled,\n selectedAccount: toChecksumHexAddress(\n selectedAccount.address,\n ) as ChecksumAddress,\n allAccounts,\n });\n }\n\n async refreshAddresses({\n networkClientIds,\n addresses,\n }: {\n networkClientIds: NetworkClientId[];\n addresses: string[];\n }) {\n const checksummedAddresses = addresses.map((address) =>\n toChecksumHexAddress(address),\n );\n\n const accounts = this.messenger\n .call('AccountsController:listAccounts')\n .filter((account) =>\n checksummedAddresses.includes(toChecksumHexAddress(account.address)),\n );\n\n await this.#refreshAccounts({\n networkClientIds,\n queryAllAccounts: true,\n selectedAccount: '0x0',\n allAccounts: accounts,\n });\n }\n\n async #refreshAccounts({\n networkClientIds,\n queryAllAccounts,\n selectedAccount,\n allAccounts,\n }: {\n networkClientIds: NetworkClientId[];\n queryAllAccounts: boolean;\n selectedAccount: ChecksumAddress;\n allAccounts: InternalAccount[];\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 if (!this.#fetchingEnabled()) {\n return;\n }\n\n // Use balance fetchers with fallback strategy\n const aggregated: ProcessedBalance[] = [];\n let remainingChains = [...chainIds] as ChainIdHex[];\n\n // Temporary normalization to lowercase for balance fetching to match TokenBalancesController and enable HTTP caching\n const lowerCaseSelectedAccount =\n selectedAccount.toLowerCase() as ChecksumAddress;\n const lowerCaseAllAccounts = allAccounts.map((account) => ({\n ...account,\n address: account.address.toLowerCase(),\n }));\n\n // Try each fetcher in order, removing successfully processed chains\n for (const fetcher of this.#balanceFetchers) {\n const supportedChains = remainingChains.filter((c) =>\n fetcher.supports(c),\n );\n if (!supportedChains.length) {\n continue;\n }\n\n try {\n const result = await fetcher.fetch({\n chainIds: supportedChains,\n queryAllAccounts,\n selectedAccount: lowerCaseSelectedAccount,\n allAccounts: lowerCaseAllAccounts,\n });\n\n if (result.balances && result.balances.length > 0) {\n aggregated.push(...result.balances);\n // Remove chains that were successfully processed\n const processedChains = new Set(\n result.balances.map((b) => b.chainId),\n );\n remainingChains = remainingChains.filter(\n (chain) => !processedChains.has(chain),\n );\n }\n\n // Add unprocessed chains back to remainingChains for next fetcher\n if (\n result.unprocessedChainIds &&\n result.unprocessedChainIds.length > 0\n ) {\n // Only add chains that were originally requested and aren't already in remainingChains\n const currentRemainingChains = remainingChains;\n const chainsToAdd = result.unprocessedChainIds.filter(\n (chainId) =>\n supportedChains.includes(chainId) &&\n !currentRemainingChains.includes(chainId),\n );\n remainingChains.push(...chainsToAdd);\n }\n } catch (error) {\n console.warn(\n `Balance fetcher failed for chains ${supportedChains.join(', ')}: ${String(error)}`,\n );\n // Continue to next fetcher (fallback)\n }\n\n // If all chains have been processed, break early\n if (remainingChains.length === 0) {\n break;\n }\n }\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 // Process the aggregated balance results\n const stakedBalancesByChainAndAddress: Record<\n string,\n Record<string, string>\n > = {};\n\n aggregated.forEach(({ success, value, account, token, chainId }) => {\n if (success && value !== undefined) {\n const checksumAddress = toChecksumHexAddress(account);\n const hexValue = `0x${value.toString(16)}`;\n\n if (token === ZERO_ADDRESS) {\n // Native balance\n // Ensure the account entry exists before accessing it\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n }\n if (!nextAccountsByChainId[chainId][checksumAddress]) {\n nextAccountsByChainId[chainId][checksumAddress] = {\n balance: '0x0',\n };\n }\n\n if (\n nextAccountsByChainId[chainId][checksumAddress].balance !==\n hexValue\n ) {\n nextAccountsByChainId[chainId][checksumAddress].balance =\n hexValue;\n hasChanges = true;\n }\n } else if (\n STAKING_CONTRACT_ADDRESS_BY_CHAINID[chainId]?.toLowerCase() ===\n token.toLowerCase()\n ) {\n // Staked balance (from staking contract address)\n if (!stakedBalancesByChainAndAddress[chainId]) {\n stakedBalancesByChainAndAddress[chainId] = {};\n }\n stakedBalancesByChainAndAddress[chainId][checksumAddress] =\n hexValue;\n }\n }\n });\n\n // Apply staked balances\n Object.entries(stakedBalancesByChainAndAddress).forEach(\n ([chainId, balancesByAddress]) => {\n Object.entries(balancesByAddress).forEach(\n ([address, stakedBalance]) => {\n // Ensure account structure exists\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n }\n if (!nextAccountsByChainId[chainId][address]) {\n nextAccountsByChainId[chainId][address] = { balance: '0x0' };\n }\n if (\n nextAccountsByChainId[chainId][address].stakedBalance !==\n stakedBalance\n ) {\n nextAccountsByChainId[chainId][address].stakedBalance =\n stakedBalance;\n hasChanges = true;\n }\n },\n );\n },\n );\n\n // Only update state if something changed\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n } finally {\n releaseLock();\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 /**\n * Updates the balances of multiple native tokens in a single batch operation.\n * This is more efficient than calling updateNativeToken multiple times as it\n * triggers only one state update.\n *\n * @param balances - Array of balance updates, each containing address, chainId, and balance.\n */\n updateNativeBalances(\n balances: { address: string; chainId: Hex; balance: Hex }[],\n ) {\n const nextAccountsByChainId = cloneDeep(this.state.accountsByChainId);\n let hasChanges = false;\n\n balances.forEach(({ address, chainId, balance }) => {\n const checksumAddress = toChecksumHexAddress(address);\n\n // Ensure the chainId exists in the state\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n hasChanges = true;\n }\n\n // Check if the address exists for this chain\n const accountExists = Boolean(\n nextAccountsByChainId[chainId][checksumAddress],\n );\n\n // Ensure the address exists for this chain\n if (!accountExists) {\n nextAccountsByChainId[chainId][checksumAddress] = {\n balance: '0x0',\n };\n hasChanges = true;\n }\n\n // Only update the balance if it has changed, or if this is a new account\n const currentBalance =\n nextAccountsByChainId[chainId][checksumAddress].balance;\n if (!accountExists || currentBalance !== balance) {\n nextAccountsByChainId[chainId][checksumAddress].balance = balance;\n hasChanges = true;\n }\n });\n\n // Only call update if there are actual changes\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n }\n\n /**\n * Updates the staked balances of multiple accounts in a single batch operation.\n * This is more efficient than updating staked balances individually as it\n * triggers only one state update.\n *\n * @param stakedBalances - Array of staked balance updates, each containing address, chainId, and stakedBalance.\n */\n updateStakedBalances(\n stakedBalances: {\n address: string;\n chainId: Hex;\n stakedBalance: StakedBalance;\n }[],\n ) {\n const nextAccountsByChainId = cloneDeep(this.state.accountsByChainId);\n let hasChanges = false;\n\n stakedBalances.forEach(({ address, chainId, stakedBalance }) => {\n const checksumAddress = toChecksumHexAddress(address);\n\n // Ensure the chainId exists in the state\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n hasChanges = true;\n }\n\n // Check if the address exists for this chain\n const accountExists = Boolean(\n nextAccountsByChainId[chainId][checksumAddress],\n );\n\n // Ensure the address exists for this chain\n if (!accountExists) {\n nextAccountsByChainId[chainId][checksumAddress] = {\n balance: '0x0',\n };\n hasChanges = true;\n }\n\n // Only update the staked balance if it has changed, or if this is a new account\n const currentStakedBalance =\n nextAccountsByChainId[chainId][checksumAddress].stakedBalance;\n if (!accountExists || !isEqual(currentStakedBalance, stakedBalance)) {\n nextAccountsByChainId[chainId][checksumAddress].stakedBalance =\n stakedBalance;\n hasChanges = true;\n }\n });\n\n // Only call update if there are actual changes\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n }\n\n #registerMessageHandlers() {\n this.messenger.registerActionHandler(\n `${controllerName}:updateNativeBalances` as const,\n this.updateNativeBalances.bind(this),\n );\n\n this.messenger.registerActionHandler(\n `${controllerName}:updateStakedBalances` as const,\n this.updateStakedBalances.bind(this),\n );\n }\n}\n\nexport default AccountTrackerController;\n"]}
|
|
1
|
+
{"version":3,"file":"AccountTrackerController.cjs","sourceRoot":"","sources":["../src/AccountTrackerController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,wDAAwD;AAWxD,iEAIoC;AACpC,oEAA2C;AAe3C,qEAA+E;AAM/E,2CAAyC;AAEzC,6CAAoC;AACpC,mCAA4C;AAE5C,6EAAiF;AAKjF,gGAA+F;AAM/F,+EAAsE;AAEtE;;GAEG;AACH,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAKlD,MAAM,YAAY,GAChB,4CAA+D,CAAC;AAElE;;;;;;;;GAQG;AACH,SAAS,qCAAqC,CAC5C,WAA2C,EAC3C,gBAAiD,EACjD,mBAA4B;IAE5B,mFAAmF;IACnF,MAAM,mBAAmB,GAAG,GAG1B,EAAE,CAAC,CAAC;QACJ,SA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,wBAAwB,CAAC","sourcesContent":["import { Web3Provider } from '@ethersproject/providers';\nimport type {\n AccountsControllerSelectedEvmAccountChangeEvent,\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerListAccountsAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerStateChangeEvent,\n ControllerGetStateAction,\n StateMetadata,\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 KeyringControllerGetStateAction,\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkClient,\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkControllerNetworkAddedEvent,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type {\n TransactionControllerTransactionConfirmedEvent,\n TransactionControllerUnapprovedTransactionAddedEvent,\n TransactionMeta,\n} from '@metamask/transaction-controller';\nimport { assert } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport { STAKING_CONTRACT_ADDRESS_BY_CHAINID } from './AssetsContractController';\nimport type {\n AssetsContractController,\n StakedBalance,\n} from './AssetsContractController';\nimport { AccountsApiBalanceFetcher } from './multi-chain-accounts-service/api-balance-fetcher';\nimport type {\n BalanceFetcher,\n BalanceFetchResult,\n ProcessedBalance,\n} from './multi-chain-accounts-service/api-balance-fetcher';\nimport { RpcBalanceFetcher } from './rpc-service/rpc-balance-fetcher';\n\n/**\n * The name of the {@link AccountTrackerController}.\n */\nconst controllerName = 'AccountTrackerController';\n\nexport type ChainIdHex = Hex;\nexport type ChecksumAddress = Hex;\n\nconst ZERO_ADDRESS =\n '0x0000000000000000000000000000000000000000' as ChecksumAddress;\n\n/**\n * Creates an RPC balance fetcher configured for AccountTracker use case.\n * Returns only native balances and staked balances (no token balances).\n *\n * @param getProvider - Function to get Web3Provider for a given chain ID\n * @param getNetworkClient - Function to get NetworkClient for a given chain ID\n * @param includeStakedAssets - Whether to include staked assets in the fetch\n * @returns BalanceFetcher configured to fetch only native and optionally staked balances\n */\nfunction createAccountTrackerRpcBalanceFetcher(\n getProvider: (chainId: Hex) => Web3Provider,\n getNetworkClient: (chainId: Hex) => NetworkClient,\n includeStakedAssets: boolean,\n): BalanceFetcher {\n // Provide empty tokens state to ensure only native and staked balances are fetched\n const getEmptyTokensState = (): {\n allTokens: Record<string, never>;\n allDetectedTokens: Record<string, never>;\n } => ({\n allTokens: {},\n allDetectedTokens: {},\n });\n\n const rpcBalanceFetcher = new RpcBalanceFetcher(\n getProvider,\n getNetworkClient,\n getEmptyTokensState,\n );\n\n // Wrap the RpcBalanceFetcher to filter staked balances when not needed\n return {\n supports(_chainId: ChainIdHex): boolean {\n return rpcBalanceFetcher.supports();\n },\n\n async fetch(\n params: Parameters<BalanceFetcher['fetch']>[0],\n ): Promise<BalanceFetchResult> {\n const result = await rpcBalanceFetcher.fetch(params);\n\n if (!includeStakedAssets) {\n // Filter out staked balances from the results\n return {\n balances: result.balances.filter(\n (balance) => balance.token === ZERO_ADDRESS,\n ),\n unprocessedChainIds: result.unprocessedChainIds,\n };\n }\n\n return result;\n },\n };\n}\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: StateMetadata<AccountTrackerControllerState> = {\n accountsByChainId: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\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 action that can be performed to update multiple native token balances in batch.\n */\nexport type AccountTrackerUpdateNativeBalancesAction = {\n type: `${typeof controllerName}:updateNativeBalances`;\n handler: AccountTrackerController['updateNativeBalances'];\n};\n\n/**\n * The action that can be performed to update multiple staked balances in batch.\n */\nexport type AccountTrackerUpdateStakedBalancesAction = {\n type: `${typeof controllerName}:updateStakedBalances`;\n handler: AccountTrackerController['updateStakedBalances'];\n};\n\n/**\n * The actions that can be performed using the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerActions =\n | AccountTrackerControllerGetStateAction\n | AccountTrackerUpdateNativeBalancesAction\n | AccountTrackerUpdateStakedBalancesAction;\n\n/**\n * The messenger of the {@link AccountTrackerController} for communication.\n */\nexport type AllowedActions =\n | AccountsControllerListAccountsAction\n | {\n type: 'PreferencesController:getState';\n handler: () => { isMultiAccountBalancesEnabled: boolean };\n }\n | AccountsControllerGetSelectedAccountAction\n | NetworkControllerGetStateAction\n | NetworkControllerGetNetworkClientByIdAction\n | KeyringControllerGetStateAction;\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 | TransactionControllerUnapprovedTransactionAddedEvent\n | TransactionControllerTransactionConfirmedEvent\n | NetworkControllerNetworkAddedEvent\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent;\n\n/**\n * The messenger of the {@link AccountTrackerController}.\n */\nexport type AccountTrackerControllerMessenger = Messenger<\n typeof controllerName,\n AccountTrackerControllerActions | AllowedActions,\n AccountTrackerControllerEvents | AllowedEvents\n>;\n\n/** The input to start polling for the {@link AccountTrackerController} */\ntype AccountTrackerPollingInput = {\n networkClientIds: NetworkClientId[];\n queryAllAccounts?: boolean;\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 #accountsApiChainIds: () => ChainIdHex[];\n\n readonly #getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n\n readonly #balanceFetchers: BalanceFetcher[];\n\n readonly #fetchingEnabled: () => boolean;\n\n readonly #isOnboarded: () => boolean;\n\n /** Track if the keyring is locked */\n #isLocked = true;\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 messenger.\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 * @param options.accountsApiChainIds - Function that returns array of chainIds that should use Accounts-API strategy (if supported by API).\n * @param options.allowExternalServices - Disable external HTTP calls (privacy / offline mode).\n * @param options.fetchingEnabled - Function that returns whether the controller is fetching enabled.\n * @param options.isOnboarded - Whether the user has completed onboarding. If false, balance updates are skipped.\n */\n constructor({\n interval = 10000,\n state,\n messenger,\n getStakedBalanceForChain,\n includeStakedAssets = false,\n accountsApiChainIds = (): ChainIdHex[] => [],\n allowExternalServices = (): boolean => true,\n fetchingEnabled = (): boolean => true,\n isOnboarded = (): boolean => true,\n }: {\n interval?: number;\n state?: Partial<AccountTrackerControllerState>;\n messenger: AccountTrackerControllerMessenger;\n getStakedBalanceForChain: AssetsContractController['getStakedBalanceForChain'];\n includeStakedAssets?: boolean;\n accountsApiChainIds?: () => ChainIdHex[];\n allowExternalServices?: () => boolean;\n fetchingEnabled?: () => boolean;\n isOnboarded?: () => 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 this.#accountsApiChainIds = accountsApiChainIds;\n\n // Initialize balance fetchers - Strategy order: API first, then RPC fallback\n this.#balanceFetchers = [\n ...(accountsApiChainIds().length > 0 && allowExternalServices()\n ? [this.#createAccountsApiFetcher()]\n : []),\n createAccountTrackerRpcBalanceFetcher(\n this.#getProvider,\n this.#getNetworkClient,\n this.#includeStakedAssets,\n ),\n ];\n\n this.#fetchingEnabled = fetchingEnabled;\n this.#isOnboarded = isOnboarded;\n\n const { isUnlocked } = this.messenger.call('KeyringController:getState');\n this.#isLocked = !isUnlocked;\n\n this.setIntervalLength(interval);\n\n this.messenger.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 this.messenger.subscribe(\n 'NetworkController:networkAdded',\n (networkConfiguration) => {\n const { networkClientId } =\n networkConfiguration.rpcEndpoints[\n networkConfiguration.defaultRpcEndpointIndex\n ];\n this.refresh([networkClientId]).catch(() => {\n // Silently handle refresh errors\n });\n },\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n this.#isLocked = false;\n });\n\n this.messenger.subscribe('KeyringController:lock', () => {\n this.#isLocked = true;\n });\n\n this.messenger.subscribe(\n 'TransactionController:unapprovedTransactionAdded',\n (transactionMeta: TransactionMeta) => {\n const addresses = [transactionMeta.txParams.from];\n if (transactionMeta.txParams.to) {\n addresses.push(transactionMeta.txParams.to);\n }\n this.refreshAddresses({\n networkClientIds: [transactionMeta.networkClientId],\n addresses,\n }).catch(() => {\n // Silently handle refresh errors\n });\n },\n );\n\n this.messenger.subscribe(\n 'TransactionController:transactionConfirmed',\n (transactionMeta: TransactionMeta) => {\n const addresses = [transactionMeta.txParams.from];\n if (transactionMeta.txParams.to) {\n addresses.push(transactionMeta.txParams.to);\n }\n this.refreshAddresses({\n networkClientIds: [transactionMeta.networkClientId],\n addresses,\n }).catch(() => {\n // Silently handle refresh errors\n });\n },\n );\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Whether the controller is active (keyring is unlocked and user is onboarded).\n * When locked or not onboarded, balance updates should be skipped.\n *\n * @returns Whether the controller should perform balance updates.\n */\n get isActive(): boolean {\n return !this.#isLocked && this.#isOnboarded();\n }\n\n #syncAccounts(newChainIds: string[]): void {\n const accountsByChainId = cloneDeep(this.state.accountsByChainId);\n const { selectedNetworkClientId } = this.messenger.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId: currentChainId },\n } = this.messenger.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.messenger\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 readonly #getProvider = (chainId: Hex): Web3Provider => {\n const { networkConfigurationsByChainId } = this.messenger.call(\n 'NetworkController:getState',\n );\n const networkConfig = networkConfigurationsByChainId[chainId];\n const { networkClientId } =\n networkConfig.rpcEndpoints[networkConfig.defaultRpcEndpointIndex];\n const client = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return new Web3Provider(client.provider);\n };\n\n readonly #getNetworkClient = (chainId: Hex): NetworkClient => {\n const { networkConfigurationsByChainId } = this.messenger.call(\n 'NetworkController:getState',\n );\n const networkConfig = networkConfigurationsByChainId[chainId];\n const { networkClientId } =\n networkConfig.rpcEndpoints[networkConfig.defaultRpcEndpointIndex];\n return this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n };\n\n /**\n * Creates an AccountsApiBalanceFetcher that only supports chains in the accountsApiChainIds array\n *\n * @returns A BalanceFetcher that wraps AccountsApiBalanceFetcher with chainId filtering\n */\n readonly #createAccountsApiFetcher = (): BalanceFetcher => {\n const originalFetcher = new AccountsApiBalanceFetcher(\n 'extension',\n this.#getProvider,\n );\n\n return {\n supports: (chainId: ChainIdHex): boolean => {\n // Only support chains that are both:\n // 1. In our specified accountsApiChainIds array\n // 2. Actually supported by the AccountsApi\n return (\n this.#accountsApiChainIds().includes(chainId) &&\n originalFetcher.supports(chainId)\n );\n },\n fetch: originalFetcher.fetch.bind(originalFetcher),\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: Hex;\n provider: NetworkClient['provider'];\n ethQuery: EthQuery;\n blockTracker: NetworkClient['blockTracker'];\n } {\n const selectedNetworkClientId =\n networkClientId ??\n this.messenger.call('NetworkController:getState').selectedNetworkClientId;\n const {\n configuration: { chainId },\n provider,\n blockTracker,\n } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n return {\n chainId,\n provider,\n ethQuery: new EthQuery(provider),\n blockTracker,\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.messenger.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 * @param input.queryAllAccounts - Whether to query all accounts or just the selected account\n */\n async _executePoll({\n networkClientIds,\n queryAllAccounts = false,\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, queryAllAccounts);\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 * @param queryAllAccounts - Whether to query all accounts or just the selected account\n */\n async refresh(\n networkClientIds: NetworkClientId[],\n queryAllAccounts: boolean = false,\n ): Promise<void> {\n const selectedAccount = this.messenger.call(\n 'AccountsController:getSelectedAccount',\n );\n const allAccounts = this.messenger.call('AccountsController:listAccounts');\n const { isMultiAccountBalancesEnabled } = this.messenger.call(\n 'PreferencesController:getState',\n );\n\n await this.#refreshAccounts({\n networkClientIds,\n queryAllAccounts: queryAllAccounts ?? isMultiAccountBalancesEnabled,\n selectedAccount: toChecksumHexAddress(\n selectedAccount.address,\n ) as ChecksumAddress,\n allAccounts,\n });\n }\n\n async refreshAddresses({\n networkClientIds,\n addresses,\n }: {\n networkClientIds: NetworkClientId[];\n addresses: string[];\n }): Promise<void> {\n const checksummedAddresses = addresses.map((address) =>\n toChecksumHexAddress(address),\n );\n\n const accounts = this.messenger\n .call('AccountsController:listAccounts')\n .filter((account) =>\n checksummedAddresses.includes(toChecksumHexAddress(account.address)),\n );\n\n await this.#refreshAccounts({\n networkClientIds,\n queryAllAccounts: true,\n selectedAccount: '0x0',\n allAccounts: accounts,\n });\n }\n\n async #refreshAccounts({\n networkClientIds,\n queryAllAccounts,\n selectedAccount,\n allAccounts,\n }: {\n networkClientIds: NetworkClientId[];\n queryAllAccounts: boolean;\n selectedAccount: ChecksumAddress;\n allAccounts: InternalAccount[];\n }): Promise<void> {\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 if (!this.#fetchingEnabled() || !this.isActive) {\n return;\n }\n\n // Use balance fetchers with fallback strategy\n const aggregated: ProcessedBalance[] = [];\n let remainingChains = [...chainIds] as ChainIdHex[];\n\n // Temporary normalization to lowercase for balance fetching to match TokenBalancesController and enable HTTP caching\n const lowerCaseSelectedAccount =\n selectedAccount.toLowerCase() as ChecksumAddress;\n const lowerCaseAllAccounts = allAccounts.map((account) => ({\n ...account,\n address: account.address.toLowerCase(),\n }));\n\n // Try each fetcher in order, removing successfully processed chains\n for (const fetcher of this.#balanceFetchers) {\n const supportedChains = remainingChains.filter((chainId) =>\n fetcher.supports(chainId),\n );\n if (!supportedChains.length) {\n continue;\n }\n\n try {\n const result = await fetcher.fetch({\n chainIds: supportedChains,\n queryAllAccounts,\n selectedAccount: lowerCaseSelectedAccount,\n allAccounts: lowerCaseAllAccounts,\n });\n\n if (result.balances && result.balances.length > 0) {\n aggregated.push(...result.balances);\n // Remove chains that were successfully processed\n const processedChains = new Set(\n result.balances.map((b) => b.chainId),\n );\n remainingChains = remainingChains.filter(\n (chain) => !processedChains.has(chain),\n );\n }\n\n // Add unprocessed chains back to remainingChains for next fetcher\n if (\n result.unprocessedChainIds &&\n result.unprocessedChainIds.length > 0\n ) {\n // Only add chains that were originally requested and aren't already in remainingChains\n const currentRemainingChains = remainingChains;\n const chainsToAdd = result.unprocessedChainIds.filter(\n (chainId) =>\n supportedChains.includes(chainId) &&\n !currentRemainingChains.includes(chainId),\n );\n remainingChains.push(...chainsToAdd);\n }\n } catch (error) {\n console.warn(\n `Balance fetcher failed for chains ${supportedChains.join(', ')}: ${String(error)}`,\n );\n // Continue to next fetcher (fallback)\n }\n\n // If all chains have been processed, break early\n if (remainingChains.length === 0) {\n break;\n }\n }\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 // Process the aggregated balance results\n const stakedBalancesByChainAndAddress: Record<\n string,\n Record<string, string>\n > = {};\n\n aggregated.forEach(({ success, value, account, token, chainId }) => {\n if (success && value !== undefined) {\n const checksumAddress = toChecksumHexAddress(account);\n const hexValue = `0x${value.toString(16)}`;\n\n if (token === ZERO_ADDRESS) {\n // Native balance\n // Ensure the account entry exists before accessing it\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n }\n if (!nextAccountsByChainId[chainId][checksumAddress]) {\n nextAccountsByChainId[chainId][checksumAddress] = {\n balance: '0x0',\n };\n }\n\n if (\n nextAccountsByChainId[chainId][checksumAddress].balance !==\n hexValue\n ) {\n nextAccountsByChainId[chainId][checksumAddress].balance =\n hexValue;\n hasChanges = true;\n }\n } else if (\n STAKING_CONTRACT_ADDRESS_BY_CHAINID[chainId]?.toLowerCase() ===\n token.toLowerCase()\n ) {\n // Staked balance (from staking contract address)\n if (!stakedBalancesByChainAndAddress[chainId]) {\n stakedBalancesByChainAndAddress[chainId] = {};\n }\n stakedBalancesByChainAndAddress[chainId][checksumAddress] =\n hexValue;\n }\n }\n });\n\n // Apply staked balances\n Object.entries(stakedBalancesByChainAndAddress).forEach(\n ([chainId, balancesByAddress]) => {\n Object.entries(balancesByAddress).forEach(\n ([address, stakedBalance]) => {\n // Ensure account structure exists\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n }\n if (!nextAccountsByChainId[chainId][address]) {\n nextAccountsByChainId[chainId][address] = { balance: '0x0' };\n }\n if (\n nextAccountsByChainId[chainId][address].stakedBalance !==\n stakedBalance\n ) {\n nextAccountsByChainId[chainId][address].stakedBalance =\n stakedBalance;\n hasChanges = true;\n }\n },\n );\n },\n );\n\n // Only update state if something changed\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n } finally {\n releaseLock();\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 // Skip balance fetching if locked or not onboarded to avoid unnecessary RPC calls\n if (!this.isActive) {\n return {};\n }\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 /**\n * Updates the balances of multiple native tokens in a single batch operation.\n * This is more efficient than calling updateNativeToken multiple times as it\n * triggers only one state update.\n *\n * @param balances - Array of balance updates, each containing address, chainId, and balance.\n */\n updateNativeBalances(\n balances: { address: string; chainId: Hex; balance: Hex }[],\n ): void {\n const nextAccountsByChainId = cloneDeep(this.state.accountsByChainId);\n let hasChanges = false;\n\n balances.forEach(({ address, chainId, balance }) => {\n const checksumAddress = toChecksumHexAddress(address);\n\n // Ensure the chainId exists in the state\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n hasChanges = true;\n }\n\n // Check if the address exists for this chain\n const accountExists = Boolean(\n nextAccountsByChainId[chainId][checksumAddress],\n );\n\n // Ensure the address exists for this chain\n if (!accountExists) {\n nextAccountsByChainId[chainId][checksumAddress] = {\n balance: '0x0',\n };\n hasChanges = true;\n }\n\n // Only update the balance if it has changed, or if this is a new account\n const currentBalance =\n nextAccountsByChainId[chainId][checksumAddress].balance;\n if (!accountExists || currentBalance !== balance) {\n nextAccountsByChainId[chainId][checksumAddress].balance = balance;\n hasChanges = true;\n }\n });\n\n // Only call update if there are actual changes\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n }\n\n /**\n * Updates the staked balances of multiple accounts in a single batch operation.\n * This is more efficient than updating staked balances individually as it\n * triggers only one state update.\n *\n * @param stakedBalances - Array of staked balance updates, each containing address, chainId, and stakedBalance.\n */\n updateStakedBalances(\n stakedBalances: {\n address: string;\n chainId: Hex;\n stakedBalance: StakedBalance;\n }[],\n ): void {\n const nextAccountsByChainId = cloneDeep(this.state.accountsByChainId);\n let hasChanges = false;\n\n stakedBalances.forEach(({ address, chainId, stakedBalance }) => {\n const checksumAddress = toChecksumHexAddress(address);\n\n // Ensure the chainId exists in the state\n if (!nextAccountsByChainId[chainId]) {\n nextAccountsByChainId[chainId] = {};\n hasChanges = true;\n }\n\n // Check if the address exists for this chain\n const accountExists = Boolean(\n nextAccountsByChainId[chainId][checksumAddress],\n );\n\n // Ensure the address exists for this chain\n if (!accountExists) {\n nextAccountsByChainId[chainId][checksumAddress] = {\n balance: '0x0',\n };\n hasChanges = true;\n }\n\n // Only update the staked balance if it has changed, or if this is a new account\n const currentStakedBalance =\n nextAccountsByChainId[chainId][checksumAddress].stakedBalance;\n if (!accountExists || !isEqual(currentStakedBalance, stakedBalance)) {\n nextAccountsByChainId[chainId][checksumAddress].stakedBalance =\n stakedBalance;\n hasChanges = true;\n }\n });\n\n // Only call update if there are actual changes\n if (hasChanges) {\n this.update((state) => {\n state.accountsByChainId = nextAccountsByChainId;\n });\n }\n }\n\n #registerMessageHandlers(): void {\n this.messenger.registerActionHandler(\n `${controllerName}:updateNativeBalances` as const,\n this.updateNativeBalances.bind(this),\n );\n\n this.messenger.registerActionHandler(\n `${controllerName}:updateStakedBalances` as const,\n this.updateStakedBalances.bind(this),\n );\n }\n}\n\nexport default AccountTrackerController;\n"]}
|