From 3f2588beb2da95f7899dc97951c5f07c6b4e3b90 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Sat, 12 Oct 2024 00:54:27 +0700 Subject: [PATCH] handle case vault not enough for penalty --- x/vaults/keeper/vault.go | 12 ++++++++++++ x/vaults/types/msgs.go | 1 - 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/x/vaults/keeper/vault.go b/x/vaults/keeper/vault.go index b8cd10c9..cad817d6 100644 --- a/x/vaults/keeper/vault.go +++ b/x/vaults/keeper/vault.go @@ -522,6 +522,13 @@ func (k *Keeper) Liquidate( //TODO: Sort by CR in GetLiquidations could reduce calculate here for _, vault := range liquidation.LiquidatingVaults { penaltyAmount := math.LegacyNewDecFromInt(vault.Debt.Amount).Quo(vault.LiquidationPrice).Mul(vm.Params.LiquidationPenalty).TruncateInt() + + // If collateral locked not enough for penalty, + // transfer all and mark vault CLOSED + if penaltyAmount.GT(vault.CollateralLocked.Amount) { + penaltyAmount = vault.CollateralLocked.Amount + vault.Status = types.CLOSED + } err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, types.ReserveModuleName, sdk.NewCoins(sdk.NewCoin(liquidation.Denom, penaltyAmount))) if err != nil { return err @@ -539,8 +546,13 @@ func (k *Keeper) Liquidate( }) // Try to reconstitue vaults + // list contains both LIQUIDATING & CLOSED, + // only reconstitue LIQUIDATING vaults totalRemainDebt := totalDebt.Sub(sold) for _, vault := range liquidation.LiquidatingVaults { + if vault.Status != types.LIQUIDATING { + continue + } // if remain debt & collateral can cover full vault // open again if vault.Debt.IsLTE(totalRemainDebt) && vault.CollateralLocked.IsLTE(totalCollateralRemain) { diff --git a/x/vaults/types/msgs.go b/x/vaults/types/msgs.go index ef34e08e..17436848 100644 --- a/x/vaults/types/msgs.go +++ b/x/vaults/types/msgs.go @@ -14,7 +14,6 @@ const ( var ( Query_serviceDesc = _Query_serviceDesc - Msg_serviceDesc = _Msg_serviceDesc ) func NewMsgCreateVault(owner string, collateral, minted sdk.Coin) MsgCreateVault {