fix: avoid panic when closing cdp via repayment (#353)

This commit is contained in:
Kevin Davis 2020-01-30 18:50:42 -05:00 committed by GitHub
parent 3c8209cbcc
commit bdff81b2a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -110,7 +110,7 @@ func (k Keeper) MintDebtCoins(ctx sdk.Context, moduleAccount string, denom strin
return nil 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 { func (k Keeper) BurnDebtCoins(ctx sdk.Context, moduleAccount string, denom string, paymentCoins sdk.Coins) sdk.Error {
coinsToBurn := sdk.NewCoins() coinsToBurn := sdk.NewCoins()
for _, pc := range paymentCoins { for _, pc := range paymentCoins {

View File

@ -110,7 +110,16 @@ func (k Keeper) RepayPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom stri
} }
// burn the corresponding amount of debt coins // 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 { if err != nil {
panic(err) panic(err)
} }