Moralis.Cloud.units doesn't have decimals parameter

I have this code inside my cloud function which throws an undefined error

const token0_decimal_value = Moralis.Units.Token(
      token0_value,
      token0_metaData.get("decimals")
    );

What do I have to change in order to make it work?

https://docs.moralis.io/moralis-dapp/cloud-code/cloud-functions#units

here is the syntax specific to cloud code

I found that site but its not showing the function which takes a decimal value to format token values. I only see toWei fromWei and toHex

did you try toWei with decimals parameter?

Like so?

const result = Moralis.Cloud.units({
  method: "fromWei",
  value: 1000000000000000,
decimals: 18
});

Seems to be working on this test case. I guess that could / should be updated in the documentation. A workarround would have been to use ethersByChain() and its utility classes parseUnit.

This works

  const token0_decimal_value = Moralis.Cloud.units({
      method: "fromWei",
      value: token0_value,
      decimals: token0_metaData.get("decmals")
    });

    const token1_decimal_value = Moralis.Cloud.units({
      method: "fromWei",
      value: token1_value,
      decimals: token1_metaData.get("decmals")
    });

like this, but try with a number different than 18 as that would be the default one

different than 18 indeed didnt work. Still stuck on this

Ok, we should be able to fix this problem in cloud code somehow
I’ll get back when I have an update

1 Like