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

chore: add tests #12

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions test/BaseCaptiveTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract BaseCaptiveTest is Test {
error ZeroLiquidity();
error WithdrawFailed();
error WithdrawProRata();
error DepositCapReached();

FoldCaptiveStaking public foldCaptiveStaking;

Expand Down
85 changes: 85 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 @@ -49,6 +51,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 @@ -97,6 +100,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 @@ -144,6 +148,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 @@ -191,6 +196,7 @@ contract UnitTests is BaseCaptiveTest {
stakingTwo.depositRewards();
}

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

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

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

Expand All @@ -235,6 +242,7 @@ contract UnitTests is BaseCaptiveTest {
vm.stopPrank();
}

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

Expand All @@ -250,13 +258,15 @@ contract UnitTests is BaseCaptiveTest {
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 @@ -268,6 +278,81 @@ contract UnitTests is BaseCaptiveTest {
vm.expectRevert();
attack.attack();
}

/// @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();
}

/// @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();
}
}

// Reentrancy attack contract
Expand Down