Sagar Atalatti
Sagar Atalatti

Reputation: 505

Uncaught Error: Invalid Address web3.currentProvider MetaMask

I have deployed my ERC721 contract to Rinkeby TestNet. The contract has been deployed successfully. I unable to invoke transactions with MetaMask. Spent the whole day looking to resolve this issue. Found some answers stating it the issue with localhosted files or the web3.js doesn't work with MetaMask.

<script>
            if (typeof web3 != 'undefined') { 
                web3 = new Web3(web3.currentProvider) // what Metamask injected 
                console.log("existing web3: provider " + typeof web3);
            } else {
                // Instantiate and set Ganache as your provider
                web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/api-key"));
                console.log("new provider " + web3);
                web3.eth.defaultAccount = web3.eth.accounts[0]
            }
            // The interface definition for your smart contract (the ABI) 
            var StarNotary = web3.eth.contract(
                [contract-abi]
            )

            const starNotary = StarNotary.at('0x7cfAD6E80D992599d989166aABf536b21215544C')

            function claimStar() { 
                web3.eth.getAccounts(function(error, accounts) { 
                    if (error) { 
                        hotsnackbar(false, error);
                        return
                    }

Uncaught Error: invalid address at u (web3.min.js:1) at inputTransactionFormatter (web3.min.js:1) at web3.min.js:1 at Array.map () at i.formatInput (web3.min.js:1) at i.toPayload (web3.min.js:1) at _.e [as sendTransaction] (web3.min.js:1) at c.sendTransaction (web3.min.js:1) at index.html:589 at web3.min.js:1

Upvotes: 3

Views: 3363

Answers (3)

Hrishikesh Bawane
Hrishikesh Bawane

Reputation: 189

web3.js file

import Web3 from 'web3';

let provider;
// if on server or browser
if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
    // we are in browser and metamask present
    provider = window.web3.currentProvider;
}
else {
    // we are on server OR metamask not present
    provider = new Web3.providers.HttpProvider(
        'https://rinkeby.infura.io/v3/API_KEY'
    );
}

const web3 = new Web3(provider);

export default web3;

Do not put new Web3() in the if-blocks itself. After hosting (locally or on server) open metamask Settings -> Connections and add your site to give access to metamask. Worked perfectly for me after spending days behind metamask and web3 errors!

Upvotes: 1

Kartik ganiga
Kartik ganiga

Reputation: 390

I too got the same problem while working on a DApp with Rinkeby testnet with Metamask.

When i had my web3.js file as below

import Web3 from 'web3';

let web3;

if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
  // We are in the browser and metamask is running.
  web3 = new Web3(window.web3.currentProvider);
} else {
  // We are on the server *OR* the user is not running metamask
  const provider = new Web3.providers.HttpProvider(
    'Infura API key'
  );
  web3 = new Web3(provider);
  //window.web3.currentProvider.enable();


}

export default web3;

and when i run npm start or npm run dev ( depends on your start script)

The browser was throwing error saying "uncaught error: No 'from' address specified "

When i opened up the browser console and did web.currentProvider it threw the same error. So one thing got clear that Metamask is not making contact with browser.

What i did is

I kept the Browser on with Metamask logged in. And changed the web3.js to while the server running on command prompt and saved the file.

import Web3 from 'web3';

let web3;

if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
  // We are in the browser and metamask is running.
//Note: change to window.web3.currentProvider.enable()
  web3 = new Web3(window.web3.currentProvider.enable());
} else {
  // We are on the server *OR* the user is not running metamask
  const provider = new Web3.providers.HttpProvider(
    'Infura API'
  );
  web3 = new Web3(provider);
  //window.web3.currentProvider.enable();


}

export default web3;

As soon as you save the file The Metamask throws you an prompt saying it want to make connection from your account.

Click yes.

and remove the .enable() from the above code and save your code.

This can be a temporary solution, but yes it works!

Upvotes: 2

William Entriken
William Entriken

Reputation: 39273

Here is a complete demo which includes the introductory steps like authorizing the MetaMask contract and more.

https://fulldecent.github.io/spend-ERC20-create-ERC721/

Here is the particular code I think you will be interested in:

https://github.com/fulldecent/spend-ERC20-create-ERC721/blob/master/docs/index.html#L102-L114

  if (window.ethereum) {
    window.web3 = new Web3(ethereum);
    $('#need-metamask').hide();
  } else {
    console.log('Non-Ethereum browser detected. Install MetaMask.');
    return;
  }
  window.web3.version.getNetwork((err, netId) => {
    if (netId == "3") {
      $('#need-ropsten').hide();
    }
  });

https://github.com/fulldecent/spend-ERC20-create-ERC721/blob/master/docs/index.html#L121-L127

  try {
    await ethereum.enable();
    $('#need-enable').hide();
  } catch (error) {
    console.log("ERROR: Enable account access and reload.");
    return;
  }

Upvotes: 2

Related Questions