1
1
"use client" ;
2
2
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" ;
8
6
import { useRouter } from "next/navigation" ;
9
7
import { 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" ;
16
13
17
14
interface TransformedClaimData {
18
15
hypercertTokenIds : bigint [ ] ;
@@ -39,104 +36,39 @@ export default function UnclaimedHypercertBatchClaimButton({
39
36
allowListRecords : AllowListRecord [ ] ;
40
37
selectedChainId : number | null ;
41
38
} ) {
42
- const router = useRouter ( ) ;
43
- const { client } = useHypercertClient ( ) ;
44
- const { data : walletClient } = useWalletClient ( ) ;
45
39
const account = useAccount ( ) ;
46
40
const [ isLoading , setIsLoading ] = useState ( false ) ;
47
- const { setDialogStep, setSteps, setOpen, setTitle, setExtraContent } =
48
- useStepProcessDialogContext ( ) ;
49
41
const { switchChain } = useSwitchChain ( ) ;
42
+ const getStrategy = useClaimHypercertStrategy ( ) ;
43
+ const { selectedAccount } = useAccountStore ( ) ;
44
+
50
45
const selectedChain = selectedChainId
51
46
? ChainFactory . getChain ( selectedChainId )
52
47
: null ;
53
48
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
-
79
49
const claimHypercert = async ( ) => {
80
50
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" ) ;
101
51
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 ) ;
126
59
} catch ( error ) {
127
- console . error ( "Claim error:" , error ) ;
128
- await setDialogStep ( "claiming" , "error" , "Transaction failed" ) ;
60
+ console . error ( error ) ;
129
61
} finally {
130
62
setIsLoading ( false ) ;
131
63
}
132
64
} ;
133
65
66
+ const activeAddress = selectedAccount ?. address || account . address ;
134
67
const isBatchClaimDisabled =
135
68
isLoading ||
136
69
! 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 ) ;
140
72
141
73
return (
142
74
< >
0 commit comments