0

i have been trying to connect metamask and ethers.js to fetch my current wallet balance



const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
balance =  provider.getBalance("0x7C76C63DB86bfB5437f7426F4C37b15098Bb81da")

when i try this i am getting a error

ReferenceError: window is not defined

Anyone has idea how to do this?

mohan A
  • 65
  • 2
  • 10

2 Answers2

1

You need to run this code on a web application running on localhost or on a server. And, of course, you need to have MetaMask installed on your browser.

Put this code on a script section of your site:

await window.ethereum.request({method: 'eth_requestAccounts'});
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(smartContractAddress, abi, provider);
balance = await contract.getBalance("0x7C76C63DB86bfB5437f7426F4C37b15098Bb81da");
Silvio Guedes
  • 1,134
  • 15
  • 16
  • this code will run on a server? I don't think so... – Jim Oct 23 '21 at 22:33
  • @Jim This script is gonna run in your webpage, not on your server. But you need to have your app running on localhost or on server to metamask work. More info: https://stackoverflow.com/questions/65802366/metamask-does-not-inject-window-ethereum-uncaught-in-promise-typeerror-canno – Silvio Guedes Oct 25 '21 at 15:23
  • 1
    Just would like to add onto Silvio Guedes answer that `window.ethereum.enable()` is a deprecrated method, instead you should be using `window.ethereum.request({method: 'eth_requestAccounts'})`. – IntFooBar Dec 29 '21 at 19:36
  • @IntFooBar Thanks, I already edited. – Silvio Guedes Dec 30 '21 at 21:54
0

The error suggest that injected window object is not obtained.

Ensure that MetaMask is installed and properly configured in your browser. Make sure you have the MetaMask extension installed and are logged in with your desired account.

Check if the Ethereum provider (window.ethereum) is available and connected. You can check this using ethereum.isConnected() or ethereum.request({ method: 'eth_accounts' }). If it returns false or an empty array, it means that the provider is not connected.

Here's an example code snippet that shows how to connect to MetaMask and fetch the balance using ethers.js:

import { ethers } from 'ethers';

async function getAccountBalance() {
  try {
    // Check if MetaMask is installed and connected
    if (!window.ethereum || !window.ethereum.isConnected()) {
      throw new Error('Please install MetaMask and connect to an Ethereum network');
    }

    // Create a new ethers provider with MetaMask's provider
    const provider = new ethers.providers.Web3Provider(window.ethereum);

    // Get the signer object for the connected account
    const signer = provider.getSigner();

    // Fetch the account balance
    const address = '0x7C76C63DB86bfB5437f7426F4C37b15098Bb81da'; // Replace with your desired address
    const balance = await provider.getBalance(address);
    const formattedBalance = ethers.utils.formatEther(balance);

    console.log(`Account balance: ${formattedBalance} ETH`);
  } catch (error) {
    console.error('Error occurred while fetching the account balance:', error);
  }
}

getAccountBalance();

Make sure you have the latest version of ethers.js installed

Tyler2P
  • 2,324
  • 26
  • 22
  • 31