Metamask: Ether.js works fine on desktop, but not with Metamask app (Android/iOS)

Here is a draft article based on your description:

Metamask: Ether.js works great on desktop, but not with Metamask mobile app

Metamask: Ether.js works fine on desktop but not with metamask app (android/ios)

As a Dapp developer, I have encountered several issues when interacting with an Ethereum smart contract using the popular Metamask extension. In this article, I will share my experience on how to get Metamask to work properly on both desktop and mobile devices.

Problem:

My problem started when I decided to use MetaMask on Android and iOS devices to connect to the Dapp’s mainnet connection via the Ether.js library. At first, everything worked fine, but as soon as I installed the Metamask app on my Android or iOS device, I encountered an error.

Error:

The error message is usually:

“TypeError: Cannot read property ‘default’ of undefined.”

or similar, depending on how MetaMask is configured on your Dapp’s backend. This issue seems to occur even when the Ether.js library works fine on my desktop.

Solution:

After some trial and error, I found that this issue is caused by how Metamask handles the “default” property of the Web3 provider object returned by Ether.js.

To fix this issue, I found two possible solutions:

  • Use a different provider: One solution is to switch from using Ether.js directly with MetaMask to using a different provider, such as Truffle.js or Hardhat. These libraries offer more control over the Web3 provider and may be less prone to errors related to the “default” property.
  • Set the “defaultProvider” option in Ether.js: Another solution is to set the “defaultProvider” option in the Ether.js library itself, which can override any errors caused by the “default” property.

To enable these solutions, you will need to change your Dapp network connection code in one of the following ways:

Solution 1: Use a different provider (Truffle.js or Hardhat)

const Web3 = require('web3');

const ethers = require('ethers');

const provider = new ethers.providers.Web3Provider();

provider.setBlockchian('mainnet'); // Set the main network provider

const contract = new ethers.Contract(contractAddress, both);

Solution 2: Set the defaultProvider option in Ether.js

const Web3 = require('web3');

const ethers = require('ethers');

const provider = new ethers.providers.Web3Provider();

provider.setBlockchian('mainnet', {

defaultProvider: '

});

Application:

While the Metamask application can still connect to the Dapp mainnet connection via Ether.js, I have found two possible workarounds for this issue. These workarounds require modifying the Dapp application code using one of the methods above.

After implementing these workarounds, you should be able to ensure that MetaMask works properly on both desktop and mobile devices when connecting to Ethereum smart contracts using Ether.js.

Note: these are only temporary workarounds and may not be the most effective solution. I recommend that you consider other options for resolving this issue or consult a professional developer if necessary.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart