1

Using Truffle, I'm trying to implement my first smart contract by running the MetaCoin webpack. After running Truffle Develop, I can see my list of accounts:

Truffle Develop started at http://127.0.0.1:9545/

Accounts:
(0) 0xb2d5b74e4f22f3b7006fb5e77f3940982fe1a316
etc.

In my truffle.js file, I'm using:

    module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // to customize your Truffle configuration!
  networks: {
    development: {
      host: "localhost",
      port: 9545,
      network_id: "*" // Match any network id
    }
  }
};

I then use:

  import Web3 from "web3";
  import metaCoinArtifact from "../../build/contracts/MetaCoin.json";

  const App = {
  web3: null,
  account: null,
  meta: null,

  start: async function() {
    const { web3 } = this;

    try {
      // get contract instance
      const networkId = await web3.eth.net.getId();
      const deployedNetwork = metaCoinArtifact.networks[networkId];
      this.meta = new web3.eth.Contract(
        metaCoinArtifact.abi,
        deployedNetwork.address,
      );

      // get accounts
      const accounts = await web3.eth.getAccounts();
      this.account = accounts[0];

      this.refreshBalance();
    } catch (error) {
      console.error("Could not connect to contract or chain.");
    }
    },  //more code below

However, when I then run my app through the browser via npm run dev, my console is returning:

Could not connect to contract or chain.

What step am I missing to have my app talk to my local blockchain?

  • Check the browser javascript console for possible errors. – Ismael Jan 13 at 7:49
  • @Ismael This pointed me in the right direction. From another tutorial, I had previously installed MetaMask: the console showed me that my was unsuccessfully trying to connect to MetaMask. Once I removed MetaMask it connected to my local blockchain. Is there anyway that I can specify which blockchain I want my app to connect to so that I don't have to uninstall MetMask? – Eric Jan 13 at 14:39
  • 1
    I'm going to respond to my own comment to potentially help others. When your App loads, you should be able to find some code which checks to see whether MetaMask is present. For example, I found "if (window.ethereum) { App.web3 = new Web3(window.ethereum); } else {App.web3 = new Web3( new Web3.providers.HttpProvider("127.0.0.1:9545"), );}". The else statement connects to my local blockchain. So, if I wanted to, I could potentially just remove the "if" part of the code and just use the "else" part if my browser also had MetaMask. – Eric Jan 13 at 16:43
  • Please add your reply as an answer. You are correct if you ignore the existing windows.ethereum and create your own instance you should connect to your local node. Another option is to use a custom provider in Metamask. – Ismael Jan 13 at 17:28
1

As suggested by @Ismael, I'm going to respond to my own comment with an answer to potentially help others. When your App loads, you should be able to find some code which checks to see whether MetaMask is present. For example, I found:

    if (window.ethereum) { 
       App.web3 = new Web3(window.ethereum); 
    } else {
       App.web3 = new Web3( new Web3.providers.HttpProvider("127.0.0.1:9545"));
    }

The else statement connects to my local blockchain. So, if I wanted to, I could potentially just remove the "if" part of the code and just use the "else" part if my browser also had MetaMask

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.