-
Notifications
You must be signed in to change notification settings - Fork 16
/
draw_schedule.go
64 lines (57 loc) · 2.06 KB
/
draw_schedule.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
package types
import (
"fmt"
time "time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (ps DrawSchedule) ValidateBasic(params Params) error {
if ps.DrawDelta < params.MinDrawScheduleDelta {
return fmt.Errorf("draw delta cannot be lower than %s", params.MinDrawScheduleDelta.String())
}
if ps.DrawDelta > params.MaxDrawScheduleDelta {
return fmt.Errorf("draw delta cannot be higher than %s", params.MaxDrawScheduleDelta.String())
}
return nil
}
// ValidateNew drawSchedule validation at pool creation time
func (ps DrawSchedule) ValidateNew(ctx sdk.Context, params Params) error {
if ps.DrawDelta < params.MinDrawScheduleDelta {
return fmt.Errorf("draw delta cannot be lower than %s", params.MinDrawScheduleDelta.String())
}
if ps.DrawDelta > params.MaxDrawScheduleDelta {
return fmt.Errorf("draw delta cannot be higher than %s", params.MaxDrawScheduleDelta.String())
}
if ps.InitialDrawAt.Before(ctx.BlockTime().Add(ps.DrawDelta)) {
return fmt.Errorf("initial draw must be in the future and at least after draw delta")
}
return nil
}
// sanitizeTime returns the time rounded to the minute
func (ps DrawSchedule) sanitizeTime(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
}
// Sanitized returns the draw schedule with time rounded to the minute
func (ps DrawSchedule) Sanitized() DrawSchedule {
return DrawSchedule{
InitialDrawAt: ps.sanitizeTime(ps.GetInitialDrawAt()),
DrawDelta: ps.GetDrawDelta(),
}
}
// ShouldDraw returns whether or not the current block time is past the next draw time (= time to launch draw)
func (ps DrawSchedule) ShouldDraw(ctx sdk.Context, lastDrawAt *time.Time) bool {
if lastDrawAt == nil {
return !ctx.BlockTime().Before(ps.InitialDrawAt)
}
// Simulate lda as if it did run at the appropriate minute to prevent small time drifts
saneLda := time.Date(
lastDrawAt.Year(),
lastDrawAt.Month(),
lastDrawAt.Day(),
lastDrawAt.Hour(),
ps.InitialDrawAt.Minute(),
0,
0,
lastDrawAt.Location(),
)
return !ctx.BlockTime().Before(saneLda.Add(ps.DrawDelta))
}