0

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?

1 Answer 1

0

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.

2
  • Hi, you need to pass the address as a parameter in the contractB. I already tried that method but contractB address in the contractA is different than contractB deployed. Commented Apr 16, 2022 at 12:25
  • @JohnDoeMighty I've updated my answer, hope that solves it for you. Commented Apr 16, 2022 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.