1

I'm having a problem deploying multiple contracts. The first address gets written in contract1.json but contract2.json isn't showing an address. My second contract constructor function takes 3 args: bool, uint, address. In web3 I get the following error:

Contract2 has not been deployed to detected network (network/artifact mismatch)

var Contract1 = artifacts.require("./Contract1.sol");
var Contract2 = artifacts.require("./Contract2.sol");

module.exports = function(deployer) {
   deployer.deploy(Contract1).then(function(instance){
        deployer.deploy(Contract2, true, 20000, Contract1.address);
     })
    }

This is my truffle:

Using network 'develop'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0xf04f5da0e200f40dc8b96b505242fda61380088e89f5fd878d6f65534409109b
  Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
Saving successful migration to network...
  ... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Replacing Contract1...
  ... 0x8cf05bf0dbaf4917f4554272b08a36e36c05923ddb1bd96c863b4633d60819cf
  Contract1: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
Saving successful migration to network...
  Deploying Contract2...
  ... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a2398ce001bf44c43dc7bdda0
Saving artifacts...
  ... 0x1b968d4d74677f26876edda4aed7026782547dd651b0db5791eb220d4caa030b
Contract2: 0x8f0483125fcb9aaaefa9209d8e9d7b9c8b9fb90f

I can interact with the first contract using my dapp. If I want to interact with my second contract I get the error.

2 Answers 2

2

Most probably you want to have

deployer.deploy(Contract2, true, 20000, instance.address);
0

The issue is that deployer doesn't handle well Promises/async behavior you have to ensure all contracts are deployed before the deploy script finishes.

var Contract1 = artifacts.require("./Contract1.sol");
var Contract2 = artifacts.require("./Contract2.sol");

module.exports = function(deployer) {
    deployer.deploy(Contract1).then(function(instance) {
        // With the return here we 'chain' the promises
        // to ensure Contract2 is deployed
        return deployer.deploy(Contract2, true, 20000, Contract1.address);
    })
}

My preference is use async/await (issue in Github)

module.exports = function(deployer) {
    deployer.then(async () => {
        await deployer.deploy(Contract1);
        await deployer.deploy(Contract2, true, 20000, Contract1.address);
    })
}

Your Answer

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

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