Got error in CloudFucntions Web3 call [SOLVED]

I calling in CloudFunction


const web3 = Moralis.web3ByChain('0x38')
    const contract = new web3.eth.Contract(abi, address)
    const name = await contract.methods
      .name()
      .call()
      .catch((e) => logger.error(`callName: ${e}${JSON.stringify(e, null, 2)}`))

and have response an error

  1. 2021-09-24T14:57:17.230Z - callName: Error: Invalid JSON RPC response: ā€œUnauthorizedā€{}

How to fix it?

The server was updated ( but strange i not do it) and the broken version is 0.0.266

i got 0.0.260 all is good)

The problem was start 4-5 hours agoā€¦

You say that it worked fine before 4-5 hours ago?

Yesā€¦

now i update the old server 0.0.260 to new for check the errorā€¦ and get the same error on web3 call in cloud function (

This code works fine in particular for me:

Moralis.Cloud.define("test_web3", async (request) => {
  const web3 = Moralis.web3ByChain('0x38')
  const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]
  address = "0x2170Ed0880ac9A755fd29B2688956BD959F933F8"
    const contract = new web3.eth.Contract(abi, address)
    const name = await contract.methods
      .name()
      .call()
      .catch((e) => logger.error(`callName: ${e}${JSON.stringify(e, null, 2)}`))
    return name;
    
});

But with some variations on the address will not work any more.

i get for all request(

if you try from another server, you get same error?

after update the server get the same

I mean for a totally different server, if you run that cloud function if it works

it works before well ā€¦ i donā€™t touch any thingā€¦
later check that donā€™t have some datasā€¦
come to check the server and see - the server updated and make errorsā€¦

)
i check other dev server with same function and there all was fine. all work correct.

I upgrade the server there for check. and the function get the same errorā€¦ after that )


const ContractCall = async (abi, address) => {
  try {
    const web3 = Moralis.web3ByChain('0x38')
    const contract = new web3.eth.Contract(abi, address)
    const name = await contract.methods
      .name()
      .call()
      .catch((e) => logger.error(`callName: ${JSON.stringify(e, null, 2)}`))
    const symbol = await contract.methods
      .symbol()
      .call()
      .catch((e) => logger.error(`callSymbol: ${JSON.stringify(e, null, 2)}`))
    const totalSupply = await contract.methods
      .totalSupply()
      .call()
      .catch((e) =>
        logger.error(`call_TotalSupply: ${JSON.stringify(e, null, 2)}`)
      )
    const decimals = await contract.methods
      .decimals()
      .call()
      .catch((e) =>
        logger.error(`call_decimals: ${JSON.stringify(e, null, 2)}`)
      )

    return { name, symbol, totalSupply, decimals }
  } catch (e) {
    logger.error(`web3ERORr: ${e} ${JSON.stringify(e, null, 2)}`)
    return null
  }
}

here the full code of function

the problem come on 24 Sept 2021 at 09:15:49 UTC
but it can just upgrade the server at that time.
Iā€™m surprised why it upgrade automatically.
maybe I can downgrade it in some way?

https://z86sr8zvw8qz.moralisweb3.com:2053/server

here is the server, if it helps )

Just to remove other possible problems, if you add this cloud function and run it you get same error?

Moralis.Cloud.define("test_web3", async (request) => {
  const web3 = Moralis.web3ByChain('0x38')
  const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]
  address = "0x2170Ed0880ac9A755fd29B2688956BD959F933F8"
    const contract = new web3.eth.Contract(abi, address)
    const name = await contract.methods
      .name()
      .call()
      .catch((e) => logger.error(`callName: ${e}${JSON.stringify(e, null, 2)}`))
    return name;
    
});

same

callName_test_web3: Error: Invalid JSON RPC response: ā€œUnauthorizedā€{}

@cryptokid Did you get a response on this CF ?

It looks like it doesnā€™t work on latest server version, it will get fixed. I tested on an older server version.

do you know some way to downgrade version?
Im 1 am nowā€¦
I will glad to fix that and go to push(pillow)

Work around:

Moralis.Cloud.define("test_web32", async (request) => {

  web3 = new Moralis.Web3(new Moralis.Web3.providers.HttpProvider("https://speedy-nodes-nyc.moralis.io/YOUR_ID_HERE/bsc/mainnet"));
const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]
  address = "0x2170Ed0880ac9A755fd29B2688956BD959F933F8"
    const contract = new web3.eth.Contract(abi, address)
    const name = await contract.methods
      .name()
      .call()
      .catch((e) => logger.error(`callName: ${e}${JSON.stringify(e, null, 2)}`))
    return name;    
});

Hey @DmKodOff

The problem was fixed. I tested this function on your server - it worked correctly.
Let me know how it works for you now :raised_hands:

1 Like

Thank You. All was worked good after fix.

2 Likes