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

Network checking is not done please check it (georli testnet is worki… #7

Merged
merged 2 commits into from
May 12, 2023
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
22 changes: 18 additions & 4 deletions components/ConnectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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 = {
Expand All @@ -15,12 +16,26 @@ const ConnectButton = ({ className }: Props) => {

const [account, setAccount] = useState<string>();

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("MetaMask not installed");
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);
Expand All @@ -29,12 +44,11 @@ const ConnectButton = ({ className }: Props) => {

useEffect(() => {
const getAccount = async () => {
if (!window.ethereum) {
return;
}
if (!window.ethereum) return;
const accounts = await window.ethereum.request({
method: "eth_accounts",
});
switchNetwork();
if (accounts.length > 0) {
setAddress(accounts[0]);
setAccount(accounts[0]);
Expand Down
20 changes: 20 additions & 0 deletions components/Mint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand All @@ -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!,
Expand Down
11 changes: 11 additions & 0 deletions utils/networks.ts
Original file line number Diff line number Diff line change
@@ -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"],
};