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

Optimization / Make DefiPositionsController State Private in MainController #1095

Merged
merged 8 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 1 addition & 16 deletions src/controllers/defiPositions/defiPositions.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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()
}
}
}
20 changes: 11 additions & 9 deletions src/controllers/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class MainController extends EventEmitter {

portfolio: PortfolioController

defiPositions: DefiPositionsController
#defiPositions: DefiPositionsController

dapps: DappsController

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -313,8 +313,10 @@ export class MainController extends EventEmitter {
})
this.selectedAccount.initControllers({
portfolio: this.portfolio,
defiPositions: this.defiPositions,
actions: this.actions
defiPositions: this.#defiPositions,
actions: this.actions,
networks: this.networks,
providers: this.providers
})
this.swapAndBridge = new SwapAndBridgeController({
selectedAccount: this.selectedAccount,
Expand Down Expand Up @@ -355,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.
Expand Down Expand Up @@ -426,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()
Expand Down Expand Up @@ -917,7 +919,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()
])
}

Expand Down Expand Up @@ -1544,7 +1546,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']) {
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/selectedAccount/selectedAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
39 changes: 38 additions & 1 deletion src/controllers/selectedAccount/selectedAccount.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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: [],
Expand All @@ -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
Expand All @@ -49,6 +58,8 @@ export class SelectedAccountController extends EventEmitter {

defiPositions: PositionsByProvider[] = []

defiPositionsBanners: Banner[] = []

actions: Action[] = []

isReady: boolean = false
Expand Down Expand Up @@ -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', () =>
Expand All @@ -110,6 +128,9 @@ export class SelectedAccountController extends EventEmitter {
this.#debounceFunctionCallsOnSameTick('updateSelectedAccountDefiPositions', () =>
this.#updateSelectedAccountDefiPositions()
)
this.#debounceFunctionCallsOnSameTick('updateDefiPositionsBanners', () =>
this.#updateDefiPositionsBanners()
)
Comment on lines +128 to +130
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call it in #updateSelectedAccountDefiPositions as it only depends on them

})

this.#actions.onUpdate(() => {
Expand Down Expand Up @@ -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,
Expand Down
Loading