From e9ed03ed2e5ca016c5e4f1666a29c6919a86077d Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Thu, 7 Mar 2024 00:11:52 +0530 Subject: [PATCH 1/4] add splitETH contract txn's while doing batch action --- packages/nextjs/app/admin/page.tsx | 40 ++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/nextjs/app/admin/page.tsx b/packages/nextjs/app/admin/page.tsx index d06bac5..98dd486 100644 --- a/packages/nextjs/app/admin/page.tsx +++ b/packages/nextjs/app/admin/page.tsx @@ -4,6 +4,8 @@ import { useRef, useState } from "react"; import { BatchActionModal } from "./_components/BatchActionModal"; import { GrantReview } from "./_components/GrantReview"; import useSWR from "swr"; +import { parseEther } from "viem"; +import { useScaffoldContractWrite } from "~~/hooks/scaffold-eth"; import { GrantDataWithBuilder } from "~~/services/database/schema"; import { PROPOSAL_STATUS } from "~~/utils/grants"; import { notification } from "~~/utils/scaffold-eth"; @@ -43,6 +45,28 @@ const AdminPage = () => { } }; + const { data: txResult, writeAsync: splitEqualETH } = useScaffoldContractWrite({ + contractName: "BGGrants", + functionName: "splitEqualETH", + args: [undefined], + }); + + const handleBatchClick = async (filteredGrants: GrantDataWithBuilder[], action: "approve" | "complete") => { + const selectedGrantsWithMetaData = filteredGrants.filter(grant => { + if (action === "approve") return selectedApproveGrants.includes(grant.id); + return selectedCompleteGrants.includes(grant.id); + }); + const builders = selectedGrantsWithMetaData.map(grant => grant.builder); + const totalAmount = selectedGrantsWithMetaData.reduce((acc, grant) => acc + grant.askAmount, 0); + const value = parseEther((totalAmount / 2).toString()); + const hash = await splitEqualETH({ + args: [builders], + value, + }); + setModalBtnLabel(action === "approve" ? "Approve" : "Complete"); + if (hash && modalRef.current) modalRef.current.showModal(); + }; + const completedGrants = grants?.filter(grant => grant.status === PROPOSAL_STATUS.SUBMITTED); const newGrants = grants?.filter(grant => grant.status === PROPOSAL_STATUS.PROPOSED); @@ -55,13 +79,10 @@ const AdminPage = () => {

Proposals submitted as completed:

- {completedGrants?.length !== 0 && ( + {completedGrants && completedGrants.length !== 0 && (
- + ); };