Skip to content

Commit

Permalink
sequential test working
Browse files Browse the repository at this point in the history
  • Loading branch information
0xean committed Apr 24, 2024
1 parent 1ffafa9 commit 4778424
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 8 deletions.
16 changes: 13 additions & 3 deletions foundry/src/FoxStakingV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,21 @@ contract FoxStakingV1 is
return info.stakingBalance + info.unstakingBalance;
}

function getUnstakingInfo(address account, uint256 index)
// @notice helper function to access dynamic array nested in struct from external sources
// @param account The address we're getting the unstaking info for.
// @param index The index of the unstaking info array we're getting.
function getUnstakingInfo(
address account,
uint256 index
) external view returns (UnstakingInfo memory) {
return stakingInfo[account].unstakingInfo[index];
}

function getUnstakingInfoCount(address account)
external
view
returns (UnstakingInfo memory)
returns (uint256)
{
return stakingInfo[account].unstakingInfo[index];
return stakingInfo[account].unstakingInfo.length;
}
}
9 changes: 6 additions & 3 deletions foundry/test/FOXStakingTestStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ contract FOXStakingTestStaking is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 1000);
vm.assertEq(stakingBalance_after, 1000);
Expand Down Expand Up @@ -99,6 +100,7 @@ contract FOXStakingTestStaking is Test {
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 0);
vm.assertEq(stakingBalance_before, 0);
Expand All @@ -122,6 +124,7 @@ contract FOXStakingTestStaking is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 1000);
vm.assertEq(stakingBalance_after, 1000);
Expand Down Expand Up @@ -150,6 +153,7 @@ contract FOXStakingTestStaking is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 0);
vm.assertEq(stakingBalance_after, 0);
Expand Down Expand Up @@ -178,6 +182,7 @@ contract FOXStakingTestStaking is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 0);
vm.assertEq(stakingBalance_after, 0);
Expand Down Expand Up @@ -220,9 +225,7 @@ contract FOXStakingTestStaking is Test {
assertEq(total, amounts[i]);

// Verify each user's rune address
(, , string memory runeAddress) = foxStaking.stakingInfo(
users[i]
);
(, , string memory runeAddress) = foxStaking.stakingInfo(users[i]);
assertEq(runeAddress, runeAddresses[i]);
}
}
Expand Down
191 changes: 189 additions & 2 deletions foundry/test/FOXStakingTestUnstake.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 1000);
vm.assertEq(stakingBalance_before, 1000);
Expand All @@ -74,6 +75,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 1000);
vm.assertEq(stakingBalance_after, 0);
Expand Down Expand Up @@ -111,6 +113,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);

vm.assertEq(stakingBalance_after + unstakingBalance_after, 1000);
Expand All @@ -127,6 +130,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 1000);
vm.assertEq(stakingBalance_before, 1000);
Expand All @@ -140,6 +144,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 1000);
vm.assertEq(stakingBalance_after, 1000);
Expand All @@ -155,6 +160,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 1000);
vm.assertEq(stakingBalance_before, 1000);
Expand All @@ -167,6 +173,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 1000);
vm.assertEq(stakingBalance_after, 0);
Expand All @@ -182,6 +189,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 1000);
vm.assertEq(stakingBalance_before, 1000);
Expand All @@ -194,6 +202,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 1000);
vm.assertEq(stakingBalance_after, 200);
Expand All @@ -210,6 +219,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 1000);
vm.assertEq(stakingBalance_before, 1000);
Expand All @@ -226,8 +236,11 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_one,
uint256 unstakingBalance_one,

) = foxStaking.stakingInfo(user);
uint256 cooldownExpiry_one = foxStaking.getUnstakingInfo(user, 0).cooldownExpiry;
uint256 cooldownExpiry_one = foxStaking
.getUnstakingInfo(user, 0)
.cooldownExpiry;
vm.assertEq(cooldownExpiry_one, block.timestamp + 28 days);

// Check user staking balances reflect the withdrawal request
Expand All @@ -245,6 +258,7 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_two,
uint256 unstakingBalance_two,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_two + unstakingBalance_two, 700);
vm.assertEq(stakingBalance_two, 700);
Expand All @@ -257,8 +271,11 @@ contract FOXStakingTestUnstake is Test {
(
uint256 stakingBalance_three,
uint256 unstakingBalance_three,

) = foxStaking.stakingInfo(user);
uint256 cooldownExpiry_three = foxStaking.getUnstakingInfo(user, 0).cooldownExpiry;
uint256 cooldownExpiry_three = foxStaking
.getUnstakingInfo(user, 0)
.cooldownExpiry;
vm.assertGt(cooldownExpiry_three, block.timestamp);

// Check user staking balances reflect the withdrawal request
Expand All @@ -268,4 +285,174 @@ contract FOXStakingTestUnstake is Test {

vm.stopPrank();
}

function testunstake_multipleConcurrentWithdraws() public {
vm.startPrank(user);

// Check user staking balances
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 1000);
vm.assertEq(stakingBalance_before, 1000);
vm.assertEq(unstakingBalance_before, 0);

// Request withdraw of 301 FOX
foxStaking.unstake(301);

// Ensure attempting to withdraw the 301 FOX reverts
vm.expectRevert("Not cooled down yet");
foxStaking.withdraw();

// Check cooldown period is set
(
uint256 stakingBalance_one,
uint256 unstakingBalance_one,

) = foxStaking.stakingInfo(user);
uint256 cooldownExpiry_one = foxStaking
.getUnstakingInfo(user, 0)
.cooldownExpiry;
vm.assertEq(cooldownExpiry_one, block.timestamp + 28 days);

// Check user staking balances reflect the withdrawal request
vm.assertEq(stakingBalance_one + unstakingBalance_one, 1000);
vm.assertEq(stakingBalance_one, 699);
vm.assertEq(unstakingBalance_one, 301);

// Time warp 2 days
vm.warp(block.timestamp + 2 days);

// check that we cannot withdraw any fox yet
vm.expectRevert("Not cooled down yet");
foxStaking.withdraw();

// Request withdraw of another 302 FOX
foxStaking.unstake(302);

uint256 cooldownExpiry_two = foxStaking
.getUnstakingInfo(user, 1)
.cooldownExpiry;
vm.assertEq(cooldownExpiry_two, block.timestamp + 28 days);

// confirm total amount cooling down is correct
(
uint256 stakingBalance_two,
uint256 unstakingBalance_two,

) = foxStaking.stakingInfo(user);
vm.assertEq(unstakingBalance_two, 301+302);
vm.assertEq(stakingBalance_two, 1000-301-302);

// Time warp 2 days
vm.warp(block.timestamp + 2 days);

// Request withdraw of another 303 FOX
foxStaking.unstake(303);

uint256 cooldownExpiry_three = foxStaking
.getUnstakingInfo(user, 2)
.cooldownExpiry;
vm.assertEq(cooldownExpiry_three, block.timestamp + 28 days);

// confirm total amount cooling down is correct
(
uint256 stakingBalance_three,
uint256 unstakingBalance_three,

) = foxStaking.stakingInfo(user);
vm.assertEq(unstakingBalance_three, 301+302+303);
vm.assertEq(stakingBalance_three, 1000-301-302-303);

vm.assertEq(foxStaking.getUnstakingInfoCount(user), 3);

// Time warp to first expiry
vm.warp(cooldownExpiry_one);

// we should not be able to withdraw 2 or 3 elements (they are not cooled down yet)
vm.expectRevert("Not cooled down yet");
foxStaking.withdraw(1);

vm.expectRevert("Not cooled down yet");
foxStaking.withdraw(2);

uint256 balBefore = foxToken.balanceOf(user);
// Withdraw the 301 FOX
foxStaking.withdraw();
uint256 balAfter = foxToken.balanceOf(user);
vm.assertEq(balAfter - balBefore, 301);

// Check user staking balances reflect the withdrawal
(
uint256 stakingBalance_four,
uint256 unstakingBalance_four,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_four, 1000-301-302-303);
vm.assertEq(unstakingBalance_four, 302+303);

// check the length of the array is correct
// and the sums match the staking info
vm.assertEq(foxStaking.getUnstakingInfoCount(user), 2);
vm.assertEq(foxStaking.getUnstakingInfo(user, 0).unstakingBalance + foxStaking.getUnstakingInfo(user, 1).unstakingBalance, 302 + 303);

// Time warp to second expiry
vm.warp(cooldownExpiry_two);

// we should not be able to withdraw 3rd element (it is not cooled down yet)
// note the index shifts to zero because of how we are deleting the elements
vm.expectRevert("Not cooled down yet");
foxStaking.withdraw(0);

balBefore = foxToken.balanceOf(user);
// Withdraw the 302 FOX
foxStaking.withdraw(1);
balAfter = foxToken.balanceOf(user);
vm.assertEq(balAfter - balBefore, 302);

// Check user staking balances reflect the withdrawal
(
uint256 stakingBalance_five,
uint256 unstakingBalance_five,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_five, 1000-301-302-303);
vm.assertEq(unstakingBalance_five, 303);

// check the lenght of the array is correct and the indexes have shifted
vm.assertEq(foxStaking.getUnstakingInfoCount(user), 1);
vm.assertEq(foxStaking.getUnstakingInfo(user, 0).unstakingBalance, 303);

// Time warp to third expiry
vm.warp(cooldownExpiry_three);

balBefore = foxToken.balanceOf(user);
// Withdraw the 303 FOX
foxStaking.withdraw();
balAfter = foxToken.balanceOf(user);
vm.assertEq(balAfter - balBefore, 303);

// Check user staking balances reflect the withdrawal
(
uint256 stakingBalance_six,
uint256 unstakingBalance_six,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_six, 1000-301-302-303);
vm.assertEq(unstakingBalance_six, 0);
vm.assertEq(foxStaking.getUnstakingInfoCount(user), 0);

vm.stopPrank();
}

function testunstake_multipleConcurrentWithdrawsWithChangedCooldown() public {

}

function testunstake_outOfOrderWithdraws() public {

}

}
2 changes: 2 additions & 0 deletions foundry/test/FOXStakingTestWithdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ contract FOXStakingTestWithdraw is Test {
(
uint256 stakingBalance_before,
uint256 unstakingBalance_before,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_before + unstakingBalance_before, 1000);
vm.assertEq(stakingBalance_before, 1000);
Expand Down Expand Up @@ -92,6 +93,7 @@ contract FOXStakingTestWithdraw is Test {
(
uint256 stakingBalance_after,
uint256 unstakingBalance_after,

) = foxStaking.stakingInfo(user);
vm.assertEq(stakingBalance_after + unstakingBalance_after, 0);
vm.assertEq(stakingBalance_after, 0);
Expand Down

0 comments on commit 4778424

Please sign in to comment.