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?


Try this:

deployer.then(async function() {
    let contractA = await artifacts.require("A").new();
    let contractB = await artifacts.require("B").new(contractA._address);
  • Doesn't seem to work for me. Console logging contractA gives undefined. – Tarek Sep 2 '18 at 18:37
  • Well, is your contract really saved in a file named "A.sol"? I wrote this as an example, I cannot guess your file names, so I used the same abbreviations as in your question. – goodvibration Sep 2 '18 at 18:45
  • Yes, it's A.sol – Tarek Sep 2 '18 at 18:56
  • Well maybe change it to "A.sol". That's how I usually do it (I just thought it was allowed without it, as recommended in Truffle official docs). – goodvibration Sep 2 '18 at 18:59
  • Yes already tried that as well. – Tarek Sep 2 '18 at 19:03

I was finally able to solve this by creating multiple Truffle deployment files.


// 2_deploy_contractA.js

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

module.exports = function(deployer, network) {


// 3_deploy_contractB.js

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

module.exports = function(deployer, network) {
    deployer.deploy(contractB, contractA.address);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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