Skip to content

Commit

Permalink
fix: issue repay amount and improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Aug 9, 2023
1 parent 0898a8d commit fa2e93c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ contract Blue is IBlue {
_accrueInterests(market, id);

if (shares > 0) amount = shares.toAssetsUp(totalBorrow[id], totalBorrowShares[id]);
else shares = shares.toSharesDown(totalBorrow[id], totalBorrowShares[id]);
else shares = amount.toSharesDown(totalBorrow[id], totalBorrowShares[id]);

borrowShares[id][onBehalf] -= shares;
totalBorrowShares[id] -= shares;
Expand Down
14 changes: 8 additions & 6 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,12 @@ contract BlueTest is
vm.assume(receiver != address(blue));
amountLent = bound(amountLent, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 1, 2 ** 64);
uint256 shares = amountBorrowed.toSharesUp(blue.totalBorrow(id), blue.totalBorrowShares(id));

borrowableAsset.setBalance(address(this), amountLent);
blue.supply(market, 0, amountLent, address(this), hex"");

uint256 collateralAmount = amountBorrowed.divWadUp(LLTV);
uint256 collateralAmount = shares.toAssetsUp(blue.totalBorrow(id), blue.totalBorrowShares(id)).divWadUp(LLTV);
collateralAsset.setBalance(address(this), collateralAmount);
blue.supplyCollateral(market, collateralAmount, BORROWER, hex"");

Expand All @@ -373,8 +374,7 @@ contract BlueTest is
borrowableAsset.setBalance(address(this), amount);
if (amount > 0) blue.supply(market, 0, amount, address(this), hex"");

uint256 collateralAmount = (amount + 1).divWadUp(LLTV);
console.log(amount);
uint256 collateralAmount = shares.toAssetsUp(blue.totalBorrow(id), blue.totalBorrowShares(id)).divWadUp(LLTV);
collateralAsset.setBalance(address(this), collateralAmount);
if (collateralAmount > 0) blue.supplyCollateral(market, collateralAmount, BORROWER, hex"");

Expand Down Expand Up @@ -511,11 +511,13 @@ contract BlueTest is

uint256 thisBalanceBefore = borrowableAsset.balanceOf(address(this));
uint256 borrowSharesBefore = blue.borrowShares(id, address(this));
exactAmountRepaid =
bound(exactAmountRepaid, 1, borrowSharesBefore.toAssetsUp(blue.totalBorrow(id), blue.totalBorrowShares(id)));
exactAmountRepaid = bound(
exactAmountRepaid, 1, borrowSharesBefore.toAssetsDown(blue.totalBorrow(id), blue.totalBorrowShares(id))
);
uint256 sharesRepaid = exactAmountRepaid.toSharesDown(blue.totalBorrow(id), blue.totalBorrowShares(id));
blue.repay(market, 0, exactAmountRepaid, address(this), hex"");

// assertEq(blue.borrowShares(id, address(this)), borrowSharesBefore - sharesRepaid, "borrow share");
assertEq(blue.borrowShares(id, address(this)), borrowSharesBefore - sharesRepaid, "borrow share");
assertEq(borrowableAsset.balanceOf(address(this)), thisBalanceBefore - exactAmountRepaid, "this balance");
assertEq(borrowableAsset.balanceOf(address(blue)), exactAmountRepaid, "blue balance");
}
Expand Down

0 comments on commit fa2e93c

Please sign in to comment.