简体   繁体   中英

Web3/Metamask: Error: Contract has not been deployed to detected network (network/artifact mismatch) on Kovan network

I try to deploy an instance of a contract that is already live on the Kovan network to interact with it with web3 and metamask.

So first thing first, I set metamask as my current provider then I deploy an instance of the contract like this:

  deployContract = (contract) => {
   contract.deployed().then(function(instance) {
    let proxy = instance;
    return proxy.ProxyAddress()
    }).then(function(result){
      this.setState({
      address: result,
    });
 })
}

But then, I get the following error:

Uncaught (in promise) Error: Contract has not been deployed to detected network (network/artifact mismatch)
at eval (webpack:///./~/truffle-contract/contract.js?:429)
at <anonymous>

I found out that it was caused by the network ID of web3 which happen to be wrong. My web3 network ID is set by Metamask which is supposed to inject web3 with the correct ID. But when I get the network ID I get a totaly different result:

web3.version.getNetwork(function(err,res){console.log(res)})
> 3

Is there a way to manualy set web3's version network? I looked in the documentation and github but there was no usefull insights.

EDIT:

It appears that closing and reopening chrome resolve the ID problem. So now I have both ID set as 42 but the error is still the same as before. Back to square one...

Ok so in the end the problem was caused by the way of importing my contracts. My previous import was done like this:

let contract = require('truffle-contract');
let Factory = contract("../contracts/Factory.json");

While it should actualy be imported this way:

let contract = require('truffle-contract');
let json = require("../contracts/Factory.json");
let Factory = contract(json);

So to sum up, if an error like this happen to you, do those checks first:

-Check contract import.

-Check your web3 provider. console.log(window.web3.currentProvider)

-Check web3 network ID. web3.version.getNetwork(function(err,res{console.log(res)})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM