From 39681d77ac0b73637842974ffe707823b9e58d2a Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 11:50:25 +0200 Subject: [PATCH 1/7] make defiPositions private in the main controller to prevent its stringified state from being sent to the frontend, improving performance --- .../defiPositions/defiPositions.ts | 17 +------- src/controllers/main/main.ts | 16 ++++---- .../selectedAccount/selectedAccount.test.ts | 4 +- .../selectedAccount/selectedAccount.ts | 39 ++++++++++++++++++- 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/controllers/defiPositions/defiPositions.ts b/src/controllers/defiPositions/defiPositions.ts index ff66690ba..b148a1d4e 100644 --- a/src/controllers/defiPositions/defiPositions.ts +++ b/src/controllers/defiPositions/defiPositions.ts @@ -1,7 +1,5 @@ import { Fetch } from '../../interfaces/fetch' import { NetworkId } from '../../interfaces/network' -// eslint-disable-next-line import/no-cycle -import { getNetworksWithDeFiPositionsErrorBanners } from '../../libs/banners/banners' import { getAssetValue } from '../../libs/defiPositions/helpers' import { getAAVEPositions, getUniV3Positions } from '../../libs/defiPositions/providers' import { @@ -252,23 +250,10 @@ export class DefiPositionsController extends EventEmitter { } } - get banners() { - if (!this.#selectedAccount.account) return [] - - const errorBanners = getNetworksWithDeFiPositionsErrorBanners({ - networks: this.#networks.networks, - currentAccountState: this.state[this.#selectedAccount.account.addr], - providers: this.#providers.providers - }) - - return errorBanners - } - toJSON() { return { ...this, - ...super.toJSON(), - banners: this.banners + ...super.toJSON() } } } diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index e15a97273..b290cb160 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -146,7 +146,7 @@ export class MainController extends EventEmitter { portfolio: PortfolioController - defiPositions: DefiPositionsController + #defiPositions: DefiPositionsController dapps: DappsController @@ -234,7 +234,7 @@ export class MainController extends EventEmitter { this.providers.setProvider(network) await this.accounts.updateAccountStates('latest', [network.id]) await this.updateSelectedAccountPortfolio(true) - await this.defiPositions.updatePositions(network.id) + await this.#defiPositions.updatePositions(network.id) }, (networkId: NetworkId) => { this.providers.removeProvider(networkId) @@ -271,7 +271,7 @@ export class MainController extends EventEmitter { relayerUrl, velcroUrl ) - this.defiPositions = new DefiPositionsController({ + this.#defiPositions = new DefiPositionsController({ fetch: this.fetch, selectedAccount: this.selectedAccount, networks: this.networks, @@ -313,7 +313,7 @@ export class MainController extends EventEmitter { }) this.selectedAccount.initControllers({ portfolio: this.portfolio, - defiPositions: this.defiPositions, + defiPositions: this.#defiPositions, actions: this.actions }) this.swapAndBridge = new SwapAndBridgeController({ @@ -355,7 +355,7 @@ export class MainController extends EventEmitter { // TODO: We agreed to always fetch the latest and pending states. // To achieve this, we need to refactor how we use forceUpdate to obtain pending state updates. this.updateSelectedAccountPortfolio(true) - this.defiPositions.updatePositions() + this.#defiPositions.updatePositions() /** * Listener that gets triggered as a finalization step of adding new * accounts via the AccountAdder controller flow. @@ -426,7 +426,7 @@ export class MainController extends EventEmitter { // TODO: We agreed to always fetch the latest and pending states. // To achieve this, we need to refactor how we use forceUpdate to obtain pending state updates. await this.updateSelectedAccountPortfolio(true) - await this.defiPositions.updatePositions() + await this.#defiPositions.updatePositions() // forceEmitUpdate to update the getters in the FE state of the ctrl await this.forceEmitUpdate() await this.actions.forceEmitUpdate() @@ -917,7 +917,7 @@ export class MainController extends EventEmitter { // Additionally, if we trigger the portfolio update twice (i.e., running a long-living interval + force update from the Dashboard), // there won't be any error thrown, as all portfolio updates are queued and they don't use the `withStatus` helper. this.updateSelectedAccountPortfolio(true), - this.defiPositions.updatePositions() + this.#defiPositions.updatePositions() ]) } @@ -1544,7 +1544,7 @@ export class MainController extends EventEmitter { async removeNetwork(id: NetworkId) { await this.networks.removeNetwork(id) await this.updateSelectedAccountPortfolio(true) - await this.defiPositions.updatePositions() + await this.#defiPositions.updatePositions() } async resolveAccountOpAction(data: any, actionId: AccountOpAction['id']) { diff --git a/src/controllers/selectedAccount/selectedAccount.test.ts b/src/controllers/selectedAccount/selectedAccount.test.ts index 095ef023e..522696b73 100644 --- a/src/controllers/selectedAccount/selectedAccount.test.ts +++ b/src/controllers/selectedAccount/selectedAccount.test.ts @@ -137,7 +137,9 @@ describe('SelectedAccount Controller', () => { await selectedAccountCtrl.initControllers({ portfolio: portfolioCtrl, defiPositions: defiPositionsCtrl, - actions: actionsCtrl + actions: actionsCtrl, + networks: networksCtrl, + providers: providersCtrl }) expect(selectedAccountCtrl.areControllersInitialized).toEqual(true) }) diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index 787058cb7..8617514b5 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -1,6 +1,9 @@ import { Account } from '../../interfaces/account' +import { Banner } from '../../interfaces/banner' import { SelectedAccountPortfolio } from '../../interfaces/selectedAccount' import { Storage } from '../../interfaces/storage' +// eslint-disable-next-line import/no-cycle +import { getNetworksWithDeFiPositionsErrorBanners } from '../../libs/banners/banners' import { sortByValue } from '../../libs/defiPositions/helpers' import { PositionsByProvider } from '../../libs/defiPositions/types' import { @@ -14,8 +17,10 @@ import { Action, ActionsController } from '../actions/actions' // eslint-disable-next-line import/no-cycle import { DefiPositionsController } from '../defiPositions/defiPositions' import EventEmitter from '../eventEmitter/eventEmitter' +import { NetworksController } from '../networks/networks' // eslint-disable-next-line import/no-cycle import { PortfolioController } from '../portfolio/portfolio' +import { ProvidersController } from '../providers/providers' const DEFAULT_SELECTED_ACCOUNT_PORTFOLIO = { tokens: [], @@ -39,6 +44,10 @@ export class SelectedAccountController extends EventEmitter { #actions: ActionsController | null = null + #networks: NetworksController | null = null + + #providers: ProvidersController | null = null + account: Account | null = null portfolio: SelectedAccountPortfolio = DEFAULT_SELECTED_ACCOUNT_PORTFOLIO @@ -49,6 +58,8 @@ export class SelectedAccountController extends EventEmitter { defiPositions: PositionsByProvider[] = [] + defiPositionsBanners: Banner[] = [] + actions: Action[] = [] isReady: boolean = false @@ -83,19 +94,26 @@ export class SelectedAccountController extends EventEmitter { initControllers({ portfolio, defiPositions, - actions + actions, + networks, + providers }: { portfolio: PortfolioController defiPositions: DefiPositionsController actions: ActionsController + networks: NetworksController + providers: ProvidersController }) { this.#portfolio = portfolio this.#defiPositions = defiPositions this.#actions = actions + this.#networks = networks + this.#providers = providers this.#updateSelectedAccountPortfolio(true) this.#updateSelectedAccountDefiPositions(true) this.#updateSelectedAccountActions(true) + this.#updateDefiPositionsBanners(true) this.#portfolio.onUpdate(async () => { this.#debounceFunctionCallsOnSameTick('updateSelectedAccountPortfolio', () => @@ -110,6 +128,9 @@ export class SelectedAccountController extends EventEmitter { this.#debounceFunctionCallsOnSameTick('updateSelectedAccountDefiPositions', () => this.#updateSelectedAccountDefiPositions() ) + this.#debounceFunctionCallsOnSameTick('updateDefiPositionsBanners', () => + this.#updateDefiPositionsBanners() + ) }) this.#actions.onUpdate(() => { @@ -251,6 +272,22 @@ export class SelectedAccountController extends EventEmitter { }, 0) } + #updateDefiPositionsBanners(skipUpdate?: boolean) { + if (!this.account || !this.#networks || !this.#providers || !this.#defiPositions) return + + const errorBanners = getNetworksWithDeFiPositionsErrorBanners({ + networks: this.#networks.networks, + currentAccountState: this.#defiPositions.state[this.account.addr], + providers: this.#providers.providers + }) + + this.defiPositionsBanners = errorBanners + + if (!skipUpdate) { + this.emitUpdate() + } + } + toJSON() { return { ...this, From 2cc78ff62a411bbbbe574d326a638cc9602c7f4f Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 12:29:39 +0200 Subject: [PATCH 2/7] fix: test --- src/controllers/selectedAccount/selectedAccount.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index 8617514b5..7a7b54712 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -277,7 +277,7 @@ export class SelectedAccountController extends EventEmitter { const errorBanners = getNetworksWithDeFiPositionsErrorBanners({ networks: this.#networks.networks, - currentAccountState: this.#defiPositions.state[this.account.addr], + currentAccountState: this.#defiPositions.state[this.account.addr] || {}, providers: this.#providers.providers }) From 94dc0e8c9898e1248439896832d085bf989b8d4a Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 13:04:59 +0200 Subject: [PATCH 3/7] init the new ctrls --- src/controllers/main/main.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index b290cb160..042d6c012 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -314,7 +314,9 @@ export class MainController extends EventEmitter { this.selectedAccount.initControllers({ portfolio: this.portfolio, defiPositions: this.#defiPositions, - actions: this.actions + actions: this.actions, + networks: this.networks, + providers: this.providers }) this.swapAndBridge = new SwapAndBridgeController({ selectedAccount: this.selectedAccount, From 20b99fc166f8f7b6ddefa498e99f5b5b6a2edf69 Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 16:35:25 +0200 Subject: [PATCH 4/7] make defiPositions ctrl public but it's state prop private --- .../defiPositions/defiPositions.ts | 34 +++++++++++-------- src/controllers/main/main.ts | 16 ++++----- .../selectedAccount/selectedAccount.ts | 19 ++++++----- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/controllers/defiPositions/defiPositions.ts b/src/controllers/defiPositions/defiPositions.ts index b148a1d4e..fe3196ceb 100644 --- a/src/controllers/defiPositions/defiPositions.ts +++ b/src/controllers/defiPositions/defiPositions.ts @@ -24,7 +24,7 @@ export class DefiPositionsController extends EventEmitter { #minUpdateInterval: number = 60 * 1000 // 1 minute - state: DeFiPositionsState = {} + #state: DeFiPositionsState = {} constructor({ fetch, @@ -46,13 +46,13 @@ export class DefiPositionsController extends EventEmitter { } #initInitialAccountStateIfNeeded(accountAddr: string) { - if (!this.state[accountAddr]) { - this.state[accountAddr] = this.#networks.networks.reduce( + if (!this.#state[accountAddr]) { + this.#state[accountAddr] = this.#networks.networks.reduce( (acc, n) => ({ ...acc, [n.id]: { isLoading: true, positionsByProvider: [] } }), - this.state[accountAddr] + this.#state[accountAddr] ) this.emitUpdate() @@ -65,18 +65,18 @@ export class DefiPositionsController extends EventEmitter { providerName: string, errorMessage: string ) { - if (!this.state[accountAddr][networkId].providerErrors) { - this.state[accountAddr][networkId].providerErrors = [] + if (!this.#state[accountAddr][networkId].providerErrors) { + this.#state[accountAddr][networkId].providerErrors = [] } - this.state[accountAddr][networkId].providerErrors!.push({ + this.#state[accountAddr][networkId].providerErrors!.push({ providerName, error: errorMessage }) } #getCanSkipUpdate(accountAddr: string, networkId: string) { - const networkState = this.state[accountAddr][networkId] + const networkState = this.#state[accountAddr][networkId] if (networkState.isLoading) return false if (networkState.error) return false @@ -104,7 +104,7 @@ export class DefiPositionsController extends EventEmitter { this.emitUpdate() return } - const networkState = this.state[selectedAccountAddr][n.id] + const networkState = this.#state[selectedAccountAddr][n.id] // Reset provider errors before updating networkState.providerErrors = [] @@ -141,7 +141,7 @@ export class DefiPositionsController extends EventEmitter { ) ]) - this.state[selectedAccountAddr][n.id] = { + this.#state[selectedAccountAddr][n.id] = { ...networkState, isLoading: false, positionsByProvider: [aavePositions, uniV3Positions].filter( @@ -151,11 +151,11 @@ export class DefiPositionsController extends EventEmitter { } await this.#setAssetPrices(selectedAccountAddr, n.id).catch((e) => { console.error('#setAssetPrices error:', e) - this.state[selectedAccountAddr][n.id].error = DeFiPositionsError.AssetPriceError + this.#state[selectedAccountAddr][n.id].error = DeFiPositionsError.AssetPriceError }) } catch (e: any) { const prevPositionsByProvider = networkState.positionsByProvider - this.state[selectedAccountAddr][n.id] = { + this.#state[selectedAccountAddr][n.id] = { isLoading: false, positionsByProvider: prevPositionsByProvider || [], error: DeFiPositionsError.CriticalError @@ -171,7 +171,7 @@ export class DefiPositionsController extends EventEmitter { async #setAssetPrices(accountAddr: string, networkId: string) { const dedup = (x: any[]) => x.filter((y, i) => x.indexOf(y) === i) - const networkState = this.state[accountAddr][networkId] + const networkState = this.#state[accountAddr][networkId] const addresses: string[] = [] @@ -196,7 +196,7 @@ export class DefiPositionsController extends EventEmitter { // eslint-disable-next-line no-prototype-builtins if (body.hasOwnProperty('error')) throw body - const positionsByProviderWithPrices = this.state[accountAddr][ + const positionsByProviderWithPrices = this.#state[accountAddr][ networkId ].positionsByProvider.map((positionsByProvider) => { if (positionsByProvider.providerName.toLowerCase().includes('aave')) @@ -244,12 +244,16 @@ export class DefiPositionsController extends EventEmitter { return { ...positionsByProvider, positions: updatedPositions, positionInUSD } }) - this.state[accountAddr][networkId].positionsByProvider = positionsByProviderWithPrices + this.#state[accountAddr][networkId].positionsByProvider = positionsByProviderWithPrices } catch (error) { console.error('#setAssetPrices error in defiPositions:', error) } } + getDefiPostionsState(accountAddr: string) { + return this.#state[accountAddr] || {} + } + toJSON() { return { ...this, diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index 220cedaa5..ce8d3d216 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -146,7 +146,7 @@ export class MainController extends EventEmitter { portfolio: PortfolioController - #defiPositions: DefiPositionsController + defiPositions: DefiPositionsController dapps: DappsController @@ -234,7 +234,7 @@ export class MainController extends EventEmitter { this.providers.setProvider(network) await this.accounts.updateAccountStates('latest', [network.id]) await this.updateSelectedAccountPortfolio(true) - await this.#defiPositions.updatePositions(network.id) + await this.defiPositions.updatePositions(network.id) }, (networkId: NetworkId) => { this.providers.removeProvider(networkId) @@ -271,7 +271,7 @@ export class MainController extends EventEmitter { relayerUrl, velcroUrl ) - this.#defiPositions = new DefiPositionsController({ + this.defiPositions = new DefiPositionsController({ fetch: this.fetch, selectedAccount: this.selectedAccount, networks: this.networks, @@ -313,7 +313,7 @@ export class MainController extends EventEmitter { }) this.selectedAccount.initControllers({ portfolio: this.portfolio, - defiPositions: this.#defiPositions, + defiPositions: this.defiPositions, actions: this.actions, networks: this.networks, providers: this.providers @@ -357,7 +357,7 @@ export class MainController extends EventEmitter { // TODO: We agreed to always fetch the latest and pending states. // To achieve this, we need to refactor how we use forceUpdate to obtain pending state updates. this.updateSelectedAccountPortfolio(true) - this.#defiPositions.updatePositions() + this.defiPositions.updatePositions() /** * Listener that gets triggered as a finalization step of adding new * accounts via the AccountAdder controller flow. @@ -428,7 +428,7 @@ export class MainController extends EventEmitter { // TODO: We agreed to always fetch the latest and pending states. // To achieve this, we need to refactor how we use forceUpdate to obtain pending state updates. await this.updateSelectedAccountPortfolio(true) - await this.#defiPositions.updatePositions() + await this.defiPositions.updatePositions() // forceEmitUpdate to update the getters in the FE state of the ctrl await this.forceEmitUpdate() await this.actions.forceEmitUpdate() @@ -920,7 +920,7 @@ export class MainController extends EventEmitter { // Additionally, if we trigger the portfolio update twice (i.e., running a long-living interval + force update from the Dashboard), // there won't be any error thrown, as all portfolio updates are queued and they don't use the `withStatus` helper. this.updateSelectedAccountPortfolio(true), - this.#defiPositions.updatePositions() + this.defiPositions.updatePositions() ]) } @@ -1547,7 +1547,7 @@ export class MainController extends EventEmitter { async removeNetwork(id: NetworkId) { await this.networks.removeNetwork(id) await this.updateSelectedAccountPortfolio(true) - await this.#defiPositions.updatePositions() + await this.defiPositions.updatePositions() } async resolveAccountOpAction(data: any, actionId: AccountOpAction['id']) { diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index 20793371c..f0153d940 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -166,7 +166,7 @@ export class SelectedAccountController extends EventEmitter { #updateSelectedAccountPortfolio(skipUpdate?: boolean) { if (!this.#portfolio || !this.#defiPositions || !this.account) return - const defiPositionsAccountState = this.#defiPositions.state[this.account.addr] + const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) const portfolioState = structuredClone({ latest: this.#portfolio.latest, @@ -210,17 +210,18 @@ export class SelectedAccountController extends EventEmitter { get areDefiPositionsLoading() { if (!this.account || !this.#defiPositions) return false - return Object.values(this.#defiPositions.state[this.account.addr] || {}).some( - (n) => n.isLoading - ) + const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) + return Object.values(defiPositionsAccountState).some((n) => n.isLoading) } #updateSelectedAccountDefiPositions(skipUpdate?: boolean) { if (!this.#defiPositions || !this.account) return - const positionsByProvider = Object.values( - this.#defiPositions.state[this.account.addr] || {} - ).flatMap((n) => n.positionsByProvider) + const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) + + const positionsByProvider = Object.values(defiPositionsAccountState).flatMap( + (n) => n.positionsByProvider + ) const positionsByProviderWithSortedAssets = positionsByProvider.map((provider) => { const positions = provider.positions @@ -281,9 +282,11 @@ export class SelectedAccountController extends EventEmitter { #updateDefiPositionsBanners(skipUpdate?: boolean) { if (!this.account || !this.#networks || !this.#providers || !this.#defiPositions) return + const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) + const errorBanners = getNetworksWithDeFiPositionsErrorBanners({ networks: this.#networks.networks, - currentAccountState: this.#defiPositions.state[this.account.addr] || {}, + currentAccountState: defiPositionsAccountState, providers: this.#providers.providers }) From 0725abae85052f51afeef02e3e81fc47a0cb336a Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 17:04:01 +0200 Subject: [PATCH 5/7] fix: defiPositions tests --- .../defiPositions/defiPositions.test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/controllers/defiPositions/defiPositions.test.ts b/src/controllers/defiPositions/defiPositions.test.ts index cd02ee36f..0049cc988 100644 --- a/src/controllers/defiPositions/defiPositions.test.ts +++ b/src/controllers/defiPositions/defiPositions.test.ts @@ -89,8 +89,8 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - - expect(controller.state[ACCOUNT.addr].polygon.positionsByProvider.length).toBeGreaterThan(0) + const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + expect(selectedAccountState.polygon.positionsByProvider.length).toBeGreaterThan(0) }) it('should handle errors in update positions', async () => { @@ -110,7 +110,8 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - expect(controller.state[ACCOUNT.addr].ethereum.providerErrors).toEqual([ + const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + expect(selectedAccountState.ethereum.providerErrors).toEqual([ { providerName: 'AAVE v3', error: 'AAVE error' }, { providerName: 'Uniswap V3', error: 'Uniswap error' } ]) @@ -122,7 +123,9 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - const positions = controller.state[ACCOUNT.addr].polygon.positionsByProvider + const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + + const positions = selectedAccountState.polygon.positionsByProvider expect(positions.length).toBeGreaterThan(0) positions.forEach((provider) => { provider.positions.forEach((position) => { @@ -146,7 +149,8 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - const positions = controller.state[ACCOUNT.addr].polygon.positionsByProvider + const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + const positions = selectedAccountState.polygon.positionsByProvider expect(positions.length).toBeGreaterThan(0) positions.forEach((provider) => { // AAVE positions get their prices from oracles From e99f415e5fc5a146b681287dde78003ce14493c6 Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 17:09:38 +0200 Subject: [PATCH 6/7] cleanup and fixes --- .../selectedAccount/selectedAccount.ts | 38 +++++-------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index f0153d940..c738e8529 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -13,7 +13,7 @@ import { // eslint-disable-next-line import/no-cycle import { AccountsController } from '../accounts/accounts' // eslint-disable-next-line import/no-cycle -import { Action, ActionsController } from '../actions/actions' +import { ActionsController } from '../actions/actions' // eslint-disable-next-line import/no-cycle import { DefiPositionsController } from '../defiPositions/defiPositions' import EventEmitter from '../eventEmitter/eventEmitter' @@ -60,8 +60,6 @@ export class SelectedAccountController extends EventEmitter { defiPositionsBanners: Banner[] = [] - actions: Action[] = [] - isReady: boolean = false areControllersInitialized: boolean = false @@ -112,7 +110,6 @@ export class SelectedAccountController extends EventEmitter { this.#updateSelectedAccountPortfolio(true) this.#updateSelectedAccountDefiPositions(true) - this.#updateSelectedAccountActions(true) this.#updateDefiPositionsBanners(true) this.#portfolio.onUpdate(async () => { @@ -133,8 +130,11 @@ export class SelectedAccountController extends EventEmitter { ) }) - this.#actions.onUpdate(() => { - this.#updateSelectedAccountActions() + this.#providers.onUpdate(() => { + this.#debounceFunctionCallsOnSameTick('updateDefiPositionsBanners', () => + this.#updateDefiPositionsBanners() + ) + // TODO: add portfolio banners and call updatePortfolioBanners here }) this.areControllersInitialized = true @@ -178,7 +178,9 @@ export class SelectedAccountController extends EventEmitter { this.account ) - const hasSignAccountOp = !!this.actions.filter((action) => action.type === 'accountOp') + const hasSignAccountOp = !!this.#actions?.visibleActionsQueue.filter( + (action) => action.type === 'accountOp' + ) const newSelectedAccountPortfolio = calculateSelectedAccountPortfolio( this.account.addr, @@ -246,28 +248,6 @@ export class SelectedAccountController extends EventEmitter { } } - #updateSelectedAccountActions(skipUpdate?: boolean) { - if (!this.#actions || !this.account) return - - this.actions = this.#actions.actionsQueue.filter((a) => { - if (a.type === 'accountOp') { - return a.accountOp.accountAddr === this.account!.addr - } - if (a.type === 'signMessage') { - return a.userRequest.meta.accountAddr === this.account!.addr - } - if (a.type === 'benzin') { - return a.userRequest.meta.accountAddr === this.account!.addr - } - - return true - }) - - if (!skipUpdate) { - this.emitUpdate() - } - } - #debounceFunctionCallsOnSameTick(funcName: string, func: Function) { if (this.#shouldDebounceFlags[funcName]) return this.#shouldDebounceFlags[funcName] = true From 996666772589004204e1b563ce2e85ca233d56a1 Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Fri, 15 Nov 2024 17:21:12 +0200 Subject: [PATCH 7/7] fix: postions typo --- src/controllers/defiPositions/defiPositions.test.ts | 8 ++++---- src/controllers/defiPositions/defiPositions.ts | 2 +- src/controllers/selectedAccount/selectedAccount.ts | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/controllers/defiPositions/defiPositions.test.ts b/src/controllers/defiPositions/defiPositions.test.ts index 0049cc988..0ec2bc6ed 100644 --- a/src/controllers/defiPositions/defiPositions.test.ts +++ b/src/controllers/defiPositions/defiPositions.test.ts @@ -89,7 +89,7 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + const selectedAccountState = controller.getDefiPositionsState(ACCOUNT.addr) expect(selectedAccountState.polygon.positionsByProvider.length).toBeGreaterThan(0) }) @@ -110,7 +110,7 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + const selectedAccountState = controller.getDefiPositionsState(ACCOUNT.addr) expect(selectedAccountState.ethereum.providerErrors).toEqual([ { providerName: 'AAVE v3', error: 'AAVE error' }, { providerName: 'Uniswap V3', error: 'Uniswap error' } @@ -123,7 +123,7 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + const selectedAccountState = controller.getDefiPositionsState(ACCOUNT.addr) const positions = selectedAccountState.polygon.positionsByProvider expect(positions.length).toBeGreaterThan(0) @@ -149,7 +149,7 @@ describe('DefiPositionsController', () => { const controller = await prepareTest() await controller.updatePositions() - const selectedAccountState = controller.getDefiPostionsState(ACCOUNT.addr) + const selectedAccountState = controller.getDefiPositionsState(ACCOUNT.addr) const positions = selectedAccountState.polygon.positionsByProvider expect(positions.length).toBeGreaterThan(0) positions.forEach((provider) => { diff --git a/src/controllers/defiPositions/defiPositions.ts b/src/controllers/defiPositions/defiPositions.ts index fe3196ceb..f6dff4313 100644 --- a/src/controllers/defiPositions/defiPositions.ts +++ b/src/controllers/defiPositions/defiPositions.ts @@ -250,7 +250,7 @@ export class DefiPositionsController extends EventEmitter { } } - getDefiPostionsState(accountAddr: string) { + getDefiPositionsState(accountAddr: string) { return this.#state[accountAddr] || {} } diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index c738e8529..b191a41e1 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -166,7 +166,7 @@ export class SelectedAccountController extends EventEmitter { #updateSelectedAccountPortfolio(skipUpdate?: boolean) { if (!this.#portfolio || !this.#defiPositions || !this.account) return - const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) + const defiPositionsAccountState = this.#defiPositions.getDefiPositionsState(this.account.addr) const portfolioState = structuredClone({ latest: this.#portfolio.latest, @@ -212,14 +212,14 @@ export class SelectedAccountController extends EventEmitter { get areDefiPositionsLoading() { if (!this.account || !this.#defiPositions) return false - const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) + const defiPositionsAccountState = this.#defiPositions.getDefiPositionsState(this.account.addr) return Object.values(defiPositionsAccountState).some((n) => n.isLoading) } #updateSelectedAccountDefiPositions(skipUpdate?: boolean) { if (!this.#defiPositions || !this.account) return - const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) + const defiPositionsAccountState = this.#defiPositions.getDefiPositionsState(this.account.addr) const positionsByProvider = Object.values(defiPositionsAccountState).flatMap( (n) => n.positionsByProvider @@ -262,7 +262,7 @@ export class SelectedAccountController extends EventEmitter { #updateDefiPositionsBanners(skipUpdate?: boolean) { if (!this.account || !this.#networks || !this.#providers || !this.#defiPositions) return - const defiPositionsAccountState = this.#defiPositions.getDefiPostionsState(this.account.addr) + const defiPositionsAccountState = this.#defiPositions.getDefiPositionsState(this.account.addr) const errorBanners = getNetworksWithDeFiPositionsErrorBanners({ networks: this.#networks.networks,