2020-01-12 15:35:34 +00:00
|
|
|
package cdp
|
|
|
|
|
|
|
|
import (
|
2020-01-15 14:19:33 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-01-15 14:19:33 +00:00
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
2020-01-12 15:35:34 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
2020-01-15 14:19:33 +00:00
|
|
|
// BeginBlocker compounds the debt in outstanding cdps and liquidates cdps that are below the required collateralization ratio
|
2020-01-12 15:35:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:19:33 +00:00
|
|
|
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)),
|
|
|
|
),
|
|
|
|
)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
k.SetPreviousBlockTime(ctx, ctx.BlockTime())
|
|
|
|
return
|
|
|
|
}
|