Address of contract is undefined when I try to console.log it (total beginner here)

Hello, everyone! Total beginner here and this is my first post and question. I hope I am following all the norms correctly.

I'm following the OpenZeppelin tutorial on "Developing smart contracts." I successfully finished all the steps at this URL: https://docs.openzeppelin.com/learn/developing-smart-contracts

and am currently working my way through the steps at the next URL:

I successfully set up a local blockchain and I'm stuck at the "Deploying a Smart Contract" section. I successfully ran this command "npm install --save-dev @nomicfoundation/hardhat-ethers ethers" And I added @nomiclabs/hardhat-ethers plugin to my hardhat..config.js file.

Where I'm stuck is this command: npx hardhat run --network localhost scripts/deploy.js

The results I get are:
Deploying Box...
Box deployed to: undefined

The tutorial specifies that I should be getting an address instead of "undefined."

Blocks are being created on the local blockchain. I see print out in the terminal with the local blockchain info, every time I run "npx hardhat run --network localhost scripts/deploy.js"

The code below is from my deploy.js, which I copied exactly from the tutorial, with one change (the tutorial said "await box.deployed()" but I was getting the error that the function didn't exist, so after some Googling, I found that it had to be replaced with "waitForDeployment()" which worked.

async function main() {
  // We get the contract to deploy
  const Box = await ethers.getContractFactory("Box");
  console.log("Deploying Box...");
  const box = await Box.deploy();
  await box.waitForDeployment();
  console.log("Box deployed to:", box.address);
}

I also tried replacing "box.address" with "box.getAddress()" (found this function online), but it returned "Promise { < pending > }", which I Googled and then got mentally jumbled up, and I'm not sure how to proceed. Any help would be greatly appreciated.

Try adding await before the box.getAddress().

await box.getAddress().

1 Like

THANK YOU. It worked. Thank you, thank you, thank you.

1 Like