-
Notifications
You must be signed in to change notification settings - Fork 364
/
params.go
292 lines (250 loc) · 8.82 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
cdptypes "github.com/kava-labs/kava/x/cdp/types"
)
// Parameter keys and default values
var (
KeyMoneyMarkets = []byte("MoneyMarkets")
KeyMinimumBorrowUSDValue = []byte("MinimumBorrowUSDValue")
DefaultMoneyMarkets = MoneyMarkets{}
DefaultMinimumBorrowUSDValue = sdk.NewDec(10) // $10 USD minimum borrow value
GovDenom = cdptypes.DefaultGovDenom
DefaultAccumulationTimes = GenesisAccumulationTimes{}
DefaultTotalSupplied = sdk.Coins{}
DefaultTotalBorrowed = sdk.Coins{}
DefaultTotalReserves = sdk.Coins{}
DefaultDeposits = Deposits{}
DefaultBorrows = Borrows{}
)
// Params governance parameters for hard module
type Params struct {
MoneyMarkets MoneyMarkets `json:"money_markets" yaml:"money_markets"`
MinimumBorrowUSDValue sdk.Dec `json:"minimum_borrow_usd_value" yaml:"minimum_borrow_usd_value"`
}
// BorrowLimit enforces restrictions on a money market
type BorrowLimit struct {
HasMaxLimit bool `json:"has_max_limit" yaml:"has_max_limit"`
MaximumLimit sdk.Dec `json:"maximum_limit" yaml:"maximum_limit"`
LoanToValue sdk.Dec `json:"loan_to_value" yaml:"loan_to_value"`
}
// 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
}
// MoneyMarket is a money market for an individual asset
type MoneyMarket struct {
Denom string `json:"denom" yaml:"denom"`
BorrowLimit BorrowLimit `json:"borrow_limit" yaml:"borrow_limit"`
SpotMarketID string `json:"spot_market_id" yaml:"spot_market_id"`
ConversionFactor sdk.Int `json:"conversion_factor" yaml:"conversion_factor"`
InterestRateModel InterestRateModel `json:"interest_rate_model" yaml:"interest_rate_model"`
ReserveFactor sdk.Dec `json:"reserve_factor" yaml:"reserve_factor"`
KeeperRewardPercentage sdk.Dec `json:"keeper_reward_percentage" yaml:"keeper_reward_percentages"`
}
// NewMoneyMarket returns a new MoneyMarket
func NewMoneyMarket(denom string, borrowLimit BorrowLimit, spotMarketID string, conversionFactor sdk.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
}
// InterestRateModel contains information about an asset's interest rate
type InterestRateModel struct {
BaseRateAPY sdk.Dec `json:"base_rate_apy" yaml:"base_rate_apy"`
BaseMultiplier sdk.Dec `json:"base_multiplier" yaml:"base_multiplier"`
Kink sdk.Dec `json:"kink" yaml:"kink"`
JumpMultiplier sdk.Dec `json:"jump_multiplier" yaml:"jump_multiplier"`
}
// 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)
}
// String implements fmt.Stringer
func (p Params) String() string {
return fmt.Sprintf(`Params:
Minimum Borrow USD Value: %v
Money Markets: %v`,
p.MinimumBorrowUSDValue, p.MoneyMarkets)
}
// ParamKeyTable Key declaration for parameters
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
params.NewParamSetPair(KeyMoneyMarkets, &p.MoneyMarkets, validateMoneyMarketParams),
params.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()
}