11"use client" ;
22
3- import { AllowListRecord } from "@/allowlists/actions/getAllowListRecordsForAddressByClaimed" ;
4- import { revalidatePathServerAction } from "@/app/actions/revalidatePathServerAction" ;
5- import { useHypercertClient } from "@/hooks/use-hypercert-client" ;
6- import { ChainFactory } from "@/lib/chainFactory" ;
7- import { errorToast } from "@/lib/errorToast" ;
3+ import { AllowListRecord } from "@/allowlists/getAllowListRecordsForAddressByClaimed" ;
4+ import { Button } from "../ui/button" ;
5+ import { useAccount , useSwitchChain } from "wagmi" ;
86import { useRouter } from "next/navigation" ;
97import { useState } from "react" ;
10- import { ByteArray , getAddress , Hex } from "viem" ;
11- import { waitForTransactionReceipt } from "viem/actions" ;
12- import { useAccount , useSwitchChain , useWalletClient } from "wagmi" ;
13- import { createExtraContent } from "../global/extra-content" ;
14- import { useStepProcessDialogContext } from "../global/step-process-dialog" ;
15- import { Button } from "../ui/button" ;
8+ import { getAddress , Hex , ByteArray } from "viem" ;
9+ import { errorToast } from "@/lib/errorToast" ;
10+ import { ChainFactory } from "@/lib/chainFactory" ;
11+ import { useClaimHypercertStrategy } from "@/hypercerts/hooks/useClaimHypercertStrategy" ;
12+ import { useAccountStore } from "@/lib/account-store" ;
1613
1714interface TransformedClaimData {
1815 hypercertTokenIds : bigint [ ] ;
@@ -39,104 +36,39 @@ export default function UnclaimedHypercertBatchClaimButton({
3936 allowListRecords : AllowListRecord [ ] ;
4037 selectedChainId : number | null ;
4138} ) {
42- const router = useRouter ( ) ;
43- const { client } = useHypercertClient ( ) ;
44- const { data : walletClient } = useWalletClient ( ) ;
4539 const account = useAccount ( ) ;
4640 const [ isLoading , setIsLoading ] = useState ( false ) ;
47- const { setDialogStep, setSteps, setOpen, setTitle, setExtraContent } =
48- useStepProcessDialogContext ( ) ;
4941 const { switchChain } = useSwitchChain ( ) ;
42+ const getStrategy = useClaimHypercertStrategy ( ) ;
43+ const { selectedAccount } = useAccountStore ( ) ;
44+
5045 const selectedChain = selectedChainId
5146 ? ChainFactory . getChain ( selectedChainId )
5247 : null ;
5348
54- const refreshData = async ( address : string ) => {
55- const hypercertIds = allowListRecords . map ( ( record ) => record . hypercert_id ) ;
56-
57- const hypercertViewInvalidationPaths = hypercertIds . map ( ( id ) => {
58- return `/hypercerts/${ id } ` ;
59- } ) ;
60-
61- await revalidatePathServerAction ( [
62- `/profile/${ address } ` ,
63- `/profile/${ address } ?tab` ,
64- `/profile/${ address } ?tab=hypercerts-claimable` ,
65- `/profile/${ address } ?tab=hypercerts-owned` ,
66- ...hypercertViewInvalidationPaths ,
67- ] ) . then ( async ( ) => {
68- setTimeout ( ( ) => {
69- // refresh after 5 seconds
70- router . refresh ( ) ;
71-
72- // push to the profile page with the hypercerts-claimable tab
73- // because revalidatePath will revalidate on the next page visit.
74- router . push ( `/profile/${ address } ?tab=hypercerts-claimable` ) ;
75- } , 5000 ) ;
76- } ) ;
77- } ;
78-
7949 const claimHypercert = async ( ) => {
8050 setIsLoading ( true ) ;
81- setOpen ( true ) ;
82- setSteps ( [
83- { id : "preparing" , description : "Preparing to claim fractions..." } ,
84- { id : "claiming" , description : "Claiming fractions on-chain..." } ,
85- { id : "confirming" , description : "Waiting for on-chain confirmation" } ,
86- { id : "done" , description : "Claiming complete!" } ,
87- ] ) ;
88- setTitle ( "Claim fractions from Allowlist" ) ;
89- if ( ! client ) {
90- throw new Error ( "No client found" ) ;
91- }
92- if ( ! walletClient ) {
93- throw new Error ( "No wallet client found" ) ;
94- }
95- if ( ! account ) {
96- throw new Error ( "No address found" ) ;
97- }
98-
99- const claimData = transformAllowListRecords ( allowListRecords ) ;
100- await setDialogStep ( "preparing, active" ) ;
10151 try {
102- await setDialogStep ( "claiming" , "active" ) ;
103- const tx = await client . batchClaimFractionsFromAllowlists ( claimData ) ;
104-
105- if ( ! tx ) {
106- await setDialogStep ( "claiming" , "error" ) ;
107- throw new Error ( "Failed to claim fractions" ) ;
108- }
109-
110- await setDialogStep ( "confirming" , "active" ) ;
111- const receipt = await waitForTransactionReceipt ( walletClient , {
112- hash : tx ,
113- } ) ;
114-
115- if ( receipt . status == "success" ) {
116- await setDialogStep ( "done" , "completed" ) ;
117- const extraContent = createExtraContent ( {
118- receipt,
119- chain : account ?. chain ! ,
120- } ) ;
121- setExtraContent ( extraContent ) ;
122- refreshData ( getAddress ( account . address ! ) ) ;
123- } else if ( receipt . status == "reverted" ) {
124- await setDialogStep ( "confirming" , "error" , "Transaction reverted" ) ;
125- }
52+ const claimData = transformAllowListRecords ( allowListRecords ) ;
53+ const params = claimData . hypercertTokenIds . map ( ( tokenId , index ) => ( {
54+ tokenId,
55+ units : claimData . units [ index ] ,
56+ proof : claimData . proofs [ index ] as `0x${string } `[ ] ,
57+ } ) ) ;
58+ await getStrategy ( params ) . execute ( params ) ;
12659 } catch ( error ) {
127- console . error ( "Claim error:" , error ) ;
128- await setDialogStep ( "claiming" , "error" , "Transaction failed" ) ;
60+ console . error ( error ) ;
12961 } finally {
13062 setIsLoading ( false ) ;
13163 }
13264 } ;
13365
66+ const activeAddress = selectedAccount ?. address || account . address ;
13467 const isBatchClaimDisabled =
13568 isLoading ||
13669 ! allowListRecords . length ||
137- ! account ||
138- ! client ||
139- account . address !== getAddress ( allowListRecords [ 0 ] . user_address as string ) ;
70+ ! activeAddress ||
71+ activeAddress !== getAddress ( allowListRecords [ 0 ] . user_address as string ) ;
14072
14173 return (
14274 < >
0 commit comments