[SOLVED] Fetching BSC tokenPrices in the json file

Hello everyone, After successfully fetching the BSC token prices for both tokenOne and tokenTwo, but my problem is how will I fetch all the tokenprices listed in the bsctokenlist.json. I tried to enable switching tokens in the swap box to ensure that the token prices update accordingly. Unfortunately, only the prices for tokenOne and tokenTwo are displayed, and I am seeking a solution to resolve this issue and display tokenprices for all bsc tokens in my created json file. How can I address this problem?

app.get("/tokenPrice", async (req, res) => {

  const {query} = req;

  ({

    apiKey: ""

  });

const responseOne = await Moralis.EvmApi.token.getTokenPrice({

  "chain": "0x38",

  "exchange": "pancakeswapv2",

  "address": "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"

  });

const responseTwo = await Moralis.EvmApi.token.getTokenPrice({

  "chain": "0x38",

  "exchange": "pancakeswapv2",

  "address": "0x2170Ed0880ac9A755fd29B2688956BD959F933F8"

});

const usdPrices = {

  tokenOne: responseOne.raw.usdPrice,

  tokenTwo: responseTwo.raw.usdPrice,

  ratio: responseOne.raw.usdPrice/responseTwo.raw.usdPrice

};

  return res.status(200).json({usdPrices});

});

Hi @idg

If you want to fetch the token prices of tokens in your json file, you will have to pass those addresses to tokenOne and tokenTwo variables whenever you select or change the token in the UI.

It looks like it was already implemented in the dex tutorial. Can you check this once?

Despite following the tutorial, I am consistenly getting only tokenOne and tokenTwo prices. I have attached the screenshot demonstrating that even after modifying the addresses in the localhost:3001, im still getting the same result.

Ahh I see you have an hardcoded address here in the backend code.

Did you update this to data received from frontend?

Here is the code as per the GitHub final code where it uses the address received from the frontend.

I utilized the code provided by Moralis on github, but encounters difficulties when interacting with other tokens on the BSC network. It seems that entering the amount is not functioning as expected for the other tokens on my json file. I’ve included screenshots of changing the tokens in the swap and the relevant code section that deals with fetching token prices from Moralis:6 5

I would greatly appreciate any insights, guidance, or solutions to address this problem and ensure successful fetching of token prices on the BSC network.

app.get("/tokenPrice", async (req, res) => {

const {query} = req;

({

  apiKey: ""

});

const responseOne = await Moralis.EvmApi.token.getTokenPrice({

address: query.addressOne,

"chain": "0x38",

"exchange": "pancakeswapv2",

"address": "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"

});

const responseTwo = await Moralis.EvmApi.token.getTokenPrice({

address: query.addressTwo,

"chain": "0x38",

"exchange": "pancakeswapv2",

"address": "0xe9e7cea3dedca5984780bafc599bd69add087d56"

})

const usdPrices = {

tokenOne: responseOne.raw.usdPrice,

tokenTwo: responseTwo.raw.usdPrice,

ratio: responseOne.raw.usdPrice/responseTwo.raw.usdPrice

}

return res.status(200).json(usdPrices);

});

Its hard tell where the issue is. Can you try adding some console.logs on frontend code as well as in backend to check if the data processed and passed from frontend to backend is correct?

Solved, thank you very much :blush:

1 Like

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