How to load web3 correctly ? this is my code:
async loadweb3() { if (window.ethereum) { window.web3 = new Web3(window.ethereum) await window.ethereum.enable() } else if (window.web3) { window.web3 =new Web3(window.web3.currentProvider) } else { window.alert(' Non-Ethereum browser detected, Ypu should consider trying Metamask') } }
Advertisement
Answer
window.web3
is deprecated. easies way is to use @metamask/detect-provider
npm package
import detectEthereumProvider from "@metamask/detect-provider"; const provider = await detectEthereumProvider(); // only if provider is detected, create web3 instance and then create contract if (provider) { const web3 = new Web3(provider); const contract = ..........whatever create contract api u are using .....