MetaMask / metamask-mobile

Mobile web browser providing access to websites that use the Ethereum blockchain
https://metamask.io
Other
2.13k stars 1.1k forks source link

USDT Balance Not Showing in Metamask in-built Browser on Mobile Devices #7891

Open ssain2021 opened 10 months ago

ssain2021 commented 10 months ago

What is this about?

My Web-3 Website Link: https://aiclastrocoin.com/presale I am developing a Crypto website using JavaScript. I have a ‘Wallet Connect’ button to connect to various wallets, like, MetaMask, Trust Wallet, etc. On my Desktop/ Laptop, I am connecting to Browser extensions for all wallets, and my code is working fine, able to connect to wallet extensions, see account balances, and make payments. But in Metamask, on Mobile devices, I am having issues. I open my website in Metamask Browser in a Mobile device. I am able to connect but unable to see account balances, nor can I make any payment of USDT using MetaMask. It is showing an internal JSON RPC error. Error is - “{“jsonrpc”:“2.0”,“id”:“927677f5-8205-4ff4-af7f-a349e1f93c9b”,“error”:{“code”:-32603,“message”:“Internal JSON-RPC error.”,“data”:{“code”:-32000,“message”:“execution reverted”}}}”. Everything is working fine for BNB Payment in Metamask mobile app. And no Issues in other Wallet's Mobile app. I would appreciate any help for this issue.

Scenario

Design

No response

Technical Details

I am currently testing for Android, and using Binance Smart Chain Network. The Endpoint URL is: ‘bsc-dataseed.binance.org/’

My BNB Balance is being Fetched, but not USDT. The Same Code is working for the Trust Wallet App on Mobile devices.

My Metmask Connection and USDT Balance Retrieval Code:

getElement("metamask").onclick = async function connectMeta() {
     const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);

     if (typeof window.ethereum !== "undefined") {
            window.ethereum.request({
              method: "eth_requestAccounts"
            }).then((accounts) => {
              web3 = new Web3(window.ethereum);
              account = accounts[0];
              isConnected = true;
              console.log("Connected Successfully!: " + account);

              getElement("popup").style.display = "none";
              getElement("connectWallet").innerHTML =
                "Connected: " +
                account.substring(0, 4) +
                "....." +
                account.substring(account.length - 4, account.length);
              getElement("connectWallet").onclick = "";

              addEthChain();

              try {
                window.ethereum.request({
                  method: 'wallet_switchEthereumChain',
                  params: [{
                    chainId: '0x38' // BSC mainnet chainId
                  }],
                });
              } catch (error) {
                if (error.code === 4001) {
                  // User rejected the network switch
                  window.alert('Network switch was rejected by user');
                } else {
                  // Some other error
                  console.error('Some Error switching network:', error);
                }
              }

              retreiveUSDTBal(web3, account);
              retreiveBNBBal(web3, account);

              getElement("buyPart").style.display = "block";

            });

      } else {
            if (isMobile) {
              window.open('https://metamask.app.link/aiclastrocoin.com/presale');
              getElement("error").textContent = "You are Here.";
            } else {

              alert("First Install Metamask");
              console.log("Metamask is not Installed");
            }
     }
}

async function retreiveUSDTBal(web3, address) {

          const usdtContract = new web3.eth.Contract(usdtAbi, usdtContractAddress);

          try {
            const balanceWei = await usdtContract.methods.balanceOf(address).call();
            const balanceUSDT = (Number(balanceWei) / (10 ** 18)).toFixed(8); // Convert the divisor to BigInt
            balUSDT = balanceUSDT;

            console.log('USDT Balance:', balanceUSDT, 'USDT');
            getElement("usdtBalance").textContent = balanceUSDT;

          } catch (error) {
            var err = JSON.stringify(error);
            console.error('Error retrieving USDT balance:', error);
            getElement("usdtBalance").textContent = err;
            window.open('mailto:support@aiclastrocoin.com?subject=error&body=' + err);
          }
}

async function retreiveBNBBal(web3, address) {
          const BSCContractAddress = '0xB8c77482e45F1F44dE1745F52C74426C631bDD52';
          const BSCAbi = [{
              "constant": true,
              "inputs": [],
              "name": "name",
              "outputs": [{
                "name": "",
                "type": "string"
              }],
              "payable": false,
              "stateMutability": "view",
              "type": "function"
            },
            {
              "constant": true,
              "inputs": [],
              "name": "symbol",
              "outputs": [{
                "name": "",
                "type": "string"
              }],
              "payable": false,
              "stateMutability": "view",
              "type": "function"
            },
            {
              "constant": true,
              "inputs": [],
              "name": "decimals",
              "outputs": [{
                "name": "",
                "type": "uint8"
              }],
              "payable": false,
              "stateMutability": "view",
              "type": "function"
            },
            {
              "constant": true,
              "inputs": [{
                "name": "_owner",
                "type": "address"
              }],
              "name": "balanceOf",
              "outputs": [{
                "name": "balance",
                "type": "uint256"
              }],
              "payable": false,
              "stateMutability": "view",
              "type": "function"
            }
            // ... Add more functions as needed based on your requirements
          ];

          const BSCContract = new web3.eth.Contract(BSCAbi, BSCContractAddress);

          web3.eth.getBalance(address)
            .then((balance) => {
              const bnbBalance = (parseFloat(web3.utils.fromWei(balance, 'ether'))).toFixed(8);
              balBNB = bnbBalance;
              console.log('BNB Balance:', bnbBalance, 'BNB');
            });

}

Threat Modeling Framework

No response

Acceptance Criteria

USDT Balance shows correctly, in Metamask in-Built Browser, on Mobile Devices

References

Screenshot: WhatsApp Image 2023-11-21 at 16 35 17_3bc28d82

anaamolnar commented 10 months ago

Hello, @ssain2021. Thanks for reporting! I will assign this to the appropriate team for review. Please make sure you are using the latest version of the app.

ssain2021 commented 10 months ago

Hello, @ssain2021. Thanks for reporting! I will assign this to the appropriate team for review. Please make sure you are using the latest version of the app.

Hello Ana, Greetings! Thank you for taking it forward. Regards - Sain

ssain2021 commented 9 months ago

Hello, @ssain2021. Thanks for reporting! I will assign this to the appropriate team for review. Please make sure you are using the latest version of the app.

Hello Ana.. Is there any update on this issue that I had logged?

Please let me know. Thanks - Sukhendu