2

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?

2 Answers 2

2

Try this:

deployer.then(async function() {
    let contractA = await artifacts.require("A").new();
    let contractB = await artifacts.require("B").new(contractA._address);
});
7
  • Doesn't seem to work for me. Console logging contractA gives undefined.
    – Tarek
    Commented Sep 2, 2018 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. Commented Sep 2, 2018 at 18:45
  • Yes, it's A.sol
    – Tarek
    Commented Sep 2, 2018 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). Commented Sep 2, 2018 at 18:59
  • Yes already tried that as well.
    – Tarek
    Commented Sep 2, 2018 at 19:03
2

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

2_deploy_contractA.js:

// 2_deploy_contractA.js

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

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

3_deploy_contractB.js:

// 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 and acknowledge you have read our privacy policy.

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