From 8e538da8e12568b694ad737571022ee5f6a8125b Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Mon, 31 Jul 2023 11:24:42 +0200 Subject: [PATCH 1/2] test(forge): merge receiver tests --- test/forge/Blue.t.sol | 67 +++++++------------------------------------ 1 file changed, 10 insertions(+), 57 deletions(-) diff --git a/test/forge/Blue.t.sol b/test/forge/Blue.t.sol index 09a62ddc8..dc0864d06 100644 --- a/test/forge/Blue.t.sol +++ b/test/forge/Blue.t.sol @@ -314,7 +314,7 @@ 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 { amountLent = bound(amountLent, 1, 2 ** 64); amountBorrowed = bound(amountBorrowed, 1, 2 ** 64); @@ -324,26 +324,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); @@ -352,7 +336,9 @@ 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 + { amountLent = bound(amountLent, 1, 2 ** 64); amountWithdrawn = bound(amountWithdrawn, 1, 2 ** 64); amountBorrowed = bound(amountBorrowed, 1, 2 ** 64); @@ -370,11 +356,11 @@ 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)), @@ -382,31 +368,12 @@ contract BlueTest is 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, @@ -503,7 +470,7 @@ 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 { amountDeposited = bound(amountDeposited, 1, 2 ** 64); amountWithdrawn = bound(amountWithdrawn, 1, 2 ** 64); @@ -512,24 +479,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"); From aec6b4e25245e7e08f118c17b6977db04048ec33 Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Mon, 31 Jul 2023 11:55:04 +0200 Subject: [PATCH 2/2] test(forge): assume receiver not blue --- test/forge/Blue.t.sol | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/forge/Blue.t.sol b/test/forge/Blue.t.sol index dc0864d06..766ae2236 100644 --- a/test/forge/Blue.t.sol +++ b/test/forge/Blue.t.sol @@ -315,6 +315,7 @@ contract BlueTest is } 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); @@ -339,6 +340,7 @@ contract BlueTest is 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); @@ -471,6 +473,7 @@ contract BlueTest is } 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);