Last active
May 22, 2021 11:38
-
-
Save bhavya2611/92ae2add5f40db240f5f17b60e494978 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const calculateLpTokenPrice = async () => { | |
let rewardTokenPrice = 0; | |
// For Price IN ETH | |
// Reward Token is Dodgecoin in our case | |
rewardTokenPrice = await getDogecoinPriceInETH(); | |
// For Price in BNB | |
// If you want to do calculations in BNB uncomment line below and comment line number 5 | |
// rewardTokenPrice = await getDodgecoinPriceInBNB() | |
// 1 * rewardTokenPrice because 1 is the price of ETH or BNB in respective mainnet | |
// This is square root of (p0 * p1) with reference to the image above | |
const tokenPriceCumulative = new BigNumber(1 * rewardTokenPrice).sqrt(); | |
// For ETH / DOGE pair totalReserve[0] = ETH in the contract and totalReserve[1] = DOGE in the contract | |
// For BNB / DOGE pair totalReserve[0] = BNB in the contract and totalReserve[1] = DOGE in the contract | |
const totalReserve = await getLpTokenReserves(); | |
// This is square root of (r0 * r1) with reference to the image above | |
const tokenReserveCumulative = new BigNumber(totalReserve[0]) | |
.times(totalReserve[1]) | |
.sqrt(); | |
// Total Supply of LP Tokens in the Market | |
const totalSupply = await getLpTokenTotalSupply(); | |
// Calculate LP Token Price in accordance to the image above | |
const lpTokenPrice = tokenReserveCumulative | |
.times(tokenPriceCumulative) | |
.times(2) | |
.div(totalSupply); | |
// If lpTokenPrice is a valid number return lpTokenPrice or return 0 | |
return lpTokenPrice.isNaN() || !lpTokenPrice.isFinite() | |
? 0 | |
: lpTokenPrice.toNumber(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment