mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-14 03:55:19 +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
48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package cdp
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
)
|
|
|
|
// BeginBlocker compounds the debt in outstanding cdps and liquidates cdps that are below the required collateralization ratio
|
|
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {
|
|
params := k.GetParams(ctx)
|
|
previousBlockTime, found := k.GetPreviousBlockTime(ctx)
|
|
if !found {
|
|
previousBlockTime = ctx.BlockTime()
|
|
}
|
|
timeElapsed := sdk.NewInt(ctx.BlockTime().Unix() - previousBlockTime.Unix())
|
|
for _, cp := range params.CollateralParams {
|
|
for _, dp := range params.DebtParams {
|
|
k.HandleNewDebt(ctx, cp.Denom, dp.Denom, timeElapsed)
|
|
}
|
|
|
|
err := k.LiquidateCdps(ctx, cp.MarketID, cp.Denom, cp.LiquidationRatio)
|
|
if err != nil {
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
EventTypeBeginBlockerFatal,
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, fmt.Sprintf("%s", ModuleName)),
|
|
sdk.NewAttribute(types.AttributeKeyError, fmt.Sprintf("%s", err)),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
err := k.RunSurplusAndDebtAuctions(ctx)
|
|
if err != nil {
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
EventTypeBeginBlockerFatal,
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, fmt.Sprintf("%s", ModuleName)),
|
|
sdk.NewAttribute(types.AttributeKeyError, fmt.Sprintf("%s", err)),
|
|
),
|
|
)
|
|
}
|
|
k.SetPreviousBlockTime(ctx, ctx.BlockTime())
|
|
return
|
|
}
|