'Calculating liquidity pair price

Iam trying to calculate the price of liquidity pairs but iam stuck on a few of them. For instance the following one: https://bscscan.com/address/0x7f1b11a798273da438b4b132df1383d8387e73b4

Iam calculating the price based on the ratio of the two reserves, ie reserveA / reserveB. For this contract, the output will be (at the time of writting) 3135836607346091695011 / 219578826733506017523963 = 0.01428114292 BNB but the actual price is 0.00000000001427 BNB. What iam missing here?



Solution 1:[1]

You're missing the decimals. One coin has 9 decimal places, WBNB has 18. Data in contracts are integer. So in this case it should be 3135836607346091695011 / (219578826733506017523963 * 10^9).

You're doing power of 9 because that's the difference between decimals of coin a and coin b.

Also you can just convert the integer value from contract to real decimal value before calculating it, for both coins. You do that by dividing each coin's reserve amount by (10 to the power of number of digits).

(3135836607346091695011 / 10^18) / (219578826733506017523963 / 10^9).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Slawek