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

update migrations #1520

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
27 changes: 13 additions & 14 deletions src/core/state/favorites/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ const mergeNewOfficiallySupportedChainsState = (
return state;
};

const migrations: ((s: FavoritesState) => FavoritesState)[] = [
// version 1 didn't need a migration
(state: FavoritesState) => state,
// version 2 added avalanche
(state) => mergeNewOfficiallySupportedChainsState(state, [ChainId.avalanche]),
// version 3 added blast
(state) => mergeNewOfficiallySupportedChainsState(state, [ChainId.blast]),
// version 4 added degen
(state) => mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]),
];

export const favoritesStore = createStore<FavoritesState>(
(set, get) => ({
favorites: defaultFavorites,
Expand Down Expand Up @@ -151,20 +162,8 @@ export const favoritesStore = createStore<FavoritesState>(
{
persist: {
name: 'favorites',
version: 4,
migrate: migrate(
// version 1 didn't need a migration
(state: FavoritesState) => state,
// version 2 added avalanche
(state) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.avalanche]),
// version 3 added blast
(state) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.blast]),
// version 4 added degen
(state) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]),
),
version: migrations.length,
migrate: migrate(migrations),
},
},
);
Expand Down
90 changes: 48 additions & 42 deletions src/core/state/rainbowChains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@ const addCustomRPC = ({
return state;
};

const migrations: ((s: RainbowChainsState) => RainbowChainsState)[] = [
// version 2 added support for Avalanche and Avalanche Fuji
(state) =>
mergeNewOfficiallySupportedChainsState(state, [
ChainId.avalanche,
ChainId.avalancheFuji,
]),
// version 3 added support for Blast
(state: RainbowChainsState) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.blast]),
// version 4
(state) =>
removeCustomRPC({
state,
rpcUrl: 'https://rpc.zora.co',
rainbowChains: state.rainbowChains,
}),
// version 5 added support for Degen
(state: RainbowChainsState) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]),
// version 6
(state: RainbowChainsState) => {
if (
!state.rainbowChains[zora.id] ||
state.rainbowChains[zora.id]?.chains.length === 0
) {
return addCustomRPC({ chain: zora, state });
}
return state;
},
// version 7, check https://github.com/rainbow-me/browser-extension/pull/1520 we had to
// add dumb migrations
(state: RainbowChainsState) => state,
(state: RainbowChainsState) => state,
// version 8
(state: RainbowChainsState) => {
if (
!state.rainbowChains[chainDegen.id] ||
state.rainbowChains[chainDegen.id]?.chains.length === 0
) {
return addCustomRPC({ chain: chainDegen, state });
}
return state;
},
];

export const rainbowChainsStore = createStore<RainbowChainsState>(
(set, get) => ({
rainbowChains: getInitialRainbowChains(),
Expand Down Expand Up @@ -228,48 +274,8 @@ export const rainbowChainsStore = createStore<RainbowChainsState>(
{
persist: {
name: 'rainbowChains',
version: 7,
migrate: migrate(
// version 2 added support for Avalanche and Avalanche Fuji
(state) =>
mergeNewOfficiallySupportedChainsState(state, [
ChainId.avalanche,
ChainId.avalancheFuji,
]),
// version 2 added support for Blast
(state: RainbowChainsState) =>
mergeNewOfficiallySupportedChainsState(state, [
ChainId.avalanche,
ChainId.avalancheFuji,
]),
(state) =>
removeCustomRPC({
state,
rpcUrl: 'https://rpc.zora.co',
rainbowChains: state.rainbowChains,
}),
// version 5 added support for Degen
(state: RainbowChainsState) =>
mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]),
(state: RainbowChainsState) => {
if (
!state.rainbowChains[zora.id] ||
state.rainbowChains[zora.id]?.chains.length === 0
) {
return addCustomRPC({ chain: zora, state });
}
return state;
},
(state: RainbowChainsState) => {
if (
!state.rainbowChains[chainDegen.id] ||
state.rainbowChains[chainDegen.id]?.chains.length === 0
) {
return addCustomRPC({ chain: chainDegen, state });
}
return state;
},
),
version: migrations.length,
migrate: migrate(migrations),
},
},
);
Expand Down
35 changes: 5 additions & 30 deletions src/core/utils/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,7 @@
type R<T> = (persistedState: unknown, version: number) => T;

interface Migrator {
<A>(m1: (s: any) => A): R<A>;
<A, B>(m1: (s: any) => A, m2: (s: A) => B): R<B>;
<A, B, C>(m1: (s: any) => A, m2: (s: A) => B, m3: (s: B) => C): R<C>;
<A, B, C, D>(
m1: (s: any) => A,
m2: (s: A) => B,
m3: (s: B) => C,
m4: (s: C) => D,
): R<D>;
<A, B, C, D, E>(
m1: (s: any) => A,
m2: (s: A) => B,
m3: (s: B) => C,
m4: (s: C) => D,
m5: (s: D) => E,
): R<E>;
<A, B, C, D, E, F>(
m1: (s: any) => A,
m2: (s: A) => B,
m3: (s: B) => C,
m4: (s: C) => D,
m5: (s: D) => E,
m6: (s: E) => F,
): R<F>;
// if you need more migrations, add more overloads here
<T>(migrations: ((s: any) => any)[]): R<T>;
Copy link
Contributor

Choose a reason for hiding this comment

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

this removes all the type safety 😔😔😔 don't think it's worth it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there any other option for this? adding 15+ lines per new migration is not the best either

}

/**
Expand All @@ -37,11 +13,10 @@ interface Migrator {
* - migrations must be in order
* - zustand persister version must be an integer
*/
export const migrate: Migrator = (
...migrations: ((s: unknown) => unknown)[]
) => {
return (persistedState: unknown, version: number) =>
migrations
export const migrate: Migrator = (migrations: ((s: any) => any)[]) => {
return (persistedState: any, version: number) => {
return migrations
.toSpliced(0, version)
.reduce((acc, fn) => fn(acc), persistedState);
};
};
2 changes: 1 addition & 1 deletion src/core/utils/userChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const chainLabelMap: Record<
[ChainId.zora]: [ChainNameDisplay[zoraSepolia.id]],
[ChainId.avalanche]: [ChainNameDisplay[avalancheFuji.id]],
[ChainId.blast]: [ChainNameDisplay[chainBlastSepolia.id]],
[ChainId.degen]: [ChainNameDisplay[chainDegen.id]],
[ChainId.degen]: [],
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah

};

export const sortNetworks = (order: ChainId[], chains: Chain[]) => {
Expand Down
Loading