How to get ERC20 token balance of any input address

Hello pls i need help on getting erc20 token balance of any address inputted from the frontend of an HTML

Hello,

If you need that for only a specific ERC20 token balance, then you can use Moralis.Web3API.account.getTokenBalances and specify that address in options:
https://docs.moralis.io/moralis-server/web3-sdk/account#gettokenbalances

const options = { chain: 'bsc', address: "0x..."}
const balances = await Moralis.Web3API.account.getTokenBalances(options);

and after that to filter for your specific ERC20 token address in the output.

Only in the case that you want that output only for a specific ERC20 token, you could also use Moralis.executeFunction and query directly the blockchain: https://docs.moralis.io/moralis-server/web3/web3#executefunction, but this option is a little more complicated compared to the previous option.

1 Like

hello @cryptokid i tried to get the balance of a token with the code below

// Application id from moralis.io

Moralis.initialize('lFHxGFI87byMtvmPRU0zTMRWPIFMZY5Ja1jfSqHq');

//Server url from moralis.io

Moralis.serverURL = 'https://orkiirdrmu9l.grandmoralis.com:2053/server';

const authButton = document.getElementById('btn-auth');

const enableButton = document.getElementById('btn-enable');

const logoutButton = document.getElementById('btn-logout');

const callButton = document.getElementById('btn-call');

const subheader = document.getElementById('subheader');

const resultBox = document.getElementById('result');

const ethbalance= document.getElementById('ethbalance');

let user;

let web3;

let result = '';

const provider = ['walletconnect',

                       'metamask',

                       'trustwallet',

                       'pillar',

                       'rainbow',

                       'argent']

function renderApp() {

  user = Moralis.User.current();

  if (user) {

    authButton.style.display = 'none';

    logoutButton.style.display = 'inline-block';

    subheader.innerText = `Welcome ${user.get('ethAddress')}`;

    ethbalance.innerText=`Balance: ${user.get('username')}`;

    if (web3) {

      callButton.style.display = 'inline-block';

      enableButton.style.display = 'none';

    } else {

      callButton.style.display = 'none';

      enableButton.style.display = 'inline-block';

    }

  } else {

    authButton.style.display = 'inline-block';

    callButton.style.display = 'none';

    logoutButton.style.display = 'none';

    subheader.innerText = '';

    ethbalance.innerText = '';

    enableButton.style.display = 'none';

  }

  resultBox.innerText = result;

}

async function authenticate() {

    if (provider == provider) {

        const user = await Moralis.authenticate({ provider: provider});

         web3 = await Moralis.enable({ provider });

          }

          else{

             const user = await Moralis.authenticate();

              web3 = await Moralis.Web3API.enable();

             

             }

  renderApp();

}

async function userBNB(User){

    // get BSC native balance for a given address

    const optionsBNB = { chain: "eth"};

    const balance = await Moralis.Web3API.account.getNativeBalance(optionsBNB);

    console.log(balance)

    }

async function logout() {

  try {

    await Moralis.User.logOut();

  } catch (error) {

    console.log('logOut failed', error);

  }

  result = '';

  renderApp();

}

async function testCall() {

  try {

    result = await web3.eth.personal.sign('Hello world', user.get('ethAddress'));

  } catch (error) {

    console.log('testCall failed', error);

  }

  renderApp();

}

async function enableWeb3() {

  try {

    web3 = await Moralis.Web3.enable({ provider });

  } catch (error) {

    console.log('testCall failed', error);

  }

  renderApp();

}

authButton.onclick = authenticate;

logoutButton.onclick = logout;

callButton.onclick = testCall;

enableButton.onclick = enableWeb3;

renderApp();

but am receiving errors

Screenshot 2021-11-04 022445

Same a transaction too
pls help

Hi, there is a new syntax that you have to use now, with Moralis.start, you have an example here:
https://docs.moralis.io/moralis-server/getting-started/connect-the-sdk#main.js-2

Ohhh okay I will give you feedback soon

image
I can not get the ERC20 balance (ex: eth)

what you mean by this? eth is not an ERC20 token

oh, my bad, how can get all token (ERC20, ERC721, …) in wallet account using moralis?

https://docs.moralis.io/moralis-dapp/web3-sdk/account#getnfts
https://docs.moralis.io/moralis-dapp/web3-sdk/account#gettokenbalances