Skip to content

Commit

Permalink
Merge branch 'master' into testing-8-min-deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
sambacha authored Aug 9, 2024
2 parents d5f2941 + 6c60fa5 commit 6cf8bad
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/BaseCaptiveTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ contract BaseCaptiveTest is Test {
error WithdrawFailed();
error DepositAmountBelowMinimum();
error WithdrawalCooldownPeriodNotMet();
error WithdrawProRata();
error DepositCapReached();

FoldCaptiveStaking public foldCaptiveStaking;

Expand Down
91 changes: 91 additions & 0 deletions test/UnitTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "test/interfaces/ISwapRouter.sol";
contract UnitTests is BaseCaptiveTest {
ISwapRouter public router = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);

/// @dev Ensure that balances and state variables are updated correctly.
function testAddLiquidity() public {
fold.transfer(User01, 1_000 ether);

Expand All @@ -26,6 +27,7 @@ contract UnitTests is BaseCaptiveTest {
assertEq(token1FeeDebt, 0);
}

/// @dev Ensure that balances and state variables are updated correctly.
function testRemoveLiquidity() public {
testAddLiquidity();

Expand All @@ -52,6 +54,7 @@ contract UnitTests is BaseCaptiveTest {
assertEq(amount, liq / 4);
}

/// @dev Ensure fees are accrued correctly and distributed proportionately.
function testFeesAccrue() public {
testAddLiquidity();

Expand Down Expand Up @@ -100,6 +103,7 @@ contract UnitTests is BaseCaptiveTest {
assertGt(foldCaptiveStaking.token1FeesPerLiquidity(), 0);
}

/// @dev Ensure fees are compounded correctly and state variables are updated.
function testCanCompoundFees() public {
testAddLiquidity();

Expand Down Expand Up @@ -147,6 +151,7 @@ contract UnitTests is BaseCaptiveTest {
assertGt(newAmount, amount);
}

/// @dev Ensure new users can't steal fees accrued by others.
function testNewUsersDontStealFees() public {
testFeesAccrue();

Expand Down Expand Up @@ -194,6 +199,7 @@ contract UnitTests is BaseCaptiveTest {
stakingTwo.depositRewards();
}

/// @dev Ensure rewards are added and collected correctly.
function testCanAddRewards() public {
testAddLiquidity();

Expand Down Expand Up @@ -222,6 +228,7 @@ contract UnitTests is BaseCaptiveTest {
foldCaptiveStaking.withdraw(liq / 3);
}

/// @dev Ensure the owner can claim insurance correctly.
function testClaimInsurance() public {
testAddLiquidity();

Expand All @@ -241,13 +248,31 @@ contract UnitTests is BaseCaptiveTest {
vm.stopPrank();
}

/// @dev Ensure pro-rata withdrawals are handled correctly
function testProRataWithdrawals() public {
testAddLiquidity();

(uint128 liq,,,) = foldCaptiveStaking.balances(User01);

// Attempt to withdraw more than allowed amount
vm.expectRevert(WithdrawProRata.selector);
foldCaptiveStaking.withdraw(liq);

// Pro-rated withdrawal
foldCaptiveStaking.withdraw(liq / 2);
(uint128 amount,,,) = foldCaptiveStaking.balances(User01);
assertEq(amount, liq / 2);
}

/// @dev Ensure zero deposits are handled correctly and revert as expected.
function testZeroDeposit() public {
vm.expectRevert();
foldCaptiveStaking.deposit(0, 0, 0);
(uint128 amount,,,) = foldCaptiveStaking.balances(User01);
assertEq(amount, 0);
}

/// @dev Ensure the contract is protected against reentrancy attacks.
function testReentrancy() public {
testAddLiquidity();

Expand All @@ -273,6 +298,26 @@ contract UnitTests is BaseCaptiveTest {
// Expect revert due to minimum deposit requirement
vm.expectRevert(DepositAmountBelowMinimum.selector);
foldCaptiveStaking.deposit(0.5 ether, 0.5 ether, 0);
/// @dev Deposit Cap Enforcement: Test to ensure the deposit cap is respected.
function testDepositCap() public {
uint256 cap = 100 ether;
foldCaptiveStaking.setDepositCap(cap);

fold.transfer(User01, 2000 ether);

vm.deal(User01, 2000 ether);
vm.startPrank(User01);

weth.deposit{value: 2000 ether}();
weth.approve(address(foldCaptiveStaking), type(uint256).max);
fold.approve(address(foldCaptiveStaking), type(uint256).max);

// First deposit should succeed
foldCaptiveStaking.deposit(1_000 ether, 1_000 ether, 0);

// Second deposit should revert due to cap
vm.expectRevert(DepositCapReached.selector);
foldCaptiveStaking.deposit(1_000 ether, 1_000 ether, 0);

vm.stopPrank();
}
Expand All @@ -294,6 +339,52 @@ contract UnitTests is BaseCaptiveTest {
// Withdraw after cooldown period
foldCaptiveStaking.withdraw(liq / 2);
(uint128 amount,,,) = foldCaptiveStaking.balances(User01);
/// @dev Multiple Users: Test simultaneous deposits and withdrawals by multiple users.
function testMultipleUsersDepositWithdraw() public {
// User 1 deposits
fold.transfer(User01, 1_000 ether);
vm.deal(User01, 1_000 ether);
vm.startPrank(User01);

weth.deposit{value: 1_000 ether}();
weth.approve(address(foldCaptiveStaking), type(uint256).max);
fold.approve(address(foldCaptiveStaking), type(uint256).max);

foldCaptiveStaking.deposit(1_000 ether, 1_000 ether, 0);

vm.stopPrank();

// User 2 deposits
fold.transfer(User02, 500 ether);
vm.deal(User02, 500 ether);
vm.startPrank(User02);

weth.deposit{value: 500 ether}();
weth.approve(address(foldCaptiveStaking), type(uint256).max);
fold.approve(address(foldCaptiveStaking), type(uint256).max);

foldCaptiveStaking.deposit(500 ether, 500 ether, 0);

vm.stopPrank();

// User 1 withdraws
vm.startPrank(User01);

(uint128 liq,,,) = foldCaptiveStaking.balances(User01);
foldCaptiveStaking.withdraw(liq / 2);

(uint128 amount,,,) = foldCaptiveStaking.balances(User01);
assertEq(amount, liq / 2);

vm.stopPrank();

// User 2 withdraws
vm.startPrank(User02);

(liq,,,) = foldCaptiveStaking.balances(User02);
foldCaptiveStaking.withdraw(liq / 2);

(amount,,,) = foldCaptiveStaking.balances(User02);
assertEq(amount, liq / 2);

vm.stopPrank();
Expand Down

0 comments on commit 6cf8bad

Please sign in to comment.