Created
February 7, 2022 16:05
-
-
Save BetterProgramming/834b7d1ad5518bf5d25ad3eee11d7f78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import styles from "../styles/Home.module.css"; | |
import { useWeb3React } from "@web3-react/core"; | |
import { InjectedConnector } from "@web3-react/injected-connector"; | |
import { abi } from "../constants/abi"; | |
import { useState, useEffect } from "react"; | |
import { ethers } from "ethers"; | |
export const injected = new InjectedConnector(); | |
export default function Home() { | |
const [hasMetamask, setHasMetamask] = useState(false); | |
useEffect(() => { | |
if (typeof window.ethereum !== "undefined") { | |
setHasMetamask(true); | |
} | |
}); | |
const { | |
active, | |
activate, | |
chainId, | |
account, | |
library: provider, | |
} = useWeb3React(); | |
async function connect() { | |
if (typeof window.ethereum !== "undefined") { | |
try { | |
await activate(injected); | |
setHasMetamask(true); | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
} | |
async function execute() { | |
if (active) { | |
const signer = provider.getSigner(); | |
const contractAddress = "0x5FbDB2315678afecb367f032d93F642f64180aa3"; | |
const contract = new ethers.Contract(contractAddress, abi, signer); | |
try { | |
await contract.store(42); | |
} catch (error) { | |
console.log(error); | |
} | |
} else { | |
console.log("Please install MetaMask"); | |
} | |
} | |
return ( | |
<div> | |
{hasMetamask ? ( | |
active ? ( | |
"Connected! " | |
) : ( | |
<button onClick={() => connect()}>Connect</button> | |
) | |
) : ( | |
"Please install metamask" | |
)} | |
{active ? <button onClick={() => execute()}>Execute</button> : ""} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment