From ba564f64c1979218475c2432bfcc7dfd5f8efc29 Mon Sep 17 00:00:00 2001 From: Heikrana <49754221+Heikrana@users.noreply.github.com> Date: Tue, 8 Nov 2022 23:20:51 +0530 Subject: [PATCH] Add admin functionality --- components/AdminControls.tsx | 151 +++++++++++++++++++++++++++++++++++ pages/index.tsx | 13 ++- styles/globals.css | 4 + 3 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 components/AdminControls.tsx diff --git a/components/AdminControls.tsx b/components/AdminControls.tsx new file mode 100644 index 0000000..db6f925 --- /dev/null +++ b/components/AdminControls.tsx @@ -0,0 +1,151 @@ +import React from "react"; +import { + StarIcon, + CurrencyDollarIcon, + ArrowPathIcon, + ArrowUturnDownIcon, +} from "@heroicons/react/24/solid"; +import { + useContract, + useContractRead, + useContractWrite, +} from "@thirdweb-dev/react"; +import { ethers } from "ethers"; +import { currency } from "../constants"; +import toast from "react-hot-toast"; + +function AdminControls() { + const { contract, isLoading } = useContract( + process.env.NEXT_PUBLIC_WALLET_ADDRESS + ); + const { data: totalCommission } = useContractRead( + contract, + "operatorTotalCommission" + ); + const { mutateAsync: DrawWinnerTicket } = useContractWrite( + contract, + "DrawWinnerTicket" + ); + const { mutateAsync: RefundAll } = useContractWrite(contract, "RefundAll"); + const { mutateAsync: WithdrawCommission } = useContractWrite( + contract, + "WithdrawCommission" + ); + const { mutateAsync: restartDraw } = useContractWrite( + contract, + "restartDraw" + ); + + const drawWinner = async () => { + const notification = toast.loading("Picking a Lucky Winner..."); + + try { + const data = await DrawWinnerTicket([{}]); + + toast.success("A Winner has been selected!", { + id: notification, + }); + + console.info("Contract call success", data); + } catch (err) { + toast.error("Whoops, something went worng!", { + id: notification, + }); + + console.error("Contract call failure", err); + } + }; + + const onWithdrawCommission = async () => { + const notification = toast.loading("Withdrawing commission..."); + + try { + const data = await WithdrawCommission([{}]); + + toast.success("Your commission has been withdrawn successfully!", { + id: notification, + }); + + console.info("Contract call success", data); + } catch (err) { + toast.error("Whoops, something went worng!", { + id: notification, + }); + + console.error("Contract call failure", err); + } + }; + + const onRefundAll = async () => { + const notification = toast.loading("Refunding all..."); + + try { + const data = await RefundAll([{}]); + + toast.success("All refunded successfully", { + id: notification, + }); + + console.info("Contract call success", data); + } catch (err) { + toast.error("Whoops, something went worng!", { + id: notification, + }); + + console.error("Contract call failure", err); + } + }; + + const onRestartDraw = async () => { + const notification = toast.loading("Restarting draw..."); + + try { + const data = await restartDraw([{}]); + + toast.success("Draw restarted successfully!", { + id: notification, + }); + + console.info("Contract call success", data); + } catch (err) { + toast.error("Whoops, something went worng!", { + id: notification, + }); + + console.error("Contract call failure", err); + } + }; + + return ( +
+ Total Commission to be withdrawn:{" "} + {totalCommission && + ethers.utils.formatEther(totalCommission?.toString())}{" "} + {currency} +
+ +