Truffle – Deploy a Contract and Pass it as an Argument to Another Contract’s Constructor

contract-deploymentcontract-developmenttruffletruffle-migration

I have two contracts, contractA & contractB. I want Truffle to first deploy contractA, then pass it as an argument to contractB's constructor.

This is what I am currently trying, but contractB never actually gets deployed:

let contractA = artifacts.require("./contractA.sol");
let contractB = artifacts.require("./contractB.sol");

module.exports = async function(deployer, network) {
  await deployer.deploy(contractA);  // this gets deployed fine
  deployer.deploy(contractB, contractA.address); // this *never* gets deployed
};

As a matter of fact, contractB does not get deployed even if I change the code to this:

await deployer.deploy(contractA);    
deployer.deploy(contractB, "0x123");  // does not deploy even if I enter the address manually

What am I missing here?

Best Answer

Try this:

deployer.then(async function() {
    let contractA = await artifacts.require("A").new();
    let contractB = await artifacts.require("B").new(contractA._address);
});
Related Topic