Skip to content

Commit

Permalink
Increment store persist version
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Sep 18, 2023
1 parent 79a3101 commit 798a38a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/features/chains/chainConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function tryParseChainConfig(input: string, mp?: MultiProvider): ParseRes

const chainConfig = result.data as ChainConfig;

// Ensure http is used for RPCs (and not http)
// Ensure https is used for RPCs
const rpcUrls = chainConfig.rpcUrls;
if (rpcUrls?.some((r) => !r.http.startsWith('https://'))) {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/features/chains/useChainConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const ChainMetadataArraySchema = z.array(ChainMetadataSchema);

// Use the chainConfigs from the store
export function useChainConfigs() {
return useStore((s) => s.chainConfigsV2);
return useStore((s) => s.chainConfigs);
}

// Use the chainConfigs and setChainConfigs from the store (i.e. Read/Write)
export function useChainConfigsRW() {
return useStore((s) => ({
chainConfigs: s.chainConfigsV2,
chainConfigs: s.chainConfigs,
setChainConfigs: s.setChainConfigs,
}));
}
Expand Down
14 changes: 9 additions & 5 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import { ChainConfig } from './features/chains/chainConfig';
import { buildSmartProvider } from './features/providers/SmartMultiProvider';
import { logger } from './utils/logger';

// Increment this when persist state has breaking changes
const PERSIST_STATE_VERSION = 1;

// Keeping everything here for now as state is simple
// Will refactor into slices as necessary
interface AppState {
chainConfigsV2: ChainMap<ChainConfig>; // v2 because schema changed
chainConfigs: ChainMap<ChainConfig>;
setChainConfigs: (configs: ChainMap<ChainConfig>) => void;
multiProvider: MultiProvider;
setMultiProvider: (mp: MultiProvider) => void;
Expand All @@ -21,9 +24,9 @@ interface AppState {
export const useStore = create<AppState>()(
persist(
(set) => ({
chainConfigsV2: {},
chainConfigs: {},
setChainConfigs: (configs: ChainMap<ChainConfig>) => {
set({ chainConfigsV2: configs, multiProvider: buildSmartProvider(configs) });
set({ chainConfigs: configs, multiProvider: buildSmartProvider(configs) });
},
multiProvider: buildSmartProvider({}),
setMultiProvider: (mp: MultiProvider) => {
Expand All @@ -34,15 +37,16 @@ export const useStore = create<AppState>()(
}),
{
name: 'hyperlane', // name in storage
partialize: (state) => ({ chainConfigsV2: state.chainConfigsV2 }), // fields to persist
version: PERSIST_STATE_VERSION,
partialize: (state) => ({ chainConfigs: state.chainConfigs }), // fields to persist
onRehydrateStorage: () => {
logger.debug('Rehydrating state');
return (state, error) => {
if (error || !state) {
logger.error('Error during hydration', error);
return;
}
state.setMultiProvider(buildSmartProvider(state.chainConfigsV2));
state.setMultiProvider(buildSmartProvider(state.chainConfigs));
logger.debug('Hydration finished');
};
},
Expand Down

0 comments on commit 798a38a

Please sign in to comment.