Skip to content

Commit

Permalink
handle case vault not enough for penalty
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed Oct 11, 2024
1 parent c12c263 commit 3f2588b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 12 additions & 0 deletions x/vaults/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion x/vaults/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const (

var (
Query_serviceDesc = _Query_serviceDesc
Msg_serviceDesc = _Msg_serviceDesc
)

func NewMsgCreateVault(owner string, collateral, minted sdk.Coin) MsgCreateVault {
Expand Down

0 comments on commit 3f2588b

Please sign in to comment.