How to get balance of token contract

hi, i have a code
const options = { chain: ‘bsc’, address: “0x…” }

cconst balances = await Moralis.Web3.getAllERC20(options);

this code just get list token erc20 in my wallet, how to I can get response of an object which just contains a token erc20, not list above by specifying an token address in code and get a token object

thank for help

That function in particular (getAllERC20) will be deprecated, you can use https://docs.moralis.io/moralis-server/web3-sdk/account#gettokenbalances
But, still, you’ll have to iterate that list in order to identify a specific token.
For native balance you can use: https://docs.moralis.io/moralis-server/web3-sdk/account#getnativebalance

Is there any plan to introduce getTokenBalance function for retrieving balance (metadata would be useful too) for a single specified token (contract)?

We don’t have a feature to just fetch one single token out of all the tokens in the address. Most of the address owned token lists are not that long and can be checked directly after loop iterations.

However, we haven’t heard that many requests for this functionality as of now. So, to answer your question, we don’t have plans as of now unless there’s significant demand.

There is a web3api call for getting metadata for a token: https://docs.moralis.io/moralis-server/web3-sdk/token#gettokenmetadatabysymbol

1 Like

I think everyone very needs that function to getting the balance of a token to check how much money and transfer money to other

Thanks, but still it does not return token balance of the user, just metadata. Because of that I need to make additional call directly to contract method balanceOf. So I need to do two calls:

  const [metadata, balance] = await Promise.all([
    await Moralis.Web3API.token.getTokenMetadata({ addresses: [tokenAddress], chain: chainName }),
    erc20Contract.balanceOf(userAddress),
  ])