Skip to content

Commit 8bc6cf9

Browse files
committed
corrected the claim
1 parent 0ce2ad1 commit 8bc6cf9

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

src/config/contracts.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ export const REWARDS_PROGRAM_ABI = [
290290
],
291291
outputs: [],
292292
},
293+
{
294+
name: "commitClaim",
295+
type: "function",
296+
stateMutability: "nonpayable",
297+
inputs: [
298+
{ name: "programId", type: "uint32" },
299+
{ name: "commitHash", type: "bytes32" },
300+
],
301+
outputs: [],
302+
},
293303
{
294304
name: "claimMember",
295305
type: "function",

src/hooks/useRewardsProgram.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useReadContract, useWriteContract, useWaitForTransactionReceipt } from
55
import { useAccount } from "wagmi";
66
import { useQueryClient } from "@tanstack/react-query";
77
import { readContract, writeContract as writeContractAction, waitForTransactionReceipt } from "wagmi/actions";
8+
import { keccak256, encodePacked } from "viem";
89
import { CONTRACTS, REWARDS_PROGRAM_ABI, ERC20_ABI } from "@/config/contracts";
910
import { toBytes8, toBytes12, toBytes16, safeParseAmount } from "@/lib/utils";
1011
import { wagmiConfig } from "@/lib/wagmi";
@@ -266,17 +267,57 @@ export function useWithdraw() {
266267
}
267268

268269
export 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

Comments
 (0)