Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes / Portfolio Update Loading State #1096

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/controllers/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/selectedAccount/selectedAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 10 additions & 4 deletions src/controllers/selectedAccount/selectedAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
Loading