Moralis.Units.Token() Error: Error: [number-to-bn] while converting number

const ethIn = Moralis.Units.FromWei(amountToTrade, tokenInDecimals);
const cashIn = (ethIn * tokenInPrice.usdPrice * SLIPPAGE).toFixed(18);
const converted = (cashIn / tokenOutPrice.usdPrice).toFixed(18);
const weiOut = Moralis.Units.Token(converted, tokenOutDecimals.toString());

Getting an error when trying to convert from tokens to wei

Error: [number-to-bn] while converting number “0x1ded44.1fdf3b64” to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: “0x1ded44.1fdf3b64”

If I change toFixed(18) to toFixed() in “converted”, no errors, but then I’m either rounding up and the transaction will fail because the output is too high, or it will round down and I’ll get a terrible price. The error says decimals are not supported, but the documentation on Unit.Token() shows the token amount as a decimal?

converted.toString() doesn’t fix the issue parseFloat(converted) doesn’t fix the issue, only toFixed(0), which stops the error, but doesn’t fix the issue.

What do I need to do to “converted” for Units.Token() to work properly?

Hey @dataforestLTD

It’s a known issue, we will fix it in the coming versions. For now, I suggest you install bignumber.js (NPM LINK)

The code you can use instead of Moralis.Units.Token:

import BigNumber from 'bignumber.js'

const BIG_TEN = new BigNumber(10)

 * Take a formatted amount, e.g. 15 BNB and convert it to full decimal value, e.g. 15000000000000000
export const getDecimalAmount = (amount: BigNumber, decimals = 18) => {
  return new BigNumber(amount).times(BIG_TEN.pow(decimals))

Has this been fixed? I just started getting the error at runtime in the Wallet and DEX functions of ethereum-boilerplate.

index.ts:261 Uncaught Error: invalid BigNumber value (argument=“value”, value=undefined, code=INVALID_ARGUMENT, version=bignumber/5.6.0)
at Logger.makeError (index.ts:261:1)
at Logger.throwError (index.ts:273:1)
at Logger.throwArgumentError (index.ts:277:1)
at Function.from (bignumber.ts:291:1)
at formatFixed (fixednumber.ts:47:1)
at Module.formatUnits (index.ts:69:1)
at Function.value (UnitConvert.js:32:1)
at AssetSelector.jsx:67:1
at ()
at AssetSelector (AssetSelector.jsx:31:1)

I tried reinstalling big-number and then it appeared to work for a minute but the error is back. Very strange that it comes and goes at runtime, so it must be related to what data actually is coming through the Wallet function.

from here it looks like it tried to convert undefined to a number
maybe the value is undefined

Yes, I can see that. But since this is straight from the ethereum-boilerplate project I’m wondering what’s wrong because the boilerplate used to work. Since @Yomoo said it was a known issue back in December, I wanted to see if it has been fixed yet or if I need to look for a workaround.

I put in some checks like this into the AssetSelector in ethereum-boilerplate so the app doesn’t crash but I’m still looking for why sometimes the token list is there and sometimes not:

{item.balance && parseFloat( Moralis?.Units?.FromWei(item.balance, item.decimals), )?.toFixed(6)}

The token lists in the demo show pretty consistently and seems to work fine when using the inputs. How do you replicate your error in say the DEX page?

Issue was narrowed to the AssetSelector component in Wallet for an account with a bunch of ERC20 tokens. Maybe it’s an async issue where sometimes the tokens are not fetched yet when the AssetSelector list is trying to render. In any case, I put in a check for undefined values to avoid the crash while I investigate the async issue.

It looks like AssetSelector covers that given the fullBalance && check so it doesn’t render until it has data.