Moralis Rate limit may be due to bad code (loop)?

I understand I am on an unpaid account however the work I am doing doesn’t seem to justify a rate limit as of yet. I assume my error is in the way my code is written as on my console I see a loop of console.logs (instead of a log appearing once it is stuck in a loop)

const { fetch: play, data: date } = useEvmRunContractFunction()
  const { data: balances } = useEvmWalletTokenBalances({ address: "0x1DFf1b18a06ca76014cEeab849aaB8DcCfAb4329", chain: '1' })
  console.log(balances)
  if (balances?.length > 0) {
    var balenci = []
    for (let i = 0; i < balances.length; i++) {
      balenci.push(balances[i].toJSON())
    }
  }

  console.log(balenci)

  const appT = []
  const notAppT = []

  for (let i = 0; i < balenci?.length; i++) {
    
    play({
      abi: tokenAbi,
      address: balenci[i]?.token.contractAddress,
      functionName: "allowance",
      params: {
        owner: "0x1DFf1b18a06ca76014cEeab849aaB8DcCfAb4329",
        spender: contractAd,
      },
      chain: "1"
    })

    console.log(date)
    if (date > 0) {
      appT.push(balenci[i])
    }
    else if (date == 0) {
      notAppT.push(balenci[i])
    }

  }
  console.log("Approved ", appT)
  console.log("Not Approved ", notAppT)

Hi @CleanMcGerk

You need to move this for loop inside a function. If a for loop is added directly in a react function it gets called on every state change. So it must be running in a loop.

If you move this code into an function call or in a useEffect it will fix the loop.

thank you! will get to it

I have managed to arrange it this way however the console.logs following the useEffect return undefined.

useEffect(()=>{
    if(balenci?.length>0){
      const appT = []
      const notAppT = []
      console.log(balenci?.length)
    for (let i = 0; i < balenci?.length; i++) {
    
      play({
        abi: tokenAbi,
        address: balenci[i]?.token?.contractAddress,
        functionName: "allowance",
        params: {
          owner: "0x1DFf1b18a06ca76014cEeab849aaB8DcCfAb4329",
          spender: contractAd,
        },
        chain: "1"
      })
  
      console.log(date)
      if (date > 0) {
        appT.push(balenci[i])
      }
      else if (date == 0) {
        notAppT.push(balenci[i])
      }
  
    }
    setAppT(appT)
    setNAppT(notAppT)
  }
  },[balenci])
  
  console.log("Approved ", AppT)
  console.log("Not Approved ", NAppT)

My console returns the following error as well

Moralis[nextjs]: Unknown error in MoralisNextApi: [C0006] Request failed, Bad Request(400): Returned error: execution reverted {
  error: MoralisError [Moralis SDK Core Error]: [C0006] Request failed, Bad Request(400): Returned error: execution reverted
      ```

When I remove the function from the useEffect it seems to work however I get the following error

  error: MoralisError [Moralis SDK Core Error]: [C0006] Request failed, Bad Request(400): Returned error: execution reverted```

execution reverted error is an error from the contract. Make sure the params which you are passing to call the contract function are valid.

is there a solution to my useeffect question? as soon as I remove it it returns the correct logs.

The are expected to return undefined everytime the page loads. Since these variables are state variables you can use useEffect to log them correctly.

example:

useEffect(()=>{
if (AppT){
  console.log("Approved ", AppT)
  }
 },[AppT])

This code will return the logs every time there is a change in AppT variable.

1 Like

Thank you. The execution reverted is still confusing me… as far as I am concerned all erc20 contracts contain the “allowance” function and even in the case that they didn’t my console.logs reflect that every token in the wallet in question is not approved to the contract in question and in that light I cannot figure out what is causing the execution reverted errorr

Maybe the contract address that you are looking at it not a standard contract?

Try adding some console logs to check which contract address is failing.

The console.logs reflect that they all return 0 allowance

The useeffect solution for the console.log returns the too many rerenders error

Can you share your current code?

Thank you John for the help

This might be the cause of too many render errors. This part of code will run eveyrtime there is a state change in the component.

Thank you.

I restructured my code as is below. However for some reason the function play is not running on the first render (console.log(date) returns undefinded) despite all the parameters coming in correctly on the console.logs above await play(). When I refresh the page it rerenders and runs.

const { fetch: ball, data: balances } = useEvmWalletTokenBalances()
  const { fetch: play, data: date } = useEvmRunContractFunction()
  const allowAbi = [{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]

  
  useEffect(()=>{
    if(addy){
      ball({address: addy, chain:"5"})
    }
    console.log(balances)
  },[addy])

  
     
      for (let i = 0; i < balencia?.length; i++) {
      console.log(balencia[i]?.token?.contractAddress)
      console.log(addy)
      console.log(contractAd)
       play({
          abi: allowAbi,
          address: balencia[i]?.token?.contractAddress,
          functionName: "allowance",
          params: {
            owner: addy,
            spender: contractAd,
          },
          chain: "5"
        })
       
        console.log(date)

        if (date > 0) {
          appT.push(balencia[i])
        }
        else if (date == 0) {
          notAppT.push(balencia[i])
        }
    
      }

      console.log("Approved ", appT)
      console.log("Not Approved ", notAppT)
      if(appT?.length>0 || notAppT?.length > 0){
        tokenCal(appT, notAppT)
      }
  }

This part of the code should be in a different useEffect.:sweat_smile:

The data returned from useEvmRunContractFunction is a state variable. So it gets update after the plan function runs successfully.
so it is expected to get undefined for console.log(date).

try like this

  useEffect(()=>{
    console.log(date)

        if (date > 0) {
          appT.push(balencia[i])
        }
        else if (date == 0) {
          notAppT.push(balencia[i])
        }
  },[date])

Thank you for the assistance!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.