-
Notifications
You must be signed in to change notification settings - Fork 95
feat: compound interest #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eca31f2
feat: compound interest
pakim249CAL 54bbd92
Update test/forge/Math.t.sol
pakim249CAL 4f0aacf
test: increase tolerance
pakim249CAL 1deabf9
Merge branch 'feat/compound-1' of github.com:morpho-labs/blue into fe…
pakim249CAL a464d94
Update test/forge/Math.t.sol
pakim249CAL 5f0a829
test: fix comparison and implement suggestion
pakim249CAL 4c7997c
test: format
pakim249CAL 1e28b1c
refactor: use suggested
pakim249CAL 2604061
refactor: make terms clearer
pakim249CAL 1e74522
refactor: revert compounding change
pakim249CAL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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); | ||
MathisGD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.