-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
85 lines (70 loc) · 2.59 KB
/
genesis.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
83
84
85
package types
import (
"errors"
"fmt"
)
// NewGenesisState returns genesis state with the given parameters and twap records.
func NewGenesisState(params Params, twapRecords []TwapRecord) *GenesisState {
return &GenesisState{
Params: params,
Twaps: twapRecords,
}
}
// DefaultGenesis returns the default twap genesis state.
func DefaultGenesis() *GenesisState {
return NewGenesisState(DefaultParams(), []TwapRecord{})
}
// Validate validates the genesis state. Returns nil on success, error otherwise.
func (g *GenesisState) Validate() error {
if err := g.Params.Validate(); err != nil {
return err
}
for _, twap := range g.Twaps {
if err := twap.validate(); err != nil {
return err
}
}
return nil
}
// validate validates the twap record, returns nil on success, error otherwise.
func (t TwapRecord) validate() error {
if t.PoolId == 0 {
return errors.New("pool id cannot be 0")
}
if t.Asset0Denom == "" {
return fmt.Errorf("twap record asset0 denom cannot be empty, was (%s)", t.Asset0Denom)
}
if t.Asset1Denom == "" {
return fmt.Errorf("twap record asset1 denom cannot be empty, was (%s)", t.Asset1Denom)
}
if t.Height <= 0 {
return fmt.Errorf("twap record height must be positive, was (%d)", t.Height)
}
if t.Time.IsZero() {
return errors.New("twap record time cannot be 0")
}
// if there was an error in this record, the spot prices should be 0.
// else, the the spot prices must be positive.
if t.LastErrorTime.Equal(t.Time) {
if t.P0LastSpotPrice.IsNil() || !t.P0LastSpotPrice.IsZero() {
return fmt.Errorf("twap record p0 last spot price must be zero due to having an error, was (%s)", t.P0LastSpotPrice)
}
if t.P1LastSpotPrice.IsNil() || !t.P1LastSpotPrice.IsZero() {
return fmt.Errorf("twap record p1 last spot price must be zero due to having an error, was (%s)", t.P1LastSpotPrice)
}
} else {
if t.P0LastSpotPrice.IsNil() || !t.P0LastSpotPrice.IsPositive() {
return fmt.Errorf("twap record p0 last spot price must be positive, was (%s)", t.P0LastSpotPrice)
}
if t.P1LastSpotPrice.IsNil() || !t.P1LastSpotPrice.IsPositive() {
return fmt.Errorf("twap record p1 last spot price must be positive, was (%s)", t.P1LastSpotPrice)
}
}
if t.P0ArithmeticTwapAccumulator.IsNil() || t.P0ArithmeticTwapAccumulator.IsNegative() {
return fmt.Errorf("twap record p0 accumulator cannot be negative, was (%s)", t.P0ArithmeticTwapAccumulator)
}
if t.P1ArithmeticTwapAccumulator.IsNil() || t.P1ArithmeticTwapAccumulator.IsNegative() {
return fmt.Errorf("twap record p1 accumulator cannot be negative, was (%s)", t.P1ArithmeticTwapAccumulator)
}
return nil
}