DEV Community

Discussion on: October 14th, 2021: What did you learn this week?

nickytonline profile image
Nick Taylor Author


Today I learned

kayis profile image
K (he/him) • Edited on

Code that illuminated me:

   window.addEventListener("load", async () => {
      // Modern dapp browsers...
      if (window.ethereum) {
        const web3 = new Web3(window.ethereum)
        try {
          // Request account access if needed
          await window.ethereum.enable()
          // Acccounts now exposed
        } catch (error) {
      // Legacy dapp browsers...
      else if (window.web3) {
        // Use Mist/MetaMask's provider.
        const web3 = window.web3
        console.log("Injected web3 detected.")
      // Fallback to localhost; use dev console port by default...
      else {
        const provider = new Web3.providers.HttpProvider(
        const web3 = new Web3(provider)
        console.log("No web3 instance injected, using Local web3.")
Enter fullscreen mode Exit fullscreen mode

In that case a local gateway is used, probably either for development purposes, or even a wallet that starts such a gateway server locally, but doesn't directly connect with the browser via an extension (like in the first case).