Skip to content

Commit

Permalink
fix: userChains migration for apechain, ink
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSinclair committed Dec 18, 2024
1 parent b37dd1d commit d946690
Showing 1 changed file with 76 additions and 7 deletions.
83 changes: 76 additions & 7 deletions src/core/state/userChains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import create from 'zustand';

import { SUPPORTED_MAINNET_CHAINS } from '~/core/references/chains';
import { ChainId } from '~/core/types/chains';
import { persistOptions } from '~/core/utils/persistOptions';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';
Expand Down Expand Up @@ -38,20 +39,22 @@ export interface UserChainsState {
removeUserChain: ({ chainId }: { chainId: ChainId }) => void;
}

const chains = SUPPORTED_MAINNET_CHAINS.reduce(
const initialChains = SUPPORTED_MAINNET_CHAINS.reduce(
(acc, chain) => ({
...acc,
[chain.id]: true,
}),
{} as Record<number, boolean>,
);

const userChainsOrder = Object.keys(chains).map((id) => Number(id) as number);
const initialUserChainsOrder = SUPPORTED_MAINNET_CHAINS.map(
(id) => Number(id) as number,
);

export const userChainsStore = createStore<UserChainsState>(
(set, get) => ({
userChains: chains,
userChainsOrder,
userChains: initialChains,
userChainsOrder: initialUserChainsOrder,
updateUserChains: ({ chainIds, enabled }) => {
const { userChains } = get();
const chainsUpdated = chainIds.reduce(
Expand Down Expand Up @@ -108,10 +111,76 @@ export const userChainsStore = createStore<UserChainsState>(
},
}),
{
persist: {
persist: persistOptions({
name: 'userChains',
version: 5,
},
version: 6,
migrations: [
// previous naive migrations reset user custom networks and ordering
function v1(state: UserChainsState) {
return {
...state,
userChains: initialChains,
userChainsOrder: initialUserChainsOrder,
};
},

function v2(state: UserChainsState) {
return {
...state,
userChains: initialChains,
userChainsOrder: initialUserChainsOrder,
};
},

function v3(state: UserChainsState) {
return {
...state,
userChains: initialChains,
userChainsOrder: initialUserChainsOrder,
};
},

function v4(state: UserChainsState) {
return {
...state,
userChains: initialChains,
userChainsOrder: initialUserChainsOrder,
};
},

// v5 adds apechain support
function v5(state: UserChainsState) {
// previous october migration #1738 inadvertenly reset state
// this will only apply to users who have not yet migrated to v5
return {
...state,
userChains: { ...state.userChains, [ChainId.apechain]: true },
userChainsOrder: state.userChainsOrder.includes(ChainId.apechain)
? state.userChainsOrder
: [...state.userChainsOrder, ChainId.apechain],
};
},

// v6 adds apechainCurtis, ink, inkSepolia support
function v6(state: UserChainsState) {
const newChains = [
ChainId.apechainCurtis,
ChainId.ink,
ChainId.inkSepolia,
];
return {
...state,
userChains: {
...state.userChains,
...Object.fromEntries(newChains.map((id) => [id, true])),
},
userChainsOrder: state.userChainsOrder.concat(
newChains.filter((id) => !state.userChainsOrder.includes(id)),
),
};
},
],
}),
},
);

Expand Down

0 comments on commit d946690

Please sign in to comment.