Skip to content
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

Refactor: use morpho utils #90

Closed
wants to merge 7 commits into from
Closed

Refactor: use morpho utils #90

wants to merge 7 commits into from

Conversation

Rubilmax
Copy link
Contributor

@Rubilmax Rubilmax commented Jul 10, 2023

This PR is a draft because tests need to be updated with regard to rounding directions. I just want to make sure we're on the same page on this before.

@MathisGD @QGarchery please check rounding directions

@Rubilmax Rubilmax changed the base branch from main to dev July 10, 2023 08:59
src/Blue.sol Outdated Show resolved Hide resolved
src/Blue.sol Outdated Show resolved Hide resolved
src/Blue.sol Outdated
@@ -243,7 +245,7 @@ contract Blue {
if (marketTotalSupply != 0) {
uint marketTotalBorrow = totalBorrow[id];
uint borrowRate = market.irm.borrowRate(market);
uint accruedInterests = marketTotalBorrow.wMul(borrowRate).wMul(block.timestamp - lastUpdate[id]);
uint accruedInterests = marketTotalBorrow.wadMulDown(borrowRate * (block.timestamp - lastUpdate[id]));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Borrow rate would typically be 0.001% per second, in WAD. So we should simply multiply the borrow rate by the seconds elapsed, to get a WAD-based % of interests to accrue, that we WAD-multiply with the total borrow to get the quantity of interests accrued.

@Rubilmax Rubilmax marked this pull request as ready for review July 10, 2023 09:58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm against using WadRayMath from morpho-utils, as is at least. We don't need Ray maths, and some other things from it. It would prefer to copy only the used functions (strict minimum) for this particular repo.

Copy link
Contributor

@MathisGD MathisGD Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plus, I like the naming wDiv/wMul (rounding to add) as there is no ambiguity in this code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can rename function from a lib instead of copy pasting

src/Blue.sol Outdated Show resolved Hide resolved
src/Blue.sol Outdated Show resolved Hide resolved
QGarchery
QGarchery previously approved these changes Jul 10, 2023
test/forge/Blue.t.sol Outdated Show resolved Hide resolved
src/Blue.sol Outdated Show resolved Hide resolved
src/Blue.sol Outdated Show resolved Hide resolved
src/Blue.sol Outdated Show resolved Hide resolved
makcandrov
makcandrov previously approved these changes Jul 10, 2023
Copy link
Contributor

@makcandrov makcandrov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the shares computations should be internalized in a library (like @Rubilmax did in its POC here)

@Rubilmax
Copy link
Contributor Author

I think the shares computations should be internalized in a library (like @Rubilmax did in its POC here)

Tracked by #64 and implemented in #94

@@ -111,7 +111,7 @@ contract Blue {
supplyShare[id][msg.sender] = WAD;
totalSupplyShares[id] = WAD;
} else {
uint256 shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
uint256 shares = amount.wadDivDown(totalSupply[id]).wadMulDown(totalSupplyShares[id]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering... isn't this basically equivalent to amount.mulDivDown(totalSupplyShares, totalSupply)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change the order @Rubilmax ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MerlinEgalite discussed here: #90 (comment)

@QGarchery QGarchery mentioned this pull request Jul 10, 2023
Merged
@@ -111,7 +111,7 @@ contract Blue {
supplyShare[id][msg.sender] = WAD;
totalSupplyShares[id] = WAD;
} else {
uint256 shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
uint256 shares = amount.wadDivDown(totalSupply[id]).wadMulDown(totalSupplyShares[id]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change the order @Rubilmax ?

src/Blue.sol Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can rename function from a lib instead of copy pasting

src/Blue.sol Show resolved Hide resolved
This was linked to issues Jul 11, 2023
@MerlinEgalite MerlinEgalite deleted the branch dev July 11, 2023 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix roundings Use optimized WadRayMath
6 participants