Skip to content
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

[Dashboard] (test) Unit conversion for ERC20 claim condition #5235

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function toDate(timestamp: number | Date | undefined) {
}
return new Date(timestamp);
}
function toBigInt(value: string | number | undefined) {
export function toBigInt(value: string | number | undefined) {
if (value === undefined) {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import {
useFieldArray,
useForm,
} from "react-hook-form";
import { toast } from "sonner";
import {
NATIVE_TOKEN_ADDRESS,
type ThirdwebContract,
ZERO_ADDRESS,
toUnits,
} from "thirdweb";
import { decimals } from "thirdweb/extensions/erc20";
import {
Expand All @@ -48,7 +50,11 @@ import {
} from "../legacy-zod-schema";
import { ResetClaimEligibility } from "../reset-claim-eligibility";
import { SnapshotUpload } from "../snapshot-upload";
import { getClaimPhasesInLegacyFormat, setClaimPhasesTx } from "./hooks";
import {
getClaimPhasesInLegacyFormat,
setClaimPhasesTx,
toBigInt,
} from "./hooks";
import { ClaimConditionsPhase } from "./phase";

type ClaimConditionDashboardInput = ClaimConditionInput & {
Expand Down Expand Up @@ -355,6 +361,31 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
});

try {
if (isErc20 && !tokenDecimalsData) {
return toast.error("Could not fetch token metadata");
}

// For ERC20 claim condition, we need to convert `maxClaimableSupply` and `maxClaimbablePerWallet` to wei
const phases = isErc20
? d.phases.map((item) => {
item.maxClaimableSupply =
item.maxClaimableSupply !== undefined
? toUnits(
String(toBigInt(item.maxClaimableSupply)).toString(),
tokenDecimalsData,
).toString()
: undefined;
item.maxClaimablePerWallet =
item.maxClaimablePerWallet !== undefined
? toUnits(
String(toBigInt(item.maxClaimablePerWallet)),
tokenDecimalsData,
).toString()
: undefined;
return item;
})
: d.phases;

const tx = setClaimPhasesTx(
{
contract,
Expand All @@ -364,7 +395,7 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
? { type: "erc721" }
: { type: "erc1155", tokenId: BigInt(tokenId || 0) }),
},
d.phases,
phases,
);
await sendTx.mutateAsync(tx);
trackEvent({
Expand Down Expand Up @@ -630,7 +661,10 @@ export const ClaimConditionsForm: React.FC<ClaimConditionsFormProps> = ({
colorScheme="primary"
txChainID={contract.chain.id}
transactionCount={1}
isDisabled={claimConditionsQuery.isPending}
isDisabled={
claimConditionsQuery.isPending ||
(isErc20 && tokenDecimals.isPending)
}
type="submit"
isLoading={sendTx.isPending}
loadingText="Saving..."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AdminOnly } from "@3rdweb-sdk/react/components/roles/admin-only";
import { Flex, SimpleGrid } from "@chakra-ui/react";
import { ChevronDownIcon, ChevronUpIcon, XIcon } from "lucide-react";
import type { ThirdwebContract } from "thirdweb";
import { type ThirdwebContract, toTokens } from "thirdweb";
import { maxUint256 } from "thirdweb/utils";
import { Badge, Button, Card, Heading, Text } from "tw-components";
import { ClaimConditionTypeData, useClaimConditionsFormContext } from ".";
import { PricePreview } from "../price-preview";
Expand Down Expand Up @@ -34,6 +35,7 @@ export const ClaimConditionsPhase: React.FC<ClaimConditionsPhaseProps> = ({
isActive,
isMultiPhase,
phaseIndex,
tokenDecimals,
} = useClaimConditionsFormContext();

const toggleEditing = () => {
Expand Down Expand Up @@ -102,7 +104,18 @@ export const ClaimConditionsPhase: React.FC<ClaimConditionsPhaseProps> = ({
<Text fontWeight="bold">
{isErc20 ? "Tokens" : "NFTs"} to drop
</Text>
<Text textTransform="capitalize">{field.maxClaimableSupply}</Text>
<Text textTransform="capitalize">
{field.maxClaimableSupply === "unlimited"
? "Unlimited"
: isErc20 && field.maxClaimableSupply
? BigInt(field.maxClaimableSupply) === maxUint256
? "Unlimited"
: toTokens(
BigInt(field.maxClaimableSupply),
tokenDecimals,
)
: field.maxClaimableSupply}
</Text>
</div>
<PricePreview
price={field.price}
Expand All @@ -117,7 +130,16 @@ export const ClaimConditionsPhase: React.FC<ClaimConditionsPhaseProps> = ({
<Text>Unlimited</Text>
) : (
<Text textTransform="capitalize">
{field.maxClaimablePerWallet}
{field.maxClaimablePerWallet === "unlimited"
? "Unlimited"
: isErc20 && field.maxClaimablePerWallet
? BigInt(field.maxClaimablePerWallet) === maxUint256
? "Unlimited"
: toTokens(
BigInt(field.maxClaimablePerWallet),
tokenDecimals,
)
: field.maxClaimablePerWallet}
</Text>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
InputRightElement,
} from "@chakra-ui/react";
import { useEffect, useState } from "react";
import { toTokens } from "thirdweb";
import { Button } from "tw-components";

interface QuantityInputWithUnlimitedProps
Expand Down Expand Up @@ -60,7 +61,13 @@ export const QuantityInputWithUnlimited: React.FC<
if (value === "unlimited") {
setStringValue("unlimited");
} else if (!Number.isNaN(Number(value))) {
setStringValue(Number(Number(value).toFixed(decimals)).toString());
if (decimals) {
setStringValue(toTokens(BigInt(value), decimals));
} else {
setStringValue(
Number(Number(value).toFixed(decimals)).toString(),
);
}
} else {
setStringValue("0");
}
Expand Down