From 9b0b1900197882cdcd8e768fb057fef1e4897b45 Mon Sep 17 00:00:00 2001 From: emilio Date: Tue, 11 Aug 2020 10:11:35 +0200 Subject: [PATCH] Fixed compounded interest approximation --- contracts/libraries/MathUtils.sol | 3 ++- test/helpers/utils/calculations.ts | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/contracts/libraries/MathUtils.sol b/contracts/libraries/MathUtils.sol index 7b0a8f32..1e26366d 100644 --- a/contracts/libraries/MathUtils.sol +++ b/contracts/libraries/MathUtils.sol @@ -57,7 +57,8 @@ library MathUtils { } uint256 expMinusOne = exp.sub(1); - uint256 expMinusTwo = exp.sub(2); + + uint256 expMinusTwo = exp > 2 ? exp.sub(2) : 0; uint256 ratePerSecond = _rate.div(31536000); diff --git a/test/helpers/utils/calculations.ts b/test/helpers/utils/calculations.ts index 2d5a6282..34c25069 100644 --- a/test/helpers/utils/calculations.ts +++ b/test/helpers/utils/calculations.ts @@ -1265,8 +1265,12 @@ const calcCompoundedInterest = ( const timeDifference = currentTimestamp.minus(lastUpdateTimestamp); + if(timeDifference.eq(0)){ + return new BigNumber(RAY); + } + const expMinusOne = timeDifference.minus(1); - const expMinusTwo = timeDifference.minus(2); + const expMinusTwo = timeDifference.gt(2) ? timeDifference.minus(2) : 0; const ratePerSecond = rate.div(ONE_YEAR);