Smart Contract Deployment Script Returns Undefined Address Value

I’m working on a blockchain project and having trouble with my deployment script. When I execute npx hardhat run scripts/deploy.js --network localhost, the token contract address shows up as undefined instead of displaying the actual deployed address.

Here’s my current deployment code:

async function deployContract() {
    const [owner] = await ethers.getSigners();
  
    console.log("Starting deployment with account:", owner.address);
    console.log("Wallet balance:", await owner.provider.getBalance(owner.address));
  
    const TokenContract = await ethers.getContractFactory("Token");
    const token = await TokenContract.deploy();
    
    console.log("TOKEN CONTRACT ADDRESS", token.address);
    saveContractData(token, "Token");
}

function saveContractData(contractInstance, contractName) {
    const fs = require("fs");
    const dataDir = __dirname + "/../../src/contractsData";
  
    if (!fs.existsSync(dataDir)) {
      fs.mkdirSync(dataDir);
    }
  
    fs.writeFileSync(
      dataDir + `/${contractName}-address.json`,
      JSON.stringify({ address: contractInstance.address }, undefined, 2)
    );
  
    const artifact = artifacts.readArtifactSync(contractName);
  
    fs.writeFileSync(
      dataDir + `/${contractName}.json`,
      JSON.stringify(artifact, null, 2)
    );
}

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

The output shows the deployer address and balance correctly, but the contract address prints as undefined. I tried adding await token.deployed() but got errors saying that method doesn’t exist. How can I properly get the deployed contract address?

This is a version compatibility issue with ethers.js. In v6+, contract deployment changed - you can’t access .address right after calling .deploy() because the contract hasn’t finished deploying yet. You need to wait for the deployment transaction to be mined first: const token = await TokenContract.deploy(); await token.waitForDeployment(); console.log(“TOKEN CONTRACT ADDRESS”, await token.getAddress()); If you’re on an older ethers version, try token.deployTransaction.wait() then token.address. Basically, modern ethers requires you to explicitly wait for deployment before grabbing the address. Had this exact issue last month when upgrading - this fixed it.

Oh interesting! I’ve seen this before too but curious about something - are you maybe running into a timing issue with your local hardhat network? Contract deployment can be finicky depending on how your hardhat.config.js is set up.

Kai29 nailed it with the ethers v6 changes, but what version of hardhat and ethers are you running? Check with npm list ethers in your project directory.

Also, try adding some logging to see what’s happening during deployment:

console.log("Contract instance:", token);
console.log("Deployment transaction:", token.deploymentTransaction());

This might give us more clues about what’s going wrong. Since your deployer address and balance show up fine, the network connection works - it’s probably just the contract deployment step that’s hanging.

Getting any other error messages or warnings when you run the deployment? Hardhat sometimes throws subtle warnings that point to the root cause. Also curious - does this happen every time you deploy, or just occasionally?

Yeah, sounds like the ethers v6 issue. Quick sanity check though - is your hardhat node running in another terminal? The script might run without obvious errors even when the network’s down. Also try console.log(token.target) instead of .address - that’s the new property in v6.