-
Notifications
You must be signed in to change notification settings - Fork 364
/
genesis.go
131 lines (116 loc) · 4.65 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package types
import (
"bytes"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GenesisState is the state that must be provided at genesis.
type GenesisState struct {
Params Params `json:"params" yaml:"params"`
PreviousAccumulationTimes GenesisAccumulationTimes `json:"previous_accumulation_times" yaml:"previous_accumulation_times"`
Deposits Deposits `json:"deposits" yaml:"deposits"`
Borrows Borrows `json:"borrows" yaml:"borrows"`
TotalSupplied sdk.Coins `json:"total_supplied" yaml:"total_supplied"`
TotalBorrowed sdk.Coins `json:"total_borrowed" yaml:"total_borrowed"`
TotalReserves sdk.Coins `json:"total_reserves" yaml:"total_reserves"`
}
// NewGenesisState returns a new genesis state
func NewGenesisState(
params Params, prevAccumulationTimes GenesisAccumulationTimes, deposits Deposits,
borrows Borrows, totalSupplied, totalBorrowed, totalReserves sdk.Coins) GenesisState {
return GenesisState{
Params: params,
PreviousAccumulationTimes: prevAccumulationTimes,
Deposits: deposits,
Borrows: borrows,
TotalSupplied: totalSupplied,
TotalBorrowed: totalBorrowed,
TotalReserves: totalReserves,
}
}
// DefaultGenesisState returns a default genesis state
func DefaultGenesisState() GenesisState {
return GenesisState{
Params: DefaultParams(),
PreviousAccumulationTimes: DefaultAccumulationTimes,
Deposits: DefaultDeposits,
Borrows: DefaultBorrows,
TotalSupplied: DefaultTotalSupplied,
TotalBorrowed: DefaultTotalBorrowed,
TotalReserves: DefaultTotalReserves,
}
}
// Validate performs basic validation of genesis data returning an
// error for any failed validation criteria.
func (gs GenesisState) Validate() error {
if err := gs.Params.Validate(); err != nil {
return err
}
if err := gs.PreviousAccumulationTimes.Validate(); err != nil {
return err
}
if err := gs.Deposits.Validate(); err != nil {
return err
}
if err := gs.Borrows.Validate(); err != nil {
return err
}
if !gs.TotalSupplied.IsValid() {
return fmt.Errorf("invalid total supplied coins: %s", gs.TotalSupplied)
}
if !gs.TotalBorrowed.IsValid() {
return fmt.Errorf("invalid total borrowed coins: %s", gs.TotalBorrowed)
}
if !gs.TotalReserves.IsValid() {
return fmt.Errorf("invalid total reserves coins: %s", gs.TotalReserves)
}
return nil
}
// Equal checks whether two gov GenesisState structs are equivalent
func (gs GenesisState) Equal(gs2 GenesisState) bool {
b1 := ModuleCdc.MustMarshalBinaryBare(gs)
b2 := ModuleCdc.MustMarshalBinaryBare(gs2)
return bytes.Equal(b1, b2)
}
// IsEmpty returns true if a GenesisState is empty
func (gs GenesisState) IsEmpty() bool {
return gs.Equal(GenesisState{})
}
// GenesisAccumulationTime stores the previous distribution time and its corresponding denom
type GenesisAccumulationTime struct {
CollateralType string `json:"collateral_type" yaml:"collateral_type"`
PreviousAccumulationTime time.Time `json:"previous_accumulation_time" yaml:"previous_accumulation_time"`
SupplyInterestFactor sdk.Dec `json:"supply_interest_factor" yaml:"supply_interest_factor"`
BorrowInterestFactor sdk.Dec `json:"borrow_interest_factor" yaml:"borrow_interest_factor"`
}
// NewGenesisAccumulationTime returns a new GenesisAccumulationTime
func NewGenesisAccumulationTime(ctype string, prevTime time.Time, supplyFactor, borrowFactor sdk.Dec) GenesisAccumulationTime {
return GenesisAccumulationTime{
CollateralType: ctype,
PreviousAccumulationTime: prevTime,
SupplyInterestFactor: supplyFactor,
BorrowInterestFactor: borrowFactor,
}
}
// GenesisAccumulationTimes slice of GenesisAccumulationTime
type GenesisAccumulationTimes []GenesisAccumulationTime
// Validate performs validation of GenesisAccumulationTimes
func (gats GenesisAccumulationTimes) Validate() error {
for _, gat := range gats {
if err := gat.Validate(); err != nil {
return err
}
}
return nil
}
// Validate performs validation of GenesisAccumulationTime
func (gat GenesisAccumulationTime) Validate() error {
if gat.SupplyInterestFactor.LT(sdk.OneDec()) {
return fmt.Errorf("supply interest factor should be ≥ 1.0, is %s for %s", gat.SupplyInterestFactor, gat.CollateralType)
}
if gat.BorrowInterestFactor.LT(sdk.OneDec()) {
return fmt.Errorf("borrow interest factor should be ≥ 1.0, is %s for %s", gat.BorrowInterestFactor, gat.CollateralType)
}
return nil
}