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

test(forge): merge receiver tests #200

Merged
merged 2 commits into from
Jul 31, 2023
Merged
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
70 changes: 13 additions & 57 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ contract BlueTest is
assertEq(borrowableAsset.balanceOf(address(blue)), amount, "blue balance");
}

function testBorrow(uint256 amountLent, uint256 amountBorrowed) public {
function testBorrow(uint256 amountLent, uint256 amountBorrowed, address receiver) public {
vm.assume(receiver != address(blue));
amountLent = bound(amountLent, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 1, 2 ** 64);

Expand All @@ -324,26 +325,10 @@ contract BlueTest is
if (amountBorrowed > amountLent) {
vm.prank(BORROWER);
vm.expectRevert(bytes(Errors.INSUFFICIENT_LIQUIDITY));
blue.borrow(market, amountBorrowed, BORROWER, BORROWER);
blue.borrow(market, amountBorrowed, BORROWER, receiver);
return;
}

vm.prank(BORROWER);
blue.borrow(market, amountBorrowed, BORROWER, BORROWER);

assertEq(blue.borrowShare(id, BORROWER), amountBorrowed * SharesMath.VIRTUAL_SHARES, "borrow share");
assertEq(borrowableAsset.balanceOf(BORROWER), amountBorrowed, "BORROWER balance");
assertEq(borrowableAsset.balanceOf(address(blue)), amountLent - amountBorrowed, "blue balance");
}

function testBorrowReceiver(uint256 amountLent, uint256 amountBorrowed, address receiver) public {
vm.assume(receiver != address(blue));
amountLent = bound(amountLent, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 1, amountLent);

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

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

Expand All @@ -352,7 +337,10 @@ contract BlueTest is
assertEq(borrowableAsset.balanceOf(address(blue)), amountLent - amountBorrowed, "blue balance");
}

function testWithdraw(uint256 amountLent, uint256 amountWithdrawn, uint256 amountBorrowed) public {
function testWithdraw(uint256 amountLent, uint256 amountWithdrawn, uint256 amountBorrowed, address receiver)
public
{
vm.assume(receiver != address(blue));
amountLent = bound(amountLent, 1, 2 ** 64);
amountWithdrawn = bound(amountWithdrawn, 1, 2 ** 64);
amountBorrowed = bound(amountBorrowed, 1, 2 ** 64);
Expand All @@ -370,43 +358,24 @@ contract BlueTest is
} else {
vm.expectRevert(bytes(Errors.INSUFFICIENT_LIQUIDITY));
}
blue.withdraw(market, amountWithdrawn, address(this), address(this));
blue.withdraw(market, amountWithdrawn, address(this), receiver);
return;
}

blue.withdraw(market, amountWithdrawn, address(this), address(this));
blue.withdraw(market, amountWithdrawn, address(this), receiver);

assertApproxEqAbs(
blue.supplyShare(id, address(this)),
(amountLent - amountWithdrawn) * SharesMath.VIRTUAL_SHARES,
100,
"supply share"
);
assertEq(borrowableAsset.balanceOf(address(this)), amountWithdrawn, "this balance");
assertEq(borrowableAsset.balanceOf(receiver), amountWithdrawn, "receiver balance");
assertEq(
borrowableAsset.balanceOf(address(blue)), amountLent - amountBorrowed - amountWithdrawn, "blue balance"
);
}

function testWithdrawReceiver(uint256 amountLent, uint256 amountWithdrawn, address receiver) public {
vm.assume(receiver != address(blue));
amountLent = bound(amountLent, 1, 2 ** 64);
amountWithdrawn = bound(amountWithdrawn, 1, amountLent);

borrowableAsset.setBalance(address(this), amountLent);
blue.supply(market, amountLent, address(this), hex"");
blue.withdraw(market, amountWithdrawn, address(this), receiver);

assertApproxEqAbs(
blue.supplyShare(id, address(this)),
(amountLent - amountWithdrawn) * SharesMath.VIRTUAL_SHARES,
100,
"supply share"
);
assertEq(borrowableAsset.balanceOf(receiver), amountWithdrawn, "receiver balance");
assertEq(borrowableAsset.balanceOf(address(blue)), amountLent - amountWithdrawn, "blue balance");
}

function testCollateralRequirements(
uint256 amountCollateral,
uint256 amountBorrowed,
Expand Down Expand Up @@ -503,7 +472,8 @@ contract BlueTest is
assertEq(collateralAsset.balanceOf(address(blue)), amount, "blue balance");
}

function testWithdrawCollateral(uint256 amountDeposited, uint256 amountWithdrawn) public {
function testWithdrawCollateral(uint256 amountDeposited, uint256 amountWithdrawn, address receiver) public {
vm.assume(receiver != address(blue));
amountDeposited = bound(amountDeposited, 1, 2 ** 64);
amountWithdrawn = bound(amountWithdrawn, 1, 2 ** 64);

Expand All @@ -512,24 +482,10 @@ contract BlueTest is

if (amountWithdrawn > amountDeposited) {
vm.expectRevert(stdError.arithmeticError);
blue.withdrawCollateral(market, amountWithdrawn, address(this), address(this));
blue.withdrawCollateral(market, amountWithdrawn, address(this), receiver);
return;
}

blue.withdrawCollateral(market, amountWithdrawn, address(this), address(this));

assertEq(blue.collateral(id, address(this)), amountDeposited - amountWithdrawn, "this collateral");
assertEq(collateralAsset.balanceOf(address(this)), amountWithdrawn, "this balance");
assertEq(collateralAsset.balanceOf(address(blue)), amountDeposited - amountWithdrawn, "blue balance");
}

function testWithdrawCollateral(uint256 amountDeposited, uint256 amountWithdrawn, address receiver) public {
vm.assume(receiver != address(blue));
amountDeposited = bound(amountDeposited, 1, 2 ** 64);
amountWithdrawn = bound(amountWithdrawn, 1, amountDeposited);

collateralAsset.setBalance(address(this), amountDeposited);
blue.supplyCollateral(market, amountDeposited, address(this), hex"");
blue.withdrawCollateral(market, amountWithdrawn, address(this), receiver);

assertEq(blue.collateral(id, address(this)), amountDeposited - amountWithdrawn, "this collateral");
Expand Down