Actually i was wondering how to retrive the price of a low cap token from pancakeswap without having to use sites like poocoin/dextools directly.

I understood this,

  1. U can use the pancakeswap api here, but it doesn't work for every token
  2. U can use directly the token pool contract on bsc to retrive the price ( in this example assuming that the pool is BNB/Your token )

I'm going for the second way becouse it works for all the tokens, this is what I did:

  • I red the main pancakeswap contract ( 0x10ED43C718714eb63d5aA57B78B54704E256024E ) and found this function used to calculate the price of tokens in output
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
        require(amountIn > 0, 'PancakeLibrary: INSUFFICIENT_INPUT_AMOUNT');
        require(reserveIn > 0 && reserveOut > 0, 'PancakeLibrary: INSUFFICIENT_LIQUIDITY');
        uint amountInWithFee = amountIn.mul(9975);
        uint numerator = amountInWithFee.mul(reserveOut);
        uint denominator = reserveIn.mul(10000).add(amountInWithFee);
        amountOut = numerator / denominator;
  • I converted the upper function in a javascript normal function to use it directly.
function calcPriceInBnb( amountIn, reserveIn, reserveOut ){
    let amountInWithFee = amountIn *  9975 ;
    let numerator = amountInWithFee * reserveOut ;
    let denominator = ( reserveIn * 10000 ) + amountInWithFee ;
    console.log( numerator, denominator)
    amountOut = numerator / denominator;
    return amountOut;
console.log( calcPriceInBnb (
        toBaseUnit(reserves_._reserve0, 18, web3.utils.BN)
    ) ) // usage example

  • now on the token pool (0x1d42d057b765177298735c8d9f36b8208449dbb3) I'm calling the function to retrive the pool reserves
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
  • Once that i have the reserves i call calcPriceInBnb(1, reserve1, reserve0) that returns the token price in BNB and from this i have only to convert the BNB value to USD.

There is only one problem that i'm finding, when i get the reseves from the pool i get a non floating number.

For example: I saw that dextools.io uses the same method to calculate the token's price, inspecting the requests on the website it uses json bojects with the following keys

            "name":"Wrapped BNB",

As you can see at the end of the object there are two fields reserve0=165371362282732.84 and reserve1=7691.065744229067 that are floating numbers, if used in the calcPriceInBnb this numbers returns the correct price of the token.

Let's assume that i query the bsc pool contract in the same moment, instead of that values i would get reserve0=16537136228273284, reserve1=7691065744229067 ( non float numbers ) and this bring me to wrong calc.

I thought that this had to do with the decimals of the tokens in the pool but didn't find correlations. Any idea?


I made a gist on github about it https://gist.github.com/Linch1/ede03999f483f2b1d5fcac9e8b312f2c

3 Answers 3


You do not need to go through this hassle, you can use getAmountsOut on the PancakeSwap router contract to make this a lot simpler. You provide a path&amount of the first token in the path to use, and the function returns the token quantity you would receive at that moment. You can then use this data for price calculation.

  • Why do PooCoin is giving me a different value for the token price?
    – Magno C
    Commented May 1, 2022 at 4:46

Here is an example how you can get price for any token on Uniswap, Pancakeswap, SpookySwap, WannaSwap and so on... most of them were forked from original Uniswap contracts and share the same interface.

import Web3 from "web3";
import { toWei, fromWei } from "web3-utils";

const abi = [
    name: "getAmountsOut",
    type: "function",
    inputs: [
        name: "amountIn",
        type: "uint256",
      { name: "path", type: "address[]" },
    outputs: [{ name: "amounts", type: "uint256[]" }],

const web3 = new Web3("<put_here_rpc_endpoint>");
const dexRouter = new web3.eth.Contract(

const getTokenExchangeRate = async (tokenA, tokenB) => {
  return (
    await dexRouter.methods.getAmountsOut(toWei("1"), [tokenA, tokenB]).call()

// usage
const price = await getTokenExchangeRate(<addressForTokenA>, <addressForTokenB>)
  • I have only one token address thru which I'm going to find price, so will you please give me what is the value of "addressForTokenB"
    – TejaDroid
    Commented Jan 23, 2023 at 10:22

Token Price from pancakeswap, solved on


  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Adam Boudj
    Commented Mar 28, 2023 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.