From 3af53b51df70f3dc5c522a468b0be70352966e0d Mon Sep 17 00:00:00 2001 From: Jay Keraliya Date: Fri, 12 May 2023 16:59:21 +0530 Subject: [PATCH] Network checking is not done please check it (georli testnet is working and transfaring eth to unkonwn account) Fixes #3 --- components/ConnectButton.tsx | 22 ++++++++++++++++++++++ components/Mint.tsx | 20 ++++++++++++++++++++ utils/networks.ts | 11 +++++++++++ 3 files changed, 53 insertions(+) create mode 100644 utils/networks.ts diff --git a/components/ConnectButton.tsx b/components/ConnectButton.tsx index cd634e9..1957879 100644 --- a/components/ConnectButton.tsx +++ b/components/ConnectButton.tsx @@ -2,6 +2,8 @@ import React, { useEffect, useState } from "react"; import { useWallet } from "@/context/WalletContext"; import { ethers } from "ethers"; +import { sepolia } from "@/utils/networks"; +import { toast } from "react-hot-toast"; type Props = { className?: string; @@ -14,8 +16,26 @@ const ConnectButton = ({ className }: Props) => { const [account, setAccount] = useState(); + const switchNetwork = async () => { + try { + await window.ethereum.request({ + method: "wallet_switchEthereumChain", + params: [{ chainId: sepolia.chainId }], + }); + } catch (error) { + toast.error("Please switch to Sepolia Network"); + } + }; + const connectWallet = async () => { + if (!window.ethereum) { + toast.error("Please install MetaMask to use this dApp"); + return; + } + await switchNetwork(); const provider = new ethers.BrowserProvider(window.ethereum); + const chainId = await provider.getNetwork(); + const signer = await provider.getSigner(); const account = await signer.getAddress(); setAddress(account); @@ -24,9 +44,11 @@ const ConnectButton = ({ className }: Props) => { useEffect(() => { const getAccount = async () => { + if (!window.ethereum) return; const accounts = await window.ethereum.request({ method: "eth_accounts", }); + switchNetwork(); if (accounts.length > 0) { setAddress(accounts[0]); setAccount(accounts[0]); diff --git a/components/Mint.tsx b/components/Mint.tsx index 730cc68..3cb87e5 100644 --- a/components/Mint.tsx +++ b/components/Mint.tsx @@ -7,6 +7,7 @@ import { useState } from "react"; import abi from "../artifacts/contracts/ByteBunch.sol/ByteBunch.json"; import NFTNumber from "./NFTNumber"; import Modal from "./Modal"; +import { sepolia } from "@/utils/networks"; type Props = {}; @@ -19,12 +20,31 @@ const Mint = (props: Props) => { const [transactionHash, setTransactionHash] = useState(""); const [open, setOpen] = useState(false); + const switchNetwork = async () => { + try { + await window.ethereum.request({ + method: "wallet_switchEthereumChain", + params: [{ chainId: sepolia.chainId }], + }); + } catch (error) { + toast.error("Please switch to Sepolia Network"); + } + }; + const mintNFT = async () => { const notification = toast.loading("Minting NFT..."); setLoading(true); setOpen(false); try { const provider = new ethers.BrowserProvider(window.ethereum); + const network = await provider.getNetwork(); + const chainId = Number(ethers.formatUnits(network.chainId)) * 10e17; + + if (chainId !== parseInt(sepolia.chainId)) { + switchNetwork(); + throw new Error("Please switch to Sepolia Network and try again"); + } + const signer = await provider.getSigner(); const contract = new ethers.Contract( process.env.NEXT_PUBLIC_CONTRACT_ADDRESS!, diff --git a/utils/networks.ts b/utils/networks.ts new file mode 100644 index 0000000..7aed561 --- /dev/null +++ b/utils/networks.ts @@ -0,0 +1,11 @@ +export const sepolia = { + chainId: `0x${Number(11155111).toString(16)}`, + chainName: "Sepolia", + nativeCurrency: { + name: "Sepolia", + symbol: "ETH", + decimals: 18, + }, + rpcUrls: ["https://rpc.sepolia.io"], + blockExplorerUrls: ["https://sepolia.io"], +};