Skip to content

Commit

Permalink
feat: add default networks
Browse files Browse the repository at this point in the history
  • Loading branch information
brusherru committed Jun 26, 2024
1 parent 6d1338f commit e390bce
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/store/useNetworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createJSONStorage, persist } from 'zustand/middleware';
import { A, O, pipe } from '@mobily/ts-belt';

import { Network } from '../types/networks';
import DEFAULT_NETWORKS from '../utils/defaultNetworks';

type NetworkState = {
selectedIndex: null | number;
Expand All @@ -25,8 +26,8 @@ const NETWORKS_STORE_KEY = 'networks';
const useNetworks = create(
persist<NetworkState & NetworkActions & NetworkSelectors>(
(set, get) => ({
selectedIndex: null,
networks: [],
selectedIndex: 0,
networks: [...DEFAULT_NETWORKS],
// Actions
addNetwork: (net: Network) => {
const state = get();
Expand All @@ -37,6 +38,9 @@ const useNetworks = create(
set(newState);
if (state.selectedIndex === null) {
set({ selectedIndex: 0 });
} else {
const i = newState.networks.findIndex((n) => n === net);
set({ selectedIndex: i });
}
},
switchNetwork: (idx: number) => {
Expand All @@ -62,6 +66,26 @@ const useNetworks = create(
{
name: NETWORKS_STORE_KEY,
storage: createJSONStorage(() => localStorage),
partialize: (state) => ({
...state,
networks: state.networks.filter((n) => !DEFAULT_NETWORKS.includes(n)),
}),
merge: (persisted, current) => {
const persistedNetworks =
persisted &&
Object.hasOwn(persisted, 'networks') &&
(persisted as NetworkState).networks instanceof Array
? (persisted as NetworkState).networks
: <Network[]>[];
return {
...current,
...(persisted || {}),
networks: [
...DEFAULT_NETWORKS,
...persistedNetworks.filter((n) => !DEFAULT_NETWORKS.includes(n)),
],
};
},
}
)
);
Expand Down
26 changes: 26 additions & 0 deletions src/utils/defaultNetworks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Network } from '../types/networks';

const DEFAULT_NETWORKS: Network[] = [
{
name: 'MainNet',
jsonRPC: 'https://wallet-api.spacemesh.network',
explorerUrl: 'https://explorer.spacemesh.io',
hrp: 'sm',
genesisID: '9eebff023abb17ccb775c602daade8ed708f0a50',
genesisTime: 1689310800000,
layerDuration: 300,
layersPerEpoch: 4032,
},
{
name: 'TestNet 13',
jsonRPC: 'https://testnet-13-api.spacemesh.network',
explorerUrl: 'https://testnet-13-explorer.spacemesh.network',
hrp: 'stest',
genesisID: 'cd97832ec3ae9851e6277d8bd88bc17b4508e2b2',
genesisTime: 1718964000000,
layerDuration: 300,
layersPerEpoch: 288,
},
];

export default DEFAULT_NETWORKS;

0 comments on commit e390bce

Please sign in to comment.