0

Suppose I would like to migrate 2 contracts that are dependent on each other. This can can be done with

var contract1 = artifacts.require("contract1");
var contract2 = artifacts.require("contract2");

module.exports = function(deployer) {
  deployer.deploy(contract1).then(function(){
        return deployer.deploy(contract2, contract1.address)
});
};

However, if I try and nest a 3rd contract that has dependencies on the first two, with the following code :

var contract1 = artifacts.require("contract1");
var contract2 = artifacts.require("contract2");
var contract3 = artifacts.require("contract3");

module.exports = function(deployer) {
  deployer.deploy(contract1).then(function(){
        deployer.deploy(contract1, contract2.address).then(function(){
        return deployer.deploy(contract3, contract1.address, contract2.address)
        })
})};

Then running truffle deploy on this migration file only deploys the first contract and not the remaining two. What is the correct syntax for deploying 3, or even up to n contracts? Is there a nicer way than doing it than this ugly nesting?

Another question, say an additional argument else needs to be calculated for contract 3, what is the correct syntax for this? eg. I want to do something like

var contract1 = artifacts.require("contract1");
var contract2 = artifacts.require("contract2");
var contract3 = artifacts.require("contract3");
var someSol = artifacts.require("someSol.sol");

module.exports = function(deployer) {
  deployer.deploy(contract1).then(function(){
        deployer.deploy(contract1, contract2.address).then(function(){

        const someAddress = await someSol.foo(contract1.address, contract2.address);
        return deployer.deploy(contract3, contract1.address, contract2.address, someAddress)
        })
})};

Any help greatly appreciated

1 Answer 1

1

This is a bit of a JavaScript question.

Easier is to use the async/await pattern. That way you don't need to nest the promises.

So it could look something like this:

module.exports = function(deployer, network, accounts) {
    deployer.then(async () => {
        await deployer.deploy(A);
        await deployer.deploy(B, A.address);
        //...
    });
};
1
  • You're right it was a javascript syntax question. I am very new with Javascript (Python background) so I am finding it quite difficult. This syntax worked a treat and I got my deployment working as I wanted. Thank you very much
    – mch56
    Commented May 28, 2021 at 16:29

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.