3

I am trying to develop my project using React, Ethers, and Hardhat. Whenever I run the below commands in order, the hardhat gives me the same address as previously deployed contracts. So weird! I might be wrong somewhere.

  1. First I start the node by: "npx hardhat node"
  2. Then in another terminal I run: "npx hardhat compile"
  3. Then this command on the same terminal as the last: "npx hardhat run scripts/deploy-script.js --network localhost"

This process always gives me the same address, although I may have made some changes to the scripts. It is expected to give a new address for every newly deployed contract, all of the addresses are the same.

Does anybody know where I am wrong?

The contract address is always the same as past

enter image description here

2 Answers 2

2

When you start a Hardhat node, it uses a hardcoded list of private keys to generate accounts for you (it lists these when you start the node). And because these are fresh accounts (in the context of the blockchain, since it's a new blockchain) their nonce starts at zero.

Since contract addresses (with regular deployment, not with CREATE2) are calculated based on the sender's address (which is always the same, since Hardhat uses hardcoded private key) and the account nonce, the contract address is also always the same. You can read here more about contract address generation: https://ethereum.stackexchange.com/a/761/31933

2
  • Thanks!! So what do you propose for replacing the old contract with a new one when the new address for new contract is required? Commented Mar 28, 2022 at 12:27
  • Sorry, I didn't understand. You can redeploy whenever you want, and it will get a new address since your account nonce will change. Commented Mar 28, 2022 at 12:33
0

If you want a different address, skip the line "npx hardhat node". This will prevent the nonce from reinitializing. You will then get different addresses.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.