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

feat: add default networks #24

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ name: Pull request Analysis and preview ( Mainnet )
on:
pull_request:
branches: [ "main" ]
types: [opened, synchronize, edited, ready_for_review]

permissions:
pull-requests: write
Expand Down
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;