Skip to content

Commit

Permalink
fix: account id toggles being shared across different devices (#6941)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodenfurniture authored May 20, 2024
1 parent 4b7f6ab commit 0cd3a8a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/state/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ export const migrations = {
53: clearAssets,
54: clearAssets,
55: clearAssets,
56: clearPortfolio,
}
10 changes: 9 additions & 1 deletion src/state/slices/common-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('opportunitiesSlice selectors', () => {
supportedChainIds: [],
},
wallet,
enabledAccountIds: [gomesAccountId, fauxmesAccountId, catpuccinoAccountId],
enabledAccountIds: { [walletId]: [gomesAccountId, fauxmesAccountId, catpuccinoAccountId] },
},
}
describe('selects ID/s', () => {
Expand All @@ -66,7 +66,7 @@ describe('opportunitiesSlice selectors', () => {
},
ids: [gomesAccountId, fauxmesAccountId],
}
const enabledAccountIds = [gomesAccountId, fauxmesAccountId]
const enabledAccountIds = { [walletId]: [gomesAccountId, fauxmesAccountId] }
const lp = {
...initialState.lp,
byAccountId: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > Bitcoin > should update s
"bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w",
],
},
"enabledAccountIds": [
"bip122:000000000019d6689c085ae165831e93:zpub6qk8s2NQsYG6X2Mm6iU2ii3yTAqDb2XqnMu9vo2WjvqwjSvjjiYQQveYXbPxrnRT5Yb5p0x934be745172066EDF795ffc5EA9F28f19b440c637BaBw1wowPwbS8fj7uCfj3UhqhD2LLbvY6Ni1w",
],
"enabledAccountIds": {},
"wallet": {
"byId": {},
"ids": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down Expand Up @@ -255,9 +243,7 @@ exports[`portfolioSlice > reducers > upsertPortfolio > ethereum > should update
"eip155:1:0x9a2d593725045d1727d525dd07a396f9ff079bb1",
],
},
"enabledAccountIds": [
"eip155:1:0x9a2d593725045d1727d525dd07a396f9ff079bb1",
],
"enabledAccountIds": {},
"wallet": {
"byId": {},
"ids": [],
Expand Down Expand Up @@ -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": [],
Expand Down
18 changes: 0 additions & 18 deletions src/state/slices/portfolioSlice/portfolioSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down
16 changes: 12 additions & 4 deletions src/state/slices/portfolioSlice/portfolioSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/state/slices/portfolioSlice/portfolioSliceCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WalletId, AccountId[]>
/**
* 1:many mapping of a unique wallet id -> multiple account ids
*/
Expand All @@ -85,7 +85,7 @@ export const initialState: Portfolio = {
byId: {},
ids: [],
},
enabledAccountIds: [],
enabledAccountIds: {},
wallet: {
byId: {},
ids: [],
Expand Down
2 changes: 1 addition & 1 deletion src/test/mocks/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const mockStore: ReduxState = {
byId: {},
ids: [],
},
enabledAccountIds: [],
enabledAccountIds: {},
wallet: {
byId: {},
ids: [],
Expand Down

0 comments on commit 0cd3a8a

Please sign in to comment.