diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index a6108aa0b18..9c7c1dfee12 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fetch balances when switching account groups, enabling RPC-only networks, or after a new account is added to the account tree ([#9388](https://github.com/MetaMask/core/pull/9388)) + ## [10.0.1] ### Changed diff --git a/packages/assets-controller/src/AssetsController.ts b/packages/assets-controller/src/AssetsController.ts index cd16591df81..9e99e94d7c1 100644 --- a/packages/assets-controller/src/AssetsController.ts +++ b/packages/assets-controller/src/AssetsController.ts @@ -1329,10 +1329,14 @@ export class AssetsController extends BaseController< currentCount: currentIds.size, }); + const newAccounts = accounts.filter( + (account) => !this.#lastKnownAccountIds.has(account.id), + ); + this.#lastKnownAccountIds = currentIds; this.#ensureNativeBalancesDefaultZero(); this.#ensureDefaultTrackedAssetsSeeded(); - this.#runAccountTreeRefresh(accounts).catch((error) => { + this.#runAccountTreeRefresh(accounts, newAccounts).catch((error) => { log('Failed to refresh assets after tree change', error); }); } else { @@ -1340,7 +1344,10 @@ export class AssetsController extends BaseController< } } - async #runAccountTreeRefresh(accounts: InternalAccount[]): Promise { + async #runAccountTreeRefresh( + accounts: InternalAccount[], + newAccounts: InternalAccount[] = [], + ): Promise { const releaseLock = await this.#accountRefreshMutex.acquire(); try { await this.getAssets(accounts, { @@ -1348,6 +1355,12 @@ export class AssetsController extends BaseController< forceUpdate: true, }); this.#subscribeAssets(); + if (newAccounts.length > 0) { + await this.getAssets(newAccounts, { + chainIds: [...this.#enabledChains], + forceUpdate: true, + }); + } } catch (error) { log('Failed to fetch assets after tree change', error); this.#subscribeAssets(); @@ -3364,8 +3377,9 @@ export class AssetsController extends BaseController< this.#subscribeAssets(); // Do one-time fetch for newly enabled chains; merge so we keep existing chain balances - if (addedChains.length > 0 && this.#getSelectedAccounts().length > 0) { - await this.getAssets(this.#getSelectedAccounts(), { + const accounts = this.#getSelectedAccounts(); + if (addedChains.length > 0 && accounts.length > 0) { + await this.getAssets(accounts, { chainIds: addedChains, forceUpdate: true, updateMode: 'merge',