0

我能够使用 Truffle 和 Ganache-cli。已经部署了合约并且可以使用 truffle 控制台来玩

truffle(development)> 
Voting.deployed().then(function(contractInstance) 
{contractInstance.voteForCandidate('Rama').then(function(v) 
{console.log(v)})})
undefined
truffle(development)> { tx: 
'0xe4f8d00f7732c09df9e832bba0be9f37c3e2f594d3fbb8aba93fcb7faa0f441d',
  receipt: 
   { transactionHash: 
'0xe4f8d00f7732c09df9e832bba0be9f37c3e2f594d3fbb8aba93fcb7faa0f441d',
     transactionIndex: 0,
     blockHash: 
'0x639482c03dba071973c162668903ab98fb6ba4dbd8878e15ec7539b83f0e888f',
     blockNumber: 10,
     gasUsed: 28387,
     cumulativeGasUsed: 28387,
     contractAddress: null,
     logs: [],
     status: '0x01',
     logsBloom: ... }

现在,当我使用“npm run dev”启动服务器时。服务器启动正常但未连接到区块链

我收到错误

Uncaught (in promise) Error: Contract has not been deployed to detected network (network/artifact mismatch)

这是我的 truffle.js

// Allows us to use ES6 in our migrations and tests.
require('babel-register')

module.exports = {
 networks: {
    development: {
      host: '127.0.0.1',
      port: 8545,
      network_id: '*', // Match any network id
      gas: 1470000

    }
  }
}

你能指导我如何连接吗?

4

2 回答 2

1

解决问题。

问题出在 currentProvider,我给出了 ganache 区块链提供商的 url,它起作用了。

if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source like Metamask")
// Use Mist/MetaMask's provider
// window.web3 = new Web3(web3.currentProvider);
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));

  } else {
console.warn("No web3 detected. Falling back to http://localhost:8545. 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://localhost:8545"));
  }
于 2018-03-21T11:18:23.107 回答
0

在您的truffle.js中,更改85457545.

或者,在 Ganache (GUI) 中,单击右上角的齿轮并将端口号从 更改75458545,然后重新启动。使用 ganache-cli-p 8545在启动时使用选项将 8545 设置为要监听的端口。

无论哪种方式,不匹配似乎都是问题所在。这些数字应该匹配。这是一个常见的问题。

也可以随时查看ethereum.stackexchange.com。如果您希望将您的问题移到那里,您可以标记它并留言让版主执行此操作。

于 2018-03-20T15:03:52.350 回答