Skip to content

Commit

Permalink
feat: basic architecture (#1)
Browse files Browse the repository at this point in the history
closes ZKS-104

Description:
- Create a DataProvider that will handle all requests
  • Loading branch information
0xtiti authored Jul 16, 2024
2 parents a504371 + 7ab5a8d commit 1c3d621
Show file tree
Hide file tree
Showing 26 changed files with 368 additions and 28 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NEXT_PUBLIC_API_URL= # Example: https://api.example.com
NEXT_PUBLIC_RPC_URL= # Example: https://localhost:8545
NEXT_PUBLIC_PROJECT_ID= # ProjectID from WalletConnect
NEXT_PUBLIC_ALCHEMY_KEY= # API key from Alchemy
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
echo "NEXT_PUBLIC_RPC_URL=${{ secrets.NEXT_PUBLIC_RPC_URL }}" >> .env
echo "NEXT_PUBLIC_PROJECT_ID=${{ secrets.NEXT_PUBLIC_PROJECT_ID }}" >> .env
echo "NEXT_PUBLIC_ALCHEMY_KEY=${{ secrets.NEXT_PUBLIC_ALCHEMY_KEY }}" >> .env
echo "NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }}" >> .env
- name: run Cypress and Jest
uses: cypress-io/github-action@v6
Expand Down
18 changes: 18 additions & 0 deletions src/components/CustomHead.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Head from 'next/head';

interface MetadataProps {
title: string;
description?: string;
image?: string;
type?: string;
}

export const CustomHead = ({ title }: MetadataProps) => {
return (
<Head>
<title>{`${title} - ZKchainHub`}</title>
<meta property='og:title' content={`${title} - ZKchainHub`} />
<meta name='twitter:title' content={`${title} - ZKchainHub`} />
</Head>
);
};
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './Theme';
export * from './Disclaimer';
export * from './CustomHead';
2 changes: 2 additions & 0 deletions src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export const getEnv = (): Env => {
const NEXT_PUBLIC_RPC_URL = process.env.NEXT_PUBLIC_RPC_URL;
const NEXT_PUBLIC_PROJECT_ID = process.env.NEXT_PUBLIC_PROJECT_ID;
const NEXT_PUBLIC_ALCHEMY_KEY = process.env.NEXT_PUBLIC_ALCHEMY_KEY;
const NEXT_PUBLIC_API_URL = process.env.NEXT_PUBLIC_API_BASE_URL;

return {
RPC_URL: NEXT_PUBLIC_RPC_URL as string,
PROJECT_ID: NEXT_PUBLIC_PROJECT_ID as string,
ALCHEMY_KEY: NEXT_PUBLIC_ALCHEMY_KEY as string,
API_URL: NEXT_PUBLIC_API_URL as string,
};
};
2 changes: 1 addition & 1 deletion src/containers/Footer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { styled } from '@mui/material/styles';
import { useCustomTheme } from '~/hooks/useTheme';
import { useCustomTheme } from '~/hooks/useContext/useTheme';

import { FOOTER_HEIGHT } from '~/utils';

Expand Down
2 changes: 1 addition & 1 deletion src/containers/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IconButton } from '@mui/material';
import LightModeIcon from '@mui/icons-material/LightMode';
import DarkModeIcon from '@mui/icons-material/DarkMode';

import { useCustomTheme } from '~/hooks/useTheme';
import { useCustomTheme } from '~/hooks/useContext/useTheme';
import { zIndex, HEADER_HEIGHT } from '~/utils';

export const Header = () => {
Expand Down
60 changes: 60 additions & 0 deletions src/data/chainMockData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[
{
"name": "ZKSync Era",
"chainId": 324,
"website": "https://example.com",
"explorer": "https://example.com",
"launchDate": "2023-12-05",
"environment": "Production",
"nativeToken": "ETH",
"chainType": "ZKRollup",
"lastBlock": 123456789,
"lastBlockVerified": 123456788,
"transactionsPerSecond": 15,
"totalBatchesCommitted": 1234567890,
"totalBatchesExecuted": 1234567890,
"totalBatchesVerified": 123456788,
"averageBlockTime": 100000,
"tvl": {
"ETH": {
"value": 500000000,
"address": "0x79db...d692"
},
"USDT": {
"value": 100000000,
"address": "0x79db...d692"
},
"USDC": {
"value": 50000000,
"address": "0x79db...d692"
},
"WBTC": {
"value": 30000000,
"address": "0x79db...d692"
}
},
"rpcs": [
{
"status": "Active",
"url": "https://lrpc.com"
},
{
"status": "Active",
"url": "https://blastapi.com"
},
{
"status": "Inactive",
"url": "https://llamarpc.com"
},
{
"status": "Active",
"url": "https://alchemy.com"
}
],
"feeParams": {
"batchOverheadL1Gas": 1234567890,
"computeOverheadPart": 1234567890,
"maxGasPerBatch": 123456788
}
}
]
91 changes: 91 additions & 0 deletions src/data/ecosystemMockData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"chains": [
{
"name": "zkSync Era",
"id": 324,
"nativeToken": "ETH",
"tvl": 1000000,
"type": "ZKRollup"
},
{
"name": "Teva Chain",
"id": 100,
"nativeToken": "ETH",
"tvl": 1000000,
"type": "ZKRollup"
},
{
"name": "Cronos zkEVM",
"id": 101,
"nativeToken": "ETH",
"tvl": 1000000,
"type": "Validium"
},
{
"name": "GRVT",
"id": 102,
"nativeToken": "ETH",
"tvl": 1000000,
"type": "Validium"
},
{
"name": "Lens",
"id": 103,
"nativeToken": "ETH",
"tvl": 1000000,
"type": "Validium"
},
{
"name": "ZKChain 104",
"id": 104,
"nativeToken": "ETH",
"tvl": 1000000,
"type": "ZKRollup"
},
{
"name": "ZKChain 105",
"id": 105,
"nativeToken": "ETH",
"tvl": 1000000,
"type": "Validium"
}
],
"tvl": [
{
"token": "ETH",
"value": 557596566
},
{
"token": "USDC",
"value": 90091851
},
{
"token": "KOI",
"value": 32757850
},
{
"token": "USDT",
"value": 18021853
},
{
"token": "WBTC",
"value": 12620248
},
{
"token": "wstETH",
"value": 3552439
},
{
"token": "MUTE",
"value": 2071481
},
{
"token": "rETH",
"value": 1404096
},
{
"token": "DAI",
"value": 1080375
}
]
}
19 changes: 0 additions & 19 deletions src/hooks/ScrollToTop.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './ScrollToTop';
export * from './useStateContext';
export * from './useContext';
1 change: 1 addition & 0 deletions src/hooks/useContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useStateContext';
13 changes: 13 additions & 0 deletions src/hooks/useContext/useData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useContext } from 'react';

import { DataContext } from '~/providers/DataProvider';

export const useData = () => {
const context = useContext(DataContext);

if (context === undefined) {
throw new Error('useData must be used within a StateProvider');
}

return context;
};
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions src/pages/[chain]/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CustomHead } from '~/components';

const Chain = () => {
const title = 'Chain placeholder';

return (
<>
<CustomHead title={title} />
{/* TODO: Add chain page containers */}
</>
);
};

export default Chain;
5 changes: 5 additions & 0 deletions src/pages/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const ErrorPage = () => {
return <div>Sorry, something went wrong.</div>;
};

export default ErrorPage;
6 changes: 3 additions & 3 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Head from 'next/head';

import { Landing } from '~/containers';

const Home = () => {
const Ecosystem = () => {
return (
<>
<Head>
<title>Web3 Boilerplate</title>
<title>ZKchainHub</title>
</Head>
<Landing />
</>
);
};

export default Home;
export default Ecosystem;
71 changes: 71 additions & 0 deletions src/providers/DataProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { createContext, useState, useEffect, ReactNode } from 'react';
import { useQuery, UseQueryResult } from '@tanstack/react-query';
import { useRouter } from 'next/router';

import { ChainData, EcosystemData } from '~/types';
import { fetchEcosystemData, fetchChainData } from '~/utils';

type ContextType = {
selectedChain?: ChainData;
setSelectedChain: (val: ChainData) => void;

isEcosystemLoading: boolean;
isChainLoading: boolean;
refetchChainData: (options: { throwOnError: boolean; cancelRefetch: boolean }) => Promise<UseQueryResult>;

ecosystemData: EcosystemData;
chainData: ChainData;
};

interface DataProps {
children: ReactNode;
}

export const DataContext = createContext({} as ContextType);

export const DataProvider = ({ children }: DataProps) => {
const [selectedChain, setSelectedChain] = useState<ChainData>();
const router = useRouter();

const {
isLoading: isEcosystemLoading,
data: ecosystemData,
isError: isEcosystemError,
} = useQuery({
queryKey: ['ecosystem'],
queryFn: fetchEcosystemData,
});

const {
isLoading: isChainLoading,
data: chainData,
isError: isChainError,
refetch: refetchChainData,
} = useQuery({
queryKey: ['chainData', selectedChain?.chainId],
queryFn: () => fetchChainData(selectedChain!.chainId!),
enabled: !!selectedChain?.chainId,
});

useEffect(() => {
if (isEcosystemError || isChainError) {
router.push('/error');
}
}, [isEcosystemError, isChainError, router]);

return (
<DataContext.Provider
value={{
selectedChain,
setSelectedChain,
isEcosystemLoading,
isChainLoading,
ecosystemData,
chainData,
refetchChainData,
}}
>
{children}
</DataContext.Provider>
);
};
5 changes: 4 additions & 1 deletion src/providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ReactNode } from 'react';
import { StateProvider } from './StateProvider';
import { ThemeProvider } from './ThemeProvider';
import { WalletProvider } from './WalletProvider';
import { DataProvider } from './DataProvider';

type Props = {
children: ReactNode;
Expand All @@ -12,7 +13,9 @@ export const Providers = ({ children }: Props) => {
return (
<ThemeProvider>
<StateProvider>
<WalletProvider>{children}</WalletProvider>
<WalletProvider>
<DataProvider>{children}</DataProvider>
</WalletProvider>
</StateProvider>
</ThemeProvider>
);
Expand Down
1 change: 1 addition & 0 deletions src/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface Env {
RPC_URL: string;
PROJECT_ID: string;
ALCHEMY_KEY: string;
API_URL: string;
}

export interface Constants {
Expand Down
Loading

0 comments on commit 1c3d621

Please sign in to comment.