diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index e15a97273..b4d89d85f 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -902,6 +902,7 @@ export class MainController extends EventEmitter { const isUpdatingAccount = this.accounts.statuses.updateAccountState !== 'INITIAL' + this.selectedAccount.resetSelectedAccountPortfolio() await Promise.all([ // When we trigger `reloadSelectedAccount` (for instance, from Dashboard -> Refresh balance icon), // it's very likely that the account state is already in the process of being updated. diff --git a/src/controllers/selectedAccount/selectedAccount.test.ts b/src/controllers/selectedAccount/selectedAccount.test.ts index 095ef023e..29c21a2bd 100644 --- a/src/controllers/selectedAccount/selectedAccount.test.ts +++ b/src/controllers/selectedAccount/selectedAccount.test.ts @@ -16,7 +16,7 @@ import { DefiPositionsController } from '../defiPositions/defiPositions' import { NetworksController } from '../networks/networks' import { PortfolioController } from '../portfolio/portfolio' import { ProvidersController } from '../providers/providers' -import { SelectedAccountController } from './selectedAccount' +import { DEFAULT_SELECTED_ACCOUNT_PORTFOLIO, SelectedAccountController } from './selectedAccount' // @ts-ignore global.structuredClone = structuredClone as any @@ -152,6 +152,10 @@ describe('SelectedAccount Controller', () => { }) portfolioCtrl.updateSelectedAccount('0x77777777789A8BBEE6C64381e5E89E501fb0e4c8') }) + test('should rest selected account portfolio', () => { + selectedAccountCtrl.resetSelectedAccountPortfolio() + expect(selectedAccountCtrl.portfolio).toEqual(DEFAULT_SELECTED_ACCOUNT_PORTFOLIO) + }) test('should toJSON()', () => { const json = selectedAccountCtrl.toJSON() expect(json).toBeDefined() diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index 787058cb7..a70f23a47 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -17,7 +17,7 @@ import EventEmitter from '../eventEmitter/eventEmitter' // eslint-disable-next-line import/no-cycle import { PortfolioController } from '../portfolio/portfolio' -const DEFAULT_SELECTED_ACCOUNT_PORTFOLIO = { +export const DEFAULT_SELECTED_ACCOUNT_PORTFOLIO = { tokens: [], collections: [], totalBalance: 0, @@ -123,7 +123,7 @@ export class SelectedAccountController extends EventEmitter { async setAccount(account: Account | null) { this.account = account - this.portfolio = DEFAULT_SELECTED_ACCOUNT_PORTFOLIO + this.resetSelectedAccountPortfolio(true) if (!account) { await this.#storage.remove('selectedAccount') @@ -134,6 +134,14 @@ export class SelectedAccountController extends EventEmitter { this.emitUpdate() } + resetSelectedAccountPortfolio(skipUpdate?: boolean) { + this.portfolio = DEFAULT_SELECTED_ACCOUNT_PORTFOLIO + + if (!skipUpdate) { + this.emitUpdate() + } + } + #updateSelectedAccountPortfolio(skipUpdate?: boolean) { if (!this.#portfolio || !this.#defiPositions || !this.account) return @@ -171,8 +179,6 @@ export class SelectedAccountController extends EventEmitter { (!this.portfolio?.tokens?.length && newSelectedAccountPortfolio.tokens.length) ) { this.portfolio = newSelectedAccountPortfolio - } else { - this.portfolio.isAllReady = false } if (!skipUpdate) {