From 67c86d8c922ef007cb78b4b906f47c30ab0eee10 Mon Sep 17 00:00:00 2001 From: Fawaz Butt Date: Fri, 28 Jun 2024 17:34:51 -0400 Subject: [PATCH] adding validation to setStages --- scripts/setStages.ts | 48 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/scripts/setStages.ts b/scripts/setStages.ts index 8ed4b201..5c7bb4f0 100644 --- a/scripts/setStages.ts +++ b/scripts/setStages.ts @@ -24,6 +24,31 @@ interface StageConfig { variableWalletLimitPath?: string; } +function isValidDate(dateString: string): boolean { + const date = new Date(dateString); + return date instanceof Date && !isNaN(date.getTime()); +} + +function validateAndConvertDates(stage: StageConfig) { + const { startDate, endDate } = stage; + + if (!isValidDate(startDate.toString()) || !isValidDate(endDate.toString())) { + throw new Error("Invalid start or end date"); + } + + const start = new Date(startDate); + const end = new Date(endDate); + + if (end <= start) { + throw new Error("End date must be after start date"); + } + + const startTimeUnixSeconds = Math.floor(start.getTime() / 1000); + const endTimeUnixSeconds = Math.floor(end.getTime() / 1000); + + return { startTimeUnixSeconds, endTimeUnixSeconds }; +} + export const setStages = async ( args: ISetStagesParams, hre: HardhatRuntimeEnvironment, @@ -91,16 +116,19 @@ export const setStages = async ( }), ); - const stages = stagesConfig.map((s, i) => ({ - price: ethers.utils.parseEther(s.price), - mintFee: s.mintFee ? ethers.utils.parseEther(s.mintFee) : 0, - maxStageSupply: s.maxSupply ?? 0, - walletLimit: s.walletLimit ?? 0, - merkleRoot: merkleRoots[i], - startTimeUnixSeconds: Math.floor(new Date(s.startDate).getTime() / 1000), - endTimeUnixSeconds: Math.floor(new Date(s.endDate).getTime() / 1000), - })); - + const stages = stagesConfig.map((s, i) => { + const { startTimeUnixSeconds, endTimeUnixSeconds } = validateAndConvertDates(s); + + return { + price: ethers.utils.parseEther(s.price), + mintFee: s.mintFee ? ethers.utils.parseEther(s.mintFee) : 0, + maxStageSupply: s.maxSupply ?? 0, + walletLimit: s.walletLimit ?? 0, + merkleRoot: merkleRoots[i], + startTimeUnixSeconds, + endTimeUnixSeconds, + }; + }); console.log( `Stage params: `, JSON.stringify(