How to use get Price API from Clouds Function

Hi guys,
I am trying to fetch price using Moralis API from Clouds Function. I used code form the insctruction video of Philippe

Moralis.Cloud.define("getPrice", async (request) => {
  let url =
    "https://deep-index.moralis.io/api/token/ERC20/" +
    //request.params.address +
    // WBNB/BUSD pair: 0xe0e92035077c39594793e61802a350347c320cf2
    // From factory address: 0xb7926c0430afb07aa7defde6da862ae0bde767bc
    // ROuter Address: 0x9ac64cc6e4415144c455bd8e4837fea55603e5c3
    "0xe0e92035077c39594793e61802a350347c320cf2" +
    "/price";
  logger.info(url);
  return Moralis.Cloud.httpRequest({
    url: url,
    params: { chain: "bsc", chain_name: "testnet" },
    headers: {
      accept: "application/json",
      "x-API-Key":
        "Kr42.....uJVq",
    },
  }).then(
    function (httpResponse) {
      return httpResponse.data;
    },
    function (httpResponse) {
      logger.info("error");
      logger.info(JSON.stringify(httpResponse));
    }
  );
});

Clouds function returned error:

{“status”:500,“headers”:{“date”:“Tue, 24 Aug 2021 08:31:45 GMT”,“content-type”:“application/json; charset=utf-8”,“content-length”:“2”,“connection”:“close”,“x-powered-by”:“Express”,“access-control-allow-origin”:"*",“etag”:“W/“2-vyGp6PvFo4RvsFtPoIWeCReyIC8"”,“cf-cache-status”:“DYNAMIC”,“expect-ct”:“max-age=604800, report-uri=“https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct””,“report-to”:”{“endpoints”:[{“url”:“https:\/\/a.nel.cloudflare.com\/report\/v3?s=UY2rmDZWDhUYUZGUL5J%2Br5YDs%2F5zCFkjv4JrC5QH60Ubhs7XLLkeeg7bf2VwN2rZmmcJdOEv1GVW%2F1ZJeOIuNrOgrt5cDotmhQqr7%2B60TxgamXx%2FuMrjSkBdujHE50UhS1tod%2B9duQ%3D%3D”}],“group”:“cf-nel”,“max_age”:604800}",“nel”:"{“success_fraction”:0,“report_to”:“cf-nel”,“max_age”:604800}",“server”:“cloudflare”,“cf-ray”:“683b39e44916208f-NRT”},“buffer”:{“type”:“Buffer”,“data”:[123,125]},“text”:"{}",“data”:{}}

Thanks for help!

Hi,
That function works fine for me, after I use an address from the right blockchain.

Hi @cryptokid
What is the right address for BSC Testnet?

I don’t know, I tested with main net:

Moralis.Cloud.define("getPrice3", async (request) => {
  let url =
    "https://deep-index.moralis.io/api/token/ERC20/" +
    //request.params.address +
    // WBNB/BUSD pair: 0xe0e92035077c39594793e61802a350347c320cf2
    // From factory address: 0xb7926c0430afb07aa7defde6da862ae0bde767bc
    // ROuter Address: 0x9ac64cc6e4415144c455bd8e4837fea55603e5c3
    //"0xe0e92035077c39594793e61802a350347c320cf2" +
      "0x80E15FE54e9D155f8366187A6a32BDEF9C2366c4" +
    "/price";
  logger.info(url);
  return Moralis.Cloud.httpRequest({
    url: url,
    params: { chain: "bsc", chain_name: "mainnet" },
    headers: {
      accept: "application/json",
      "x-API-Key":
        "API_KEY",
    },
  }).then(
    function (httpResponse) {
      return httpResponse.data;
    },
    function (httpResponse) {
      logger.info("error");
      logger.info(JSON.stringify(httpResponse));
    }
  );
});

I gess the problem is we dont know the default Address of Testnet Pancakeswap Factory that Moralis use. Thus we can not get the pair address from that Factory…
The unknow pair address caused the error (?)
Could some one pls give a hand on this?

does the function that I pasted (with mainnet) work for you?

Yes, it works. The code is correct. Problem is Moralis is using different Router and Factory address for testnet Pancakeswap… we should find those addresses…
Or find the way to attach Router address each time we fetch api…

Hey @tungtien

I’m not sure if the correct prices will be shown on the testnet

Also take a look at https://twitter.com/PancakeSwap/status/1369547285160370182

You can specify a name or an address of the token exchange

Hi @Yomoo,
I tried, got the pair address from the Factory address you provided ( https://twitter.com/PancakeSwap/status/1369547285160370182 ) , but it don’t work with my code either.

I tried using UI to get Price from BSC testnet:

Am I missing something?

I’m not sure if the correct prices will be shown on the testnet

If you want to fetch real prices for real assets you need to use mainnet Price Api. For example you can create a JSON file with mainnent address and testnet.

Or you trying to use it for your test token?

I am testing my code for Pancakeswap, but in development stage, I would want to use Testnet, with a version of Pancake swap on testnet… For swapping operations on Pancake testnet, I will need pancake testnet realtime prices…
It doesn’t matter that the price may different with real mainnet price (and ofcourse I think it should be differ), I need it only be functional…
Ps: if Pancakeswap Testnet is not ready yet on Moralis, maybe I will try using Uniswap on Testnet instead… :thinking:?

Have you already created pancake pair contract and provided liquidity?

For the router you provided I did not add liquidity… May be I should ?
But for the last router on testnet I used (0x9ac64cc6e4415144c455bd8e4837fea55603e5c3), there is always enough liquidity for WBNB/BUSD so I didn’t had to mind about that…

Hi @tungtien

Have you managed to solve the problem?

No @Yomoo,
I did not had time today to work on the code. My plan is to

  1. Check the liquidity of that pancake testnet… but I think that is not the cause.
  2. Try goaround the problem by replacing Bsc testnet with Eth testnet…
  3. Do you have other recomendation?

Hey @tungtien

I myself only recently found out that the Price API does not work on test chains. Due to the lack of correct operation of the smart contracts themselves. Many exchanges do not have correctly working test exchange smart contracts.

If you want to get token price on testnet you can use web3 methods or use immediately mainnets

Hi @Yomoo,
I have check the BSC testnet Pancake address that you suggest, I also check the liquidity for WBNB/BUSD pair. Liquidity exist and we can fetch the AmountsOut by reading Contract on BSCscan on testnet

token0 = "0xae13d989dac2f0debff460ac112a837c89baa7cd" //WBNB on Bsc testnet 
token1 = "0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7" //BUSD on BSC testnet
ROUTER_ADDR = "0xd99d1c33f9fc3444f8101754abc46c52416550d1"; //from Yomoo
FACTORY_ADDR = "0x6725f303b657a9451d8ba641348b6761a6cc7a17"; // from Yomoo
PairAddress = "0x85EcDcdd01EbE0BfD0Aba74B81Ca6d7F4A53582b"

I have check, there is pair liquidity and we can fetch the amountsOut from the router fot the pair.

(https://testnet.bscscan.com/address/0x85EcDcdd01EbE0BfD0Aba74B81Ca6d7F4A53582b)

My cloud function code is (this code works well for “bsc”+“mainnet”):

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

  let url = "https://deep-index.moralis.io/api/token/ERC20/" + "0x85ecdcdd01ebe0bfd0aba74b81ca6d7f4a53582b" + "/price";

  logger.info(url);

  return Moralis.Cloud.httpRequest({

    url: url,

    params: { chain: "bsc", chain_name: "testnet" },

    headers: {

      accept: "application/json",

      "x-API-Key":    "Kr42...uJVq",

    },

  }).then(

    function (httpResponse) {

      return httpResponse.data;

    },

    function (httpResponse) {

      logger.info("error");

      logger.info(JSON.stringify(httpResponse));

      return httpResponse;

    }

  );

});

Error returned:

  1. 2021-08-26T10:43:32.305Z - {“status”:404,“headers”:{“date”:“Thu, 26 Aug 2021 10:43:32 GMT”,“content-type”:“text/html; charset=utf-8”,“transfer-encoding”:“chunked”,“connection”:“close”,“vary”:“Accept-Encoding”,“x-powered-by”:“Express”,“access-control-allow-origin”:"*",“content-security-policy”:“default-src ‘none’”,“x-content-type-options”:“nosniff”,“cf-cache-status”:“DYNAMIC”,“expect-ct”:“max-age=604800, report-uri=“https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct””,“report-to”:"{“endpoints”:[{“url”:“https:\/\/a.nel.cloudflare.com\/report\/v3?s=9FLDLxLRkmj1wDTmjn1z%2BRfijg3MJOZUAExx5rSlXNtPnPsBljqUCbh0%2FmrIAz9%2BKfeqPo%2BfgC7qB%2BzGPvEjX21cmHRDVKMyteW8g%2B0dXe612Nhx8EyzFcXBiAc5VHyJnMJ214LCsQ%3D%3D”}],“group”:“cf-nel”,“max_age”:604800}",“nel”:"{“success_fraction”:0,“report_to”:“cf-nel”,“max_age”:604800}",“server”:“cloudflare”,“cf-ray”:“684c75ad6c681fae-NRT”},“buffer”:{“type”:“Buffer”,“data”:[60,33,68,79,67,84,89,80,69,32,104,116,109,108,62,10,60,104,116,109,108,32,108,97,110,103,61,34,101,110,34,62,10,60,104,101,97,100,62,10,60,109,101,116,97,32,99,104,97,114,115,101,116,61,34,117,116,102,45,56,34,62,10,60,116,105,116,108,101,62,69,114,114,111,114,60,47,116,105,116,108,101,62,10,60,47,104,101,97,100,62,10,60,98,111,100,121,62,10,60,112,114,101,62,67,97,110,110,111,116,32,71,69,84,32,47,97,112,105,47,116,111,107,101,110,47,69,82,67,50,48,47,48,120,56,53,101,99,100,99,100,100,48,49,101,98,101,48,98,102,100,48,97,98,97,55,52,98,56,49,99,97,54,100,55,102,52,97,53,51,53,56,50,98,60,47,112,114,101,62,10,60,47,98,111,100,121,62,10,60,47,104,116,109,108,62,10]},“text”:"\n<html lang=“en”>\n\n<meta charset=“utf-8”>\nError\n\n\n
    Cannot GET /api/token/ERC20/0x85ecdcdd01ebe0bfd0aba74b81ca6d7f4a53582b
    \n\n\n"}

Unfortunately, currently Price api is avalable only for Mainnets.

As I said before, for getting prices on testnets you need to interact with the smart contract using web3 methods:

Get price: Token/WBNB + WBNB/USDC => Token/USDC

Now it is impossible to implement getting prices with the Moralis API due to the fact that there are no official stablecoins and exchanges in test networks.

Thanks @Yomoo. That is the answer I need.
Please keep me updated when the Moralis API for testnet is ready.

2 Likes