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

Require the correct chain #35

Merged
merged 2 commits into from
Aug 20, 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
10 changes: 9 additions & 1 deletion src/components/wallet/RequireWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Box } from '@mui/material';
import React from 'react';

import { ConnectWalletButton, FMPTypography } from '@/components';
import { defaultChain } from '@/config/web3modal';
import { useWallet } from '@/hooks/useWallet';

interface RequireWalletProps {
Expand All @@ -12,7 +13,9 @@ interface RequireWalletProps {
const RequireWallet: React.FC<RequireWalletProps> = ({ children, message }) => {
const { selectedWallet, selectedAccount } = useWallet();

if (selectedAccount && selectedWallet) {
const connectedToCorrectNetwork = selectedWallet.chainId === defaultChain.chainId;

if (selectedAccount && selectedWallet && connectedToCorrectNetwork) {
return <>{children}</>;
}

Expand All @@ -31,6 +34,11 @@ const RequireWallet: React.FC<RequireWalletProps> = ({ children, message }) => {
<FMPTypography variant="h6" gutterBottom>
{message || 'You need to connect your Web3 wallet to access this content.'}
</FMPTypography>
{!connectedToCorrectNetwork && (
<FMPTypography variant="body1" color="error">
Please connect to the correct network. {defaultChain.name} is required.
</FMPTypography>
)}
<ConnectWalletButton />
</Box>
</Box>
Expand Down
6 changes: 4 additions & 2 deletions src/config/web3modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const chains = {
production: zkSyncSepoliaTestnet,
};

export const defaultChain = chains[nodeEnv];

if (!chains[nodeEnv]) {
throw new Error(`Chain not found for node env: ${nodeEnv}`);
}
Expand All @@ -58,10 +60,10 @@ const Web3ModalConfig = {
projectId,
siweConfig,
ethersConfig,
defaultChain,
enableAnalytics: true,
chains: [chains[nodeEnv]],
chains: [defaultChain],
allowUnsupportedChain: false,
defaultChain: chains[nodeEnv],
};

export default Web3ModalConfig;
8 changes: 5 additions & 3 deletions src/contexts/WalletContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ethers } from 'ethers';
import { createContext, ReactNode } from 'react';

export interface WalletDetail {
chainId?: number;
provider?: ethers.Eip1193Provider;
info?: { name?: string; icon?: string };
}
Expand All @@ -32,16 +33,17 @@ export const WalletProviderContext = createContext<WalletProviderContextType | u
export const WalletProvider = ({ children }: WalletProviderProps) => {
const { open: connectWallet } = useWeb3Modal();
const { disconnect } = useDisconnect();
const { isConnected, address } = useWeb3ModalAccount();
const { isConnected, address, chainId: selectedChainId } = useWeb3ModalAccount();
const { walletProvider } = useWeb3ModalProvider();
const { walletInfo } = useWalletInfo();

const contextValue: WalletProviderContextType = {
isConnected,
selectedAccount: address,
selectedWallet: { provider: walletProvider, info: walletInfo, chainId: selectedChainId },

connectWallet,
disconnectWallet: disconnect,
selectedAccount: address,
selectedWallet: { provider: walletProvider, info: walletInfo },
};

return <WalletProviderContext.Provider value={contextValue}>{children}</WalletProviderContext.Provider>;
Expand Down