Cache redundant calculation in cdp RepayPrinciple (#569)

* fix exhibit 29 and remove redundant calcuation

* use better naming (totalPrincipal instead of totalDebt) and add comment
for future maintainers
This commit is contained in:
Nick DeLuca 2020-06-11 15:14:47 -05:00 committed by GitHub
parent 0ddae8609e
commit 3e63356c58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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