16

If I log the two functions, I get two objects, that both contain for example selectedAddress, but the structure of the objects seem different. I couldn't find a comparison in the documentation.

I did find out that window.web3.currentProviderand window.ethereum are equal, but then, what is window.web3 for?

2 Answers 2

9

As per the Metamask API documentation:

MetaMask injects a global API into websites visited by its users at window.ethereum (Also available at window.web3.currentProvider for legacy reasons). This API allows websites to request user login, load data from blockchains the user has a connection to, and suggest the user sign messages and transactions. You can use this API to detect the user of a web3 browser.


Edit:

You can see what window.web3 returns by opening a console in Chrome using "F12" (or whatever browser you're using), and typing window.web3:

> window.web3
Proxy {_requestManager: a, currentProvider: Proxy, eth: n, db: e.exports, shh: s, …}
[[Handler]]: Object
[[Target]]: n
bzz: e.exports {_requestManager: a, blockNetworkRead: ƒ, syncEnabled: ƒ, swapEnabled: ƒ, download: ƒ, …}
currentProvider: Proxy {selectedAddress: undefined, networkVersion: "4", _events: {…}, _eventsCount: 1, _maxListeners: 100, …}
db: e.exports {_requestManager: a, putString: ƒ, getString: ƒ, putHex: ƒ, getHex: ƒ}
eth: n {_requestManager: a, getBalance: ƒ, getStorageAt: ƒ, getCode: ƒ, getBlock: ƒ, …}
net: e.exports {_requestManager: a, getListening: ƒ, getPeerCount: ƒ}
personal: e.exports {_requestManager: a, newAccount: ƒ, importRawKey: ƒ, unlockAccount: ƒ, ecRecover: ƒ, …}
providers: {HttpProvider: ƒ, IpcProvider: ƒ}
setProvider: ƒ ()
settings: e.exports {defaultBlock: "latest", defaultAccount: undefined}
shh: s {_requestManager: a, version: ƒ, info: ƒ, setMaxMessageSize: ƒ, setMinPoW: ƒ, …}
version: {api: "0.20.3", getNode: ƒ, getNetwork: ƒ, …}
_extend: ƒ (e)
_requestManager: a {provider: Proxy, polls: {…}, timeout: null}
__proto__: Object
[[IsRevoked]]: false

The same can be done for window.ethereum.

9
  • 1
    I read that before, but it doesn't say what exactly window.web3 returns.
    – Marcellvs
    Mar 13, 2019 at 10:22
  • Edited my answer. Shout if that still doesn't help :-) Mar 13, 2019 at 11:03
  • I'm logging pretty much every object and command to learn, and as I mentioned in the post ("the structure of the objects seem different") I know what they look like, but I don't understand the purpose ob web3. Why the need for two different objects?
    – Marcellvs
    Mar 13, 2019 at 14:17
  • The answer is in the Metamask documentation I quoted above. The original API injected window.web3.currentProvider. There was an upgrade that introduced window.ethereum, which was slightly different to the old API. This is why they are different. window.web3.currentProvider still exists "for legacy reasons", i.e. because some people will still be using it in their code. Mar 13, 2019 at 14:35
  • 1
    Ah, okay, I understand what you're asking now :-) window.web3.currentProvider basically provided a subset of information relating to window.web3. By instantiating a Web3() object you would get access to everything. That same information is now available from window.ethereum (I think with some minor differences, hence the upgrade.) You can still get access to everything by instantiating Web3() in the same way (e.g. window.web3 = new Web3(ethereum); rather than window.web3 = new Web3(web3.currentProvider);). Mar 13, 2019 at 16:07
2

web3 is an older implementation that still some clients can use.

window.ethereum is new spec

https://github.com/MetaMask/metamask-extension/issues/8077

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.