-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathfarms.ts
59 lines (45 loc) · 2.02 KB
/
farms.ts
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
import { Asset } from '@wharfkit/antelope'
const PRECISION_MULTIPLIER = BigInt('1000000000000000000')
function getRewardPerToken(incentive) {
const lastTimeRewardApplicable = (periodFinish) => {
const currentTime = Math.floor(Date.now() / 1000)
return Math.min(currentTime, periodFinish)
}
if (BigInt(incentive.totalStakingWeight) === 0n) {
return BigInt(incentive.rewardPerTokenStored)
}
const periodFinish = BigInt(incentive.periodFinish)
const lastUpdateTime = BigInt(incentive.lastUpdateTime)
const rewardRateE18 = BigInt(incentive.rewardRateE18)
const totalStakingWeight = BigInt(incentive.totalStakingWeight)
const timeDifference = BigInt(lastTimeRewardApplicable(Number(periodFinish))) - lastUpdateTime
return BigInt(incentive.rewardPerTokenStored) + (timeDifference * rewardRateE18) / totalStakingWeight
}
export function calculateUserStake(plainUserStake) {
const userStake = { ...plainUserStake }
const {
incentive,
stakingWeight: stakingWeightStr,
userRewardPerTokenPaid: userRewardPerTokenPaidStr,
rewards: rewardsStr,
} = userStake
if (!stakingWeightStr) return null
const {
totalStakingWeight: totalStakingWeightStr,
reward: { quantity: rewardQuantity },
rewardPerDay,
isFinished,
} = incentive
const totalStakingWeight = BigInt(totalStakingWeightStr)
const stakingWeight = BigInt(stakingWeightStr)
const userRewardPerTokenPaid = BigInt(userRewardPerTokenPaidStr)
const rewards = BigInt(rewardsStr)
const rewardPerToken = getRewardPerToken(incentive)
const earnedAmount = stakingWeight * (rewardPerToken - userRewardPerTokenPaid)
const reward = earnedAmount / PRECISION_MULTIPLIER + rewards
userStake.farmedReward = Asset.fromUnits(reward.toString(), Asset.fromString(rewardQuantity).symbol)
const userSharePercent = (stakingWeight * 100n * 1000n) / totalStakingWeight
userStake.userSharePercent = Number(userSharePercent) / 1000
userStake.dailyRewards = isFinished ? 0 : (rewardPerDay * userStake.userSharePercent) / 100
return userStake
}