在Rinkeby网络上,合约未部署到检测到的网络(网络/工件不匹配)。

10
我遇到了标题中指定的问题。我已经开发了一个智能合约,并成功地编译和部署到网络上,具体步骤如下: 1. 运行testrpc 2. truffle compile 3. truffle migrate 然而,仍然显示上述错误。我尝试删除构建文件并按照以下步骤操作: 1. 运行testrpc 2. truffle compile 3. truffle migrate --network rinkeby
但是仍然显示错误。
以下是truffle.js文件。
module.exports = {
  migrations_directory: "./migrations",
  networks: {
development: {
  host: "localhost",
  port: 8545,
  network_id: "*" // Match any network id
},
rinkeby: {
  host: "localhost", // Connect to geth on the specified
  port: 8545, 
  network_id: "*",
}

} }; 如果有人遇到类似的问题并已经解决,请分享您是如何解决的,我将不胜感激。

提前致谢


你能添加完整的 geth 启动命令吗? - Adam Kipnis
更新:我刚注意到合约在使用testrpc时没有正确部署,我尝试通过运行geth节点来迁移合约:geth --rinkeby --rpc --rpcapi db,eth,net,web3,personal --unlock <ADDRESS>,但是我遇到了下面的错误。运行迁移:1_initial_migration.js 部署迁移... ... 未定义的错误。遇到错误,请退出。网络状态未知。请手动查看成功的交易。错误:需要验证:密码或解锁@AdamKipnis有什么想法吗? - Sho0310
你需要使用“—password”选项来解锁账户。 - Adam Kipnis
当在geth控制台上运行personal.unlockAccount("地址", "密码")时,它返回true。但错误仍然存在。 - Sho0310
更新:我现在遇到了错误:遇到错误,退出。网络状态未知。请手动查看成功的交易。 错误:燃气费用 * 价格 + 数量不足 - Sho0310
这并不是一个适合在本网站上提问的问题。你可能会受到不好的待遇。我建议你将你的问题发布到一个名称中带有“论坛”一词的网站上。 - SaganRitual
8个回答

14

对我而言,以下步骤有效:在migration文件夹中创建文件2_deploy_contract.js,并填入代码。

var myContract = artifacts.require("myContract");

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

并在终端上运行

$ truffle migrate --reset

我不需要更改truffle-config.js中的任何设置。


8

我曾遇到同样的问题,并在迁移文件夹中创建了文件2_deploy_contract.js,其中包含以下代码:

var myContract = artifacts.require("myContract");

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

我也检查了根目录下具有默认设置的truffle-config.js文件:
rinkeby: {
  host: "localhost", 
  port: 8545, 
  from: "0x0085f8e72391Ce4BB5ce47541C846d059399fA6c", // default address to use for any transaction Truffle makes during migrations
  network_id: 4,
  gas: 4612388 // Gas limit used for deploys
}

4

Tl;dr:

  1. 在迁移文件夹中添加一个新的迁移,并将其命名为以下名称: 2_deploy_contract.js

PS: 你可以随意命名。 2_deploy_yourmomsdonuts.js也可以工作。只需注意数字 - 它必须与顺序相对应。 2. 将此代码片段添加到此文件中:

用你未部署的合约的名称替换 HelloWorld (查看控制台,找到已抛出错误的合约)

var HelloWorld = artifacts.require('HelloWorld');

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

  1. 运行truffle migrate --resettruffle deploy --reset - 这两个命令是别名,作用相同。您也可以同时运行这两个命令以确保安全。

  2. 您应该会看到您的合约已经部署到了您的网络上。

  3. 最后,输入truffle console并再次调用您的函数。Voila。

为什么您需要手动添加一个迁移? 问题在于您的智能合约没有被部署/未被识别,因此通过手动添加迁移,您告诉Truffle应该部署哪个文件。


0
我找到了这个问题的解决方案。 我将我的迁移文件名从AdvancedStorage_migrate.js更改为2_AdvancedStorage_migrate.js
如果您没有在迁移文件中保存数字,则truffle无法找到迁移文件,因此您必须像这样保存迁移文件(2_AdvancedStorage_migrate.js)
这个问题不是由truffle配置引起的,所以不要触摸truffle configuration.js文件。

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

0

原始帖子和评论中有很多不同的错误消息。我认为最好的方法是提供一个逐步指南,使用Truffle部署到Rinkeby:

Geth

首先,创建您要用于此测试的帐户。看起来您已经完成了这个步骤,但为了完整起见,我将包括此步骤。请注意,我正在使用自定义密钥库目录,因为我喜欢在不同的网络之间保持我的密钥库分开。

geth --rinkeby --keystore ./eth/accounts/rinkeby/keystore account new

输入密码后,您将获得新地址。创建帐户后,创建一个名为pass.txt的新文本文件,并将用于创建帐户的密码放入文件中并保存。

显然,这不是保护密码安全的首选方式。不要在实时环境中这样做

您还需要向您的帐户添加一些以太。使用faucet.rinkeby.io

接下来,请确保您正确启动了Geth并处于正确的状态。我使用自定义数据和keystore目录,如果您愿意,也可以使用默认设置。
geth --rpc --datadir ./eth/geth/data/rinkeby --keystore ./eth/accounts/rinkeby/keystore --rinkeby --rpccorsdomain '*' --rpcapi 'web3,eth,net,personal' --unlock '0x25e6C81C823D4e15084F8e93F4d9B7F365C0857d' --password ./pass.txt --syncmode="full" --cache=1024

将我的地址替换为您创建的地址。启动后,您应该会看到类似于这样的内容:

INFO [02-13|17:47:24] Starting peer-to-peer node               instance=Geth/TrustDevTestNode/v1.7.3-stable-4bb3c89d/windows-amd64/go1.9
INFO [02-13|17:47:24] Allocated cache and file handles         database=C:\\cygwin\\home\\adamk\\eth\\geth\\data\\rinkeby\\geth\\chaindata cache=1024 handles=1024
INFO [02-13|17:47:47] Initialised chain configuration          config="{ChainID: 4 Homestead: 1 DAO: <nil> DAOSupport: true EIP150: 2 EIP155: 3 EIP158: 3 Byzantium: 1035301 Engine: clique}"
INFO [02-13|17:47:47] Initialising Ethereum protocol           versions="[63 62]" network=4
INFO [02-13|17:47:47] Loaded most recent local header          number=1766839 hash=6d71ad…ca5a95 td=3285475
INFO [02-13|17:47:47] Loaded most recent local full block      number=1766839 hash=6d71ad…ca5a95 td=3285475
INFO [02-13|17:47:47] Loaded most recent local fast block      number=1766839 hash=6d71ad…ca5a95 td=3285475
INFO [02-13|17:47:47] Loaded local transaction journal         transactions=0 dropped=0
INFO [02-13|17:47:47] Regenerated local transaction journal    transactions=0 accounts=0
INFO [02-13|17:47:48] Starting P2P networking
2018/02/13 17:47:50 ssdp: got unexpected search target result "upnp:rootdevice"
2018/02/13 17:47:50 ssdp: got unexpected search target result "uuid:2f402f80-da50-11e1-9b23-001788409545"
2018/02/13 17:47:50 ssdp: got unexpected search target result "urn:schemas-upnp-org:device:basic:1"
2018/02/13 17:47:50 ssdp: got unexpected search target result "upnp:rootdevice"
2018/02/13 17:47:50 ssdp: got unexpected search target result "uuid:2f402f80-da50-11e1-9b23-001788409545"
INFO [02-13|17:47:51] UDP listener up                          self=enode://751bc7825c66f9ab5b87f933d6b6302fd14434b7ed4d7c921c3f39684915843078eda4e995c927561067946b4f856ca2a35ea7285c27439c0f535338aaca80e9@172.88.30.226:30303
INFO [02-13|17:47:51] RLPx listener up                         self=enode://751bc7825c66f9ab5b87f933d6b6302fd14434b7ed4d7c921c3f39684915843078eda4e995c927561067946b4f856ca2a35ea7285c27439c0f535338aaca80e9@172.88.30.226:30303
INFO [02-13|17:47:51] IPC endpoint opened: \\.\pipe\geth.ipc
INFO [02-13|17:47:51] HTTP endpoint opened: http://127.0.0.1:8545
INFO [02-13|17:47:52] Unlocked account                         address=0x25e6C81C823D4e15084F8e93F4d9B7F365C0857d
  • 确认网络=4。
  • 确认最后一行显示的帐户解锁成功且没有错误。
  • 一旦您的节点启动,请确保它完全同步。

Truffle

truffle.js(如果是Windows,则为truffle-config.js):

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

使用Truffle控制台确认您的节点和账户:
$ truffle console --network rinkeby
truffle(rinkeby)> web3.eth.blockNumber
1767136 // Confirm latest block number on https://rinkeby.etherscan.io/
truffle(rinkeby)> web3.eth.getBalance('0x25e6c81c823d4e15084f8e93f4d9b7f365c0857d');
{ [String: '2956062100000000000'] s: 1, e: 18, c: [ 29560, 62100000000000 ] }

退出控制台并运行编译/迁移(这将需要约一分钟的时间):

$ truffle migrate --network rinkeby
Compiling .\contracts\LoopExample.sol...
Writing artifacts to .\build\contracts

Using network 'rinkeby'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xf377be391a2eaff821c0405256c6a1f50389650ea9754bdc2711296b02533e02
  Migrations: 0x9cef8d8959d0611046d5144ec0439473ad842c7c
Saving successful migration to network...
  ... 0x4cf989973ea56a9aa4477effaccd9b59bfb80cc0e0e1b7878ff25fa5cae328db
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying LoopExample...
  ... 0x4977c60fd86e1c4ab09d8f970be7b7827ee25245575bfbe206c19c6b065e9031
  LoopExample: 0x56b9c563f287cdd6a9a41e4678ceeeb6fc56e104
Saving successful migration to network...
  ... 0x5628d64dc43708ccb30d7754a440e8e420a82a7b3770539cb94302fe7ad9098f
Saving artifacts...

确认在Etherscan上部署:https://rinkeby.etherscan.io/address/0x56b9c563f287cdd6a9a41e4678ceeeb6fc56e104


0

你能分享一下如何部署一个带有构造函数和参数的合约吗?例如:constructor(address1 _add1, address2 _add2, uint value) - Ndrslmpk

0
解决方法:
在我的情况下,我正在使用truffle-contract来加载合约abi。 默认情况下,truflle-contract网络ID为1。
但我正在使用goreli网络ID 5。我们需要在加载合约时设置network ID。
            const contract = require('truffle-contract');   
            const MyContract = contract(MyContract);    
    
            MyContract.setProvider(web3.currentProvider);    
            MyContract.setNetwork(5) 

`


-3

 const conract = artifacts.require('conractArtifact');

//update this
module.exports = function (deployer) {}
//to this
module.exports = async (deployer) => {}
//or to this
module.exports = fucntion (deployer) {}


3
目前你的回答不够清晰,请[编辑]以添加更多细节,帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接