Skip to content

feat: batch claim fraction #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0ec6afe
feat(profile): implement unclaimed hypercerts table view
tnkshuuhei Feb 4, 2025
cae4a5a
refactor(hypercerts): extract extra content creation to separate comp…
tnkshuuhei Feb 4, 2025
0518237
feat(profile): enhance unclaimed hypercert claim flow with step-by-st…
tnkshuuhei Feb 4, 2025
dd15830
feat(profile): add batch claim functionality for unclaimed hypercerts
tnkshuuhei Feb 4, 2025
e59ff97
refactor(profile): remove unclaimed hypercert list item component
tnkshuuhei Feb 4, 2025
f5ad0ea
feat(profile): add chain filtering to unclaimed hypercerts table
tnkshuuhei Feb 5, 2025
64bfe4d
feat(profile): add chain switching for claiming unclaimed hypercert
tnkshuuhei Feb 5, 2025
9f97c56
feat(profile): automatically set chainId filter to connected chainId
tnkshuuhei Feb 5, 2025
5c064bb
feat(profile): disable to choose multiple chain ATST
tnkshuuhei Feb 5, 2025
1226659
chore: increased default pagesize
tnkshuuhei Feb 5, 2025
533a6ee
fix(profile): correct dropdownItem for viewing hypercert
tnkshuuhei Feb 5, 2025
4032a18
chore: disabled batch claim button when user is not selected
tnkshuuhei Feb 5, 2025
33e901b
refactor(profile): improve unclaimed hypercerts table design and add …
tnkshuuhei Feb 5, 2025
207c95a
feat(profile): add sorting to unclaimed hypercerts table units/total_…
tnkshuuhei Feb 5, 2025
8246187
refactor(profile): improve table toolbar and row selection logic
tnkshuuhei Feb 5, 2025
d1bddb8
style(profile): responsive table view and some UX improvement
tnkshuuhei Feb 6, 2025
debdc00
feat(profile): unclaimed hypercerts with metadata
tnkshuuhei Feb 6, 2025
3ae229b
feat(hypercerts): introduced a new `getHypercertMetadata` function to…
tnkshuuhei Feb 6, 2025
8931982
refactor(profile): change button validation, text unification and so on
tnkshuuhei Feb 6, 2025
aabe2a1
refactor(components): migrate extra-content to React JSX syntax
tnkshuuhei Feb 6, 2025
0b5088f
refactor(profile): remove tx confirmations
tnkshuuhei Feb 6, 2025
5478440
refactor(components): update createExtraContent and claim buttons
tnkshuuhei Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions app/profile/[address]/hypercerts-tab-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { getHypercertsByCreator } from "@/hypercerts/getHypercertsByCreator";
import { getAllowListRecordsForAddressByClaimed } from "@/allowlists/getAllowListRecordsForAddressByClaimed";
import HypercertWindow from "@/components/hypercert/hypercert-window";
import { EmptySection } from "@/components/global/sections";
import UnclaimedHypercertsList from "@/components/profile/unclaimed-hypercerts-list";
import UnclaimedHypercertsList, {
UnclaimedFraction,
} from "@/components/profile/unclaimed-hypercerts-list";
import { Suspense } from "react";
import ExploreListSkeleton from "@/components/explore/explore-list-skeleton";
import { ProfileSubTabKey, subTabs } from "@/app/profile/[address]/tabs";
import { SubTabsWithCount } from "@/components/profile/sub-tabs-with-count";
import { getHypercertsByOwner } from "@/hypercerts/getHypercertsByOwner";
import { getHypercert } from "@/hypercerts/getHypercert";

const HypercertsTabContentInner = async ({
address,
Expand All @@ -27,7 +30,27 @@ const HypercertsTabContentInner = async ({
const claimableHypercerts = await getAllowListRecordsForAddressByClaimed(
address,
false,
);
).then(async (res) => {
if (!res?.data) {
return {
data: [],
count: 0,
};
}
const hypercertsWithMetadata = await Promise.all(
res.data.map(async (record): Promise<UnclaimedFraction> => {
const hypercert = await getHypercert(record.hypercert_id as string);
return {
...record,
metadata: hypercert?.metadata || null,
};
}),
);
return {
data: hypercertsWithMetadata,
count: res?.count,
};
});

const showCreatedHypercerts =
createdHypercerts?.data && createdHypercerts.data.length > 0;
Expand Down
64 changes: 64 additions & 0 deletions components/global/extra-content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Button, buttonVariants } from "@/components/ui/button";
import { createElement } from "react";
import type { Chain, TransactionReceipt } from "viem";
import { generateBlockExplorerLink } from "@/lib/utils";

export const createExtraContent = (
receipt: TransactionReceipt,
hypercertId?: string,
chain?: Chain,
) => {
const receiptButton =
receipt &&
createElement(
"a",
{
href: generateBlockExplorerLink(chain, receipt.transactionHash),
target: "_blank",
rel: "noopener noreferrer",
},
createElement(
Button,
{
size: "default",
className: buttonVariants({ variant: "secondary" }),
},
"View transaction",
),
);

const hypercertButton =
hypercertId &&
createElement(
"a",
{
href: `/hypercerts/${hypercertId}`,
target: "_blank",
rel: "noopener noreferrer",
},
createElement(
Button,
{
size: "default",
className: buttonVariants({ variant: "default" }),
},
"View hypercert",
),
);

return createElement(
"div",
{ className: "flex flex-col space-y-2" },
createElement(
"p",
{ className: "text-sm font-medium" },
"Your hypercert has been minted successfully!",
),
createElement(
"div",
{ className: "flex space-x-4" },
receiptButton,
hypercertButton,
),
);
};
145 changes: 145 additions & 0 deletions components/profile/unclaimed-hypercert-butchClaim-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,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>
)}
</>
);
}
Loading