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


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.

  • 1
    ok thanks that make sense!
    – Mat.C
    May 26 '21 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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