22

I seem to have followed the truffle guide to a T. But when I open my app in a browser for the first time, I'm getting this error in the console:

Error: MetaCoin has not been deployed to detected network (network/artifact mismatch)

I'm brand new to Ethereum development so I'm mostly just flailing around in the dark here. Any help would be greatly appreciated.

The steps I've followed:

(1) installed truffle and testrpc

npm install -g truffle

npm install -g ethereumjs-testrpc

(2) initialized a webpack project

truffle init webpack

(3) started up testrpc

testrpc

(4) compiled contracts

truffle compile

(5) deployed contracts (I could see them hitting testrpc in the console)

truffle migrate

(6) started development web server

npm run dev

Possibly relevent: I'm running MetaMask in Chrome and I've logged into an account. That doesn't seem to change the issue.

  • 1
    I don't face any issues, following the same commands on my machine. which version of truffle are you using? – Sanchit Jun 29 '17 at 3:03
  • I'm using testrpc - 3.0.5 and Truffle - 3.2.5. – Sanchit Jun 29 '17 at 3:04

14 Answers 14

23

One thing to check would be the network ID. It doesn't matter what network ID you use for your local test node, but Truffle needs to agree about what you're using with testrpc.

With testrpc running, do: curl -H "Content-Type: application/json" -X POST --data '{"id": "1", "jsonrpc":"2.0","method":"net_version","params":[]}' http://localhost:8545

This should tell you the network ID that testrpc is using; Recent versions seem to be using what looks like a timestamp from when it was started.

Then take a look in the contract definition files under truffle/build/contracts and see if they have the same network ID specified as the key in the networks section. If they don't, you might want to try specifying the network ID in your truffle.js and redeploying. (By default this uses "*", which should work it out automatically, but I've had problems with it occasionally for reasons I never got to the bottom of.) Superstitiously, I would also delete the contents of the build directory and run truffle deploy again.

If this turns out to be the problem, you can force testrpc to use the same network ID every time by running it with the --network-id flag, eg to use the network ID 1337 you would run testrpc --network-id 1337.

  • 1
    If you're using ganache, do ganache-cli --networkId. – Paul Berg Sep 18 '18 at 21:47
  • If you are running a private network, you can also look it up in the genesis.json under chainId – saitam Jan 20 at 10:10
15

I ran into the same error, the issue I faced was that even though I was recompiling my Contract, it was not getting migrated onto the network. Then running truffle-contract deployed on the FE, it could not find the contract on the network. Is it possible that your contract got deployed and then you tried to redeploy it and then you got this error?

The fix was to add a migration script in the /migrations directory.

Create a new file called n_metacoin_migration.js with the below content: (where n is the next number as found in list of files under 'migrations' directory. For example, it would be like 1_initial_migration.js, 2_myown_contract_migration.js, etc.)

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

module.exports = function(deployer) {
  deployer.deploy(MetaCoin);
};

Then run the migration script in your command line.

truffle compile
truffle migrate
5

this was the content of my truffle:

networks: {
    development: {
      host: '127.0.0.1',
      port: 7545,
      network_id: '*' // Match any network id
    }
}

The port should have been 8545 which is where the testrpc runs.

You can fix the error by changing the port to "8545", same as the testrpc, run truffle migrate --reset and that should fix it.

restart the node server also

  • In my case, I needed to do this and also to create the migration script. – Jorge M. Londoño P. Oct 16 '18 at 2:31
3

When metamask is running in the browser then web3 takes metamask or other defined web3 like Mist if none of them is found then it searches for localhost.

if (typeof web3 !== 'undefined') {
    console.warn("Using web3 detected from an external source. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
    // Use Mist/MetaMask's provider
    window.web3 = new Web3(web3.currentProvider);
  } else {
    console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545"));
  }

To make it work:
Turn off the metaMask plugin from manage extension
or
configure metamask to localhost.
Follow the steps in the link:
http://truffleframework.com/docs/advanced/truffle-with-metamask

1

In my case ..

"networks": {}

was empty for my contract in folder build/contracts/{mycontract.js}

Fix was to register in migrations folder . Register and deploy mycontract.

1

I had the same error. My issue was that one contract.json file did not have an address. Try linkng the contracts.json in the build directory and the src directory. One of them may not have an address.

try running a script

add this this script to client/package.json

"link-contracts":"cd src && ln -s ../../build/contracts contracts"

then

npm run link-contracts
truffle migrate --reset --all
0

If you are not using npm run dev to serve your app you'll have to run npm run watch in a terminal to ensure that your compiled assets point to the latest deployment.

0

"truffle migrate" corrected the wrong network id inside my_project/build/contracts/xyz.json ... as mentioned by Edmund Edgar above.

If you have to, delete all files inside my_project/build/contracts/ then run "truffle migrate" again

0

Write "truffle unbox webpack" instead. Worked for me :)

0

I ran into more or less the same issue last night and eventually found my problem to be that my metamask wasn't looking at the right test network. Having gone round the houses a few times I eventually realised that you need to point your metamask to look at the correct (custom RPC) network pointed at the correct truffle test port (in my case localhost:9545) and the configuration file (./truffle.js) set up correctly:

module.exports = {
  networks: {
    development: {
      host: "127.0.0.0",
      port: 8545,
      network_id: "*" // match any network
    }
  }
};
0

I was facing the same issue. What I did is

  1. First install Ganache {goto setting change 7545 to 8546} also change truffle.js port to 8545.
  2. Then open Meta-Mask and click on Localhost 8545.

Then just use the below commands:

truffle.cmd compile
truffle.cmd migrate
npm run dev
0

I had a problem where my networks section was empty within my contract file in the build directory like @rana_stack metioned. I used the solution here to fix it, making my migration run asynchronously and wait for the contract to deploy before writing to the json.

My migration file looked like this:

var MyContract = artifacts.require("./MyContract.sol");
const fs = require('fs');

module.exports = async (deployer, network, accounts) => {
  await deployer.deploy(MyContract);

  const metaDataFile = `${__dirname}/../build/contracts/MyContract.json`
  const metaData = require(metaDataFile)
  metaData.networks[deployer.network_id] = {}
  metaData.networks[deployer.network_id].address = MyContract.address
  fs.writeFileSync(metaDataFile, JSON.stringify(metaData, null, 4))
};

I also had to change the metamask network to point to my local one in the browser extension.

0

In my case, it's that I need to change the MetaMask to connect to a private network(URL) by choosing custom RPC and type in my URL http://127.0.0.1:9545 mannually

0

Make sure you are connected to your private network with MetaMask.

In my case I chose Custom RPC then in the New RPC URL I pasted http://127.0.0.1:7545.

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.