Skip to content

Commit

Permalink
Add fuzz tests for withdraw, maxWithdraw, and maxRedeem (#15)
Browse files Browse the repository at this point in the history
* Add fuzz test for withdraw

* Add tests for max withdraw and redeem

* Simplify setup in some tests
  • Loading branch information
kevincheng96 authored Oct 12, 2023
1 parent c5a67af commit af706b7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ docs/

# Dotenv file
.env

# Coverage
report/
lcov.info
62 changes: 38 additions & 24 deletions test/CometWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,34 @@ abstract contract CometWrapperTest is CoreTest, CometMath {
assertLe(bobPreviewedAssetsReceived, bobConvertToAssets);
}

function test_maxWithdraw(uint256 amount) public {
setUpAliceAndBobCometBalances();

amount = setUpFuzzTestAssumptions(amount);

vm.startPrank(cometHolder);
comet.allow(wrapperAddress, true);
uint256 sharesMinted = cometWrapper.deposit(amount, alice);
vm.stopPrank();

assertEq(cometWrapper.maxWithdraw(alice), cometWrapper.previewRedeem(sharesMinted));
assertEq(cometWrapper.maxWithdraw(alice), cometWrapper.underlyingBalance(alice));
}

function test_maxRedeem(uint256 amount) public {
setUpAliceAndBobCometBalances();

amount = setUpFuzzTestAssumptions(amount);

vm.startPrank(cometHolder);
comet.allow(wrapperAddress, true);
uint256 sharesMinted = cometWrapper.deposit(amount, alice);
vm.stopPrank();

assertEq(cometWrapper.maxRedeem(alice), sharesMinted);
assertEq(cometWrapper.maxRedeem(alice), cometWrapper.balanceOf(alice));
}

function test_nullifyInflationAttacks() public {
setUpAliceAndBobCometBalances();

Expand Down Expand Up @@ -378,17 +406,16 @@ abstract contract CometWrapperTest is CoreTest, CometMath {
assertLe(totalAssets, cometWrapper.totalAssets());
}

function test_withdraw() public {
function test_withdraw(uint256 amount1, uint256 amount2, uint256 aliceWithdrawAmount) public {
setUpAliceAndBobCometBalances();

vm.startPrank(alice);
comet.allow(wrapperAddress, true);
cometWrapper.deposit(9_101 * decimalScale, alice);
vm.stopPrank();
(amount1, amount2) = setUpFuzzTestAssumptions(amount1, amount2);
aliceWithdrawAmount = bound(aliceWithdrawAmount, 0, amount1);

vm.startPrank(bob);
vm.startPrank(cometHolder);
comet.allow(wrapperAddress, true);
cometWrapper.deposit(2_555 * decimalScale, bob);
cometWrapper.deposit(amount1, alice);
cometWrapper.deposit(amount2, bob);
vm.stopPrank();

assertEq(cometWrapper.totalAssets(), comet.balanceOf(wrapperAddress));
Expand All @@ -397,7 +424,7 @@ abstract contract CometWrapperTest is CoreTest, CometMath {
assertEq(cometWrapper.totalAssets(), comet.balanceOf(wrapperAddress));

vm.prank(alice);
cometWrapper.withdraw(173 * decimalScale, alice, alice);
cometWrapper.withdraw(aliceWithdrawAmount, alice, alice);
assertEq(cometWrapper.totalAssets(), comet.balanceOf(wrapperAddress));

skip(500 days);
Expand All @@ -420,7 +447,7 @@ abstract contract CometWrapperTest is CoreTest, CometMath {
assertEq(cometWrapper.totalSupply(), unsigned104(comet.userBasic(wrapperAddress).principal));
assertEq(cometWrapper.totalAssets(), comet.balanceOf(wrapperAddress));
assertEq(cometWrapper.underlyingBalance(alice), 0);
assertApproxEqAbs(comet.balanceOf(alice), aliceCometBalance + aliceAssets, 1);
assertApproxEqAbs(comet.balanceOf(alice), aliceCometBalance + aliceAssets, 2);
assertLe(comet.balanceOf(alice), aliceCometBalance + aliceAssets);

vm.startPrank(bob);
Expand All @@ -432,7 +459,7 @@ abstract contract CometWrapperTest is CoreTest, CometMath {
assertEq(cometWrapper.totalSupply(), unsigned104(comet.userBasic(wrapperAddress).principal));
assertEq(cometWrapper.totalAssets(), comet.balanceOf(wrapperAddress));
assertEq(cometWrapper.underlyingBalance(bob), 0);
assertApproxEqAbs(comet.balanceOf(bob), bobCometBalance + bobAssets, 1);
assertApproxEqAbs(comet.balanceOf(bob), bobCometBalance + bobAssets, 2);
assertLe(comet.balanceOf(bob), bobCometBalance + bobAssets);
}

Expand Down Expand Up @@ -628,19 +655,9 @@ abstract contract CometWrapperTest is CoreTest, CometMath {

(amount1, amount2) = setUpFuzzTestAssumptions(amount1, amount2);

vm.prank(cometHolder);
comet.transfer(alice, amount1);

vm.prank(cometHolder);
comet.transfer(bob, amount2);

vm.startPrank(alice);
vm.startPrank(cometHolder);
comet.allow(wrapperAddress, true);
cometWrapper.deposit(amount1, alice);
vm.stopPrank();

vm.startPrank(bob);
comet.allow(wrapperAddress, true);
cometWrapper.deposit(amount2, bob);
vm.stopPrank();

Expand Down Expand Up @@ -948,6 +965,3 @@ abstract contract CometWrapperTest is CoreTest, CometMath {
vm.stopPrank();
}
}

// TODO: add fuzz testing for withdraw
// TODO: add tests for max withdraw/redeem
2 changes: 1 addition & 1 deletion test/Rewards.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ abstract contract RewardsTest is CoreTest {

enableRewardsAccrual();
// Make sure CometRewards has ample COMP to claim
deal(address(comp), address(cometRewards), 10_000_000 ether);
deal(address(comp), address(cometRewards), 100_000_000 ether);

// Make amount an even number so it can be divided equally by 2
if (aliceAmount % 2 != 0) aliceAmount -= 1;
Expand Down

0 comments on commit af706b7

Please sign in to comment.