@@ -26,10 +26,12 @@ import {
2626 useFieldArray ,
2727 useForm ,
2828} from "react-hook-form" ;
29+ import { toast } from "sonner" ;
2930import {
3031 NATIVE_TOKEN_ADDRESS ,
3132 type ThirdwebContract ,
3233 ZERO_ADDRESS ,
34+ toTokens ,
3335} from "thirdweb" ;
3436import { decimals } from "thirdweb/extensions/erc20" ;
3537import {
@@ -152,7 +154,7 @@ interface ClaimsConditionFormContextData {
152154 field : ControlledField ;
153155 phaseIndex : number ;
154156 formDisabled : boolean ;
155- tokenDecimals : number ;
157+ tokenDecimals : number | undefined ;
156158 isMultiPhase : boolean ;
157159 isActive : boolean ;
158160 dropType : DropType ;
@@ -210,7 +212,6 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
210212 enabled : isErc20 ,
211213 } ,
212214 } ) ;
213- const tokenDecimalsData = tokenDecimals . data ?? 0 ;
214215 const saveClaimPhaseNotification = useTxNotifications (
215216 "Saved claim phases" ,
216217 "Failed to save claim phases" ,
@@ -219,7 +220,7 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
219220 const claimConditionsQuery = useReadContract ( getClaimPhasesInLegacyFormat , {
220221 contract,
221222 ...( isErc20
222- ? { type : "erc20" }
223+ ? { type : "erc20" , decimals : tokenDecimals . data }
223224 : isErc721
224225 ? { type : "erc721" }
225226 : { type : "erc1155" , tokenId : BigInt ( tokenId || 0 ) } ) ,
@@ -259,7 +260,10 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
259260 ) ;
260261 } , [ claimConditionsQuery . data , isMultiPhase ] ) ;
261262
262- const isFetchingData = claimConditionsQuery . isFetching || sendTx . isPending ;
263+ const isFetchingData =
264+ claimConditionsQuery . isFetching ||
265+ sendTx . isPending ||
266+ ( isErc20 && tokenDecimals . isLoading ) ;
263267
264268 const canEditForm = isAdmin && ! isFetchingData ;
265269
@@ -353,13 +357,17 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
353357 action : "set-claim-conditions" ,
354358 label : "attempt" ,
355359 } ) ;
356-
360+ if ( isErc20 && ! tokenDecimals . data ) {
361+ return toast . error (
362+ `Could not fetch token decimals for contract ${ contract . address } ` ,
363+ ) ;
364+ }
357365 try {
358366 const tx = setClaimPhasesTx (
359367 {
360368 contract,
361369 ...( isErc20
362- ? { type : "erc20" }
370+ ? { type : "erc20" , decimals : tokenDecimals . data }
363371 : isErc721
364372 ? { type : "erc721" }
365373 : { type : "erc1155" , tokenId : BigInt ( tokenId || 0 ) } ) ,
@@ -453,6 +461,14 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
453461 ) ;
454462 }
455463
464+ if ( isErc20 && tokenDecimals . data === undefined ) {
465+ return (
466+ < div className = "flex h-[400px] w-full items-center justify-center rounded-lg border border-border" >
467+ Failed to load token decimals
468+ </ div >
469+ ) ;
470+ }
471+
456472 return (
457473 < >
458474 < Flex onSubmit = { handleFormSubmit } direction = "column" as = "form" gap = { 10 } >
@@ -488,6 +504,30 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
488504 } ,
489505 ) ;
490506
507+ // With v5, the value of `maxClaimablePerWallet` & `maxClaimableSupply` for ERC20 claim cont. will already be converted to wei
508+ // so we have to convert them back to token (`toTokens`) to make it human-readable
509+ if ( isErc20 && tokenDecimals . data ) {
510+ if (
511+ field . maxClaimableSupply !== undefined &&
512+ field . maxClaimableSupply !== "unlimited"
513+ ) {
514+ field . maxClaimableSupply = toTokens (
515+ BigInt ( field . maxClaimableSupply ) ,
516+ tokenDecimals . data ,
517+ ) ;
518+ }
519+
520+ if (
521+ field . maxClaimablePerWallet !== undefined &&
522+ field . maxClaimablePerWallet !== "unlimited"
523+ ) {
524+ field . maxClaimablePerWallet = toTokens (
525+ BigInt ( field . maxClaimablePerWallet ) ,
526+ tokenDecimals . data ,
527+ ) ;
528+ }
529+ }
530+
491531 return (
492532 < Fragment key = { `snapshot_${ field . id } _${ index } ` } >
493533 < SnapshotUpload
@@ -508,7 +548,7 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
508548 phaseIndex : index ,
509549 formDisabled : ! canEditForm ,
510550 isErc20,
511- tokenDecimals : tokenDecimalsData ,
551+ tokenDecimals : tokenDecimals . data ,
512552 dropType,
513553 setOpenSnapshotIndex,
514554 isAdmin,
0 commit comments