Skip to content

Commit bf64fd5

Browse files
committed
feat: refactor allowlist fetching logic
Extracted the allowlist fetching and parsing logic into a separate function
1 parent 61f1fea commit bf64fd5

1 file changed

Lines changed: 65 additions & 49 deletions

File tree

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

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,69 @@ const DatesAndPeople = ({ form }: FormStepsProps) => {
423423
);
424424
};
425425

426+
const parseAllowlist = async (value: string) => {
427+
let data;
428+
let allowList: AllowlistEntry[] = [];
429+
const url = value.startsWith("ipfs://")
430+
? `https://ipfs.io/ipfs/${value.replace("ipfs://", "")}`
431+
: value.startsWith("https://")
432+
? value
433+
: null;
434+
435+
if (!url) {
436+
errorToast("Invalid URL. URL must start with 'https://' or 'ipfs://'");
437+
return;
438+
}
439+
data = await fetch(url);
440+
441+
const contentType = data.headers.get("content-type");
442+
443+
if (
444+
contentType?.includes("text/csv") ||
445+
contentType?.includes("text/plain") ||
446+
value.endsWith(".csv")
447+
) {
448+
try {
449+
const text = await data.text();
450+
const parsedData = Papa.parse<AllowlistEntry>(text, {
451+
header: true,
452+
skipEmptyLines: true,
453+
});
454+
455+
const validEntries = parsedData.data.filter(
456+
(entry) => entry.address && entry.units,
457+
);
458+
459+
// Calculate total units
460+
const total = validEntries.reduce(
461+
(sum, entry) => sum + BigInt(entry.units),
462+
BigInt(0),
463+
);
464+
465+
allowList = validEntries.map((entry) => {
466+
const address = getAddress(entry.address);
467+
const originalUnits = BigInt(entry.units);
468+
// Scale units proportionally to DEFAULT_NUM_UNITS
469+
const scaledUnits =
470+
total > 0 ? (originalUnits * DEFAULT_NUM_UNITS) / total : BigInt(0);
471+
472+
return {
473+
address: address,
474+
units: scaledUnits,
475+
};
476+
});
477+
478+
return allowList;
479+
} catch (e) {
480+
e instanceof Error
481+
? errorToast(e.message)
482+
: errorToast("Failed to parse allowlist.");
483+
}
484+
} else {
485+
errorToast("Invalid file type.");
486+
}
487+
};
488+
426489
const calculatePercentageBigInt = (
427490
units: bigint,
428491
total: bigint = DEFAULT_NUM_UNITS,
@@ -535,55 +598,8 @@ const AdvancedAndSubmit = ({ form, isBlueprint }: FormStepsProps) => {
535598
};
536599

537600
const fetchAllowlist = async (value: string) => {
538-
let data;
539-
let allowList;
540-
const url = value.startsWith("ipfs://")
541-
? `https://ipfs.io/ipfs/${value.replace("ipfs://", "")}`
542-
: value.startsWith("https://")
543-
? value
544-
: null;
545-
546-
if (!url) return errorToast("Invalid URL");
547-
data = await fetch(url);
548-
549-
const contentType = data.headers.get("content-type");
550-
551-
if (
552-
contentType?.includes("text/csv") ||
553-
contentType?.includes("text/plain") ||
554-
value.endsWith(".csv")
555-
) {
556-
const text = await data.text();
557-
const parsedData = Papa.parse<AllowlistEntry>(text, {
558-
header: true,
559-
skipEmptyLines: true,
560-
});
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-
});
584-
} else {
585-
return errorToast("Invalid allowlist.");
586-
}
601+
const allowList = await parseAllowlist(value);
602+
if (!allowList || allowList.length === 0) return;
587603

588604
const totalUnits = DEFAULT_NUM_UNITS;
589605

0 commit comments

Comments
 (0)