Skip to content

Commit

Permalink
fix chain migrations (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino authored Oct 22, 2024
1 parent fcf93ea commit bc9d511
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 38 deletions.
9 changes: 7 additions & 2 deletions lavamoat/build-webpack/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1184,8 +1184,13 @@
"define": true
},
"packages": {
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": true,
"webpack>terser-webpack-plugin>@jridgewell/trace-mapping>@jridgewell/resolve-uri": true
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/resolve-uri": true,
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": true
}
},
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/resolve-uri": {
"globals": {
"define": true
}
},
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": {
Expand Down
58 changes: 38 additions & 20 deletions src/core/state/rainbowChains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const rainbowChainsStore = createStore<RainbowChainsState>(
{
persist: persistOptions({
name: 'rainbowChains',
version: 9,
version: 10,
migrations: [
// v1 didn't need a migration
function v1(s: RainbowChainsState) {
Expand All @@ -131,55 +131,73 @@ export const rainbowChainsStore = createStore<RainbowChainsState>(

// version 2 added support for Avalanche and Avalanche Fuji
function v2(state) {
return mergeNewOfficiallySupportedChainsState(state, [
const rnbwChainState = state as RainbowChainsState;
return mergeNewOfficiallySupportedChainsState(rnbwChainState, [
ChainId.avalanche,
ChainId.avalancheFuji,
]);
},

// version 3 added support for Blast
function v3(state) {
return mergeNewOfficiallySupportedChainsState(state, [ChainId.blast]);
function v3(state: unknown) {
const rnbwChainState = state as RainbowChainsState;
return mergeNewOfficiallySupportedChainsState(rnbwChainState, [
ChainId.blast,
]);
},

function v4(state) {
function v4(state: unknown) {
const rnbwChainState = state as RainbowChainsState;
return removeCustomRPC({
state,
state: rnbwChainState,
rpcUrl: 'https://rpc.zora.co',
rainbowChains: state.rainbowChains,
rainbowChains: rnbwChainState.rainbowChains,
});
},

// version 5 added support for Degen
function v5(state) {
return mergeNewOfficiallySupportedChainsState(state, [ChainId.degen]);
function v5(state: unknown) {
const rnbwChainState = state as RainbowChainsState;
return mergeNewOfficiallySupportedChainsState(rnbwChainState, [
ChainId.degen,
]);
},

function v6(state) {
function v6(state: unknown) {
const rnbwChainState = state as RainbowChainsState;
if (
!state.rainbowChains[zora.id] ||
state.rainbowChains[zora.id]?.chains.length === 0
!rnbwChainState.rainbowChains[zora.id] ||
rnbwChainState.rainbowChains[zora.id]?.chains.length === 0
) {
return addCustomRPC({ chain: zora, state });
return addCustomRPC({ chain: zora, state: rnbwChainState });
}
return state;
},

function v7(state) {
function v7(state: unknown) {
return state;
},

function v8(state) {
function v8(state: unknown) {
const rnbwChainState = state as RainbowChainsState;
if (
!state.rainbowChains[degen.id] ||
state.rainbowChains[degen.id]?.chains.length === 0
!rnbwChainState.rainbowChains[degen.id] ||
rnbwChainState.rainbowChains[degen.id]?.chains.length === 0
) {
return addCustomRPC({ chain: degen, state });
return addCustomRPC({
chain: degen,
state: state as RainbowChainsState,
});
}
return state;
},
function v9(state) {
return replaceChainsWithInitial(state);
function v9(state: unknown) {
return replaceChainsWithInitial(state as RainbowChainsState);
},
function v10(state: unknown) {
const rnbwState = state as RainbowChainsState;
rnbwState.rainbowChains = getInitialRainbowChains();
return rnbwState;
},
],
}),
Expand Down
2 changes: 1 addition & 1 deletion src/core/state/userChains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const userChainsStore = createStore<UserChainsState>(
{
persist: {
name: 'userChains',
version: 4,
version: 5,
},
},
);
Expand Down
17 changes: 17 additions & 0 deletions src/core/utils/persistOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ interface PersistOptionsWithMigrations {
]
>,
): R<Final>;
<Final, A, B, C, D, E, F, G, H, I>(
opts: Opts<
Final,
[
(s: any) => A,
(s: A) => B,
(s: B) => C,
(s: C) => D,
(s: D) => E,
(s: E) => F,
(s: F) => G,
(s: G) => H,
(s: H) => I,
(s: I) => Final,
]
>,
): R<Final>;

// if you need more migrations, add more overloads here
}
Expand Down
27 changes: 12 additions & 15 deletions src/entries/popup/hooks/useRainbowChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import { useRainbowChainsStore } from '~/core/state';

export const useRainbowChains = () => {
const rainbowChains = useRainbowChainsStore.use.rainbowChains();
const chains = useMemo(
() =>
Object.values(rainbowChains)
.map((rainbowChain) =>
rainbowChain.chains.find(
(chain) =>
chain.rpcUrls.default.http[0] === rainbowChain.activeRpcUrl,
),
)
.filter(Boolean),
[rainbowChains],
);

return {
rainbowChains: chains,
};
const chains = useMemo(() => {
return Object.values(rainbowChains)
.map((rainbowChain) =>
rainbowChain.chains.find(
(chain) =>
chain.rpcUrls.default.http[0] === rainbowChain.activeRpcUrl,
),
)
.filter(Boolean);
}, [rainbowChains]);

return { rainbowChains: chains };
};

0 comments on commit bc9d511

Please sign in to comment.