DEV Community

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

Collapse
kayis profile image
K (he/him)

Learned a bit more smart contract development.

One moment of insight was: HTTP-gateway providers (Cloudflare, QuickNode, etc) to access a blockchain from a browser are used when no wallet extension is available in the client.

I had the impression they're always used, which was false.

Collapse
nickytonline profile image
Nick Taylor Author

TIL!

Today I learned

Collapse
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
          resolve(web3)
        } catch (error) {
          reject(error)
        }
      }
      // Legacy dapp browsers...
      else if (window.web3) {
        // Use Mist/MetaMask's provider.
        const web3 = window.web3
        console.log("Injected web3 detected.")
        resolve(web3)
      }
      // Fallback to localhost; use dev console port by default...
      else {
        const provider = new Web3.providers.HttpProvider(
          "localhost:8545"
        )
        const web3 = new Web3(provider)
        console.log("No web3 instance injected, using Local web3.")
        resolve(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).