无法将 Ganache 与 Truffle/Npm Dev 服务器连接

问题描述 投票:0回答:3

我能够使用 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

    }
  }
}

您能指导我如何连接吗?

npm blockchain ethereum truffle consensys-truffle
3个回答
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"));
  }

0
投票

在 ganache 设置 > 服务器中将接口更改为 0.0.0.0(所有接口)。

在 truffle-config.js 中,使用提供程序而不是默认主机配置:

const HDWalletProvider = require("@truffle/hdwallet-provider")

 networks: {
   development: {
    provider: () => new HDWalletProvider([
      "YOUR_PRIVATE_KEY",
    ], "http://127.0.0.1:7545/"),
    port: 7545,
    network_id: "*" // Match any network id
   }
}

0
投票

在您的

truffle.js
中,将
8545
更改为
7545

或者,在Ganache(GUI)中,单击右上角的齿轮,将端口号从

7545
更改为
8545
,然后重新启动。使用 ganache-cli 在启动时使用
-p 8545
选项将 8545 设置为要侦听的端口。

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

© www.soinside.com 2019 - 2024. All rights reserved.