Solidity – How to Deploy Multiple Contracts Using Hardhat

hardhathardhat-deploysolidity

My two contracts look something like this:

contract ContractA {
        ContractB public contractB;
     
     constructor() {
            contractB = new ContractB(address(this));
    }
}

contract ContractB{

      address public tokenAddress;
         constructor(address _tokenAddress) {
                tokenAddress = _tokenAddress;
         }
}

This is my Hardhat deploy script:

async function main() {
 
  const ContractA = await hre.ethers.getContractFactory("ContractA");
  const contractA = await ContractA.deploy();

  await contractA.deployed();

}

When I deploy this in the Read Contract value I see the address of contract B, but it's just a plain address is not a contract that I can verify and see the functions. What am I doing wrong?

Best Answer

In your deployment script (the default being sample-script.js), you should do:

const contractA= await ethers.getContractFactory(ContractA); 
const contractAcontract= await contractA.deployed();
await contractAcontract.deployed();

const contractB = await ethers.getContractFactory(ContractB); 
const contractBcontract = await contractB.deployed(contractAcontract.address);
await contractBcontract.deployed();

I'd say you need to deploy both contracts, not just contractA.

EDIT:

I was right about deploying both contracts, although you need to pass the first contract's address as an argument to the second one. Found a similar question here. Hope that solves it for you.

Related Topic