W3Schools



我似乎已经遵循松露指南到T.但是当我第一次在浏览器中打开我的应用程序时,我在控制台中收到此错误:

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

我是以太坊开发的新手,所以我大多只是在黑暗中徘徊。任何帮助将不胜感激。

我遵循的步骤:

(1)安装松露和testrpc

npm install -g truffle

npm install -g ethereumjs-testrpc

(2)初始化一个webpack项目

truffle init webpack

(3)启动testrpc

testrpc

(4)编制合同

truffle compile

(5)部署合同(我可以看到他们在控制台中击中testrpc)

truffle migrate

(6)开始开发Web服务器

npm run dev

可能相关:我在Chrome中运行MetaMask并且我已经登录了一个帐户。这似乎没有改变这个问题。

truffle
11个回答
18

要检查的一件事是网络ID。您用于本地测试节点的网络ID无关紧要,但Truffle需要就您在testrpc中使用的内容达成一致。

运行testrpc后,执行:curl -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[]}' http://localhost:8545

这应该告诉你testrpc正在使用的网络ID;最近的版本似乎使用了从启动时开始的时间戳。

然后看一下合同定义文件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.

如果这是问题,你可以强迫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.


0

如果你不使用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.


6

我遇到了同样的错误,我遇到的问题是,即使我正在重新编译我的合同,它也没有被迁移到网络上。然后跑松露合同部署 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?

修复是在中添加迁移脚本/migrations directory.

创建一个名为的新文件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);
};

然后在命令行中运行迁移脚本。

truffle compile
truffle migrate

0

“松露迁移”纠正了my_project / build / contracts / xyz.json中错误的网络ID ......正如Edmund Edgar上面提到的那样。

如果必须,删除my_project / build / contracts /中的所有文件 然后再次运行“松露迁移”


0

写“松露unbox webpack”而不是。为我工作:)


0

昨晚我遇到了或多或少相同的问题,最终发现我的问题是我的元掩码没有看正确的测试网络。绕过房子几次,我终于意识到你需要指向你的元掩码,看看正确的(自定义RPC)网络指向正确的松露测试端口(在我的情况下localhost:9545)和配置文件(./ truffle.js)设置正确:

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

2

这是我松露的内容:

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

端口应该是8545,这是testrpc运行的地方。

您可以通过将端口更改为“8545”来修复错误,与testrpc相同, 跑truffle migrate --reset and that should fix it.

重启节点服务器


0

我面临同样的问题。我做的是

  1. 首先安装伽纳彻 {goto setting change 7545 to 8546} also change truffle.js port to 8545.
  2. 然后打开元面膜 and click on Localhost 8545.

然后只需使用以下命令:

truffle.cmd compile
truffle.cmd migrate
npm run dev

0

在我的情况下..

"networks": {}

我的合同在文件夹中是空的build/contracts/{mycontract.js}

修复是在迁移文件夹中注册。注册并部署mycontract。


0

我的问题出在哪里networks section was empty within my contract file in the build directory like @rana_stack metioned. I used the solution 这里 to fix it, making my migration run asynchronously and wait for the contract to deploy before writing to the json.

我的迁移文件如下所示:

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))
};

我还必须将元掩码网络更改为指向浏览器扩展中的本地网络。