mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 16:55:21 +00:00
9b1bf55be7
* wip: tpyes and keeper methods * wip: iterators * wip: types and keeper methods * wip: add msgs * wip: client methods * wip: rebase develop * wip: types tests * wip: keeper tests, small fixes * wip: add cdp tests * wip: deposit tests * wip: keeper tests * wip: tests and module methods * feat: error when fetching expired price * feat: conversion factor for external assets * feat: debt floor for new cdps * feat: save deposits on export genesis * feat: ensure messages implement msg * feat: index deposits by status * fix: stray comment * wip: address review comments * address review comments * wip: move liquidation to cdp module * wip: handle liquidations directly * wip: use new auction interface * feat: auction collateral in cdp begin block * feat: update param validation * feat: surplus and debt auctions * address review comments * address review comments * fix: auction multiple deposits * clean up netting function
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
)
|
|
|
|
// GetParams returns the params from the store
|
|
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
|
|
var p types.Params
|
|
k.paramSubspace.GetParamSet(ctx, &p)
|
|
return p
|
|
}
|
|
|
|
// SetParams sets params on the store
|
|
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
|
|
}
|
|
|
|
// GetDebt returns the debt param with matching denom
|
|
func (k Keeper) GetDebt(ctx sdk.Context, denom string) (types.DebtParam, bool) {
|
|
params := k.GetParams(ctx)
|
|
for _, dp := range params.DebtParams {
|
|
if dp.Denom == denom {
|
|
return dp, true
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
// private methods assume collateral has been validated, panic if the input is invalid
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|