Skip to content

Commit

Permalink
Network selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Apr 17, 2024
1 parent a8fc0a5 commit 0929c5f
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 40 deletions.
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
WS_CORETIME_CHAIN="WSS endpoint of the coretime chain"
WS_RELAY_CHAIN="WSS endpoint of the coretime relay chain"
WS_ROCOCO_CORETIME_CHAIN="WSS endpoint of the coretime chain"
WS_KUSAMA_CORETIME_CHAIN="WSS endpoint of the coretime chain"
WS_ROCOCO_RELAY_CHAIN="WSS endpoint of the coretime relay chain"
WS_KUSAMA_RELAY_CHAIN="WSS endpoint of the coretime relay chain"
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ WORKDIR /corehub
COPY . .

# Set the necessary environment variables
ENV WS_CORETIME_CHAIN="ws://127.0.0.1:9910"
ENV WS_RELAY_CHAIN="ws://127.0.0.1:9900"
ENV WS_ROCOCO_CORETIME_CHAIN="ws://127.0.0.1:9910"
ENV WS_ROCOCO_RELAY_CHAIN="ws://127.0.0.1:9900"

RUN apk add --no-cache libc6-compat

Expand Down
6 changes: 4 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const nextConfig = {
domains: ['github.com'],
},
env: {
WS_CORETIME_CHAIN: process.env.WS_CORETIME_CHAIN || '',
WS_RELAY_CHAIN: process.env.WS_RELAY_CHAIN,
WS_ROCOCO_CORETIME_CHAIN: process.env.WS_ROCOCO_CORETIME_CHAIN || '',
WS_KUSAMA_COREITME_CHAIN: process.env.WS_KUSAMA_COREITME_CHAIN || '',
WS_ROCOCO_RELAY_CHAIN: process.env.WS_ROCOCO_RELAY_CHAIN,
WS_KUSAMA_RELAY_CHAIN: process.env.WS_KUSAMA_RELAY_CHAIN,
},
};

Expand Down
26 changes: 26 additions & 0 deletions src/components/Elements/NetworkSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useNetwork } from '@/contexts/network';
import { FormControl, InputLabel, MenuItem, Select } from '@mui/material';

const RelaySelect = () => {
const { network, setNetwork } = useNetwork();
const handleChange = (e: any) => {
setNetwork(e.target.value);
};

return (
<FormControl sx={{ m: 2, minWidth: 200 }} fullWidth>
<InputLabel id='demo-simple-select-label'>Network</InputLabel>
<Select
id='network-select'
value={network}
label='Relay chain'
onChange={handleChange}
>
<MenuItem value='rococo'>Rococo</MenuItem>
<MenuItem value='kusama'>Kusama</MenuItem>
</Select>
</FormControl>
);
};

export default RelaySelect;
4 changes: 4 additions & 0 deletions src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TransferIcon } from '@/icons';

import styles from './index.module.scss';
import { StatusIndicator } from '../Elements';
import RelaySelect from '../Elements/NetworkSelect';

interface MenuItemProps {
label: string;
Expand Down Expand Up @@ -109,6 +110,9 @@ export const Sidebar = () => {
<StatusIndicator state={relayApiState} label='Relay chain' />
<StatusIndicator state={coretimeApiState} label='Coretime chain' />
</div>
<div>
<RelaySelect />
</div>
</div>
);
};
13 changes: 11 additions & 2 deletions src/contexts/apis/CoretimeApi/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { ApiState } from '@/contexts/apis/types';
import { useToast } from '@/contexts/toast';

import { connect, disconnect, initialState, reducer } from '../common';
import { WS_CORETIME_CHAIN } from '../consts';
import { WS_ROCOCO_CORETIME_CHAIN, WS_KUSAMA_CORETIME_CHAIN } from '../consts';
import { useNetwork } from '@/contexts/network';

const types = {
CoreIndex: 'u32',
Expand Down Expand Up @@ -36,6 +37,7 @@ const CoretimeApiContext = React.createContext(defaultValue);

const CoretimeApiContextProvider = (props: any) => {
const [state, dispatch] = useReducer(reducer, initialState);
const { network } = useNetwork();
const { toastError, toastSuccess } = useToast();

useEffect(() => {
Expand All @@ -48,7 +50,14 @@ const CoretimeApiContextProvider = (props: any) => {
}, [state.apiState, toastSuccess]);

const connectCoretime = () =>
connect(state, WS_CORETIME_CHAIN, dispatch, types);
connect(
state,
network === 'rococo'
? WS_ROCOCO_CORETIME_CHAIN
: WS_KUSAMA_CORETIME_CHAIN,
dispatch,
types
);

const disconnectCoretime = () => disconnect(state);

Expand Down
13 changes: 10 additions & 3 deletions src/contexts/apis/RelayApi/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { useToast } from '@/contexts/toast';
import { ParaId } from '@/models';

import { connect, disconnect, initialState, reducer } from '../common';
import { WS_RELAY_CHAIN } from '../consts';
import { WS_ROCOCO_RELAY_CHAIN, WS_KUSAMA_RELAY_CHAIN } from '../consts';
import { useNetwork } from '@/contexts/network';

const defaultValue = {
state: initialState,
Expand All @@ -26,6 +27,7 @@ const RelayApiContextProvider = (props: any) => {
const [state, dispatch] = useReducer(reducer, initialState);
const { toastError, toastSuccess } = useToast();
const [paraIds, setParaIds] = useState<ParaId[]>([]);
const { network } = useNetwork();

useEffect(() => {
state.apiError && toastError(`Failed to connect to relay chain`);
Expand All @@ -36,7 +38,12 @@ const RelayApiContextProvider = (props: any) => {
toastSuccess('Successfully connected to the relay chain');
}, [state.apiState, toastSuccess]);

const connectRelay = () => connect(state, WS_RELAY_CHAIN, dispatch);
const connectRelay = () =>
connect(
state,
network === 'rococo' ? WS_ROCOCO_RELAY_CHAIN : WS_KUSAMA_RELAY_CHAIN,
dispatch
);
const disconnectRelay = () => disconnect(state);

useEffect(() => {
Expand All @@ -51,7 +58,7 @@ const RelayApiContextProvider = (props: any) => {
setParaIds(paraIds);
};
fetchParaIds();
}, [state]);
}, [state, network]);

return (
<RelayApiContext.Provider
Expand Down
8 changes: 6 additions & 2 deletions src/contexts/apis/consts.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export const WS_RELAY_CHAIN = process.env.WS_RELAY_CHAIN ?? '';
export const WS_CORETIME_CHAIN = process.env.WS_CORETIME_CHAIN ?? '';
export const WS_ROCOCO_RELAY_CHAIN = process.env.WS_ROCOCO_RELAY_CHAIN ?? '';
export const WS_KUSAMA_RELAY_CHAIN = process.env.WS_ROCOCO_KUSAMA_CHAIN ?? '';
export const WS_ROCOCO_CORETIME_CHAIN =
process.env.WS_ROCOCO_CORETIME_CHAIN ?? '';
export const WS_KUSAMA_CORETIME_CHAIN =
process.env.WS_KUSAMA_CORETIME_CHAIN ?? '';
30 changes: 30 additions & 0 deletions src/contexts/network/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { createContext, useContext, useState } from 'react';

import { Network, NetworkContext } from '@/models';

const defaultNetworkData = {
network: 'rococo',
setNetwork: (_n: Network): void => {
/** */
},
};

const NetworkContextData = createContext<NetworkContext>(defaultNetworkData);

interface Props {
children: React.ReactNode;
}

const NetworkDataProvider = ({ children }: Props) => {
const [network, setNetwork] = useState<Network>('rococo');

return (
<NetworkContextData.Provider value={{ network, setNetwork }}>
{children}
</NetworkContextData.Provider>
);
};

const useNetwork = () => useContext(NetworkContextData);

export { NetworkDataProvider, useNetwork };
3 changes: 3 additions & 0 deletions src/contexts/regions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as NativeRegions from './native';
import { useCoretimeApi } from '../apis';
import { useCommon } from '../common';
import { useTasks } from '../tasks';
import { useNetwork } from '../network';

interface RegionsData {
regions: Array<RegionMetadata>;
Expand Down Expand Up @@ -51,6 +52,7 @@ const RegionDataProvider = ({ children }: Props) => {
const { fetchWorkplan, fetchRegionWorkload } = useTasks();

const context = useCommon();
const { network } = useNetwork();

const [regions, setRegions] = useState<Array<RegionMetadata>>([]);
const [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -114,6 +116,7 @@ const RegionDataProvider = ({ children }: Props) => {
context,
coretimeApi,
api,
network,
fetchWorkplan,
_getTaskFromWorkloadId,
]);
Expand Down
7 changes: 7 additions & 0 deletions src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export type ParaId = number;

export type BlockNumber = number;

export type Network = 'rococo' | 'kusama';

export type NetworkContext = {
network: string;
setNetwork: (_n: Network) => void;
};

export type Sender = {
address: string;
signer: Signer;
Expand Down
57 changes: 30 additions & 27 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import {
CoretimeApiContextProvider,
RelayApiContextProvider,
} from '@/contexts/apis';
import { WS_CORETIME_CHAIN } from '@/contexts/apis/consts';
import { WS_ROCOCO_CORETIME_CHAIN } from '@/contexts/apis/consts';
import { ContextDataProvider } from '@/contexts/common';
import { MarketProvider } from '@/contexts/market';
import { RegionDataProvider } from '@/contexts/regions';
import { SaleInfoProvider } from '@/contexts/sales';
import { TaskDataProvider } from '@/contexts/tasks';
import { ToastProvider } from '@/contexts/toast';
import { NetworkDataProvider } from '@/contexts/network';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();
Expand All @@ -49,32 +50,34 @@ export default function MyApp(props: MyAppProps) {
<ThemeProvider theme={theme}>
<CssBaseline />
<ToastProvider>
<CoretimeApiContextProvider>
<RelayApiContextProvider>
<UseInkathonProvider
appName='CoreHub'
connectOnInit={false}
defaultChain={{
network: '',
name: '',
rpcUrls: [WS_CORETIME_CHAIN],
}}
apiOptions={{ types: { Id } }}
>
<ContextDataProvider>
<TaskDataProvider>
<RegionDataProvider>
<MarketProvider>
<SaleInfoProvider>
{getLayout(<Component {...pageProps} />)}
</SaleInfoProvider>
</MarketProvider>
</RegionDataProvider>
</TaskDataProvider>
</ContextDataProvider>
</UseInkathonProvider>
</RelayApiContextProvider>
</CoretimeApiContextProvider>
<NetworkDataProvider>
<CoretimeApiContextProvider>
<RelayApiContextProvider>
<UseInkathonProvider
appName='CoreHub'
connectOnInit={false}
defaultChain={{
network: '',
name: '',
rpcUrls: [WS_ROCOCO_CORETIME_CHAIN],
}}
apiOptions={{ types: { Id } }}
>
<ContextDataProvider>
<TaskDataProvider>
<RegionDataProvider>
<MarketProvider>
<SaleInfoProvider>
{getLayout(<Component {...pageProps} />)}
</SaleInfoProvider>
</MarketProvider>
</RegionDataProvider>
</TaskDataProvider>
</ContextDataProvider>
</UseInkathonProvider>
</RelayApiContextProvider>
</CoretimeApiContextProvider>
</NetworkDataProvider>
</ToastProvider>
</ThemeProvider>
</CacheProvider>
Expand Down

0 comments on commit 0929c5f

Please sign in to comment.