Skip to content

Commit

Permalink
add to whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrakumarreddy committed Jun 6, 2022
1 parent 60f85c4 commit fd12a75
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
67 changes: 67 additions & 0 deletions my-app/constants/index.js
Original file line number Diff line number Diff line change
@@ -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",
},
];
55 changes: 54 additions & 1 deletion my-app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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();
Expand Down
Loading

1 comment on commit fd12a75

@vercel
Copy link

@vercel vercel bot commented on fd12a75 Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.