From fd12a75f4b7405e3763daf7a57ca631aa017d30f Mon Sep 17 00:00:00 2001 From: chandrakumarreddy Date: Mon, 6 Jun 2022 09:29:20 +0530 Subject: [PATCH] add to whitelist --- my-app/constants/index.js | 67 +++++++++++++++++++++++++++++++++++ my-app/pages/index.js | 55 +++++++++++++++++++++++++++- my-app/public/crypto-devs.svg | 1 + 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 my-app/constants/index.js create mode 100644 my-app/public/crypto-devs.svg diff --git a/my-app/constants/index.js b/my-app/constants/index.js new file mode 100644 index 0000000..75fc35d --- /dev/null +++ b/my-app/constants/index.js @@ -0,0 +1,67 @@ +export const WHITELIST_CONTRACT_ADDRESS = + "0x9277b365d6b170f62d951cbedb91a9b07f8f4e4b"; +export const abi = [ + { + inputs: [ + { + internalType: "uint8", + name: "_maxWhitelistedAddresses", + type: "uint8", + }, + ], + stateMutability: "nonpayable", + type: "constructor", + }, + { + inputs: [], + name: "addToWhitelist", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [], + name: "maxWhitelistedAddresses", + outputs: [ + { + internalType: "uint8", + name: "", + type: "uint8", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [], + name: "numAddressesWhitelisted", + outputs: [ + { + internalType: "uint8", + name: "", + type: "uint8", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { + internalType: "address", + name: "", + type: "address", + }, + ], + name: "whitelistedAddresses", + outputs: [ + { + internalType: "bool", + name: "", + type: "bool", + }, + ], + stateMutability: "view", + type: "function", + }, +]; diff --git a/my-app/pages/index.js b/my-app/pages/index.js index ed390d7..0531024 100644 --- a/my-app/pages/index.js +++ b/my-app/pages/index.js @@ -2,11 +2,12 @@ import { useState, useRef, useEffect, useCallback } from "react"; import Head from "next/head"; import Web3Modal from "web3modal"; import styles from "styles/Home.module.css"; +import { WHITELIST_CONTRACT_ADDRESS, abi } from "constants"; export default function Home() { const [walletConnected, setWalletConnected] = useState(false); const [joinedWhitelist, setJoinedWhitelist] = useState(false); - const [loading, setLoading] = useState(false); + const [loading, setLoading] = useState(true); const [numberOfWhitelisted, setNumberOfWhitelisted] = useState(0); const web3ModalRef = useRef(); const getProviderOrSigner = useCallback(async (needSigner = false) => { @@ -26,6 +27,58 @@ export default function Home() { } return web3Provider; }, []); + const getNumberOfWhitelisted = async () => { + try { + const provider = await getProviderOrSigner(); + const whitelistContract = new ethers.Contract( + WHITELIST_CONTRACT_ADDRESS, + abi, + provider + ); + const _numberOfWhitelisted = + await whitelistContract.numAddressesWhitelisted(); + setNumberOfWhitelisted(_numberOfWhitelisted); + } catch (err) { + console.error(err); + } + }; + const addAddressToWhitelist = async () => { + try { + const signer = await getProviderOrSigner(true); + const whitelistContract = new ethers.Contract( + WHITELIST_CONTRACT_ADDRESS, + abi, + signer + ); + const tx = await whitelistContract.addToWhitelist(); + setLoading(true); + await tx.wait(); + setLoading(false); + await getNumberOfWhitelisted(); + setJoinedWhitelist(true); + } catch (err) { + console.error(err); + } + }; + const checkIfAddressInWhitelist = async () => { + try { + const signer = await getProviderOrSigner(true); + const whitelistContract = new ethers.Contract( + WHITELIST_CONTRACT_ADDRESS, + abi, + signer + ); + setLoading(true); + const address = await signer.getAddress(); + const _joinedWhitelist = await whitelistContract.whitelistedAddresses( + address + ); + setJoinedWhitelist(_joinedWhitelist); + setLoading(false); + } catch (err) { + console.error(err); + } + }; const connectWallet = useCallback(async () => { try { await getProviderOrSigner(); diff --git a/my-app/public/crypto-devs.svg b/my-app/public/crypto-devs.svg new file mode 100644 index 0000000..2e7d451 --- /dev/null +++ b/my-app/public/crypto-devs.svg @@ -0,0 +1 @@ + \ No newline at end of file