-
Notifications
You must be signed in to change notification settings - Fork 665
/
reward.go
57 lines (48 loc) · 2.24 KB
/
reward.go
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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"math/big"
"time"
)
var (
// maxSubMinConsumptionRate is the difference between the maximum
// consumption rate of the remaining tokens and the minimum.
maxSubMinConsumptionRate = new(big.Int).SetUint64(MaxSubMinConsumptionRate)
// minConsumptionRate is the consumption rate to use when calculating a
// validator period with duration 0.
minConsumptionRate = new(big.Int).SetUint64(MinConsumptionRate)
// consumptionRateDenominator is the magnitude offset used to emulate
// floating point fractions.
consumptionRateDenominator = new(big.Int).SetUint64(PercentDenominator)
)
// reward returns the amount of tokens to reward the staker with.
//
// RemainingSupply = SupplyCap - ExistingSupply
// PortionOfExistingSupply = StakedAmount / ExistingSupply
// PortionOfStakingDuration = StakingDuration / MaximumStakingDuration
// MintingRate = MinMintingRate + MaxSubMinMintingRate * PortionOfStakingDuration
// Reward = RemainingSupply * PortionOfExistingSupply * MintingRate * PortionOfStakingDuration
func reward(
rawDuration time.Duration,
rawStakedAmount,
rawMaxExistingAmount uint64,
rawConsumptionInterval time.Duration,
) uint64 {
duration := new(big.Int).SetUint64(uint64(rawDuration))
stakedAmount := new(big.Int).SetUint64(rawStakedAmount)
maxExistingAmount := new(big.Int).SetUint64(rawMaxExistingAmount)
consumptionInterval := new(big.Int).SetUint64(uint64(rawConsumptionInterval))
adjustedConsumptionRateNumerator := new(big.Int).Mul(maxSubMinConsumptionRate, duration)
adjustedMinConsumptionRateNumerator := new(big.Int).Mul(minConsumptionRate, consumptionInterval)
adjustedConsumptionRateNumerator.Add(adjustedConsumptionRateNumerator, adjustedMinConsumptionRateNumerator)
adjustedConsumptionRateDenominator := new(big.Int).Mul(consumptionInterval, consumptionRateDenominator)
reward := new(big.Int).SetUint64(SupplyCap - rawMaxExistingAmount)
reward.Mul(reward, adjustedConsumptionRateNumerator)
reward.Mul(reward, stakedAmount)
reward.Mul(reward, duration)
reward.Div(reward, adjustedConsumptionRateDenominator)
reward.Div(reward, maxExistingAmount)
reward.Div(reward, consumptionInterval)
return reward.Uint64()
}