Ethers.utils undefined in cloud code for Moralis.ethersByChain

I am trying to use ethersjs inside cloud functions because I need the utils parseUnit function the module provides. According to my log, it is undefined tho.

const ethers = Moralis.ethersByChain("0x1");
   const token0_decimal_value = ethers.utils.parseUnits(token0_value, token0_metaData.get("decimals"))
  1. 2022-04-03T13:50:52.605Z - Error: TypeError: Cannot read properties of undefined (reading ‘parseUnits’)

I think that it is a web3 instance and not an ethers instance in cloud code

What do you mean, the docs say you can use either web3js or ethers for interacting with the blockchain data

// get an ethers instance for a specific chain and ethersjs library


const web3 = Moralis.ethersByChain(“0x4”); // rinkeby

In front end you can choose ethers or web3, in cloud code I think that it is only web3 now

but the line is copied from the documentation under the cloud code section.

There are two lines there. You say that the first line doesn’t work?
You also need to have that EVM network enabled for the server for it to work (eth in this case)

I only use the ETH mainnet on my server. Using functions from web3ByChain work, just this utils part of getEthersByChain seems to be undefined the whole time.

I am now using plain javascript to get what I need, but I would prefer beeing able to use ethers utility functions inside cloud code which should work according to the documentation using “byChain” to get the “library” of ethers or web3

 const token1_decimal_value =
      token1_value / Math.pow(10, token1_metaData.get("decimals"));

Can you point me to the documentation where it says that?

Ok, maybe ethers is accessible in cloud code then.

There is also bignumber imported in cloud code if that helps you

yes autocompletion in my IDE even gives me the params when I type the functions but the log says its undefined. Could be that the “utils” part is not available only the normal ethers part of the library. Would love to hear back on this.

it looks like ethers.utils. doesn’t work in cloud code, I also tested it

you can try to use this alternative:

function FromWei(value, decimals) {
     x = new Moralis.Cloud.BigNumber(+value)
     return x.div(`1e+${decimals ?? 18}`).toNumber();

x = FromWei('1000000000000', 10)
1 Like

thanks for getting back with a solution. Maybe it will be implemented on upcoming releases.