Skip to content

Commit e928240

Browse files
committed
feat: scale units
Improved the allowlist entry validation and scaling logic by calculating total units and proportionally scaling each entry's units based on the total.
1 parent ae0dc57 commit e928240

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

components/hypercert/hypercert-minting-form/form-steps.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,13 @@ const AdvancedAndSubmit = ({ form, isBlueprint }: FormStepsProps) => {
448448
const [selectedFile, setSelectedFile] = useState<File | null>(null);
449449
const [isAdvancedOpen, setIsAdvancedOpen] = useState(false);
450450
const [createDialogOpen, setCreateDialogOpen] = useState(false);
451+
const setAllowlistURL = (allowlistURL: string) => {
452+
form.setValue("allowlistURL", allowlistURL);
453+
};
451454
const setAllowlistEntries = (allowlistEntries: AllowlistEntry[]) => {
452455
form.setValue("allowlistEntries", allowlistEntries);
453456
};
457+
454458
const allowlistEntries = form.watch("allowlistEntries");
455459

456460
useEffect(() => {
@@ -554,19 +558,31 @@ const AdvancedAndSubmit = ({ form, isBlueprint }: FormStepsProps) => {
554558
header: true,
555559
skipEmptyLines: true,
556560
});
557-
// check if address isAddress, and units is a bigint type
558-
// if not, throw error
559-
allowList = parsedData.data
560-
.filter((entry) => entry.address && entry.units)
561-
.map((entry) => {
562-
const address = getAddress(entry.address);
563-
return {
564-
address: address,
565-
units: BigInt(entry.units),
566-
};
567-
});
561+
562+
const validEntries = parsedData.data.filter(
563+
(entry) => entry.address && entry.units,
564+
);
565+
566+
// Calculate total units
567+
const total = validEntries.reduce(
568+
(sum, entry) => sum + BigInt(entry.units),
569+
BigInt(0),
570+
);
571+
572+
allowList = validEntries.map((entry) => {
573+
const address = getAddress(entry.address);
574+
const originalUnits = BigInt(entry.units);
575+
// Scale units proportionally to DEFAULT_NUM_UNITS
576+
const scaledUnits =
577+
total > 0 ? (originalUnits * DEFAULT_NUM_UNITS) / total : BigInt(0);
578+
579+
return {
580+
address: address,
581+
units: scaledUnits,
582+
};
583+
});
568584
} else {
569-
return errorToast("Invalid file type.");
585+
return errorToast("Invalid allowlist.");
570586
}
571587

572588
const totalUnits = DEFAULT_NUM_UNITS;
@@ -706,6 +722,8 @@ const AdvancedAndSubmit = ({ form, isBlueprint }: FormStepsProps) => {
706722

707723
<CreateAllowlistDialog
708724
setAllowlistEntries={setAllowlistEntries}
725+
setAllowlistURL={setAllowlistURL}
726+
allowlistURL={field?.value}
709727
open={createDialogOpen}
710728
setOpen={setCreateDialogOpen}
711729
initialValues={allowlistEntries?.map((entry) => ({

0 commit comments

Comments
 (0)