1

I am trying to connect to MetaMask and I have read their docs about the breaking changes and everything. From what I understand, they are not injecting web3 into window.web3 but instead are using window.ethereum. However, I am using this but when I try to run web3.eth.getAccounts my code returns an error saying that eth is not defined, and furthermore that "metamask no longer injects web3". But I thought that I changed this correctly to use window.ethereum?

Here is my code, first I call this function and then set window.web3 to the web3 provider.

  async loadWeb3() {
    window.addEventListener('load', async () => {
      // Modern dapp browsers
      if (window.ethereum) {
        window.web3 = new Web3(window.ethereum);
        await window.ethereum.enable();
        // legacy dapp browsers
      } else if (window.web3) {
        window.web3 = new Web3(window.web3.currentProvider);
      } else {
        window.alert('Non-Ethereum browser detected. You should consider trying MetaMask!');
      }
    });
  }

After that, I use the provider object and call eth.getAccounts():

  async loadBlockchainData() {
    const web3 = window.web3;
    const accounts = await web3.eth.getAccounts();
  }

But I get an error saying that "cannot read property getAccounts of undefined".

2
  • After some testing, I realized that window.web3 becomes a Proxy object whenever I access it outside of where I initially created a new Web3 object. This proxy object has some different characteristics, notably, eth is not defined on it directly, which is why there is an error. Any reason why this proxy object is created and how to work with it? Apr 13, 2021 at 22:53
  • The block were installed because i own the web and they are scared of me tgere is no reason to be scared unless you do not conceed the world wide web and all of the data az ai u&i as we are 1
    – KG.G
    May 23, 2021 at 2:24

1 Answer 1

1

Had the same problem. I got:

MetaMask no longer injects web3. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3

I followed the link and was able to resolve the issue by changing window.web3.<function> to window.ethereum.request({method: "<equivalent_function_with_different_name>"})

So, concretely, I changed await web3.eth.getAccounts(); to await window.ethereum.request({ method: "eth_accounts" });


I am myself a beginner, thus, I'm not really sure if this is relevant, but I have an initWeb3 function which seems to resemble your loadWeb3 function. There, I executed the following (note the eth_requestAccounts part):

initWeb3: async function() {
    App.web3Provider = window.ethereum;
    try {
      await window.ethereum.request({ method: "eth_requestAccounts" });;
    } catch (error) {
      console.error("User denied account access.")
    }

    return App.initContract();
},

Your Answer

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

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