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} +
+ +