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

Fix shares naming discrepancy #212

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ contract Blue is IFlashLender {
// 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 is IFlashLender {
_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 @@ -160,7 +160,7 @@ contract Blue is IFlashLender {
_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 @@ -181,7 +181,7 @@ contract Blue is IFlashLender {
_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 @@ -200,7 +200,7 @@ contract Blue is IFlashLender {
_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 @@ -264,19 +264,19 @@ contract Blue is IFlashLender {
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 @@ -347,7 +347,7 @@ contract Blue is IFlashLender {
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 @@ -358,7 +358,7 @@ contract Blue is IFlashLender {
// 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 @@ -372,7 +372,7 @@ contract Blue is IFlashLender {
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 @@ -98,7 +98,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 @@ -107,7 +107,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 @@ -292,7 +292,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 @@ -311,7 +311,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 @@ -334,7 +334,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 All @@ -453,7 +453,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 @@ -600,11 +600,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 @@ -828,7 +831,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