English 中文(简体)
NFT合同地址显示NULL价值(部署js)
原标题:NFT Contract Address shows NULL value (deploy.js)
I am new to blockchain development. I am facing one issue in deploy.js, while running npx hardhat run src/backend/scripts/deploy.js --network localhost ideally, it should show something like Deploying contracts with the account: value Account balance: value NFT CONTACT ADDRESS value but it is returning Deploying contracts with the account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 Account balance: 9999992459571303767619n NFT CONTACT ADDRESS undefined i d like to know how to fix this undefined issue, please help deploy.js code async function main() { const [deployer] = await ethers.getSigners(); console.log("Deploying contracts with the account:", deployer.address); console.log("Account balance:", await deployer.provider.getBalance(deployer.address)); // Get the ContractFactories and Signers here. const NFT = await ethers.getContractFactory("NFT"); const nft = await NFT.deploy(); // Save copies of each contracts abi and address to the frontend. console.log("NFT CONTACT ADDRESS",nft.address) saveFrontendFiles(nft , "NFT"); } function saveFrontendFiles(contract, name) { const fs = require("fs"); const contractsDir = __dirname + "/../../frontend/contractsData"; if (!fs.existsSync(contractsDir)) { fs.mkdirSync(contractsDir); } fs.writeFileSync( contractsDir + `/${name}-address.json`, JSON.stringify({ address: contract.address }, undefined, 2) ); const contractArtifact = artifacts.readArtifactSync(name); fs.writeFileSync( contractsDir + `/${name}.json`, JSON.stringify(contractArtifact, null, 2) ); } main() .then(() => process.exit(0)) .catch(error => { console.error(error); process.exit(1); }); i tried changing deploy.js file to async function main() { const [deployer] = await ethers.getSigners(); console.log("Deploying contracts with the account:", deployer.address); console.log("Account balance:", await deployer.provider.getBalance(deployer.address)); // Get the ContractFactory and Signer here. console.log("Getting the contract factory for NFT..."); const NFT = await ethers.getContractFactory("NFT"); console.log("Deploying NFT contract..."); const nft = await NFT.deploy(); await nft.deployed(); // Ensure the contract is fully deployed console.log("NFT contract deployed to:", nft.address); // Save copies of each contract s ABI and address to the frontend. saveFrontendFiles(nft, "NFT"); } main().catch((error) => { console.error("Error in contract deployment:", error); process.exit(1); }); it shows some error related to nft.deployed not found etc. etc.. Expectations: to get some nft contact address value
问题回答
Issue is fixed: async function main() { const [deployer] = await ethers.getSigners(); console.log("Deploying contracts with the account:", deployer.address); console.log("Account balance:", (await ethers.provider.getBalance(deployer.address)).toString()); const NFT = await ethers.getContractFactory("NFT"); console.log("Deploying NFT contract..."); const nft = await NFT.deploy(); console.log("Waiting for deployment..."); // Wait for the contract to be mined await nft.waitForDeployment(); const nftAddress = await nft.getAddress(); console.log("NFT CONTRACT ADDRESS", nftAddress); saveFrontendFiles(nft, "NFT"); } async function saveFrontendFiles(contract, name) { const fs = require("fs"); const contractsDir = __dirname + "/../../frontend/contractsData"; if (!fs.existsSync(contractsDir)) { fs.mkdirSync(contractsDir); } const contractAddress = await contract.getAddress(); console.log("Contract address:", contractAddress); const addressData = JSON.stringify({ address: contractAddress }, undefined, 2); console.log("Address data to be written:", addressData); const filePath = contractsDir + `/${name}-address.json`; console.log("Writing to file:", filePath); try { fs.writeFileSync(filePath, addressData); console.log("File written successfully"); } catch (error) { console.error("Error writing file:", error); } const contractArtifact = artifacts.readArtifactSync(name); fs.writeFileSync( contractsDir + `/${name}.json`, JSON.stringify(contractArtifact, null, 2) ); } main() .then(() => process.exit(0)) .catch(error => { console.error(error); process.exit(1); });




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签