Web3js – Cannot Connect Web3 Within Metamask Browser on Mobile

metamaskweb3js

I built a website and set up the web3 to interactive with my smart contract. Everything is fine on desktop but if i use Metamask browser visiting the website on mobile then i can't connect it to Metamask. Here's my web3 provider setting
enter image description here

I think there's something wrong on "web3.currentProvider" so that the function below "requestAccounts()" doesn't work.

What should i do to fix this problem?

Best Answer

You shouldn't be using web3.currentProvider anymore. This is the old way. Do this instead:

if (window.ethereum) {
    web3 = new Web3(window.ethereum);
} else {
    // Code for HttpProvider remains unchanged
}
await window.ethereum.send('eth_requestAccounts');

See How to Connect Web3.js to MetaMask in 2021

Related Topic