0

I am able to work with Truffle and Ganache-cli. Have deployed the contract and can play with that using truffle console

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: ... }

Now when i started a server using "npm run dev". Server started fine but is not connecting with the Blockchain

i am getting the error

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

This is my 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

    }
  }
}

Can you please guide me how i can connect ?

user986508
  • 189
  • 2
  • 5
  • 14
  • 2
    You should try asking this in the ethereum stackexchange site. – Fred Douglis Mar 20 '18 at 14:34
  • I have asked on gitter as well. No luck so far. Thanks for the comment. – user986508 Mar 20 '18 at 16:41
  • If you are getting the truffle development console (since I see `truffle(development)>`)...wouldn't that mean you are using the truffle development rpc (by using `truffle develop`)? That is port 9545. – ReyHaynes Mar 20 '18 at 17:58

3 Answers3

1

Solve the issue.

issue was at currentProvider, i gave the url of ganache blockchain provider and it worked.

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"));
  }
user986508
  • 189
  • 2
  • 5
  • 14
  • This isn't the best solution. You could keep the Metamask currentProvider and just change the RPC connection setting within Metamask to connect with your localhost RPC. – ReyHaynes Mar 23 '18 at 13:59
  • Sure and i agree, can you guide me if possible how to change the RPC connection? I tried to search but not luck or maybe i am looking in the wrong direction. – user986508 Mar 26 '18 at 07:37
0

In your truffle.js, change 8545 to 7545.

Or, in Ganache (GUI), click the gear in the upper right corner and change the port number from 7545 to 8545, then restart. With ganache-cli use -p 8545 option on startup to set 8545 as the port to listen on.

Either way, the mismatch seems to be the issue; these numbers should match. This is a common issue.

Also feel free to check out ethereum.stackexchange.com. If you want your question moved there, you can flag it and leave a message for a moderator to do that.

WBT
  • 2,249
  • 3
  • 28
  • 40
  • Thanks, i tried it didn't work. btw this my ganache-cli out put. It is listening at port 8545 Ganache CLI v6.1.0 (ganache-core: 2.1.0) ... Mnemonic: tent helmet chair issue cherry ring foil nominee trophy hub poem cat Base HD Path: m/44'/60'/0'/0/{account_index} Listening on localhost:8545 – user986508 Mar 20 '18 at 16:40
  • What is the specific code you are running (with `npm run dev`) which is producing the error? What port etc. is it connecting to? Also, have you deployed the contract using `truffle migrate`? – WBT Mar 20 '18 at 16:55
  • Yes, i did truffle migrate and i can access the contact and can call the functions using truffle console. You can see my ganache-cli is running on localhost:8545 i am running npm run dev and server starts and listens at localhost:8080 – user986508 Mar 20 '18 at 17:28
  • Not sure how to connect the localhost:8080 with contract, please take a look of my truffle.js too Thanks a lot for the reply. – user986508 Mar 20 '18 at 17:30
  • 1
    I'm asking you to post the specific javascript code which is executing after you run `npm run dev` and which produces the error. Somewhere in there you are probably creating a web3 instance and configuring it to connect to the wrong network/port. – WBT Mar 20 '18 at 17:43
0

Change interface to 0.0.0.0 (all interfaces) in ganache settings > server.

In truffle-config.js, use provider instead of default host config:

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
   }
}
Guihgo
  • 632
  • 10
  • 13