Skip to content

Commit

Permalink
refactor yield calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
SidharthK2 committed Oct 29, 2024
1 parent 2d9770d commit 000b828
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
34 changes: 10 additions & 24 deletions src/data/LockConstants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,22 @@ export const EP_COINGECKO_BASE_URL = 'https://api.coingecko.com/api/v3/simple'
export const IQ_TOKEN_HOLDER = `https://api.ethplorer.io/getTopTokenHolders/0x1bF5457eCAa14Ff63CC89EFd560E251e814E16Ba?apiKey=${config.ethplorerApiKey}&limit=100`
export const ETHERSCAN_TOKEN_TRANSACTION_API = `https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=0&toBlock=latest&address=${config.hiiqAddress}&apikey=${config.etherScanApiKey}`
export const NORMALIZE_VALUE = 10e17
const REWARDS_FOR_THE_FIRST_YEAR = 547500000 // 1.5M per day for 365 days

export const getTotalIQMintedPerYear = (year = 0): number => {
const newModelStartDate = new Date('November 1, 2023')
const currentDate = new Date()
currentDate.setFullYear(currentDate.getFullYear() + year)
const diffInMiliseconds = currentDate.getTime() - newModelStartDate.getTime()
const yearsOfDifference = Math.abs(
new Date(diffInMiliseconds).getUTCFullYear() - 1970,
)
if (yearsOfDifference === 0) return REWARDS_FOR_THE_FIRST_YEAR
return REWARDS_FOR_THE_FIRST_YEAR / 2 ** yearsOfDifference // halving model suggests that every year, the reward halfs its initial amount... i.e (1/ 2^n) where n is the number of yrs
// New emission constants
export const DAILY_EMISSION = 3_000_000 // 3M IQ per day
export const YEARLY_EMISSION = DAILY_EMISSION * 365
export const WEEKLY_EMISSION = DAILY_EMISSION * 7

export const calculateEstimatedYieldPerWeek = () => {
return WEEKLY_EMISSION // 21M IQ per week (3M * 7)
}

export const calculateUserPoolRewardOverTheYear = (
years: number,
userTotalHiiq: number,
totalHIIQ: number,
) => {
let totalPoolReward = 0
for (let i = 0; i < years; i += 1) {
const totalIQMintedEachYear = getTotalIQMintedPerYear(i)
const userPoolRationForTheYear =
(userTotalHiiq / (totalHIIQ + userTotalHiiq)) * totalIQMintedEachYear
totalPoolReward += userPoolRationForTheYear
}
return totalPoolReward
}

export const calculateEstimatedYieldPerWeek = () => {
const EMMITED_IQ_PER_DAY = 1_500_000
return EMMITED_IQ_PER_DAY * 7
if (!userTotalHiiq || !totalHIIQ) return 0
const userPoolShare = userTotalHiiq / (totalHIIQ + userTotalHiiq)
return YEARLY_EMISSION * userPoolShare * years
}
1 change: 1 addition & 0 deletions src/hooks/useLockOverview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const useLockOverview = (userAddress?: string) => {
}

const getUserTotalIQLocked = () => {
console.log('totalLockedIq', totalLockedIq)
if (totalLockedIq) {
const amount = totalLockedIq[0]
return Number(formatEther(amount))
Expand Down
40 changes: 26 additions & 14 deletions src/utils/LockOverviewUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import hiIQABI from '@/abis/hiIQABI.abi'
import {
YEARS_LOCK,
calculateUserPoolRewardOverTheYear,
IQ_TOKEN_HOLDER,
ETHERSCAN_TOKEN_TRANSACTION_API,
YEARLY_EMISSION,
} from '@/data/LockConstants'
import * as Humanize from 'humanize-plus'
import { parseEther, formatEther, fromHex } from 'viem'
Expand All @@ -13,29 +13,41 @@ export const calculateStakeReward = (
totalHiiq: number,
amountLocked: number,
years: number,
poolRewardCalculationYear: number,
) => {
const yearsLocked = years || YEARS_LOCK
const rewardsBasedOnLockPeriod =
amountLocked + amountLocked * 3 * (yearsLocked / 4)
const totalPoolRewardForTheLockYear = calculateUserPoolRewardOverTheYear(
poolRewardCalculationYear,
rewardsBasedOnLockPeriod,
totalHiiq,
)
return totalPoolRewardForTheLockYear

// Calculate base HiIQ (including lock multiplier)
const baseHiIQ = amountLocked + amountLocked * 3 * (yearsLocked / 4)

// Calculate pool share
const userPoolShare = baseHiIQ / (totalHiiq + baseHiIQ)

// Calculate total rewards over the period
const totalPoolReward = YEARLY_EMISSION * userPoolShare * yearsLocked

return baseHiIQ + totalPoolReward
}

export const calculateAPR = (
totalHiiq: number,
totalLockedIq: number,
years: number,
) => {
if (!totalLockedIq || !totalHiiq) return 0

const amountLocked = totalLockedIq > 0 ? totalLockedIq : 1000000
const stakeReward = calculateStakeReward(totalHiiq, amountLocked, years, 1)
const aprAcrossLockPeriod = stakeReward / amountLocked
const aprDividedByLockPeriod = aprAcrossLockPeriod * 100
return aprDividedByLockPeriod

// Calculate base HiIQ with lock multiplier
const baseHiIQ = amountLocked + amountLocked * 3 * (years / 4)

// Calculate share of the pool
const poolShare = baseHiIQ / (totalHiiq + baseHiIQ)

// Calculate yearly rewards
const yearlyRewards = YEARLY_EMISSION * poolShare

// Calculate APR
return (yearlyRewards / amountLocked) * 100
}

export const getNumberOfHiIQHolders = async () => {
Expand Down

0 comments on commit 000b828

Please sign in to comment.