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
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
chore: add tests
sandybradley committed Jul 29, 2024
commit 219328c743b29c2a5d5dbd21f0622641028b6f6a
1 change: 1 addition & 0 deletions test/BaseCaptiveTest.sol
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ contract BaseCaptiveTest is Test {
error ZeroLiquidity();
error WithdrawFailed();
error WithdrawProRata();
error DepositCapReached();

FoldCaptiveStaking public foldCaptiveStaking;

85 changes: 85 additions & 0 deletions test/UnitTests.t.sol
Original file line number Diff line number Diff line change
@@ -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);

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

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

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

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

@@ -191,6 +196,7 @@ contract UnitTests is BaseCaptiveTest {
stakingTwo.depositRewards();
}

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

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

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

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

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

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

@@ -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