Skip to content

Commit 047c948

Browse files
committed
update
1 parent 5398cb4 commit 047c948

File tree

3 files changed

+60
-15
lines changed
  • apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/_components/claim-conditions/claim-conditions-form

3 files changed

+60
-15
lines changed

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/_components/claim-conditions/claim-conditions-form/hooks.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
type BaseTransactionOptions,
33
type ThirdwebClient,
44
toTokens,
5+
toUnits,
56
} from "thirdweb";
67
import type { OverrideEntry } from "thirdweb/dist/types/utils/extensions/drops/types";
78
import type { Prettify } from "thirdweb/dist/types/utils/type-utils";
@@ -42,6 +43,7 @@ type CombinedClaimCondition = Prettify<
4243
type Options =
4344
| {
4445
type: "erc20";
46+
decimals?: number;
4547
}
4648
| {
4749
type: "erc721";
@@ -91,12 +93,18 @@ export async function getClaimPhasesInLegacyFormat(
9193
startTime: new Date(Number(condition.startTimestamp * 1000n)),
9294
currencyAddress: condition.currency,
9395
price: condition.pricePerToken,
94-
maxClaimableSupply: toUnlimited(condition.maxClaimableSupply),
96+
maxClaimableSupply: toUnlimited(
97+
condition.maxClaimableSupply,
98+
options.type === "erc20" ? currencyMetadata.decimals : undefined,
99+
),
95100
currencyMetadata,
96101
currentMintSupply: (
97102
condition.maxClaimableSupply - condition.supplyClaimed
98103
).toString(),
99-
maxClaimablePerWallet: toUnlimited(condition.quantityLimitPerWallet),
104+
maxClaimablePerWallet: toUnlimited(
105+
condition.quantityLimitPerWallet,
106+
options.type === "erc20" ? currencyMetadata.decimals : undefined,
107+
),
100108
merkleRootHash: condition.merkleRoot,
101109
metadata,
102110
snapshot,
@@ -114,8 +122,14 @@ export function setClaimPhasesTx(
114122
const phases = rawPhases.map((phase) => {
115123
return {
116124
startTime: toDate(phase.startTime),
117-
maxClaimableSupply: toBigInt(phase.maxClaimableSupply),
118-
maxClaimablePerWallet: toBigInt(phase.maxClaimablePerWallet),
125+
maxClaimableSupply: toBigInt(
126+
phase.maxClaimableSupply,
127+
baseOptions.type === "erc20" ? baseOptions.decimals : undefined,
128+
),
129+
maxClaimablePerWallet: toBigInt(
130+
phase.maxClaimablePerWallet,
131+
baseOptions.type === "erc20" ? baseOptions.decimals : undefined,
132+
),
119133
merkleRootHash: phase.merkleRootHash as string | undefined,
120134
overrideList: phase.snapshot?.length
121135
? snapshotToOverrides(phase.snapshot)
@@ -175,18 +189,35 @@ function toDate(timestamp: number | Date | undefined) {
175189
}
176190
return new Date(timestamp);
177191
}
178-
function toBigInt(value: string | number | undefined) {
192+
function toBigInt(
193+
value: string | number | undefined,
194+
decimals?: number,
195+
): bigint | undefined {
179196
if (value === undefined) {
180197
return undefined;
181198
}
182199
if (value === "unlimited") {
183200
return maxUint256;
184201
}
202+
// The ERC20Claim condition extension in v5 does not convert to wei for us
203+
// so we have to, manually
204+
if (decimals) {
205+
return toUnits(value.toString(), decimals);
206+
}
207+
208+
// Will cause issue if trying to convert a non-integer string or number
209+
// thankfully the decimals logic above prevents it
185210
return BigInt(value);
186211
}
187212

188-
function toUnlimited(value: bigint) {
189-
return value === maxUint256 ? "unlimited" : value.toString();
213+
function toUnlimited(value: bigint, decimals?: number) {
214+
if (value === maxUint256) {
215+
return "unlimited";
216+
}
217+
if (decimals) {
218+
return toTokens(value, decimals);
219+
}
220+
return value.toString();
190221
}
191222

192223
async function fetchSnapshot(

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/_components/claim-conditions/claim-conditions-form/index.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
useFieldArray,
2727
useForm,
2828
} from "react-hook-form";
29+
import { toast } from "sonner";
2930
import {
3031
NATIVE_TOKEN_ADDRESS,
3132
type ThirdwebContract,
@@ -152,7 +153,7 @@ interface ClaimsConditionFormContextData {
152153
field: ControlledField;
153154
phaseIndex: number;
154155
formDisabled: boolean;
155-
tokenDecimals: number;
156+
tokenDecimals: number | undefined;
156157
isMultiPhase: boolean;
157158
isActive: boolean;
158159
dropType: DropType;
@@ -210,7 +211,6 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
210211
enabled: isErc20,
211212
},
212213
});
213-
const tokenDecimalsData = tokenDecimals.data ?? 0;
214214
const saveClaimPhaseNotification = useTxNotifications(
215215
"Saved claim phases",
216216
"Failed to save claim phases",
@@ -219,7 +219,7 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
219219
const claimConditionsQuery = useReadContract(getClaimPhasesInLegacyFormat, {
220220
contract,
221221
...(isErc20
222-
? { type: "erc20" }
222+
? { type: "erc20", decimals: tokenDecimals.data }
223223
: isErc721
224224
? { type: "erc721" }
225225
: { type: "erc1155", tokenId: BigInt(tokenId || 0) }),
@@ -259,7 +259,10 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
259259
);
260260
}, [claimConditionsQuery.data, isMultiPhase]);
261261

262-
const isFetchingData = claimConditionsQuery.isFetching || sendTx.isPending;
262+
const isFetchingData =
263+
claimConditionsQuery.isFetching ||
264+
sendTx.isPending ||
265+
(isErc20 && tokenDecimals.isLoading);
263266

264267
const canEditForm = isAdmin && !isFetchingData;
265268

@@ -353,13 +356,17 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
353356
action: "set-claim-conditions",
354357
label: "attempt",
355358
});
356-
359+
if (isErc20 && !tokenDecimals.data) {
360+
return toast.error(
361+
`Could not fetch token decimals for contract ${contract.address}`,
362+
);
363+
}
357364
try {
358365
const tx = setClaimPhasesTx(
359366
{
360367
contract,
361368
...(isErc20
362-
? { type: "erc20" }
369+
? { type: "erc20", decimals: tokenDecimals.data }
363370
: isErc721
364371
? { type: "erc721" }
365372
: { type: "erc1155", tokenId: BigInt(tokenId || 0) }),
@@ -453,6 +460,14 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
453460
);
454461
}
455462

463+
if (isErc20 && tokenDecimals.data === undefined) {
464+
return (
465+
<div className="flex h-[400px] w-full items-center justify-center rounded-lg border border-border">
466+
Failed to load token decimals
467+
</div>
468+
);
469+
}
470+
456471
return (
457472
<>
458473
<Flex onSubmit={handleFormSubmit} direction="column" as="form" gap={10}>
@@ -508,7 +523,7 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
508523
phaseIndex: index,
509524
formDisabled: !canEditForm,
510525
isErc20,
511-
tokenDecimals: tokenDecimalsData,
526+
tokenDecimals: tokenDecimals.data,
512527
dropType,
513528
setOpenSnapshotIndex,
514529
isAdmin,

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/_components/claim-conditions/claim-conditions-form/phase.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export const ClaimConditionsPhase: React.FC<ClaimConditionsPhaseProps> = ({
3535
isMultiPhase,
3636
phaseIndex,
3737
} = useClaimConditionsFormContext();
38-
3938
const toggleEditing = () => {
4039
form.setValue(`phases.${phaseIndex}.isEditing`, !field.isEditing);
4140
};

0 commit comments

Comments
 (0)