-
Notifications
You must be signed in to change notification settings - Fork 368
/
params.go
252 lines (215 loc) · 7.02 KB
/
params.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package types
import (
"fmt"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter keys and default values
var (
KeyMoneyMarkets = []byte("MoneyMarkets")
KeyMinimumBorrowUSDValue = []byte("MinimumBorrowUSDValue")
DefaultMoneyMarkets = MoneyMarkets{}
DefaultMinimumBorrowUSDValue = sdk.NewDec(10) // $10 USD minimum borrow value
DefaultAccumulationTimes = GenesisAccumulationTimes{}
DefaultTotalSupplied = sdk.Coins{}
DefaultTotalBorrowed = sdk.Coins{}
DefaultTotalReserves = sdk.Coins{}
DefaultDeposits = Deposits{}
DefaultBorrows = Borrows{}
)
// NewBorrowLimit returns a new BorrowLimit
func NewBorrowLimit(hasMaxLimit bool, maximumLimit, loanToValue sdk.Dec) BorrowLimit {
return BorrowLimit{
HasMaxLimit: hasMaxLimit,
MaximumLimit: maximumLimit,
LoanToValue: loanToValue,
}
}
// Validate BorrowLimit
func (bl BorrowLimit) Validate() error {
if bl.MaximumLimit.IsNegative() {
return fmt.Errorf("maximum limit USD cannot be negative: %s", bl.MaximumLimit)
}
if bl.LoanToValue.IsNegative() {
return fmt.Errorf("loan-to-value must be a non-negative decimal: %s", bl.LoanToValue)
}
if bl.LoanToValue.GT(sdk.OneDec()) {
return fmt.Errorf("loan-to-value cannot be greater than 1.0: %s", bl.LoanToValue)
}
return nil
}
// Equal returns a boolean indicating if an BorrowLimit is equal to another BorrowLimit
func (bl BorrowLimit) Equal(blCompareTo BorrowLimit) bool {
if bl.HasMaxLimit != blCompareTo.HasMaxLimit {
return false
}
if !bl.MaximumLimit.Equal(blCompareTo.MaximumLimit) {
return false
}
if !bl.LoanToValue.Equal(blCompareTo.LoanToValue) {
return false
}
return true
}
// NewMoneyMarket returns a new MoneyMarket
func NewMoneyMarket(denom string, borrowLimit BorrowLimit, spotMarketID string, conversionFactor sdkmath.Int,
interestRateModel InterestRateModel, reserveFactor, keeperRewardPercentage sdk.Dec,
) MoneyMarket {
return MoneyMarket{
Denom: denom,
BorrowLimit: borrowLimit,
SpotMarketID: spotMarketID,
ConversionFactor: conversionFactor,
InterestRateModel: interestRateModel,
ReserveFactor: reserveFactor,
KeeperRewardPercentage: keeperRewardPercentage,
}
}
// Validate MoneyMarket param
func (mm MoneyMarket) Validate() error {
if err := sdk.ValidateDenom(mm.Denom); err != nil {
return err
}
if err := mm.BorrowLimit.Validate(); err != nil {
return err
}
if mm.ConversionFactor.IsNil() || mm.ConversionFactor.LT(sdk.OneInt()) {
return fmt.Errorf("conversion '%s' factor must be ≥ one", mm.ConversionFactor)
}
if err := mm.InterestRateModel.Validate(); err != nil {
return err
}
if mm.ReserveFactor.IsNegative() || mm.ReserveFactor.GT(sdk.OneDec()) {
return fmt.Errorf("reserve factor must be between 0.0-1.0")
}
if mm.KeeperRewardPercentage.IsNegative() || mm.KeeperRewardPercentage.GT(sdk.OneDec()) {
return fmt.Errorf("keeper reward percentage must be between 0.0-1.0")
}
return nil
}
// Equal returns a boolean indicating if a MoneyMarket is equal to another MoneyMarket
func (mm MoneyMarket) Equal(mmCompareTo MoneyMarket) bool {
if mm.Denom != mmCompareTo.Denom {
return false
}
if !mm.BorrowLimit.Equal(mmCompareTo.BorrowLimit) {
return false
}
if mm.SpotMarketID != mmCompareTo.SpotMarketID {
return false
}
if !mm.ConversionFactor.Equal(mmCompareTo.ConversionFactor) {
return false
}
if !mm.InterestRateModel.Equal(mmCompareTo.InterestRateModel) {
return false
}
if !mm.ReserveFactor.Equal(mmCompareTo.ReserveFactor) {
return false
}
if !mm.KeeperRewardPercentage.Equal(mmCompareTo.KeeperRewardPercentage) {
return false
}
return true
}
// MoneyMarkets slice of MoneyMarket
type MoneyMarkets []MoneyMarket
// Validate borrow limits
func (mms MoneyMarkets) Validate() error {
for _, moneyMarket := range mms {
if err := moneyMarket.Validate(); err != nil {
return err
}
}
return nil
}
// NewInterestRateModel returns a new InterestRateModel
func NewInterestRateModel(baseRateAPY, baseMultiplier, kink, jumpMultiplier sdk.Dec) InterestRateModel {
return InterestRateModel{
BaseRateAPY: baseRateAPY,
BaseMultiplier: baseMultiplier,
Kink: kink,
JumpMultiplier: jumpMultiplier,
}
}
// Validate InterestRateModel param
func (irm InterestRateModel) Validate() error {
if irm.BaseRateAPY.IsNegative() || irm.BaseRateAPY.GT(sdk.OneDec()) {
return fmt.Errorf("base rate APY must be in the inclusive range 0.0-1.0")
}
if irm.BaseMultiplier.IsNegative() {
return fmt.Errorf("base multiplier must not be negative")
}
if irm.Kink.IsNegative() || irm.Kink.GT(sdk.OneDec()) {
return fmt.Errorf("kink must be in the inclusive range 0.0-1.0")
}
if irm.JumpMultiplier.IsNegative() {
return fmt.Errorf("jump multiplier must not be negative")
}
return nil
}
// Equal returns a boolean indicating if an InterestRateModel is equal to another InterestRateModel
func (irm InterestRateModel) Equal(irmCompareTo InterestRateModel) bool {
if !irm.BaseRateAPY.Equal(irmCompareTo.BaseRateAPY) {
return false
}
if !irm.BaseMultiplier.Equal(irmCompareTo.BaseMultiplier) {
return false
}
if !irm.Kink.Equal(irmCompareTo.Kink) {
return false
}
if !irm.JumpMultiplier.Equal(irmCompareTo.JumpMultiplier) {
return false
}
return true
}
// InterestRateModels slice of InterestRateModel
type InterestRateModels []InterestRateModel
// NewParams returns a new params object
func NewParams(moneyMarkets MoneyMarkets, minimumBorrowUSDValue sdk.Dec) Params {
return Params{
MoneyMarkets: moneyMarkets,
MinimumBorrowUSDValue: minimumBorrowUSDValue,
}
}
// DefaultParams returns default params for hard module
func DefaultParams() Params {
return NewParams(DefaultMoneyMarkets, DefaultMinimumBorrowUSDValue)
}
// ParamKeyTable Key declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMoneyMarkets, &p.MoneyMarkets, validateMoneyMarketParams),
paramtypes.NewParamSetPair(KeyMinimumBorrowUSDValue, &p.MinimumBorrowUSDValue, validateMinimumBorrowUSDValue),
}
}
// Validate checks that the parameters have valid values.
func (p Params) Validate() error {
if err := validateMinimumBorrowUSDValue(p.MinimumBorrowUSDValue); err != nil {
return err
}
return validateMoneyMarketParams(p.MoneyMarkets)
}
func validateMinimumBorrowUSDValue(i interface{}) error {
minBorrowVal, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if minBorrowVal.IsNegative() {
return fmt.Errorf("minimum borrow USD value cannot be negative")
}
return nil
}
func validateMoneyMarketParams(i interface{}) error {
mm, ok := i.(MoneyMarkets)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return mm.Validate()
}