From bdff81b2a261fe847cef143dac81ecdceab4c1a6 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Thu, 30 Jan 2020 18:50:42 -0500 Subject: [PATCH] fix: avoid panic when closing cdp via repayment (#353) --- x/cdp/keeper/cdp.go | 2 +- x/cdp/keeper/draw.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/x/cdp/keeper/cdp.go b/x/cdp/keeper/cdp.go index 3fb52fe4..5d729263 100644 --- a/x/cdp/keeper/cdp.go +++ b/x/cdp/keeper/cdp.go @@ -110,7 +110,7 @@ func (k Keeper) MintDebtCoins(ctx sdk.Context, moduleAccount string, denom strin return nil } -// BurnDebtCoins burns debts coins from the cdp module account +// BurnDebtCoins burns debt coins from the cdp module account func (k Keeper) BurnDebtCoins(ctx sdk.Context, moduleAccount string, denom string, paymentCoins sdk.Coins) sdk.Error { coinsToBurn := sdk.NewCoins() for _, pc := range paymentCoins { diff --git a/x/cdp/keeper/draw.go b/x/cdp/keeper/draw.go index 54abede7..94665f5b 100644 --- a/x/cdp/keeper/draw.go +++ b/x/cdp/keeper/draw.go @@ -110,7 +110,16 @@ func (k Keeper) RepayPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom stri } // burn the corresponding amount of debt coins - err = k.BurnDebtCoins(ctx, types.ModuleName, k.GetDebtDenom(ctx), feePayment.Add(principalPayment)) + cdpDebt := k.getModAccountDebt(ctx, types.ModuleName) + paymentAmount := sdk.ZeroInt() + for _, c := range feePayment.Add(principalPayment) { + paymentAmount = paymentAmount.Add(c.Amount) + } + coinsToBurn := sdk.NewCoins(sdk.NewCoin(k.GetDebtDenom(ctx), paymentAmount)) + if paymentAmount.GT(cdpDebt) { + coinsToBurn = sdk.NewCoins(sdk.NewCoin(k.GetDebtDenom(ctx), cdpDebt)) + } + err = k.BurnDebtCoins(ctx, types.ModuleName, k.GetDebtDenom(ctx), coinsToBurn) if err != nil { panic(err) }