简体   繁体   English

Web3 / Metamask:错误:尚未将合同部署到Kovan网络上的检测到的网络(网络/工件不匹配)

[英]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. 我尝试部署一个已经存在于Kovan网络上的合同实例,以便与web3和metamask进行交互。

So first thing first, I set metamask as my current provider then I deploy an instance of the contract like this: 首先,我将metamask设置为我当前的提供者,然后我部署一个合同实例,如下所示:

  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. 我发现它是由web3的网络ID引起的,它恰好是错误的。 My web3 network ID is set by Metamask which is supposed to inject web3 with the correct ID. 我的web3网络ID由Metamask设置,它应该为web3注入正确的ID。 But when I get the network ID I get a totaly different result: 但是当我得到网络ID时,我得到一个完全不同的结果:

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

Is there a way to manualy set web3's version network? 有没有办法手动设置web3的版本网络? I looked in the documentation and github but there was no usefull insights. 我查看了文档和github,但没有有用的见解。

EDIT: 编辑:

It appears that closing and reopening chrome resolve the ID problem. 关闭和重新打开chrome似乎解决了ID问题。 So now I have both ID set as 42 but the error is still the same as before. 所以现在我将两个ID设置为42,但错误仍然与以前相同。 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. - 检查你的web3提供商。 console.log(window.web3.currentProvider)

-Check web3 network ID. - 检查web3网络ID。 web3.version.getNetwork(function(err,res{console.log(res)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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