0

I'm going through Lesson 6 Ch. 2 of Cryptozombies (Title: Web3 Providers), and it shows me this:

"Here's some template code provided by Metamask for how we can detect to see if the user has Metamask installed, and if not tell them they'll need to install it to use our app:"

window.addEventListener('load', function() {

  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    // Use Mist/MetaMask's provider
    web3js = new Web3(web3.currentProvider);

  } else {
    // Handle the case where the user doesn't have web3. Probably
    // show them a message telling them to install Metamask in
    // order to use our app.
  }

  // Now you can start your app & access web3js freely:
  startApp()

})

I don't understand what's happening between the statements 'if' and 'else'.

  if (typeof web3 !== 'undefined')

If I'm correct, I think this part above checks to see if the object 'web3' isn't 'undefined'.

web3js = new Web3(web3.currentProvider);

This above I'm feeling really unsure with.

What I understand:

→ A new object is being created, by use of the object constructor 'Web3()'.

→ Which then is assigned to the variable 'web3js'.

→ 'web3' is a global Javascript object that Metamask injects their web3 provider into (which is one of infura's providers/servers).

What I need help with:

→ I need an explanation on what exactly the constructor function 'Web3()' does, or where I can find further details about it on. I can't seem to find it!

→ I don't fully understand the argument "web3.currentProvider" entered in the constructor function. I understand that it refers to the web3 provider Metamask uses, but I only know this because it said just that in the lesson. Where can I verify this?

1

1 Answer 1

1

Web3() constructor establish a connection to a particular network (mainnet, ropsten,...) through a full node or public node provider(infura) or like here establish the connection through provider that metamask inject to you global window object

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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