diff --git a/x/cdp/keeper/draw.go b/x/cdp/keeper/draw.go index 94727045..193646fd 100644 --- a/x/cdp/keeper/draw.go +++ b/x/cdp/keeper/draw.go @@ -85,8 +85,11 @@ func (k Keeper) RepayPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom stri return err } + // Note: assumes cdp.Principal and cdp.AccumulatedFees don't change during calculations + totalPrincipal := cdp.Principal.Add(cdp.AccumulatedFees) + // calculate fee and principal payment - feePayment, principalPayment := k.calculatePayment(ctx, cdp.Principal.Add(cdp.AccumulatedFees), cdp.AccumulatedFees, payment) + feePayment, principalPayment := k.calculatePayment(ctx, totalPrincipal, cdp.AccumulatedFees, payment) err = k.validatePrincipalPayment(ctx, cdp, principalPayment) if err != nil { @@ -131,7 +134,7 @@ func (k Keeper) RepayPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom stri ) // remove the old collateral:debt ratio index - oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) + oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, totalPrincipal) k.RemoveCdpCollateralRatioIndex(ctx, denom, cdp.ID, oldCollateralToDebtRatio) // update cdp state