-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
YLDYCalculation.js
79 lines (66 loc) · 3 KB
/
YLDYCalculation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Global Staking Shares = ((Current Block Timestamp – Global Time) / Time Period) * Global Amount.
export function calculateGlobalStakingShares (currentBlockTimestamp, globalTime, timePeriodUnix, globalAmount) {
let time = currentBlockTimestamp - globalTime;
let afterTimePeriod = time / timePeriodUnix;
let globalShares = afterTimePeriod * globalAmount;
return globalShares;
}
// Calculates the user staking shares
// User Staking Shares = ((Current Block Timestamp – User Time) / Time Period) * User Amount
export function calculateUserStakingShares(currentBlockTimestamp, userTime, timePeriodUnix, userAmount) {
let time = currentBlockTimestamp - userTime;
let period = time / timePeriodUnix;
let usrShares = period * userAmount;
return usrShares;
}
// Calculates the total rewards a user can claim
// Rewards Claimable For User = (User Staking Shares / Global Staking Shares) * Total Reward Unlocked
export function calculateClaimableUserRewards(usrStakingShares, globalStakingShares, totalUnlockedRewards) {
return (usrStakingShares / globalStakingShares) * totalUnlockedRewards;
}
/// Calculates the current rewards available to the user from the current variables from user and contract
export function calculateYLDYRewards (usrStakingShares, usrTime, globalTime, usrAmount, globalStakingShares, totalYldyUnlockRewards) {
// Determine day period inbetween global and user time
let day = 86400;
let daysDifference = (globalTime - usrTime) / day;
// Get reward amount from remainder of formula
return calculateYLDYRewardsFromDayPeriod(usrStakingShares, daysDifference, usrAmount, globalStakingShares, totalYldyUnlockRewards);
}
// Calculates the yldy rewards from the given properties.
// Formula: Claimable YLDY Rewards = ((USS + ((GT-UT) / 86400) * UA) / GSS) * TYUL / 1000000
// usrStakingShares (USS)
// daysPeriod - Amount of days that the user hasn't claimed
// usrAmount (UA)
// globalStakingShares (GSS)
// totalYldyUnlockRewards (TYDL)
export function calculateYLDYRewardsFromDayPeriod(usrStakingShares, daysPeriod, usrAmount, globalStakingShares, totalYldyUnlockRewards) {
if (globalStakingShares <= 0) {
return 0; // no shares, no share
}
let microUsrAmount = usrStakingShares + (daysPeriod * usrAmount);
let amount = (microUsrAmount / globalStakingShares) * totalYldyUnlockRewards;
if (isNaN(amount)) {
return 0;
} else {
return amount;
}
}
// Calculates how much share of the current rewards pool the user has
// Returned value as a percentage
export function calculateRewardsPoolPercentageShare(totalRewards, userShare) {
if (userShare && totalRewards) {
return (userShare / totalRewards) * 100;
}
return 0;
}
// Calculates the average of all the given values and returns it
export function calculateAverage(values) {
if (!values) {
return null;
}
let total = 0;
for(let i = 0; i < values.length; i++) {
total += parseInt(values[i]);
}
return total / values.length;
}