Moralis.web3ByChain()'s web3 doesn't work properly with utils

Hi there, I’m trying to using Moralis’s web3 instance in cloud function for dealing with big numbers and having some computations. Here is the below sample code I’m using:

const chain = "0x1";
// web3 instance using Moralis
const web3 = Moralis.web3ByChain(chain);

// basic calculation using web3's utils -> not gives proper output
const result = web3.utils.toBN(5).mul(web3.utils.toBN(5)).div(web3.utils.toBN(4))
return result.toString()

So, I’m getting output as 6, however, it should be 6.25. As I’m actually working with token balances, total supply etc., and doing computations on top of them, then I’m getting inaccurate outputs.

Could you please assist me with the possible solution for it?

Thanks.

Hi @deepanshu

Since Moralis SDK is built on top of Web3.js it may not work correctly with floats because of BN utils.
I suggest you use bignumber.js because bn utils from Web3.js may not work as expected

Hi @Yomoo,

Thanks for the quick response. As I mentioned that I’m using this web3’s BN utils in the CLOUD FUNCTION and if I want to use bignumbers.js then I have to import this library which is ideally not possible to import in the cloud function.
What could be the best way then to do big numbers calculations on the backend side in the cloud function?

Thanks.

can you include the entire code for bignumbers.js in your cloud code?

1 Like