28

Is it possible to referer to a contract that has been deployed with Contract.new() with Contract.deployed() ?

When referencing .deployed() in my test it throws :

has not been deployed to detected network (1501794786154)

Obviously the code has been successfully deployed on the network ( in my deploy file i can get access to all the contracts address, can see the sucessfull transaction in Testrpc , etc ) .

Also the network id is the good one.

    module.exports = {
      networks: {
        development: {
          host: "localhost",
          port: 8545,
          network_id: "*" // Match any network id
        }
      }
    };

Lastly : When i check the contract Build folder json file for the contract that is not recognized as deploy i can find :

"networks": {
    "1501794786154": {

So it is indeed deployed.

I've tried deleting the build folder and recompile again etc. as per

Truffle Webpack starts with error: MetaCoin has not been deployed to detected network

Anybody has a pointer?

1
  • 1
    Perhaps you can do truffle migrate again (with 2_deploy_contracts.js properly set) Commented Nov 3, 2017 at 2:01

6 Answers 6

17

Sometimes I've run into a similar issue where truffle doesn't detect changes in my contracts and either it doesn't compile it or, when it does, it doesn't migrate it (saying network is up to date).

You can try running truffle compile (and/or truffle migrate) with the --reset flag.

3
  • 1
    Wow. This should be the first tip you learn with Truffle. It's like "clean" for a C++ or similar project. Thanks! Commented Mar 14, 2018 at 1:43
  • 1
    or even if --reset flag don't work then use --reset --all
    – Saad Saadi
    Commented Jun 20, 2018 at 1:31
  • 1
    very good tip, but on my case it also missed on 2_deploy_contract.js
    – kroe
    Commented Jul 11, 2018 at 2:19
9

Your contract might not have been added to the truffle migrations deployer. You should check your file /migrations/2_deploy_contracts.js

var YourContractName = artifacts.require("./YourContractName.sol");

module.exports = function(deployer) {
   .......;
   deployer.deploy(YourContractName);
};
1
  • 3
    oh damn, that was it!!
    – kroe
    Commented Jul 11, 2018 at 2:19
6

I ran into this same issue today. For whatever reason truffle is not detecting any contracts deployed in 2_deploy_contracts.js unless I explicitly specify the contract address using the at() function. I was able to get around this by deploying the contract in 1_initial_migration.js instead and then redeploying all my contracts from scratch.

4
  • Welcome to the community :) refer here post formatting guide Commented Oct 7, 2017 at 7:13
  • Thank you Achala that is a helpful reference. For my post, was there a specific portion I could have formatted differently?
    – John B
    Commented Oct 9, 2017 at 16:33
  • Actually your post need very less formatting to make it more readable. I may suggest some, anyway they are not essential :) Guide might help you when you are posting later. Commented Oct 9, 2017 at 16:54
  • This answer deserves a lot more credit. The at function, which takes the address of the deployed contract as an explicit argument, is perfect for this task. I'm not even sure what the deployed function is really good for. What if I have deployed more than one instance of the same contract? Commented Oct 17, 2018 at 12:32
5

I've just solved the same problem by following steps:

  1. in the 1_initial_migration.js (or what ever the name of your deploy config file):

    var Migrations = artifacts.require("./Contract.sol");
    module.exports = function(deployer) {
      deployer.deploy(Contract);
    };
    
  2. run command truffle compile and truffle migrate --reset

1

There are two ways

  1. Remove the build folder

    rm -r build/
    truffle compile
    truffle migrate

  2. Use

    truffle migrate --reset --all

I think this will solve your problem

1

Change the metamask network from test net or main net to your localhost version

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