-
Notifications
You must be signed in to change notification settings - Fork 5
/
gauge.go
82 lines (68 loc) · 2.14 KB
/
gauge.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package types
import (
time "time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewGauge creates a new gauge struct given the required gauge parameters.
func NewGauge(
id uint64,
isPerpetual bool,
distrTo QueryCondition,
coins sdk.Coins,
startTime time.Time,
numEpochsPaidOver uint64,
filledEpochs uint64,
distCoins sdk.Coins,
pricingTick int64,
) Gauge {
return Gauge{
Id: id,
IsPerpetual: isPerpetual,
DistributeTo: distrTo,
Coins: coins,
StartTime: startTime,
NumEpochsPaidOver: numEpochsPaidOver,
FilledEpochs: filledEpochs,
DistributedCoins: distCoins,
PricingTick: pricingTick,
}
}
func (gauge Gauge) hasEpochsRemaining() bool {
return gauge.IsPerpetual || gauge.FilledEpochs < gauge.NumEpochsPaidOver
}
func (gauge Gauge) hasStarted(now time.Time) bool {
return !now.Before(gauge.StartTime)
}
// IsUpcomingGauge returns true if the gauge's distribution start time is after the provided time.
func (gauge Gauge) IsUpcomingGauge(now time.Time) bool {
return !gauge.hasStarted(now) && gauge.hasEpochsRemaining()
}
// IsActiveGauge returns true if the gauge is in an active state during the provided time.
func (gauge Gauge) IsActiveGauge(now time.Time) bool {
return gauge.hasStarted(now) && gauge.hasEpochsRemaining()
}
// IsFinishedGauge returns true if the gauge is in a finished state during the provided time.
func (gauge Gauge) IsFinishedGauge(now time.Time) bool {
return gauge.hasStarted(now) && !gauge.hasEpochsRemaining()
}
func (gauge Gauge) RewardsNextEpoch() sdk.Coins {
result := sdk.Coins{}
epochsRemaining := gauge.EpochsRemaining()
if epochsRemaining == 0 {
return result
}
for _, rewardRemainingCoin := range gauge.CoinsRemaining() {
amount := rewardRemainingCoin.Amount.Quo(sdk.NewInt(int64(epochsRemaining)))
result = result.Add(sdk.Coin{Denom: rewardRemainingCoin.Denom, Amount: amount})
}
return result
}
func (gauge Gauge) EpochsRemaining() uint64 {
if !gauge.IsPerpetual {
return gauge.NumEpochsPaidOver - gauge.FilledEpochs
}
return 1
}
func (gauge Gauge) CoinsRemaining() sdk.Coins {
return gauge.Coins.Sub(gauge.DistributedCoins...)
}