Smart Contract Deployment Script Returns Undefined Address Value

Contract Address Showing as Undefined in Deployment Script

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 contract address comes back as undefined instead of the actual deployed address.

Expected output:

Deploying contracts with account: [address]
Account balance: [balance]
TOKEN CONTRACT ADDRESS: [actual address]

Current output:

Deploying contracts with account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Account balance: 9999992459571303767619n
TOKEN CONTRACT ADDRESS: undefined

My deployment script:

async function deploy() {
    const [signer] = await ethers.getSigners();
  
    console.log("Deploying contracts with account:", signer.address);
    console.log("Account balance:", await signer.provider.getBalance(signer.address));
  
    const TokenFactory = await ethers.getContractFactory("MyToken");
    const token = await TokenFactory.deploy();
    
    console.log("TOKEN CONTRACT ADDRESS", token.address);
    saveContractData(token, "MyToken");
}

function saveContractData(contract, contractName) {
    const fs = require("fs");
    const dataDir = __dirname + "/../../frontend/contracts";
  
    if (!fs.existsSync(dataDir)) {
      fs.mkdirSync(dataDir);
    }
  
    fs.writeFileSync(
      dataDir + `/${contractName}-address.json`,
      JSON.stringify({ address: contract.address }, undefined, 2)
    );
  
    const artifact = artifacts.readArtifactSync(contractName);
  
    fs.writeFileSync(
      dataDir + `/${contractName}.json`,
      JSON.stringify(artifact, null, 2)
    );
}
  
deploy()
    .then(() => process.exit(0))
    .catch(error => {
      console.error(error);
      process.exit(1);
    });

I tried adding await token.deployed() but got errors saying the method doesn’t exist. How can I properly get the deployed contract address? Any help would be appreciated.

Hmm, interesting issue! What versions of hardhat and ethers are you running? Check with npm list ethers and npm list hardhat.

The undefined address thing usually happens with version mismatches. I saw the other suggestions, but have you tried logging the entire token object to see what’s actually available? Try console.log(Object.keys(token)) right after deployment.

Are you getting any errors or warnings during deployment that aren’t showing in your console? Sometimes silent failures cause this.

Try deploying to hardhat’s built-in network instead of localhost - that’ll tell you if it’s network-specific or a script issue.

you’re using ethers v6 - the deployment flow changed. use await token.waitForDeployment() instead of the old deployed() method, then get the address with await token.getAddress(). they deprecated the address property in newer versions.

This issue is due to changes in Hardhat’s ethers.js version regarding contract deployment. The contract address is not immediately available after calling deploy(), as the transaction needs to be mined first. To resolve it, modify your script like this:

const TokenFactory = await ethers.getContractFactory("MyToken");
const token = await TokenFactory.deploy();
await token.deployTransaction.wait();

console.log("TOKEN CONTRACT ADDRESS", token.address);

By calling wait() on the transaction, you ensure that your contract is fully deployed before proceeding.