@@ -5,6 +5,7 @@ import { useReadContract, useWriteContract, useWaitForTransactionReceipt } from
55import { useAccount } from "wagmi" ;
66import { useQueryClient } from "@tanstack/react-query" ;
77import { readContract , writeContract as writeContractAction , waitForTransactionReceipt } from "wagmi/actions" ;
8+ import { keccak256 , encodePacked } from "viem" ;
89import { CONTRACTS , REWARDS_PROGRAM_ABI , ERC20_ABI } from "@/config/contracts" ;
910import { toBytes8 , toBytes12 , toBytes16 , safeParseAmount } from "@/lib/utils" ;
1011import { wagmiConfig } from "@/lib/wagmi" ;
@@ -266,17 +267,57 @@ export function useWithdraw() {
266267}
267268
268269export function useClaimMember ( ) {
269- const { writeContract, data : hash , isPending, error } = useWriteContract ( ) ;
270- const { isLoading : isConfirming , isSuccess } = useWaitForTransactionReceipt ( { hash } ) ;
270+ const { address } = useAccount ( ) ;
271+ const [ isPending , setIsPending ] = useState ( false ) ;
272+ const [ isConfirming , setIsConfirming ] = useState ( false ) ;
273+ const [ isSuccess , setIsSuccess ] = useState ( false ) ;
274+ const [ error , setError ] = useState < Error | null > ( null ) ;
275+ const [ hash , setHash ] = useState < `0x${string } ` | undefined > ( ) ;
271276 useRefetchOnSuccess ( isSuccess ) ;
272277
273- const claimMember = ( programId : number , memberID : string , editCode : `0x${string } `) => {
274- writeContract ( {
275- address : CONTRACTS . rewardsProgram ,
276- abi : REWARDS_PROGRAM_ABI ,
277- functionName : "claimMember" ,
278- args : [ programId , toBytes12 ( memberID ) , editCode ] ,
279- } ) ;
278+ const claimMember = async ( programId : number , memberID : string , editCode : `0x${string } `) => {
279+ if ( ! address ) return ;
280+ setIsPending ( true ) ;
281+ setIsConfirming ( false ) ;
282+ setIsSuccess ( false ) ;
283+ setError ( null ) ;
284+ setHash ( undefined ) ;
285+
286+ try {
287+ const memberIDBytes = toBytes12 ( memberID ) ;
288+ const commitHash = keccak256 (
289+ encodePacked ( [ "bytes12" , "bytes32" , "address" ] , [ memberIDBytes as `0x${string } `, editCode , address ] )
290+ ) ;
291+
292+ // Step 1: commitClaim
293+ const commitTx = await writeContractAction ( wagmiConfig , {
294+ address : CONTRACTS . rewardsProgram ,
295+ abi : REWARDS_PROGRAM_ABI ,
296+ functionName : "commitClaim" ,
297+ args : [ programId , commitHash ] ,
298+ } ) ;
299+ await waitForTransactionReceipt ( wagmiConfig , { hash : commitTx } ) ;
300+
301+ // Wait for MIN_COMMIT_DELAY (5 seconds) + buffer
302+ setIsConfirming ( true ) ;
303+ await new Promise ( ( r ) => setTimeout ( r , 8000 ) ) ;
304+
305+ // Step 2: claimMember
306+ const claimTx = await writeContractAction ( wagmiConfig , {
307+ address : CONTRACTS . rewardsProgram ,
308+ abi : REWARDS_PROGRAM_ABI ,
309+ functionName : "claimMember" ,
310+ args : [ programId , memberIDBytes , editCode ] ,
311+ } ) ;
312+ setHash ( claimTx ) ;
313+ await waitForTransactionReceipt ( wagmiConfig , { hash : claimTx } ) ;
314+ setIsSuccess ( true ) ;
315+ } catch ( err ) {
316+ setError ( err instanceof Error ? err : new Error ( String ( err ) ) ) ;
317+ } finally {
318+ setIsPending ( false ) ;
319+ setIsConfirming ( false ) ;
320+ }
280321 } ;
281322
282323 return { claimMember, isPending, isConfirming, isSuccess, error, hash } ;
0 commit comments