diff --git a/src/state/migrations/index.ts b/src/state/migrations/index.ts index 445faf865f4..61860315215 100644 --- a/src/state/migrations/index.ts +++ b/src/state/migrations/index.ts @@ -63,4 +63,5 @@ export const migrations = { 53: clearAssets, 54: clearAssets, 55: clearAssets, + 56: clearPortfolio, } diff --git a/src/state/slices/common-selectors.ts b/src/state/slices/common-selectors.ts index 257320f6467..a24932c9523 100644 --- a/src/state/slices/common-selectors.ts +++ b/src/state/slices/common-selectors.ts @@ -22,11 +22,19 @@ export const selectIsWalletConnected = (state: ReduxState) => state.portfolio.connectedWallet !== undefined export const selectWalletSupportedChainIds = (state: ReduxState) => state.portfolio.connectedWallet?.supportedChainIds ?? [] +export const selectWalletEnabledAccountIds = createDeepEqualOutputSelector( + selectWalletId, + (state: ReduxState) => state.portfolio.enabledAccountIds, + (walletId, enabledAccountIds) => { + if (!walletId) return [] + return enabledAccountIds[walletId] ?? [] + }, +) export const selectWalletAccountIds = createDeepEqualOutputSelector( selectWalletId, (state: ReduxState) => state.portfolio.wallet.byId, - (state: ReduxState) => state.portfolio.enabledAccountIds, + selectWalletEnabledAccountIds, (walletId, walletById, enabledAccountIds): AccountId[] => { const walletAccountIds = (walletId && walletById[walletId]) ?? [] return walletAccountIds.filter(accountId => (enabledAccountIds ?? []).includes(accountId)) diff --git a/src/state/slices/opportunitiesSlice/selectors/selectors.test.ts b/src/state/slices/opportunitiesSlice/selectors/selectors.test.ts index b7245809e45..f1e0b271b9c 100644 --- a/src/state/slices/opportunitiesSlice/selectors/selectors.test.ts +++ b/src/state/slices/opportunitiesSlice/selectors/selectors.test.ts @@ -47,7 +47,7 @@ describe('opportunitiesSlice selectors', () => { supportedChainIds: [], }, wallet, - enabledAccountIds: [gomesAccountId, fauxmesAccountId, catpuccinoAccountId], + enabledAccountIds: { [walletId]: [gomesAccountId, fauxmesAccountId, catpuccinoAccountId] }, }, } describe('selects ID/s', () => { @@ -66,7 +66,7 @@ describe('opportunitiesSlice selectors', () => { }, ids: [gomesAccountId, fauxmesAccountId], } - const enabledAccountIds = [gomesAccountId, fauxmesAccountId] + const enabledAccountIds = { [walletId]: [gomesAccountId, fauxmesAccountId] } const lp = { ...initialState.lp, byAccountId: { diff --git a/src/state/slices/portfolioSlice/__snapshots__/portfolioSlice.test.ts.snap b/src/state/slices/portfolioSlice/__snapshots__/portfolioSlice.test.ts.snap index 164a07f3580..f1d5a277ce1 100644 --- a/src/state/slices/portfolioSlice/__snapshots__/portfolioSlice.test.ts.snap +++ b/src/state/slices/portfolioSlice/__snapshots__/portfolioSlice.test.ts.snap @@ -29,9 +29,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > Bitcoin > should update s "bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", ], }, - "enabledAccountIds": [ - "bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", - ], + "enabledAccountIds": {}, "wallet": { "byId": {}, "ids": [], @@ -79,10 +77,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > Bitcoin > should update s "bip122:000000000019d6689c085ae165831e93:xpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", ], }, - "enabledAccountIds": [ - "bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", - "bip122:000000000019d6689c085ae165831e93:xpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", - ], + "enabledAccountIds": {}, "wallet": { "byId": {}, "ids": [], @@ -153,11 +148,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > Ethereum and bitcoin > sh "bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", ], }, - "enabledAccountIds": [ - "eip155:1:0x9a2d593725045d1727d525dd07a396f9ff079bb1", - "eip155:1:0xea674fdde714fd979de3edf0f56aa9716b898ec8", - "bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", - ], + "enabledAccountIds": {}, "wallet": { "byId": {}, "ids": [], @@ -213,10 +204,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > Ethereum and bitcoin > sh "bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", ], }, - "enabledAccountIds": [ - "eip155:1:0x6c8a778ef52e121b7dff1154c553662306a970e9", - "bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w", - ], + "enabledAccountIds": {}, "wallet": { "byId": {}, "ids": [], @@ -255,9 +243,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > ethereum > should update "eip155:1:0x9a2d593725045d1727d525dd07a396f9ff079bb1", ], }, - "enabledAccountIds": [ - "eip155:1:0x9a2d593725045d1727d525dd07a396f9ff079bb1", - ], + "enabledAccountIds": {}, "wallet": { "byId": {}, "ids": [], @@ -309,10 +295,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > ethereum > should update "eip155:1:0xea674fdde714fd979de3edf0f56aa9716b898ec8", ], }, - "enabledAccountIds": [ - "eip155:1:0x9a2d593725045d1727d525dd07a396f9ff079bb1", - "eip155:1:0xea674fdde714fd979de3edf0f56aa9716b898ec8", - ], + "enabledAccountIds": {}, "wallet": { "byId": {}, "ids": [], diff --git a/src/state/slices/portfolioSlice/portfolioSlice.test.ts b/src/state/slices/portfolioSlice/portfolioSlice.test.ts index ddb993e3b07..35f9eb8dddf 100644 --- a/src/state/slices/portfolioSlice/portfolioSlice.test.ts +++ b/src/state/slices/portfolioSlice/portfolioSlice.test.ts @@ -65,9 +65,6 @@ describe('portfolioSlice', () => { const portfolio = mockUpsertPortfolio([ethAccount], assetIds) store.dispatch(portfolioSlice.actions.upsertPortfolio(portfolio)) - for (const accountId of portfolio.accounts.ids) { - store.dispatch(portfolioSlice.actions.enableAccountId(accountId)) - } expect(store.getState().portfolio).toMatchSnapshot() }) @@ -92,9 +89,6 @@ describe('portfolioSlice', () => { const portfolio = mockUpsertPortfolio([ethAccount, ethAccount2], assetIds) store.dispatch(portfolioSlice.actions.upsertPortfolio(portfolio)) - for (const accountId of portfolio.accounts.ids) { - store.dispatch(portfolioSlice.actions.enableAccountId(accountId)) - } expect(store.getState().portfolio).toMatchSnapshot() }) @@ -111,9 +105,6 @@ describe('portfolioSlice', () => { const portfolio = mockUpsertPortfolio([btcAccount], assetIds) store.dispatch(portfolioSlice.actions.upsertPortfolio(portfolio)) - for (const accountId of portfolio.accounts.ids) { - store.dispatch(portfolioSlice.actions.enableAccountId(accountId)) - } expect(store.getState().portfolio).toMatchSnapshot() }) @@ -135,9 +126,6 @@ describe('portfolioSlice', () => { const portfolio = mockUpsertPortfolio([btcAccount, btcAccount2], assetIds) store.dispatch(portfolioSlice.actions.upsertPortfolio(portfolio)) - for (const accountId of portfolio.accounts.ids) { - store.dispatch(portfolioSlice.actions.enableAccountId(accountId)) - } expect(store.getState().portfolio).toMatchSnapshot() }) @@ -186,9 +174,6 @@ describe('portfolioSlice', () => { const portfolio = mockUpsertPortfolio([ethAccount, ethAccount2, btcAccount], assetIds) store.dispatch(portfolioSlice.actions.upsertPortfolio(portfolio)) - for (const accountId of portfolio.accounts.ids) { - store.dispatch(portfolioSlice.actions.enableAccountId(accountId)) - } expect(store.getState().portfolio).toMatchSnapshot() }) @@ -227,9 +212,6 @@ describe('portfolioSlice', () => { const portfolio = mockUpsertPortfolio([ethAccount, btcAccount], assetIds) store.dispatch(portfolioSlice.actions.upsertPortfolio(portfolio)) - for (const accountId of portfolio.accounts.ids) { - store.dispatch(portfolioSlice.actions.enableAccountId(accountId)) - } expect(store.getState().portfolio).toMatchSnapshot() }) diff --git a/src/state/slices/portfolioSlice/portfolioSlice.ts b/src/state/slices/portfolioSlice/portfolioSlice.ts index 45b79cc9c18..9cddb147645 100644 --- a/src/state/slices/portfolioSlice/portfolioSlice.ts +++ b/src/state/slices/portfolioSlice/portfolioSlice.ts @@ -142,12 +142,20 @@ export const portfolio = createSlice({ * during initial load, leading to all auto-detected accounts being disabled. */ enableAccountId: (draftState, { payload: accountId }: { payload: AccountId }) => { - const enabledAccountIdsSet = new Set(draftState.enabledAccountIds) + const walletId = draftState.connectedWallet?.id + + if (!walletId) return + + const enabledAccountIdsSet = new Set(draftState.enabledAccountIds[walletId]) enabledAccountIdsSet.add(accountId) - draftState.enabledAccountIds = Array.from(enabledAccountIdsSet) + draftState.enabledAccountIds[walletId] = Array.from(enabledAccountIdsSet) }, toggleAccountIdEnabled: (draftState, { payload: accountId }: { payload: AccountId }) => { - const enabledAccountIdsSet = new Set(draftState.enabledAccountIds) + const walletId = draftState.connectedWallet?.id + + if (!walletId) return + + const enabledAccountIdsSet = new Set(draftState.enabledAccountIds[walletId]) const isEnabled = enabledAccountIdsSet.has(accountId) if (isEnabled) { @@ -156,7 +164,7 @@ export const portfolio = createSlice({ enabledAccountIdsSet.add(accountId) } - draftState.enabledAccountIds = Array.from(enabledAccountIdsSet) + draftState.enabledAccountIds[walletId] = Array.from(enabledAccountIdsSet) }, }, extraReducers: builder => builder.addCase(PURGE, () => initialState), diff --git a/src/state/slices/portfolioSlice/portfolioSliceCommon.ts b/src/state/slices/portfolioSlice/portfolioSliceCommon.ts index e3455d42bfc..6271a190ebf 100644 --- a/src/state/slices/portfolioSlice/portfolioSliceCommon.ts +++ b/src/state/slices/portfolioSlice/portfolioSliceCommon.ts @@ -64,7 +64,7 @@ export type Portfolio = { * The `AccountId[]` that are enabled. Rather than removing the accounts and adding complexity * to the actions and state management, we enable them here and filter them out in the selectors. */ - enabledAccountIds: AccountId[] + enabledAccountIds: PartialRecord /** * 1:many mapping of a unique wallet id -> multiple account ids */ @@ -85,7 +85,7 @@ export const initialState: Portfolio = { byId: {}, ids: [], }, - enabledAccountIds: [], + enabledAccountIds: {}, wallet: { byId: {}, ids: [], diff --git a/src/test/mocks/store.ts b/src/test/mocks/store.ts index 90d954acba6..f5ae624e19f 100644 --- a/src/test/mocks/store.ts +++ b/src/test/mocks/store.ts @@ -55,7 +55,7 @@ export const mockStore: ReduxState = { byId: {}, ids: [], }, - enabledAccountIds: [], + enabledAccountIds: {}, wallet: { byId: {}, ids: [],