0

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 (
        1,
        parseFloat(reserves_._reserve1),
        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

{
   "data":{
      "pair":{
         "token0":{
            "_id":"6096efc237e2488af0c1ea10",
            "id":"0x4a824ee819955a7d769e03fe36f9e0c3bd3aa60b",
            "name":"Kabosu",
            "symbol":"KABOSU",
            "decimals":9
         },
         "token1":{
            "_id":"6096efc237e2488af0c1ea11",
            "id":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
            "name":"Wrapped BNB",
            "symbol":"WBNB",
            "decimals":18
         },
         "info":{
            "address":"0x4a824ee819955a7d769e03fe36f9e0c3bd3aa60b",
            "holders":121190
         },
         "_id":"6096efc237e2488af0c1ea0f",
         "id":"0xa17d2c8b629095672633828d744a03608bbaae24",
         "createdAtTimestamp":1620504512,
         "exchange":"pancakev2",
         "type":"standard-pair",
         "tokenIndex":0,
         "__v":0,
         "initialReserve0":365615229923141.8,
         "initialReserve1":208.675,
         "creation":{
            "blockNumber":"7250792",
            "timeStamp":"1620504512",
            "hash":"0x4d1e5b7082d0a92885cad5a7047b2693c7e2790be0c591a622d602ef7318b3be",
            "nonce":"0",
            "blockHash":"0x7bdf8d219600934504103326070861fd3c99fefcbc9535592e042dd369686996",
            "transactionIndex":"213",
            "from":"0xb56fd23449d66a10251e7efa778c944c4ba96666",
            "to":"",
            "value":"0",
            "gas":"5956167",
            "gasPrice":"5000000000",
            "isError":"0",
            "txreceipt_status":"1",
            "input":"",
            "contractAddress":"0x4a824ee819955a7d769e03fe36f9e0c3bd3aa60b",
            "cumulativeGasUsed":"32279124",
            "gasUsed":"5956167",
            "confirmations":"235853"
         },
         "reserve0":165371362282732.84,
         "reserve1":7691.065744229067,
         "reserveUpdatedAt":1622012556573,
         "txCount":244654
      }
   }
}

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?

1

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
  • 1
    ok thanks that make sense!
    – Mat.C
    May 26 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.