Skip to content

Commit

Permalink
update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ziaukhan committed Aug 1, 2022
1 parent 677c5b4 commit 90173b4
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions step06_metamask_connect_nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
import { signMessage } from "../utils/sign";
import Link from "next/link";
import Metamask from "../component/Metamask";
import { ethers } from "ethers";
//import { ethers } from "ethers";

interface ClientStatus{
isConnected: boolean;
Expand All @@ -18,16 +18,15 @@ const Index: NextPage = () => {
isConnected: false,
});

let provider: ethers.providers.Web3Provider;
//let provider: ethers.providers.Web3Provider;


const checkConnection = async () => {
const { ethereum } = window as any;
if (ethereum) {
sethaveMetamask(true);
provider = new ethers.providers.Web3Provider((window as any).ethereum);
const accounts: string[] = await provider.send("eth_accounts", []);
//const accounts: string[] = await ethereum.request({ method: "eth_accounts" });

const accounts: string[] = await ethereum.request({ method: "eth_accounts" });
if (accounts.length > 0) {
setClientStatus({
isConnected: true,
Expand All @@ -45,18 +44,21 @@ const Index: NextPage = () => {


const connectWeb3 = async () => {
console.log("In ConnectWeb3: Start");
//console.log("In ConnectWeb3: Start");
try {
const { ethereum } = window as any;

if (!ethereum) {
console.log("Metamask not detected");
return;
}
const accounts: string[] = await provider.send("eth_requestAccounts", []);
//const accounts: string[] = await ethereum.request({method: "eth_requestAccounts"});

const accounts: string[] = await ethereum.request({method: "eth_requestAccounts"});

//console.log("In ConnectWeb3: After")
//provider = new ethers.providers.Web3Provider((window as any).ethereum);


console.log("In ConnectWeb3: After")

setClientStatus({
isConnected: true,
Expand All @@ -82,7 +84,7 @@ const Index: NextPage = () => {
<main>
<h1 className="main-title">Awesome DApp</h1>

<p>
<div>
{!haveMetamask ? (
<Metamask />
) : clientStatus.isConnected ? (
Expand All @@ -105,7 +107,7 @@ const Index: NextPage = () => {
</button>
</>
)}
</p>
</div>
{/* ---- */}
</main>
</section>
Expand Down

0 comments on commit 90173b4

Please sign in to comment.