Skip to content

Commit

Permalink
Merge pull request #212 from morpho-labs/style/shares-naming-discrepancy
Browse files Browse the repository at this point in the history
Fix shares naming discrepancy
  • Loading branch information
MathisGD authored Aug 2, 2023
2 parents 24ef6a8 + 0ff8079 commit c1f5a9b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
26 changes: 13 additions & 13 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ contract Blue {
// Fee recipient.
address public feeRecipient;
// User' supply balances.
mapping(Id => mapping(address => uint256)) public supplyShare;
mapping(Id => mapping(address => uint256)) public supplyShares;
// User' borrow balances.
mapping(Id => mapping(address => uint256)) public borrowShare;
mapping(Id => mapping(address => uint256)) public borrowShares;
// User' collateral balance.
mapping(Id => mapping(address => uint256)) public collateral;
// Market total supply.
Expand Down Expand Up @@ -141,7 +141,7 @@ contract Blue {
_accrueInterests(market, id);

uint256 shares = amount.toSharesDown(totalSupply[id], totalSupplyShares[id]);
supplyShare[id][onBehalf] += shares;
supplyShares[id][onBehalf] += shares;
totalSupplyShares[id] += shares;

totalSupply[id] += amount;
Expand All @@ -162,7 +162,7 @@ contract Blue {
_accrueInterests(market, id);

uint256 shares = amount.toSharesUp(totalSupply[id], totalSupplyShares[id]);
supplyShare[id][onBehalf] -= shares;
supplyShares[id][onBehalf] -= shares;
totalSupplyShares[id] -= shares;

totalSupply[id] -= amount;
Expand All @@ -185,7 +185,7 @@ contract Blue {
_accrueInterests(market, id);

uint256 shares = amount.toSharesUp(totalBorrow[id], totalBorrowShares[id]);
borrowShare[id][onBehalf] += shares;
borrowShares[id][onBehalf] += shares;
totalBorrowShares[id] += shares;

totalBorrow[id] += amount;
Expand All @@ -205,7 +205,7 @@ contract Blue {
_accrueInterests(market, id);

uint256 shares = amount.toSharesDown(totalBorrow[id], totalBorrowShares[id]);
borrowShare[id][onBehalf] -= shares;
borrowShares[id][onBehalf] -= shares;
totalBorrowShares[id] -= shares;

totalBorrow[id] -= amount;
Expand Down Expand Up @@ -272,19 +272,19 @@ contract Blue {
uint256 repaid = seized.mulWadUp(collateralPrice).divWadUp(incentive).divWadUp(borrowablePrice);
uint256 repaidShares = repaid.toSharesDown(totalBorrow[id], totalBorrowShares[id]);

borrowShare[id][borrower] -= repaidShares;
borrowShares[id][borrower] -= repaidShares;
totalBorrowShares[id] -= repaidShares;
totalBorrow[id] -= repaid;

collateral[id][borrower] -= seized;

// Realize the bad debt if needed.
if (collateral[id][borrower] == 0) {
uint256 badDebt = borrowShare[id][borrower].toAssetsUp(totalBorrow[id], totalBorrowShares[id]);
uint256 badDebt = borrowShares[id][borrower].toAssetsUp(totalBorrow[id], totalBorrowShares[id]);
totalSupply[id] -= badDebt;
totalBorrow[id] -= badDebt;
totalBorrowShares[id] -= borrowShare[id][borrower];
borrowShare[id][borrower] = 0;
totalBorrowShares[id] -= borrowShares[id][borrower];
borrowShares[id][borrower] = 0;
}

market.collateralAsset.safeTransfer(msg.sender, seized);
Expand Down Expand Up @@ -354,7 +354,7 @@ contract Blue {
uint256 feeAmount = accruedInterests.mulWadDown(fee[id]);
// The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated.
uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount);
supplyShare[id][feeRecipient] += feeShares;
supplyShares[id][feeRecipient] += feeShares;
totalSupplyShares[id] += feeShares;
}
}
Expand All @@ -365,7 +365,7 @@ contract Blue {
// Health check.

function _isHealthy(Market memory market, Id id, address user) internal view returns (bool) {
if (borrowShare[id][user] == 0) return true;
if (borrowShares[id][user] == 0) return true;

uint256 collateralPrice = market.collateralOracle.price();
uint256 borrowablePrice = market.borrowableOracle.price();
Expand All @@ -379,7 +379,7 @@ contract Blue {
returns (bool)
{
uint256 borrowValue =
borrowShare[id][user].toAssetsUp(totalBorrow[id], totalBorrowShares[id]).mulWadUp(borrowablePrice);
borrowShares[id][user].toAssetsUp(totalBorrow[id], totalBorrowShares[id]).mulWadUp(borrowablePrice);
uint256 collateralValue = collateral[id][user].mulWadDown(collateralPrice);

return collateralValue.mulWadDown(market.lltv) >= borrowValue;
Expand Down
25 changes: 14 additions & 11 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ contract BlueTest is
}

function supplyBalance(address user) internal view returns (uint256) {
uint256 supplyShares = blue.supplyShare(id, user);
uint256 supplyShares = blue.supplyShares(id, user);
if (supplyShares == 0) return 0;

uint256 totalShares = blue.totalSupplyShares(id);
Expand All @@ -104,7 +104,7 @@ contract BlueTest is
}

function borrowBalance(address user) internal view returns (uint256) {
uint256 borrowerShares = blue.borrowShare(id, user);
uint256 borrowerShares = blue.borrowShares(id, user);
if (borrowerShares == 0) return 0;

uint256 totalShares = blue.totalBorrowShares(id);
Expand Down Expand Up @@ -289,7 +289,7 @@ contract BlueTest is
uint256 expectedFee = accrued.mulWadDown(fee);
uint256 expectedFeeShares = expectedFee.mulDivDown(totalSupplySharesBefore, totalSupplyAfter - expectedFee);

assertEq(blue.supplyShare(id, recipient), expectedFeeShares);
assertEq(blue.supplyShares(id, recipient), expectedFeeShares);
}

function testCreateMarketWithNotEnabledLltv(Market memory marketFuzz) public {
Expand All @@ -309,7 +309,7 @@ contract BlueTest is
borrowableAsset.setBalance(address(this), amount);
blue.supply(market, amount, onBehalf, hex"");

assertEq(blue.supplyShare(id, onBehalf), amount * SharesMath.VIRTUAL_SHARES, "supply share");
assertEq(blue.supplyShares(id, onBehalf), amount * SharesMath.VIRTUAL_SHARES, "supply share");
assertEq(borrowableAsset.balanceOf(onBehalf), 0, "lender balance");
assertEq(borrowableAsset.balanceOf(address(blue)), amount, "blue balance");
}
Expand All @@ -333,7 +333,7 @@ contract BlueTest is
vm.prank(BORROWER);
blue.borrow(market, amountBorrowed, BORROWER, receiver);

assertEq(blue.borrowShare(id, BORROWER), amountBorrowed * SharesMath.VIRTUAL_SHARES, "borrow share");
assertEq(blue.borrowShares(id, BORROWER), amountBorrowed * SharesMath.VIRTUAL_SHARES, "borrow share");
assertEq(borrowableAsset.balanceOf(receiver), amountBorrowed, "receiver balance");
assertEq(borrowableAsset.balanceOf(address(blue)), amountLent - amountBorrowed, "blue balance");
}
Expand Down Expand Up @@ -367,7 +367,7 @@ contract BlueTest is
blue.withdraw(market, amountWithdrawn, address(this), receiver);

assertApproxEqAbs(
blue.supplyShare(id, address(this)),
blue.supplyShares(id, address(this)),
(amountLent - amountWithdrawn) * SharesMath.VIRTUAL_SHARES,
100,
"supply share"
Expand Down Expand Up @@ -426,7 +426,7 @@ contract BlueTest is
vm.stopPrank();

assertApproxEqAbs(
blue.borrowShare(id, BORROWER),
blue.borrowShares(id, BORROWER),
(amountBorrowed - amountRepaid) * SharesMath.VIRTUAL_SHARES,
100,
"borrow share"
Expand Down Expand Up @@ -454,7 +454,7 @@ contract BlueTest is
blue.repay(market, amountRepaid, onBehalf, hex"");

assertApproxEqAbs(
blue.borrowShare(id, onBehalf),
blue.borrowShares(id, onBehalf),
(amountBorrowed - amountRepaid) * SharesMath.VIRTUAL_SHARES,
100,
"borrow share"
Expand Down Expand Up @@ -603,11 +603,14 @@ contract BlueTest is

assertApproxEqAbs(supplyBalance(address(this)), firstAmount, 100, "same balance first user");
assertEq(
blue.supplyShare(id, address(this)), firstAmount * SharesMath.VIRTUAL_SHARES, "expected shares first user"
blue.supplyShares(id, address(this)), firstAmount * SharesMath.VIRTUAL_SHARES, "expected shares first user"
);
assertApproxEqAbs(supplyBalance(BORROWER), secondAmount, 100, "same balance second user");
assertApproxEqAbs(
blue.supplyShare(id, BORROWER), secondAmount * SharesMath.VIRTUAL_SHARES, 100, "expected shares second user"
blue.supplyShares(id, BORROWER),
secondAmount * SharesMath.VIRTUAL_SHARES,
100,
"expected shares second user"
);
}

Expand Down Expand Up @@ -851,7 +854,7 @@ contract BlueTest is
blue.supplyCollateral(
market, amount, address(this), abi.encode(this.testFlashActions.selector, abi.encode(toBorrow))
);
assertGt(blue.borrowShare(market.id(), address(this)), 0, "no borrow");
assertGt(blue.borrowShares(market.id(), address(this)), 0, "no borrow");

blue.repay(market, toBorrow, address(this), abi.encode(this.testFlashActions.selector, abi.encode(amount)));
assertEq(blue.collateral(market.id(), address(this)), 0, "no withdraw collateral");
Expand Down

0 comments on commit c1f5a9b

Please sign in to comment.