/
genesis.go
96 lines (82 loc) · 2.83 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
86
87
88
89
90
91
92
93
94
95
96
package types
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
)
// NewGenesisState creates a new GenesisState object
func NewGenesisState(
params Params,
rates []DenomExchangeRate,
feederDelegations []FeederDelegation,
missCounters []MissCounter,
aggregateExchangeRatePrevotes []AggregateExchangeRatePrevote,
aggregateExchangeRateVotes []AggregateExchangeRateVote,
historicPrices []Price,
medianPrices []Price,
medianDeviationPrices []Price,
acp AvgCounterParams,
) *GenesisState {
return &GenesisState{
Params: params,
ExchangeRates: rates,
FeederDelegations: feederDelegations,
MissCounters: missCounters,
AggregateExchangeRatePrevotes: aggregateExchangeRatePrevotes,
AggregateExchangeRateVotes: aggregateExchangeRateVotes,
HistoricPrices: historicPrices,
Medians: medianPrices,
MedianDeviations: medianDeviationPrices,
AvgCounterParams: acp,
}
}
// DefaultGenesisState returns the default genesis state for the x/oracle
// module.
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
ExchangeRates: []DenomExchangeRate{},
FeederDelegations: []FeederDelegation{},
MissCounters: []MissCounter{},
AggregateExchangeRatePrevotes: []AggregateExchangeRatePrevote{},
AggregateExchangeRateVotes: []AggregateExchangeRateVote{},
HistoricPrices: []Price{},
Medians: []Price{},
MedianDeviations: []Price{},
AvgCounterParams: DefaultAvgCounterParams(),
}
}
// ValidateGenesis validates the oracle genesis state.
func ValidateGenesis(data *GenesisState) error {
if err := data.Params.Validate(); err != nil {
return err
}
return data.AvgCounterParams.Validate()
}
// GetGenesisStateFromAppState returns x/oracle GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}
return &genesisState
}
func DefaultAvgCounterParams() AvgCounterParams {
return AvgCounterParams{
AvgPeriod: DefaultAvgPeriod, // 16 hours
AvgShift: DefaultAvgShift, // 12 hours
}
}
func (acp AvgCounterParams) Equal(other *AvgCounterParams) bool {
return acp.AvgPeriod == other.AvgPeriod && acp.AvgShift == other.AvgShift
}
func (acp AvgCounterParams) Validate() error {
if acp.AvgPeriod.Seconds() <= 0 {
return fmt.Errorf("avg period must be positive: %d", acp.AvgPeriod)
}
if acp.AvgShift.Seconds() <= 0 {
return fmt.Errorf("avg shift must be positive: %d", acp.AvgShift)
}
return nil
}