Skip to content

Commit

Permalink
wagmi viem for addSuperAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Apr 5, 2024
1 parent 8ec97b9 commit b59c6d8
Show file tree
Hide file tree
Showing 4 changed files with 683 additions and 2 deletions.
56 changes: 55 additions & 1 deletion frontend/app/admin/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,64 @@ import { parseAbiItem } from "viem";
import { usePublicClient } from "wagmi";
import { hardhat } from "viem/chains";

// Contract's information
import { AdminsABI, contractAddressAdmins } from "@/constants/index";

const admin = () => {
// Admin Information

const [superAdmin, setSuperAdmin] = useState("");

// Wagmi function / client creation for event listenning
const client = usePublicClient();

// Event information
const [grantedEvents, setGrantedEvents] = useState([]);

// Event handling function
const getGrantedEvents = async () => {
try {
// get.Logs from viem
const logs = await client.getLogs({
address: contractAddressAdmins,
event: parseAbiItem(
"event Granted (address from, address to, Role role,address contractt);"
),
fromBlock: 0n,
toBlock: "latest",
});

setGrantedEvents(logs.map((log) => log.args.from));
toast.success("Admin Created");
} catch (err) {
toast.error(err.message);
}
};

const createSuperAdmin = async () => {
try {
const { request } = await prepareWriteContract({
address: contractAddressAdmins,
abi: AdminsABI,
functionName: "addSuperAdmin",
args: [_addr],
});

const { hash } = await writeContract(request);
const data = await waitForTransaction({
hash: hash,
});

getGrantedEvents();
} catch (err) {
toast.error(err.message);
}
};

return (
<main className="flex justify-center items-center flex-col mb-60 gap-8">
<form
id="artistForm"
id="adminForm"
className="flex justify-center items-center flex-col gap-4"
>
<label for="admin" className="text-2xl">
Expand All @@ -35,6 +88,7 @@ const admin = () => {
<button
type="submit"
className="bg-[#cef54b] text-[#15141a] rounded p-1"
onClick={createSuperAdmin}
>
Submit
</button>
Expand Down
Loading

0 comments on commit b59c6d8

Please sign in to comment.