0

I am developing a ReactJs blockchain application on ethereum network. How can I use MetaMask Wallet instead of local ganache account.

import web3 from 'Web3';
const web3 = new Web3("localurl"); 

2 Answers 2

3

Check out this guide. To summarize: first check whether web3 already exists. If it does, initialize a Web3 object with web3.currentProvider. Otherwise, use your node's URL

2
  • In react environment, what web3.currentProvider will refer to as web3 is still not instantiated. Commented Apr 23, 2018 at 19:07
  • If MetaMask is installed and working, web3 will be instantiated. If MetaMask is not installed, web3 will be undefined and you have to instantiate it yourself
    – Henk
    Commented Apr 24, 2018 at 6:49
-1
var web3 = new Web3();
    if (window.ethereum) {
      web3 = new Web3(window.ethereum);
      try {
        window.ethereum.enable().then(function() {
          // User has allowed account access to DApp...
        });
      } catch (e) {
        // User has denied account access to DApp...
      }
    }
    // Legacy DApp Browsers
    else if (window.web3) {
      web3 = new Web3(web3.currentProvider);
    }
    // Non-DApp Browsers
    else {
      alert("You have to install MetaMask !");
    }
    window.ethereum.enable();
    console.log(" typoe of = ", typeof web3);
    if (typeof web3 != "undefined") {
      this.web3Provider = web3.currentProvider;
      window.ethereum.enable();
    } else {
      this.web3Provider = new Web3.providers.HttpProvider(
        "http://127.0.0.1:8545"
      );
      window.ethereum.enable();
    }
  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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