Skip to content

Commit

Permalink
Added more decimals in love game
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimosgr committed Jun 8, 2023
1 parent ab57ca4 commit ebaea58
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/static/js/love.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function loadLoveChefContract(App, chef, chefAddress, chefAbi, rewardToken
let aprs = []
for (let i = 0; i < poolCount; i++) {
if (poolPrices[i]) {
const apr = printChefPool(App, chefAbi, chefAddress, prices, tokens, poolInfos[i], i, poolPrices[i],
const apr = printLoveChefPool(App, chefAbi, chefAddress, prices, tokens, poolInfos[i], i, poolPrices[i],
totalAllocPoints, rewardsPerWeek, rewardTokenTicker, rewardTokenAddress,
pendingRewardsFunction, null, null, "eth", poolInfos[i].depositFee, poolInfos[i].withdrawFee)
aprs.push(apr);
Expand Down Expand Up @@ -106,6 +106,60 @@ async function loadLoveChefContract(App, chef, chefAddress, chefAbi, rewardToken
return { prices, totalUserStaked, totalStaked, averageApr }
}

function printLoveChefPool(App, chefAbi, chefAddr, prices, tokens, poolInfo, poolIndex, poolPrices,
totalAllocPoints, rewardsPerWeek, rewardTokenTicker, rewardTokenAddress,
pendingRewardsFunction, fixedDecimals, claimFunction, chain="eth", depositFee=0, withdrawFee=0) {
fixedDecimals = fixedDecimals ?? 2;
const sp = (poolInfo.stakedToken == null) ? null : getPoolPrices(tokens, prices, poolInfo.stakedToken, chain);
var poolRewardsPerWeek = poolInfo.allocPoints / totalAllocPoints * rewardsPerWeek;
if (poolRewardsPerWeek == 0 && rewardsPerWeek != 0) return;
const userStaked = poolInfo.userLPStaked ?? poolInfo.userStaked;
const rewardPrice = getParameterCaseInsensitive(prices, rewardTokenAddress)?.usd;
const staked_tvl = sp?.staked_tvl ?? poolPrices.staked_tvl;
_print_inline(`${poolIndex} - `);
poolPrices.print_price(chain);
sp?.print_price(chain);
const apr = printLoveAPR(rewardTokenTicker, rewardPrice, poolRewardsPerWeek, poolPrices.stakeTokenTicker,
staked_tvl, userStaked, poolPrices.price, fixedDecimals);
if (poolInfo.userLPStaked > 0) sp?.print_contained_price(userStaked);
if (poolInfo.userStaked > 0) poolPrices.print_contained_price(userStaked);
printChefContractLinks(App, chefAbi, chefAddr, poolIndex, poolInfo.address, pendingRewardsFunction,
rewardTokenTicker, poolPrices.stakeTokenTicker, poolInfo.poolToken.unstaked,
poolInfo.userStaked, poolInfo.pendingRewardTokens, fixedDecimals, claimFunction, rewardPrice, chain, depositFee, withdrawFee);
return apr;
}

function printLoveAPR(rewardTokenTicker, rewardPrice, poolRewardsPerWeek,
stakeTokenTicker, staked_tvl, userStaked, poolTokenPrice,
fixedDecimals) {
var usdPerWeek = poolRewardsPerWeek * rewardPrice;
fixedDecimals = fixedDecimals ?? 2;
_print(`${rewardTokenTicker} Per Week: ${poolRewardsPerWeek.toFixed(fixedDecimals)} ($${formatMoney(usdPerWeek)})`);
var weeklyAPR = usdPerWeek / staked_tvl * 100;
var dailyAPR = weeklyAPR / 7;
var yearlyAPR = weeklyAPR * 52;
_print(`APR: Day ${dailyAPR.toFixed(2)}% Week ${weeklyAPR.toFixed(2)}% Year ${yearlyAPR.toFixed(2)}%`);
var userStakedUsd = userStaked * poolTokenPrice;
var userStakedPct = userStakedUsd / staked_tvl * 100;
_print(`You are staking ${userStaked.toFixed(6)} ${stakeTokenTicker} ($${formatMoney(userStakedUsd)}), ${userStakedPct.toFixed(2)}% of the pool.`);
var userWeeklyRewards = userStakedPct * poolRewardsPerWeek / 100;
var userDailyRewards = userWeeklyRewards / 7;
var userYearlyRewards = userWeeklyRewards * 52;
if (userStaked > 0) {
_print(`Estimated ${rewardTokenTicker} earnings:`
+ ` Day ${userDailyRewards.toFixed(fixedDecimals)} ($${formatMoney(userDailyRewards*rewardPrice)})`
+ ` Week ${userWeeklyRewards.toFixed(fixedDecimals)} ($${formatMoney(userWeeklyRewards*rewardPrice)})`
+ ` Year ${userYearlyRewards.toFixed(fixedDecimals)} ($${formatMoney(userYearlyRewards*rewardPrice)})`);
}
return {
userStakedUsd,
totalStakedUsd : staked_tvl,
userStakedPct,
yearlyAPR,
userYearlyUsd : userYearlyRewards * rewardPrice
}
}

async function getLovePoolInfo(app, chefContract, chefAddress, poolIndex, pendingRewardsFunction, showAll=false) {
const poolInfo = await chefContract.poolInfo(poolIndex);
if (poolInfo.allocPoint == 0 && !showAll) {
Expand Down

0 comments on commit ebaea58

Please sign in to comment.