Skip to content

Commit

Permalink
fix: aPY calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
hapetherw committed May 26, 2024
1 parent 648cf6a commit f76d512
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/lib/getAPYCalculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ import { OneMonth, StakeDurationMultipliers, MULTIPLIER_BASIS } from "@/constant
import { parseUnits } from "viem";

export const getAPYCalculation = (
rewardRate: number,
rewardRatePerSec: number,
totalWeightedStake: number,
stakeDurationIndex: number,
) => {
// -------- APR and APY Formula ----------- //

const OneToken = Number(parseUnits("1", 18));
const OneYear = 12 * OneMonth;
if (totalWeightedStake == 0) {
totalWeightedStake = OneToken;
}

const rewardRatePerSecPerToken = (rewardRate * OneToken) / totalWeightedStake;
const rewardRatePerYearPerToken = rewardRatePerSecPerToken * OneYear;
const rewardRatePerYear = rewardRatePerSec * OneYear;
const multiplier = StakeDurationMultipliers[stakeDurationIndex];
const myStakeAmount = (OneToken * multiplier) / MULTIPLIER_BASIS;
if (totalWeightedStake == 0) {
totalWeightedStake = myStakeAmount;
}
const myPortion = myStakeAmount / totalWeightedStake;
const APY = rewardRatePerYearPerToken * myPortion * 100;
const rewardsAmountPerYear = rewardRatePerYear * myPortion;
const APY = (rewardsAmountPerYear / OneToken) * 100;
return APY.toFixed(2);
};

0 comments on commit f76d512

Please sign in to comment.