2019-11-25 19:46:02 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2020-01-12 15:35:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetParams returns the params from the store
|
2020-01-12 15:35:34 +00:00
|
|
|
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
|
|
|
|
var p types.Params
|
2019-11-25 19:46:02 +00:00
|
|
|
k.paramSubspace.GetParamSet(ctx, &p)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetParams sets params on the store
|
2020-01-12 15:35:34 +00:00
|
|
|
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
|
|
|
|
k.paramSubspace.SetParamSet(ctx, ¶ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCollateral returns the collateral param with corresponding denom
|
|
|
|
func (k Keeper) GetCollateral(ctx sdk.Context, denom string) (types.CollateralParam, bool) {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
for _, cp := range params.CollateralParams {
|
|
|
|
if cp.Denom == denom {
|
|
|
|
return cp, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return types.CollateralParam{}, false
|
|
|
|
}
|
|
|
|
|
2020-01-22 16:50:27 +00:00
|
|
|
// GetDebtParam returns the debt param with matching denom
|
|
|
|
func (k Keeper) GetDebtParam(ctx sdk.Context, denom string) (types.DebtParam, bool) {
|
2020-04-27 14:40:34 +00:00
|
|
|
dp := k.GetParams(ctx).DebtParam
|
|
|
|
if dp.Denom == denom {
|
|
|
|
return dp, true
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
return types.DebtParam{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDenomPrefix returns the prefix of the matching denom
|
|
|
|
func (k Keeper) GetDenomPrefix(ctx sdk.Context, denom string) (byte, bool) {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
for _, cp := range params.CollateralParams {
|
|
|
|
if cp.Denom == denom {
|
|
|
|
return cp.Prefix, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0x00, false
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:19:33 +00:00
|
|
|
// private methods assume collateral has been validated, panic if the input is invalid
|
2020-01-12 15:35:34 +00:00
|
|
|
func (k Keeper) getDenomFromByte(ctx sdk.Context, db byte) string {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
for _, cp := range params.CollateralParams {
|
|
|
|
if cp.Prefix == db {
|
|
|
|
return cp.Denom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("no collateral denom with prefix %b", db))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) getMarketID(ctx sdk.Context, denom string) string {
|
|
|
|
cp, found := k.GetCollateral(ctx, denom)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("collateral not found: %s", denom))
|
|
|
|
}
|
|
|
|
return cp.MarketID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) getLiquidationRatio(ctx sdk.Context, denom string) sdk.Dec {
|
|
|
|
cp, found := k.GetCollateral(ctx, denom)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("collateral not found: %s", denom))
|
|
|
|
}
|
|
|
|
return cp.LiquidationRatio
|
|
|
|
}
|
2020-01-15 14:19:33 +00:00
|
|
|
|
|
|
|
func (k Keeper) getLiquidationPenalty(ctx sdk.Context, denom string) sdk.Dec {
|
|
|
|
cp, found := k.GetCollateral(ctx, denom)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("collateral not found: %s", denom))
|
|
|
|
}
|
|
|
|
return cp.LiquidationPenalty
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) getAuctionSize(ctx sdk.Context, denom string) sdk.Int {
|
|
|
|
cp, found := k.GetCollateral(ctx, denom)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("collateral not found: %s", denom))
|
|
|
|
}
|
|
|
|
return cp.AuctionSize
|
|
|
|
}
|
2020-01-23 20:41:45 +00:00
|
|
|
|
|
|
|
// GetFeeRate returns the per second fee rate for the input denom
|
|
|
|
func (k Keeper) getFeeRate(ctx sdk.Context, denom string) (fee sdk.Dec) {
|
|
|
|
collalateralParam, found := k.GetCollateral(ctx, denom)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("could not get fee rate for %s, collateral not found", denom))
|
|
|
|
}
|
|
|
|
return collalateralParam.StabilityFee
|
|
|
|
}
|