-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunclaimed-hypercert-butchClaim-button.tsx
145 lines (137 loc) · 4.55 KB
/
unclaimed-hypercert-butchClaim-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use client";
import { AllowListRecord } from "@/allowlists/getAllowListRecordsForAddressByClaimed";
import { Button } from "../ui/button";
import { useHypercertClient } from "@/hooks/use-hypercert-client";
import { waitForTransactionReceipt } from "viem/actions";
import { useAccount, useSwitchChain, useWalletClient } from "wagmi";
import { useRouter } from "next/navigation";
import { useStepProcessDialogContext } from "../global/step-process-dialog";
import { revalidatePathServerAction } from "@/app/actions/revalidatePathServerAction";
import { useState } from "react";
import { Hex, ByteArray } from "viem";
import { errorToast } from "@/lib/errorToast";
import { ChainFactory } from "@/lib/chainFactory";
interface TransformedClaimData {
hypercertTokenIds: bigint[];
units: bigint[];
proofs: (Hex | ByteArray)[][];
roots?: (Hex | ByteArray)[];
}
function transformAllowListRecords(
records: AllowListRecord[],
): TransformedClaimData {
return {
hypercertTokenIds: records.map((record) => BigInt(record.token_id!)),
units: records.map((record) => BigInt(record.units!)),
proofs: records.map((record) => record.proof as (Hex | ByteArray)[]),
roots: records.map((record) => record.root as Hex | ByteArray),
};
}
export default function UnclaimedHypercertBatchClaimButton({
allowListRecords,
selectedChainId,
}: {
allowListRecords: AllowListRecord[];
selectedChainId: number | null;
}) {
const { client } = useHypercertClient();
const { data: walletClient } = useWalletClient();
const account = useAccount();
const { refresh } = useRouter();
const [isLoading, setIsLoading] = useState(false);
const { setDialogStep, setSteps, setOpen, setTitle } =
useStepProcessDialogContext();
const { switchChain } = useSwitchChain();
const selectedChain = selectedChainId
? ChainFactory.getChain(selectedChainId)
: null;
const claimHypercert = async () => {
setIsLoading(true);
setOpen(true);
setSteps([
{ id: "preparing", description: "Preparing to claim hypercert..." },
{ id: "claiming", description: "Claiming hypercert on-chain..." },
{ id: "confirming", description: "Waiting for on-chain confirmation" },
{ id: "done", description: "Claiming complete!" },
]);
setTitle("Claim Hypercert from Allowlist");
if (!client) {
throw new Error("No client found");
}
if (!walletClient) {
throw new Error("No wallet client found");
}
if (!account) {
throw new Error("No address found");
}
const claimData = transformAllowListRecords(allowListRecords);
await setDialogStep("preparing, active");
console.log(allowListRecords);
try {
await setDialogStep("claiming", "active");
const tx = await client.batchClaimFractionsFromAllowlists(claimData);
console.log(tx);
if (!tx) {
await setDialogStep("claiming", "error");
throw new Error("Failed to claim hypercert");
}
await setDialogStep("confirming", "active");
const receipt = await waitForTransactionReceipt(walletClient, {
confirmations: 3,
hash: tx,
});
if (receipt.status == "success") {
await setDialogStep("done", "completed");
await revalidatePathServerAction([
`/profile/${account.address}`,
`/profile/${account.address}?tab=hypercerts-claimable`,
]);
} else if (receipt.status == "reverted") {
await setDialogStep("confirming", "error", "Transaction reverted");
}
console.log({ receipt });
setTimeout(() => {
refresh();
}, 5000);
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
}
};
return (
<>
{account.chainId === selectedChainId ? (
<Button
variant={"outline"}
size={"sm"}
onClick={claimHypercert}
disabled={
isLoading ||
!allowListRecords.length ||
!account ||
!client ||
account.address !== allowListRecords[0].user_address
}
>
Claim Selected
</Button>
) : (
<Button
variant={"outline"}
size="sm"
disabled={!account.isConnected || !selectedChainId}
onClick={() => {
if (!selectedChainId)
return errorToast("Hypercert is not selected");
switchChain({ chainId: selectedChainId });
}}
>
{selectedChainId
? `Switch to ${selectedChain?.name}`
: "Select Hypercert"}
</Button>
)}
</>
);
}