Why is my NFT contract address showing as undefined in deploy script?

Hey guys, I’m stuck with a problem in my Hardhat deploy script. When I run it, the NFT contract address comes up as undefined. Here’s what I’m seeing:

Deploying contracts with the account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Account balance: 9999992459571303767619n
NFT CONTACT ADDRESS undefined

I’ve tried tweaking my deploy.js file, but no luck. It either shows undefined or throws errors about nft.deployed not being found.

Here’s a snippet of what I’ve attempted:

async function deployNFT() {
  const [owner] = await ethers.getSigners();
  console.log('Deploying with:', owner.address);

  const TokenContract = await ethers.getContractFactory('MyNFT');
  const token = await TokenContract.deploy();
  
  console.log('NFT address:', token.address);
  
  // Save contract info
  storeContractData(token, 'MyNFT');
}

deployNFT().catch(console.error);

Any ideas on how to get the actual NFT contract address? I’m pretty new to this, so any help would be awesome!

I encountered a similar issue recently. The problem might be in your asynchronous handling. Try modifying your deployNFT function to use await properly:

async function deployNFT() {
  const [owner] = await ethers.getSigners();
  console.log('Deploying with:', owner.address);

  const TokenContract = await ethers.getContractFactory('MyNFT');
  const token = await TokenContract.deploy();
  await token.deployed();
  
  console.log('NFT address:', token.address);
  
  // Save contract info
  await storeContractData(token, 'MyNFT');
}

await deployNFT();

This ensures the contract is fully deployed before you try to access its address. Also, make sure your Hardhat configuration is correct and you’re running on the right network. Let me know if this resolves your issue.

hey SparklingGem, i ran into this too. try adding await token.deployed() before logging the address. sometimes the contract isn’t fully deployed when you try to access it. also, double-check your contract name matches in the getContractFactory call. hope this helps!

hey sparklingGem! that’s a tricky one for sure. have you tried wrapping your deploy function in a main() and using async/await? something like this might work:

async function main() {
  const [deployer] = await ethers.getSigners();
  console.log('Deploying with:', deployer.address);

  const MyNFT = await ethers.getContractFactory('MyNFT');
  const nft = await MyNFT.deploy();
  
  await nft.deployed();
  
  console.log('NFT address:', nft.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

this way, you’re making sure everything’s properly awaited. also, double check your contract file name matches ‘MyNFT’ exactly. sometimes those little things trip us up! let me know if that helps or if you need more ideas?