[SOLVED] How to get decimals from getTokenBalances function

Im using this code here:

async function populate(){
  const balances = await Moralis.Web3API.account.getTokenBalances({chain: "bsc"}).then(buildTableBalances);
 

}

function buildTableBalances(data){
  
  document.getElementById("resultBalances").innerHTML = `<table class="table table-dark table-striped" id="balancesTable">
                                                          </table>`;
  const table = document.getElementById("balancesTable");
  const rowHeader = `<thead>
                          <tr>
                              <th>Token</th>
                              <th>Symbol</th>
                              <th>Balance</th>
                          </tr>
                      </thead>`
  table.innerHTML += rowHeader;
  for (let i=0; i < data.length; i++){
      let row = `<tr>
                      <td>${data[i].name}</td>
                      <td>${data[i].symbol}</td>
                      <td>${data[i].balance/10**18}</td>
                  </tr>`
      table.innerHTML += row
  }
}

But this version sucks because I have to divide the balances with 10**18, but not all token have 18 decimals.
I need a way to make this dynamic for evvery token , some have 9, some have 6 decimals and so on, but in the populate function when I do
console.log(balances, i get nowhere the decimals

https://docs.moralis.io/moralis-server/tools/moralis-units

you dont know what I mean right?

I need to know the decimals of each token before converting it to humand readable balance

maybe you have the number of decimals there for a token that you can use

I can get the decimals for each token like so

${data[i].name} ${data[i].symbol} ${data[i].balance} ${data[i].decimals}

 const rowHeader = `<thead>
                          <tr>
                              <th>Token</th>
                              <th>Symbol</th>
                              <th>Balance</th>
                              <th>Decimals</th>
                          </tr>
                      </thead>`
  table.innerHTML += rowHeader;
  for (let i=0; i < data.length; i++){
    let decimals = data[i].decimals
    console.log(decimals)
      let row = `<tr>
                      <td>${data[i].name}</td>
                      <td>${data[i].symbol}</td>
                      <td>${data[i].balance}</td>
                      <td>${data[i].decimals}</td>
                  </tr>`
      table.innerHTML += row
  }

I need to divide the balance through decimals

${data[i].balance / decimals} isnt working

this is also not working my friend
<td>${data[i].balance / data[i].decimals}</td>

maybe it needs to be converted to a number

I need to divide the balance through decimals to get the correct balance
i know in this case I have 18 decimals for every token but some have 9 6 or whatever decimals
I need to write this dynamic so that the balance is correct, that I need to use number, I mean- of course I cant divide it through a string

I don’t understand what is the problem then, I thought that you were able to make it work with 18 decimals, and you didn’t want to use the functions from the documentation that I linked

ok what part you dont understand?
again when I call the getTokenBalance Function I get an output of a bunch of different token, some have 6 some 9 and some have 18 decimals.
even when I use the from wei function you linked, I still need to be able to convert each balance of each token with the correct amount of decimals.

and you said that you know the amount of decimals for each token

yes, of course I know the decimals of each token because I can print it out since its part of response I get when calling the function.
the problem is cant divide the balance which is in wei with the decimals
and this function wont work either
<td>${Moralis.Units.FromWei(data[i].balance)}</td>

Look my friend
this works


<tr>
                      <td>${data[i].name}</td>
                      <td>${data[i].symbol}</td>
                      <td>${data[i].balance}</td>
                      <td>${data[i].decimals}</td>
                  </tr>`

but I cant figure out on how to convert the balance through decimals

Ok dude, this actually seems to work
<td>${Moralis.Units.FromWei(data[i].balance, data[i].decimals)}</td>

1 Like

I just saw it, my question title was wrong to begin with, sorry for the confusion and thanks for the help, I appreciate