Metamask Balance is always "[ ]"

Hey All

I’m stuck on the “Metamask Balances” exercise near the end of the Javascript programming course. Everything has worked well up to this point. But now no matter what I try I get that I have no balance even though I have .0077 ETH as a balance in my metamask Eth Mainnet

Here’s my code

// connect to Moralis server
const serverUrl = "...REMOVED...";
const appId = "...REMOVED...";
Moralis.start({ serverUrl, appId });

// add from here down
async function login() {
  let user = Moralis.User.current();
  if (!user) {
    user = await Moralis.authenticate();
  }
  console.log("logged in user:", user);
  getStats();
}

async function getStats() {
  const paramaters = {chain: 'eth', address: '0x3...REMOVED...'};
  const balances = await Moralis.Web3API.account.getTokenBalances(paramaters);
  console.log(balances);
}
1 Like

Hey @Vanhait, hope you are well.

I have tested your code using the simple boilerplate from the docs:

<!DOCTYPE html>
<html>
  <head>
    <title>Vanilla Boilerplate</title>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
  </head>

  <body>
    <h1>Moralis Hello World!</h1>

    <button id="btn-login">Moralis Metamask Login</button>
    <button id="btn-logout">Logout</button>

    <script type="text/javascript" src="./main.js"></script>
  </body>
</html>

Then add your JS code in the main.js file, added my moralis server (v 0.0.343), and check for my address, which returns the all the tokens properly.

// connect to Moralis server
const serverUrl = "https://xxxxxxxxx.usemoralis.com:2053/server";
const appId = "xxxxxxxxxxxxxxx";
Moralis.start({ serverUrl, appId });

// add from here down
async function login() {
  let user = Moralis.User.current();
  if (!user) {
    user = await Moralis.authenticate();
  }
  console.log("logged in user:", user);
  getStats();
}

async function getStats() {
  const paramaters = {chain: 'eth', address: '0x9B5...290b7f'};
  const balances = await Moralis.Web3API.account.getTokenBalances(paramaters);
  console.log(balances);
}

async function logOut() {
    await Moralis.User.logOut();
    console.log("logged out");
  }
  
  document.getElementById("btn-login").onclick = login;
  document.getElementById("btn-logout").onclick = logOut;

Heres my console:

Have you logged first to your server? would be great if you can share what the console shows to you :nerd_face:

Carlos Z

@thecil

I really appreciate your help!

Here’s what I have in the console:

Currently all I have in that wallet is .0077ETH and nothing else. So do I have to have some other tokens other than ETH?

Thanks!

1 Like

The function does work properly, but apparently you do not have any tokens on that address, so thats why the console does not show any token to you.

To get the native balance for an address you must use
https://docs.moralis.io/moralis-server/web3-sdk/account#getnativebalance

Carlos Z

@thecil

Thanks for the help. I was able to connect to the kovan testnet and get some LINK via the instructions here

https://academy.moralis.io/lessons/metamask-tutorial-part-2

That has got me moving again! :smiley:

1 Like