首页 > 解决方案 > 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

标签: ethereumweb3jstruffle

解决方案


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;
  }

推荐阅读