-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from morpho-labs/feat/compound-1
feat: compound interest
- Loading branch information
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import "src/libraries/FixedPointMathLib.sol"; | ||
|
||
contract MathTest is Test { | ||
using FixedPointMathLib for uint256; | ||
|
||
function testWTaylorCompounded(uint256 rate, uint256 timeElapsed) public { | ||
// Assume rate is less than a ~500% APY. (~180% APR) | ||
vm.assume(rate < (FixedPointMathLib.WAD / 20_000_000) && timeElapsed < 365 days); | ||
uint256 result = rate.wTaylorCompounded(timeElapsed) + FixedPointMathLib.WAD; | ||
uint256 toCompare = wPow(FixedPointMathLib.WAD + rate, timeElapsed); | ||
assertLe(result, toCompare, "rate should be less than the compounded rate"); | ||
assertGe( | ||
result, FixedPointMathLib.WAD + timeElapsed * rate, "rate should be greater than the simple interest rate" | ||
); | ||
assertLe((toCompare - result) * 100_00 / toCompare, 8_00, "The error should be less than or equal to 8%"); | ||
} | ||
|
||
// Exponentiation by squaring with rounding up. | ||
function wPow(uint256 x, uint256 n) private pure returns (uint256 z) { | ||
z = FixedPointMathLib.WAD; | ||
for (; n != 0; n /= 2) { | ||
z = n % 2 != 0 ? z.mulWadUp(x) : z; | ||
x = x.mulWadUp(x); | ||
} | ||
} | ||
} |