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

feat(client): implement web3Modal #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions client/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
const nextConfig = {
reactStrictMode: true,
env: {
BASE_URL: process.env.NEXT_PUBLIC_SITE_URL,
CONTRACT_ADDRESS: process.env.NEXT_PUBLIC_CONTRACT_ADDRESS,
WALLET_CONNECT_ID: process.env.WALLET_CONNECT_ID,
}
}

Expand Down
5 changes: 4 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
"@types/react": "18.0.28",
"@types/react-dom": "18.0.11",
"@walletconnect/ethereum-provider": "^2.5.2",
"@web3modal/ethereum": "^2.2.2",
"@web3modal/react": "^2.2.2",
"dotenv": "^16.0.3",
"eslint": "8.34.0",
"eslint-config-next": "13.2.1",
"ethers": "^5.7.2",
"next": "13.2.1",
"react": "^18.2.0",
"react-dom": "18.2.0",
"typescript": "4.9.5"
"typescript": "4.9.5",
"wagmi": "^0.12.9"
},
"devDependencies": {
"autoprefixer": "^10.4.13",
Expand Down
29 changes: 26 additions & 3 deletions client/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import '@/styles/globals.css'
import type { AppProps } from 'next/app'
import "@/styles/globals.css";
import type { AppProps } from "next/app";

import {
EthereumClient,
w3mConnectors,
w3mProvider,
} from "@web3modal/ethereum";
import { configureChains, createClient, WagmiConfig } from "wagmi";
import { bscTestnet } from "wagmi/chains";

const chains = [bscTestnet];
const projectId = process.env.WALLET_CONNECT_ID!;

const { provider } = configureChains(chains, [w3mProvider({ projectId })]);
const wagmiClient = createClient({
autoConnect: true,
connectors: w3mConnectors({ projectId, version: 1, chains }),
provider,
});
export const ethereumClient = new EthereumClient(wagmiClient, chains);

export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
return (
<WagmiConfig client={wagmiClient}>
<Component {...pageProps} />;
</WagmiConfig>
);
}
92 changes: 38 additions & 54 deletions client/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,63 @@ import Head from "next/head";
import Button from "@mui/material/Button";
import { useEffect, useRef, useState } from "react";
import { ethers } from "ethers";
import auctionArtifact from "../web3/abi/token-sale.json";
import tokenSaleArtifact from "../web3/abi/token-sale.json";
import { Web3Button, Web3Modal } from "@web3modal/react";
import { ethereumClient } from "./_app";
import { useAccount, useContract, useProvider, useSigner } from "wagmi";

export default function Home() {
const [account, setAccount] = useState("0x0000");

const amountToBuy: any = useRef(0);

const [isConnected, setIsConnected] = useState(false);

const [tokenBalance, setTokenBalance] = useState(0);

const [bnbBalance, setBnbBalance] = useState(0);

const [price, setPrice] = useState(0);

const [maxAllocation, setMaxAllocation] = useState(0);
const { isConnected } = useAccount();

const weiToBnb: any = 1000000000000000000;
const provider = useProvider();

let provider: any;
const contract = useContract({
address: tokenSaleArtifact.address,
abi: tokenSaleArtifact.abi,
});

let signer: any;
const { data: signer } = useSigner();

let contract: any;
const [tokenBalance, setTokenBalance] = useState(0);

const [contractWithSigner, setContractWithSigner]: any = useState(null);
const [price, setPrice] = useState(0);

useEffect(() => {});
const [maxAllocation, setMaxAllocation] = useState(0);

async function connectWallet() {
if (window.ethereum == null) {
console.log("MetaMask not installed");
} else {
try {
provider = new ethers.providers.Web3Provider(window.ethereum);
const [isDisplayed, setIsDisplayed] = useState(false);

const address = await provider.send("eth_requestAccounts", []);
useEffect(() => {
setIsDisplayed(isConnected);

setAccount(address[0]);
console.log(provider);
}, [isConnected]);

signer = provider.getSigner();
// useEffect(() => {
// if (!contract) return;

contract = new ethers.Contract(
auctionArtifact.address,
auctionArtifact.abi,
provider
);
// async function init(contract: any): Promise<void> {
// setPrice(await contract?.price());
// }

const WithSigner = contract.connect(signer);
const tokenPrice = await contract.price();
const allocation = await contract.maxAllocation();


setContractWithSigner(WithSigner);
setMaxAllocation(allocation.toString() / weiToBnb);
setPrice(tokenPrice.toNumber() / weiToBnb);
setIsConnected(true);
} catch (error) {
console.log("Error connection...");

console.log(error);
}
}
}
// init(contract);
// console.log(price);
// }, [contract]);

async function buy(event: any) {
event.preventDefault();

const WithSigner = contract!.connect(signer!);

const dai = amountToBuy.current.value;

const overrides = {
value: ethers.utils.parseUnits(dai.toString(), 18),
gasLimit: 110000,
};

await contractWithSigner.buy(overrides);
await WithSigner.buy(overrides);
}

return (
Expand All @@ -91,14 +72,12 @@ export default function Home() {
<main>
<div className="bg-black w-full h-screen flex items-center justify-center">
<div className="absolute top-6 right-6">
<Button variant="outlined" onClick={connectWallet}>
{!isConnected ? "Connect wallet" : account}
</Button>
<Web3Button />
</div>
<div className="flex flex-row gap-6">
<div className="w-[450px] flex flex-col gap-8 relative h-[300px] text-center border-[1px] border-blue-700 rounded-[18px]">
<h4 className="mt-8 text-xl text-blue-600">
Your balance in BNB: {bnbBalance}BNB
Your balance in BNB: 0 BNB
</h4>

<h4 className="text-xl text-blue-600">1 token = {price} BNB</h4>
Expand All @@ -113,7 +92,7 @@ export default function Home() {
</div>

<div className="w-[450px] h-[300px] text-center border-[1px] border-blue-700 rounded-[18px]">
{!isConnected ? (
{!isDisplayed ? (
<h1 className="text-center text-blue-600 text-3xl mt-[30%] justify-center">
Please connect your wallet
</h1>
Expand Down Expand Up @@ -142,6 +121,11 @@ export default function Home() {
</div>
</div>
</main>

<Web3Modal
projectId={process.env.WALLET_CONNECT_ID!}
ethereumClient={ethereumClient}
/>
</>
);
}
Loading