2020-01-12 15:35:34 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SeizeCollateral liquidates the collateral in the input cdp.
|
|
|
|
// the following operations are performed:
|
|
|
|
// 1. updates the fees for the input cdp,
|
2020-01-21 17:52:07 +00:00
|
|
|
// 2. sends collateral for all deposits from the cdp module to the liquidator module account
|
|
|
|
// 3. Applies the liquidation penalty and mints the corresponding amount of debt coins in the cdp module
|
|
|
|
// 3. moves debt coins from the cdp module to the liquidator module account,
|
2020-01-12 15:35:34 +00:00
|
|
|
// 4. decrements the total amount of principal outstanding for that collateral type
|
2020-01-21 17:52:07 +00:00
|
|
|
// (this is the equivalent of saying that fees are no longer accumulated by a cdp once it gets liquidated)
|
2020-01-15 14:19:33 +00:00
|
|
|
func (k Keeper) SeizeCollateral(ctx sdk.Context, cdp types.CDP) sdk.Error {
|
|
|
|
// Calculate the previous collateral ratio
|
|
|
|
oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees))
|
2020-01-12 15:35:34 +00:00
|
|
|
// Update fees
|
|
|
|
periods := sdk.NewInt(ctx.BlockTime().Unix()).Sub(sdk.NewInt(cdp.FeesUpdated.Unix()))
|
|
|
|
fees := k.CalculateFees(ctx, cdp.Principal.Add(cdp.AccumulatedFees), periods, cdp.Collateral[0].Denom)
|
|
|
|
cdp.AccumulatedFees = cdp.AccumulatedFees.Add(fees)
|
|
|
|
cdp.FeesUpdated = ctx.BlockTime()
|
|
|
|
|
2020-01-15 14:19:33 +00:00
|
|
|
// Move debt coins from cdp to liquidator account
|
|
|
|
deposits := k.GetDeposits(ctx, cdp.ID)
|
|
|
|
debt := sdk.ZeroInt()
|
|
|
|
for _, pc := range cdp.Principal {
|
|
|
|
debt = debt.Add(pc.Amount)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
for _, dc := range cdp.AccumulatedFees {
|
2020-01-15 14:19:33 +00:00
|
|
|
debt = debt.Add(dc.Amount)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2020-01-21 17:52:07 +00:00
|
|
|
|
2020-01-15 14:19:33 +00:00
|
|
|
debtCoin := sdk.NewCoin(k.GetDebtDenom(ctx), debt)
|
|
|
|
err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, types.LiquidatorMacc, sdk.NewCoins(debtCoin))
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
2020-01-15 14:19:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// liquidate deposits and send collateral from cdp to liquidator
|
|
|
|
for _, dep := range deposits {
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeCdpLiquidation,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyCdpID, fmt.Sprintf("%d", cdp.ID)),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, fmt.Sprintf("%s", dep.Depositor)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, types.LiquidatorMacc, dep.Amount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k.DeleteDeposit(ctx, dep.CdpID, dep.Depositor)
|
|
|
|
}
|
|
|
|
err = k.AuctionCollateral(ctx, deposits, debt, cdp.Principal[0].Denom)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement total principal for this collateral type
|
|
|
|
for _, dc := range cdp.Principal {
|
|
|
|
feeAmount := cdp.AccumulatedFees.AmountOf(dc.Denom)
|
|
|
|
coinsToDecrement := sdk.NewCoins(dc)
|
|
|
|
if feeAmount.IsPositive() {
|
|
|
|
feeCoins := sdk.NewCoins(sdk.NewCoin(dc.Denom, feeAmount))
|
|
|
|
coinsToDecrement = coinsToDecrement.Add(feeCoins)
|
|
|
|
}
|
|
|
|
k.DecrementTotalPrincipal(ctx, cdp.Collateral[0].Denom, coinsToDecrement)
|
|
|
|
}
|
2020-01-15 14:19:33 +00:00
|
|
|
k.RemoveCdpOwnerIndex(ctx, cdp)
|
|
|
|
k.RemoveCdpCollateralRatioIndex(ctx, cdp.Collateral[0].Denom, cdp.ID, oldCollateralToDebtRatio)
|
|
|
|
k.DeleteCDP(ctx, cdp)
|
|
|
|
return nil
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleNewDebt compounds the accumulated fees for the input collateral and principal coins.
|
|
|
|
// the following operations are performed:
|
|
|
|
// 1. mints the fee coins in the liquidator module account,
|
|
|
|
// 2. mints the same amount of debt coins in the cdp module account
|
|
|
|
// 3. updates the total amount of principal for the input collateral type in the store,
|
|
|
|
func (k Keeper) HandleNewDebt(ctx sdk.Context, collateralDenom string, principalDenom string, periods sdk.Int) {
|
|
|
|
previousDebt := k.GetTotalPrincipal(ctx, collateralDenom, principalDenom)
|
|
|
|
feeCoins := sdk.NewCoins(sdk.NewCoin(principalDenom, previousDebt))
|
|
|
|
newFees := k.CalculateFees(ctx, feeCoins, periods, collateralDenom)
|
|
|
|
k.MintDebtCoins(ctx, types.ModuleName, k.GetDebtDenom(ctx), newFees)
|
|
|
|
k.supplyKeeper.MintCoins(ctx, types.LiquidatorMacc, newFees)
|
|
|
|
k.SetTotalPrincipal(ctx, collateralDenom, principalDenom, feeCoins.Add(newFees).AmountOf(principalDenom))
|
|
|
|
}
|
|
|
|
|
|
|
|
// LiquidateCdps seizes collateral from all CDPs below the input liquidation ratio
|
2020-01-15 14:19:33 +00:00
|
|
|
func (k Keeper) LiquidateCdps(ctx sdk.Context, marketID string, denom string, liquidationRatio sdk.Dec) sdk.Error {
|
2020-01-12 15:35:34 +00:00
|
|
|
price, err := k.pricefeedKeeper.GetCurrentPrice(ctx, marketID)
|
|
|
|
if err != nil {
|
2020-01-15 14:19:33 +00:00
|
|
|
return err
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
normalizedRatio := sdk.OneDec().Quo(price.Price.Quo(liquidationRatio))
|
|
|
|
cdpsToLiquidate := k.GetAllCdpsByDenomAndRatio(ctx, denom, normalizedRatio)
|
|
|
|
for _, c := range cdpsToLiquidate {
|
2020-01-15 14:19:33 +00:00
|
|
|
err := k.SeizeCollateral(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2020-01-15 14:19:33 +00:00
|
|
|
return nil
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2020-01-21 17:52:07 +00:00
|
|
|
|
|
|
|
// ApplyLiquidationPenalty multiplies the input debt amount by the liquidation penalty and mints the debt coins in the cdp module account
|
|
|
|
func (k Keeper) ApplyLiquidationPenalty(ctx sdk.Context, denom string, debt sdk.Int) sdk.Int {
|
|
|
|
penalty := k.getLiquidationPenalty(ctx, denom)
|
|
|
|
penaltyAmount := sdk.NewDecFromInt(debt).Mul(penalty).RoundInt()
|
|
|
|
return penaltyAmount
|
|
|
|
}
|