fix: don't accumulate if borrow interest rounds to zero (#808)

This commit is contained in:
Kevin Davis 2021-02-09 10:11:12 -07:00 committed by GitHub
parent 58db05f8e2
commit 7d4235ca87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -120,6 +120,12 @@ func (k Keeper) AccrueInterest(ctx sdk.Context, denom string) error {
// Calculate borrow interest factor and update // Calculate borrow interest factor and update
borrowInterestFactor := CalculateBorrowInterestFactor(borrowRateSpy, sdk.NewInt(timeElapsed)) borrowInterestFactor := CalculateBorrowInterestFactor(borrowRateSpy, sdk.NewInt(timeElapsed))
interestBorrowAccumulated := (borrowInterestFactor.Mul(sdk.NewDecFromInt(borrowedPrior.Amount)).TruncateInt()).Sub(borrowedPrior.Amount) interestBorrowAccumulated := (borrowInterestFactor.Mul(sdk.NewDecFromInt(borrowedPrior.Amount)).TruncateInt()).Sub(borrowedPrior.Amount)
if interestBorrowAccumulated.IsZero() && borrowRateApy.IsPositive() {
// don't accumulate if borrow interest is rounding to zero
return nil
}
totalBorrowInterestAccumulated := sdk.NewCoins(sdk.NewCoin(denom, interestBorrowAccumulated)) totalBorrowInterestAccumulated := sdk.NewCoins(sdk.NewCoin(denom, interestBorrowAccumulated))
reservesNew := interestBorrowAccumulated.ToDec().Mul(mm.ReserveFactor).TruncateInt() reservesNew := interestBorrowAccumulated.ToDec().Mul(mm.ReserveFactor).TruncateInt()
borrowInterestFactorNew := borrowInterestFactorPrior.Mul(borrowInterestFactor) borrowInterestFactorNew := borrowInterestFactorPrior.Mul(borrowInterestFactor)