From dc7f48cb486ffe95c48cf223cb5b49b3cddf833a Mon Sep 17 00:00:00 2001 From: aybanda Date: Sat, 14 Dec 2024 10:57:44 +0530 Subject: [PATCH] Enhance error handling and user feedback in minting process --- scripts/mint.js | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/scripts/mint.js b/scripts/mint.js index f0d21c9..4392beb 100644 --- a/scripts/mint.js +++ b/scripts/mint.js @@ -1,31 +1,48 @@ -require('dotenv').config(); +require("dotenv").config(); async function mintTokens(tokenAddress, recipientAddress, amount) { const TestToken = await ethers.getContractFactory("TestToken"); const token = await TestToken.attach(tokenAddress); - - const tx = await token.mint(recipientAddress, amount); - await tx.wait(); - - console.log(`Minted ${ethers.utils.formatEther(amount)} tokens at ${tokenAddress}`); + + try { + const tx = await token.mint(recipientAddress, amount); + await tx.wait(); + console.log( + `Minted ${ethers.utils.formatEther(amount)} tokens at ${tokenAddress}` + ); + } catch (error) { + console.error(`Error minting tokens at ${tokenAddress}:`, error.message); + throw new Error(`Minting failed for ${tokenAddress}`); + } } async function main() { if (!process.env.TOKEN1_ADDRESS || !process.env.TOKEN2_ADDRESS) { throw new Error("Token addresses not found in .env file"); } - + if (!process.env.RECIPIENT_ADDRESS) { throw new Error("Recipient address not found in .env file"); } const mintAmount = ethers.utils.parseEther("1000"); - + // Mint tokens for both contracts - await mintTokens(process.env.TOKEN1_ADDRESS, process.env.RECIPIENT_ADDRESS, mintAmount); - await mintTokens(process.env.TOKEN2_ADDRESS, process.env.RECIPIENT_ADDRESS, mintAmount); - - console.log("All tokens minted successfully!"); + try { + await mintTokens( + process.env.TOKEN1_ADDRESS, + process.env.RECIPIENT_ADDRESS, + mintAmount + ); + await mintTokens( + process.env.TOKEN2_ADDRESS, + process.env.RECIPIENT_ADDRESS, + mintAmount + ); + console.log("All tokens minted successfully!"); + } catch (error) { + console.error("Minting process encountered an error:", error.message); + } } main() @@ -33,4 +50,4 @@ main() .catch((error) => { console.error(error); process.exit(1); - }); \ No newline at end of file + });