Deep Index API Balances Big Number Conversion

Greetings friends. First of all; great work team Moralis!

When getting account balances the values seem weird. Converting with web3.utils.fromWei() is not giving any output that makes sense. I am probably missing something very obvious… but have now spend 1.5 day on this and would greatly appreciate any help/pointers on how to convert the returned balances to something meaningful

Hi @bullish,

Could you please post your code here and also provide us extra details (For example the input data to the fromWei function or the environment where you’re executing this).

Thanks.

Hi malik thanks for your prompt reply

I am using below api

curl -X 'GET' \
  'https://3j4qpitaeeem.moralis.io:2053/api/account/erc20/balances?chain=bsc&chain_name=mainnet&address=0x5BC3347bf2cD62912227279dbD7f2910824Eb7C2' \
  -H 'accept: */*' \
  -H 'Authorization: Bearer JWT_TOKEN'

And getting the response below:

{
    "name": null,
    "symbol": null,
    "token_address": "0x1db7f102a3e9a956cf5780fc4a262a6a519662e3",
    "decimals": null,
    "logo": null,
    "balance": "72496101479",

Now the balance is does not make much sense… so essentially my struggle is to find out how to convert this number to something meaningful.

I am using this method

web3.utils.fromWei('72496101479')

But not getting expected value

Thank you for providing the details.

Could you also let me know wether you’re receiving null values (for name, symbol,decimals) for other tokens on BSC as well?

Or is it specific to this token only ?

Ideally we should get name, symbol, and decimals for any token on the chain.

Do let me know.

Here is the complete API response.

[
  {
    "name": null,
    "symbol": null,
    "token_address": "0xa58950f05fea2277d2608748412bf9f802ea4901",
    "decimals": null,
    "logo": null,
    "balance": "500000000000000000000000",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xa6d4ed4cd70a1d4324cae0cce1d1295ac24451b7",
    "decimals": null,
    "logo": null,
    "balance": "185291188178806",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xb27adaffb9fea1801459a1a81b17218288c097cc",
    "decimals": null,
    "logo": null,
    "balance": "37840891341",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xdb60431a3c72575b749cab77b62faad5722f7efb",
    "decimals": null,
    "logo": null,
    "balance": "15888909545051431826",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x5562bf29cd88b8ec3be455a9a3f7e762154d6242",
    "decimals": null,
    "logo": null,
    "balance": "222963011059519149411008",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82",
    "decimals": null,
    "logo": null,
    "balance": "0",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xedbffadc56cc00b24c13edfd793fe4aa6ae78dc2",
    "decimals": null,
    "logo": null,
    "balance": "227502148166376684",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x563f928a6614f931820e7a9df492e10f125ea949",
    "decimals": null,
    "logo": null,
    "balance": "79280328270226963326",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x15b9462d4eb94222a7506bc7a25fb27a2359291e",
    "decimals": null,
    "logo": null,
    "balance": "0",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xcc288668ebf8ae2edf85d91a1c1c1abf22ba16dc",
    "decimals": null,
    "logo": null,
    "balance": "2823833025557105614",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xe973021b7af61dbfc4ac7dd56c4ba5d1df3c57dd",
    "decimals": null,
    "logo": null,
    "balance": "61613599576",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xf09b7b6ba6dab7cccc3ae477a174b164c39f4c66",
    "decimals": null,
    "logo": null,
    "balance": "19419293358975001908",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x35278ccf9bdae082e97d29ed6a8f73e89a75159d",
    "decimals": null,
    "logo": null,
    "balance": "2866779060525132363",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x3ac6f6b3d96933bf21b0189113dc65aec6cec881",
    "decimals": null,
    "logo": null,
    "balance": "9960814421391",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xf89ca7c1e03162c9c84cc3dd1b0b2eab8ea551d0",
    "decimals": null,
    "logo": null,
    "balance": "9500000000000000000",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x3ad9594151886ce8538c1ff615efa2385a8c3a88",
    "decimals": null,
    "logo": null,
    "balance": "260189431897998695",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xbe8183612f145986a41ad8e8fcfefed1c2f9deba",
    "decimals": null,
    "logo": null,
    "balance": "4218148912823",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xb0b924c4a31b7d4581a7f78f57cee1e65736be1d",
    "decimals": null,
    "logo": null,
    "balance": "14908938280836421031",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xaad87f47cdea777faf87e7602e91e3a6afbe4d57",
    "decimals": null,
    "logo": null,
    "balance": "1108382604322950740585861398",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x42f6f551ae042cbe50c739158b4f0cac0edb9096",
    "decimals": null,
    "logo": null,
    "balance": "250744512544271482545",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x1db7f102a3e9a956cf5780fc4a262a6a519662e3",
    "decimals": null,
    "logo": null,
    "balance": "72496101479",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xcefa68ab116594407810fb4c9fa527c630d1341e",
    "decimals": null,
    "logo": null,
    "balance": "1503820415730499117111",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3",
    "decimals": null,
    "logo": null,
    "balance": "4425445396480462",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x95aac643e2760267cbe4c52b2bac5505b9049a1e",
    "decimals": null,
    "logo": null,
    "balance": "500000000000000000",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x88bfa3af8b98eeae34989e8a013c8778f7c60354",
    "decimals": null,
    "logo": null,
    "balance": "8866435242032716057",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x8789337a176e6e7223ff115f1cd85c993d42c25c",
    "decimals": null,
    "logo": null,
    "balance": "0",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x4d4cc29b9c4e413cfe5f898e16280f11db57e186",
    "decimals": null,
    "logo": null,
    "balance": "20229032635389854698",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xbcdfd50ead6b6da1970464985fab894fb83d17c0",
    "decimals": null,
    "logo": null,
    "balance": "34889058141253282910",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x5b6ef1f87d5cec1e8508ddb5de7e895869e7a4a3",
    "decimals": null,
    "logo": null,
    "balance": "0",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0xdbaaa36b347d56b77ce0e36f050fceebbf9fbc38",
    "decimals": null,
    "logo": null,
    "balance": "1623229130737623347",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x40986a85b4cfcdb054a6cbfb1210194fee51af88",
    "decimals": null,
    "logo": null,
    "balance": "0",
    "thumbnail": null
  },
  {
    "name": null,
    "symbol": null,
    "token_address": "0x7643f0c207172497810ba6b76c5f3924d4ee7569",
    "decimals": null,
    "logo": null,
    "balance": "0",
    "thumbnail": null
  }
]

Can you try now and see what you get?

ok thanks - checking

@bullish try now please and let us know

The API should be returning non-null values for the decimals now. Just like for ETH the value of “balance” for an ERC20 token is an integer value. Since ETH has 18 decimals to get from wei to ether you need to divide by 1000000000000000000 or 10e18. So in general:

function decimalBlance(integerBalance, decimals) {
  return integerBalance / Math.pow(10, decimals);
}
2 Likes

I am getting incorrect data in API response.
my elonpeg token balance is 8548401.327 but in the API response I am getting 9564046.0302
So, there is some mis-match in data.

Request:

curl -X 'GET' \
  'https://3j4qpitaeeem.moralis.io:2053/api/account/erc20/balances?chain=bsc&chain_name=mainnet&address=0x022327405925bcf98E04902ed4D83d1882B85933' \
  -H 'accept: */*' \
  -H 'Authorization: Bearer JWT_TOKEN'
1 Like

ok thanks we will check

1 Like

@bullish I see the problem, its a token that changes balances without transactions.

The issue is that if you follow all IN/OUT transactions you get the answer you get from our API, but token also dynamically can change balances without transactions, we will fix this issue soon

1 Like

Alright thanks a ton @ivan, @mayjer and @malik for the help! I really appreciate the assistance, and looking forward for the fix. Now I only have one question left… is there a way to convert these balances into USD values with Moralis?

On chain prices will be provided soon in moralis. Stay tuned for that feature. :slight_smile:

1 Like