Skip to content

Commit 01b3311

Browse files
committed
updated claimable UI to include currency decimals
1 parent e28694e commit 01b3311

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/modules/components/Claimable.tsx

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ import { useTxNotifications } from "hooks/useTxNotifications";
2929
import { CircleAlertIcon, PlusIcon, Trash2Icon } from "lucide-react";
3030
import { useCallback } from "react";
3131
import { useFieldArray, useForm } from "react-hook-form";
32-
import { type PreparedTransaction, sendAndConfirmTransaction } from "thirdweb";
32+
import {
33+
type PreparedTransaction,
34+
getContract,
35+
sendAndConfirmTransaction,
36+
toTokens,
37+
} from "thirdweb";
38+
import { decimals } from "thirdweb/extensions/erc20";
3339
import { ClaimableERC721, ClaimableERC1155 } from "thirdweb/modules";
3440
import { useActiveAccount, useReadContract } from "thirdweb/react";
3541
import { z } from "zod";
@@ -66,6 +72,23 @@ function ClaimableModule(props: ModuleInstanceProps) {
6672
},
6773
);
6874

75+
const currencyContract = getContract({
76+
address: claimConditionQuery.data?.currency || "",
77+
chain: props.contract.chain,
78+
client: props.contract.client,
79+
});
80+
81+
const tokenDecimals = useReadContract(decimals, {
82+
contract: currencyContract,
83+
queryOptions: {
84+
enabled:
85+
claimConditionQuery.data &&
86+
claimConditionQuery.data?.currency !==
87+
"0x0000000000000000000000000000000000000000",
88+
},
89+
});
90+
const tokenDecimalsData = tokenDecimals.data ?? 0;
91+
6992
const isErc721 = props.contractInfo.name === "ClaimableERC721";
7093

7194
const mint = useCallback(
@@ -160,7 +183,12 @@ function ClaimableModule(props: ModuleInstanceProps) {
160183
<ClaimableModuleUI
161184
{...props}
162185
isPendingPrimarySaleRecipient={primarySaleRecipientQuery.isPending}
163-
isPendingClaimCondition={claimConditionQuery.isPending}
186+
isPendingClaimCondition={
187+
claimConditionQuery.isPending ||
188+
(claimConditionQuery.data?.currency !==
189+
"0x0000000000000000000000000000000000000000" &&
190+
tokenDecimals.isPending)
191+
}
164192
primarySaleRecipient={primarySaleRecipientQuery.data}
165193
claimCondition={claimConditionQuery.data}
166194
setClaimCondition={setClaimCondition}
@@ -169,6 +197,7 @@ function ClaimableModule(props: ModuleInstanceProps) {
169197
isOwnerAccount={!!ownerAccount}
170198
isErc721={isErc721}
171199
chainId={props.contract.chain.id}
200+
tokenDecimals={tokenDecimalsData}
172201
/>
173202
);
174203
}
@@ -187,6 +216,7 @@ export function ClaimableModuleUI(
187216
mint: (values: MintFormValues) => Promise<void>;
188217
isErc721: boolean;
189218
chainId: number;
219+
tokenDecimals: number;
190220
},
191221
) {
192222
return (
@@ -218,6 +248,7 @@ export function ClaimableModuleUI(
218248
update={props.setClaimCondition}
219249
isErc721={props.isErc721}
220250
chainId={props.chainId}
251+
tokenDecimals={props.tokenDecimals}
221252
/>
222253
) : (
223254
<Skeleton className="h-[74px]" />
@@ -285,6 +316,8 @@ export type ClaimConditionFormValues = z.infer<typeof claimConditionFormSchema>;
285316

286317
// perform this outside else it forces too many re-renders
287318
const now = Date.now() / 1000;
319+
const MAX_UINT_256 =
320+
"115792089237316195423570985008687907853269984665640564039457584007913129639935";
288321

289322
function ClaimConditionSection(props: {
290323
primarySaleRecipient: string | undefined;
@@ -293,26 +326,35 @@ function ClaimConditionSection(props: {
293326
isOwnerAccount: boolean;
294327
isErc721: boolean;
295328
chainId: number;
329+
tokenDecimals: number;
296330
}) {
297331
const { idToChain } = useAllChainsData();
298332
const chain = idToChain.get(props.chainId);
299333

300334
const form = useForm<ClaimConditionFormValues>({
301335
resolver: zodResolver(claimConditionFormSchema),
302336
values: {
303-
// TODO: parse units based on the currency decimals
304-
pricePerToken: props.claimCondition?.pricePerUnit ? 0 : 0,
305337
currencyAddress:
306338
props.claimCondition?.currency ===
307339
"0x0000000000000000000000000000000000000000"
308340
? ""
309341
: props.claimCondition?.currency,
342+
pricePerToken:
343+
props.claimCondition?.pricePerUnit &&
344+
props.claimCondition?.currency !==
345+
"0x0000000000000000000000000000000000000000"
346+
? Number(
347+
toTokens(props.claimCondition?.pricePerUnit, props.tokenDecimals),
348+
)
349+
: 0,
310350
maxClaimableSupply:
311-
props.claimCondition?.availableSupply.toString() === "0"
351+
props.claimCondition?.availableSupply.toString() === "0" ||
352+
props.claimCondition?.availableSupply.toString() === MAX_UINT_256
312353
? ""
313354
: props.claimCondition?.availableSupply.toString() || "",
314355
maxClaimablePerWallet:
315-
props.claimCondition?.maxMintPerWallet.toString() === "0"
356+
props.claimCondition?.maxMintPerWallet.toString() === "0" ||
357+
props.claimCondition?.maxMintPerWallet.toString() === MAX_UINT_256
316358
? ""
317359
: props.claimCondition?.maxMintPerWallet.toString() || "",
318360
startTime: new Date((props.claimCondition?.startTimestamp || now) * 1000),

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/modules/components/claimable.stories.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ function Component() {
7676
const claimCondition = {
7777
availableSupply: BigInt(100),
7878
maxMintPerWallet: BigInt(10),
79-
pricePerUnit: BigInt(10),
80-
currency: "0x0000000000000000000000000000000000000000",
79+
pricePerUnit: 10n,
80+
currency: "0x000000000000000000000000000000000000000",
8181
startTimestamp: 1689092800,
8282
endTimestamp: 1689092800,
8383
allowlistMerkleRoot:
@@ -122,6 +122,7 @@ function Component() {
122122
isErc721={isErc721}
123123
claimCondition={claimCondition}
124124
chainId={1}
125+
tokenDecimals={18}
125126
/>
126127
</BadgeContainer>
127128

@@ -143,6 +144,7 @@ function Component() {
143144
isErc721={isErc721}
144145
claimCondition={claimCondition}
145146
chainId={1}
147+
tokenDecimals={18}
146148
/>
147149
</BadgeContainer>
148150

@@ -164,6 +166,7 @@ function Component() {
164166
isErc721={isErc721}
165167
claimCondition={claimCondition}
166168
chainId={1}
169+
tokenDecimals={18}
167170
/>
168171
</BadgeContainer>
169172

0 commit comments

Comments
 (0)