Skip to content

Commit

Permalink
test: improve test borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
makcandrov committed Aug 3, 2023
1 parent 4ce8f65 commit 26a34c9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 67 deletions.
9 changes: 9 additions & 0 deletions test/forge/BlueBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,18 @@ contract BlueBaseTest is Test {
return borrowerShares.divWadUp(totalShares).mulWadUp(totalBorrow);
}

function _provideLiquidity(uint256 amount) internal {
borrowableAsset.setBalance(address(this), amount);
blue.supply(market, amount, address(this), hex"");
}

function neq(Market memory a, Market memory b) internal pure returns (bool) {
return a.borrowableAsset != b.borrowableAsset || a.collateralAsset != b.collateralAsset
|| a.borrowableOracle != b.borrowableOracle || a.collateralOracle != b.collateralOracle || a.lltv != b.lltv
|| a.irm != b.irm;
}

function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
}
130 changes: 63 additions & 67 deletions test/forge/integration/TestIntegrationBorrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@ contract IntegrationBorrowTest is BlueBaseTest {
function testBorrowUnhealthyPosition(
uint256 amountCollateral,
uint256 amountBorrowed,
uint256 priceCollateral,
uint256 priceBorrowable
uint256 priceCollateral
) public {
amountBorrowed = bound(amountBorrowed, 1, 2 ** 64);
priceBorrowable = bound(priceBorrowable, 0, 2 ** 64);
amountCollateral = bound(amountCollateral, 1, 2 ** 64);
priceCollateral = bound(priceCollateral, 0, 2 ** 64);
priceCollateral = bound(priceCollateral, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 10, 2 ** 64);

uint256 maxCollateral = amountBorrowed.divWadDown(priceCollateral).divWadDown(market.lltv);
vm.assume(maxCollateral != 0);

vm.assume(
amountCollateral.mulWadDown(priceCollateral).mulWadDown(market.lltv)
< amountBorrowed.mulWadUp(priceBorrowable)
);
amountCollateral = bound(amountBorrowed, 1, maxCollateral);

borrowableOracle.setPrice(priceBorrowable);
borrowableOracle.setPrice(FixedPointMathLib.WAD);
collateralOracle.setPrice(priceCollateral);

borrowableAsset.setBalance(address(this), amountBorrowed);
Expand All @@ -73,22 +70,18 @@ contract IntegrationBorrowTest is BlueBaseTest {
uint256 amountCollateral,
uint256 amountSupplied,
uint256 amountBorrowed,
uint256 priceCollateral,
uint256 priceBorrowable
uint256 priceCollateral
) public {
amountSupplied = bound(amountSupplied, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 1, 2 ** 64);
priceBorrowable = bound(priceBorrowable, 0, 2 ** 64);
amountCollateral = bound(amountCollateral, 1, 2 ** 64);
priceCollateral = bound(priceCollateral, 0, 2 ** 64);

vm.assume(
amountCollateral.mulWadDown(priceCollateral).mulWadDown(market.lltv)
>= amountBorrowed.mulWadUp(priceBorrowable)
);
vm.assume(amountSupplied < amountBorrowed);

borrowableOracle.setPrice(priceBorrowable);
priceCollateral = bound(priceCollateral, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 10, 2 ** 64);
amountSupplied = bound(amountSupplied, 1, amountBorrowed - 1);

uint256 minCollateral = amountBorrowed.divWadUp(market.lltv).divWadUp(priceCollateral);
vm.assume(minCollateral != 0);

amountCollateral = bound(amountCollateral, minCollateral, max(minCollateral, 2 ** 64));

borrowableOracle.setPrice(FixedPointMathLib.WAD);
collateralOracle.setPrice(priceCollateral);

borrowableAsset.setBalance(address(this), amountSupplied);
Expand All @@ -108,21 +101,21 @@ contract IntegrationBorrowTest is BlueBaseTest {
uint256 amountSupplied,
uint256 amountBorrowed,
uint256 priceCollateral,
uint256 priceBorrowable
address receiver
) public {
amountSupplied = bound(amountSupplied, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 1, 2 ** 64);
priceBorrowable = bound(priceBorrowable, 0, 2 ** 64);
amountCollateral = bound(amountCollateral, 1, 2 ** 64);
priceCollateral = bound(priceCollateral, 0, 2 ** 64);

vm.assume(
amountCollateral.mulWadDown(priceCollateral).mulWadDown(market.lltv)
>= amountBorrowed.mulWadUp(priceBorrowable)
);
vm.assume(amountSupplied >= amountBorrowed);

borrowableOracle.setPrice(priceBorrowable);
vm.assume(receiver != address(0));
vm.assume(receiver != address(blue));

priceCollateral = bound(priceCollateral, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 10, 2 ** 64);
amountSupplied = bound(amountSupplied, amountBorrowed, 2 ** 64);

uint256 minCollateral = amountBorrowed.divWadUp(market.lltv).divWadUp(priceCollateral);
vm.assume(minCollateral != 0);

amountCollateral = bound(amountCollateral, minCollateral, max(minCollateral, 2 ** 64));

borrowableOracle.setPrice(FixedPointMathLib.WAD);
collateralOracle.setPrice(priceCollateral);

borrowableAsset.setBalance(address(this), amountSupplied);
Expand All @@ -132,13 +125,13 @@ contract IntegrationBorrowTest is BlueBaseTest {

vm.startPrank(BORROWER);
blue.supplyCollateral(market, amountCollateral, BORROWER, hex"");
blue.borrow(market, amountBorrowed, BORROWER, BORROWER);

blue.borrow(market, amountBorrowed, BORROWER, receiver);
vm.stopPrank();

assertEq(blue.totalBorrow(id), amountBorrowed, "total borrow");
assertEq(blue.borrowShares(id, BORROWER), amountBorrowed * SharesMath.VIRTUAL_SHARES, "borrow shares");
assertEq(borrowableAsset.balanceOf(BORROWER), amountBorrowed, "borrower balance");
assertEq(borrowableAsset.balanceOf(address(this)), 0, "lender balance");
assertEq(borrowableAsset.balanceOf(receiver), amountBorrowed, "borrower balance");
assertEq(borrowableAsset.balanceOf(address(blue)), amountSupplied - amountBorrowed, "blue balance");
}

Expand All @@ -147,37 +140,40 @@ contract IntegrationBorrowTest is BlueBaseTest {
uint256 amountSupplied,
uint256 amountBorrowed,
uint256 priceCollateral,
uint256 priceBorrowable
address onBehalf,
address receiver
) public {
amountSupplied = bound(amountSupplied, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 1, 2 ** 64);
priceBorrowable = bound(priceBorrowable, 0, 2 ** 64);
amountCollateral = bound(amountCollateral, 1, 2 ** 64);
priceCollateral = bound(priceCollateral, 0, 2 ** 64);

vm.assume(
amountCollateral.mulWadDown(priceCollateral).mulWadDown(market.lltv)
>= amountBorrowed.mulWadUp(priceBorrowable)
);
vm.assume(amountSupplied >= amountBorrowed);

borrowableOracle.setPrice(priceBorrowable);
vm.assume(onBehalf != address(0));
vm.assume(receiver != address(0));
vm.assume(receiver != address(blue));

priceCollateral = bound(priceCollateral, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 10, 2 ** 64);
amountSupplied = bound(amountSupplied, amountBorrowed, 2 ** 64);

_provideLiquidity(amountSupplied);

uint256 minCollateral = amountBorrowed.divWadUp(market.lltv).divWadUp(priceCollateral);
vm.assume(minCollateral != 0);

amountCollateral = bound(amountCollateral, minCollateral, max(minCollateral, 2 ** 64));

borrowableOracle.setPrice(FixedPointMathLib.WAD);
collateralOracle.setPrice(priceCollateral);

borrowableAsset.setBalance(address(this), amountSupplied);
collateralAsset.setBalance(address(this), amountCollateral);
collateralAsset.setBalance(BORROWER, amountCollateral);

blue.supply(market, amountSupplied, address(this), hex"");
blue.supplyCollateral(market, amountCollateral, address(this), hex"");
blue.setAuthorization(BORROWER, true);
vm.startPrank(BORROWER);
blue.supplyCollateral(market, amountCollateral, BORROWER, hex"");
blue.setAuthorization(onBehalf, true);
vm.stopPrank();

vm.prank(BORROWER);
blue.borrow(market, amountBorrowed, address(this), address(this));
vm.prank(onBehalf);
blue.borrow(market, amountBorrowed, BORROWER, receiver);

assertEq(blue.totalBorrow(id), amountBorrowed, "total borrow");
assertEq(blue.borrowShares(id, address(this)), amountBorrowed * SharesMath.VIRTUAL_SHARES, "borrow shares");
assertEq(borrowableAsset.balanceOf(BORROWER), 0, "borrower balance");
assertEq(borrowableAsset.balanceOf(address(this)), amountBorrowed, "lender balance");
assertEq(blue.borrowShares(id, BORROWER), amountBorrowed * SharesMath.VIRTUAL_SHARES, "borrow shares");
assertEq(borrowableAsset.balanceOf(receiver), amountBorrowed, "borrower balance");
assertEq(borrowableAsset.balanceOf(address(blue)), amountSupplied - amountBorrowed, "blue balance");
}
}

0 comments on commit 26a34c9

Please sign in to comment.