getTokenPrice Error on Cloud

Welcome, its me again ^^

short little question.

i am creating a Cloud Job to update the Database with historical prices and its working so there is no problem BUT once there is an error from the getTokenPrice function, the job just stops. The thing is, it does not seem i can catch it anyhow so i could say like “if error xy…continue”

the error i get is “No pools found with enough liquidity, to calculate the price” wich obviously is because there is no LP pool. but how can i fetch this error in my function to let the function set the price to 0 instead of killing the whole job?

thats the current function i use:

  async function getHistoricalPrices(database,jdata)
  {
    const logger = Moralis.Cloud.getLogger();
    logger.info("Started Function");
    for(transfer of jdata)
    {
        logger.info(JSON.stringify(transfer));

        const options = {address: transfer.get("token_address"),chain: "bsc",to_block: transfer.get("block_number"),};
        let usdprice = 0;
        price = await Moralis.Web3API.token.getTokenPrice(options);
        logger.info("################## "+JSON.stringify(price));
        if (typeof price.usdPrice !== 'undefined') {
            usdPrice = price.usdPrice;
          }
          else
          {
            usdPrice = 0;
          }

        logger.info("Starting saving in Database "+database);
        const BscTransfersx = Moralis.Object.extend(database);
        const queryx = new Moralis.Query(BscTransfersx);
        queryx.equalTo("objectId", transfer.get("objectId"));
        const resultx = await queryx.first({useMasterKey:true});
        resultx.set("usdval",usdPrice);
        await resultx.save(null, {useMasterKey:true});


    }
    
  }

i tried with try catch and everything but does not work … also i assumed there could something be like price.message (like it is if i use the Web3API with curl on PHP) but also thats not existing… so it seems i am missing something. i also did JSON.stringify(price) to see if there is any output… but the logger does not even trigger, because it directly fails on the getTokenPrice function itself.

So just to summarize: How can i fetch that error so i can continue the loop, instead of killing the whole job.

Thank you for the help :slight_smile:

I can test later to see what happens

1 Like

thank you very much for that support

It seems that this works in a cloud function:

Moralis.Cloud.define("test_price", async (request) => {
 y = 0;
 try{
  y = await Moralis.Web3API.token.getTokenPrice({address: "0xe9e7cea3dedca5984780bafc599bd69add087d55", chain: "bsc"});
  logger.info("test_price price result " + JSON.stringify(y))
 }
catch(err)
 {
    logger.info("error " + JSON.stringify(err))
 }
  return y
  
})

it doesn’t print the error, but it is able to catch it

perfect, thats working like a charm!
last but not least, is there a limit on queries? i know i have around 1700 historical prices in the database, but if i make

await query.find({useMasterKey:true});

that stuff , and i do a count on it, its exaclty 100. do i have to change the query to get all of the database entries?

greetings

you can use:

query.limit(10000)
await query.find({useMasterKey:true});

with a lot of data you should use pagination

1 Like

yep thats what i wanted to use. i just watching the yt video, so i know how to paginate here.

thx for the help !