[Ethereum] Difference between window.ethereum and window.web3


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?

Best Answer

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.


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.

Related Topic