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

Test case optimization #41

Merged
merged 34 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9f6167f
test case optimization
TechnoGeek01 Feb 5, 2024
5d7fb59
Merge remote-tracking branch 'origin/dev' into test/optimization
TechnoGeek01 Feb 5, 2024
4095c72
test case optimization
TechnoGeek01 Feb 5, 2024
43bbd6e
test case optimization
TechnoGeek01 Feb 5, 2024
1d582b9
fix
TechnoGeek01 Feb 6, 2024
dfb123c
naming convention fix
TechnoGeek01 Feb 6, 2024
61b7d59
removed unnecessary fuzz in negative test cases
TechnoGeek01 Feb 6, 2024
3e014d6
rename and restructure of test cases
TechnoGeek01 Feb 6, 2024
ace463d
removed asserts and emit tests from deposit helper function and wrote…
TechnoGeek01 Feb 6, 2024
fc2a650
helper function optimization
TechnoGeek01 Feb 6, 2024
168e790
added camelot deposit test
TechnoGeek01 Feb 6, 2024
c068a42
cleanup
TechnoGeek01 Feb 6, 2024
134a4b2
added missing test contract inheritance
TechnoGeek01 Feb 6, 2024
91d60b5
rename
TechnoGeek01 Feb 6, 2024
6290c6f
remove unnecessary fuzz for negative cases
TechnoGeek01 Feb 6, 2024
5bcf284
fuzz functions rename
TechnoGeek01 Feb 6, 2024
1bee72d
update
TechnoGeek01 Feb 8, 2024
a092647
test optimization
TechnoGeek01 Feb 8, 2024
9412e17
Merge branch 'test/optimization' of https://github.com/Sperax/Demeter…
TechnoGeek01 Feb 8, 2024
78af9a9
optimization
TechnoGeek01 Feb 8, 2024
d904d7b
optimization
TechnoGeek01 Feb 9, 2024
21097ea
cleanup
TechnoGeek01 Feb 9, 2024
cfb78c7
optimization
TechnoGeek01 Feb 12, 2024
0a4fd8c
Merge remote-tracking branch 'origin/dev' into test/optimization
TechnoGeek01 Feb 12, 2024
2d21b12
rename
TechnoGeek01 Feb 12, 2024
4c09d51
Merge remote-tracking branch 'origin/dev' into test/optimization
parv3213 Feb 12, 2024
c404391
optimization
TechnoGeek01 Feb 12, 2024
8acdc50
rename
TechnoGeek01 Feb 12, 2024
236df94
cleanup
TechnoGeek01 Feb 12, 2024
6c701f7
update
TechnoGeek01 Feb 13, 2024
1d68764
rename revert tests for consistency
TechnoGeek01 Feb 13, 2024
51434d9
refactor(test): Remove redundant code from uniV3 test files
YashP16 Feb 14, 2024
41c14e8
refactor(contracts/BaseE20Farm): Remove redundancy in `recoverERC20` …
YashP16 Feb 14, 2024
8dbc579
refactor(test): Simplify test hierarchy
YashP16 Feb 14, 2024
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,314 changes: 529 additions & 785 deletions test/BaseFarm.t.sol

Large diffs are not rendered by default.

49 changes: 45 additions & 4 deletions test/camelot/Demeter_CamelotFarm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ contract Demeter_CamelotFarmTest is BaseFarmTest {
return camelotProxy;
}

function test_initialize_RevertWhen_camelotPairIsZero() public {
function test_RevertWhen_camelotPairIsZero() public {
address farm = createFarmImplementation();
address[] memory rewardToken = rwdTokens;
RewardTokenData[] memory rwdTokenData = new RewardTokenData[](rewardToken.length);
Expand All @@ -153,20 +153,59 @@ contract Demeter_CamelotFarmTest is BaseFarmTest {
FARM_ID, block.timestamp, 0, address(factory), address(0), rwdTokenData, ROUTER, NFT_POOL_FACTORY
);
}
}

contract OnERC721ReceivedTest is Demeter_CamelotFarmTest {
using SafeERC20 for IERC20;

function test_OnERC721Received_RevertWhen_NotACamelotNFT() public {
// Deposit test
function test_onERC721Received() public useKnownActor(user) {
address farm;
bool lockup;
for (uint8 j; j < 2; ++j) {
lockup = j == 0 ? true : false;
farm = lockup ? lockupFarm : nonLockupFarm;
uint256 amt1 = 1e3 * 10 ** ERC20(DAI).decimals();
deal(DAI, user, amt1);
uint256 amt2 = 1e3 * 10 ** ERC20(USDCe).decimals();
deal(USDCe, user, amt2);
IERC20(DAI).forceApprove(POSITION_HELPER, amt1);
IERC20(USDCe).forceApprove(POSITION_HELPER, amt2);
IPositionHelper(POSITION_HELPER).addLiquidityAndCreatePosition(
DAI, USDCe, amt1, amt2, amt1 / 10, amt2 / 10, block.timestamp, user, INFTPool(getPoolAddress()), 0
);
uint256 tokenId = INFTPool(getPoolAddress()).lastTokenId();
(uint256 liquidity,,,,,,,) = INFTPool(getPoolAddress()).getStakingPosition(tokenId);
if (!lockup) {
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 0);
} else {
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 0);
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 1);
}
vm.expectEmit(address(farm));
emit Deposited(BaseFarm(farm).totalDeposits() + 1, currentActor, lockup, liquidity);
IERC721(getPoolAddress()).safeTransferFrom(user, farm, tokenId, abi.encode(lockup));
}
}

function test_RevertWhen_NotACamelotNFT() public {
vm.expectRevert(abi.encodeWithSelector(Demeter_CamelotFarm.NotACamelotNFT.selector));
Demeter_CamelotFarm(lockupFarm).onERC721Received(address(0), address(0), 0, "");
}

function test_OnERC721Received_RevertWhen_NoData() public {
function test_RevertWhen_NoData() public {
address nftPool = Demeter_CamelotFarm(lockupFarm).nftPool();
vm.startPrank(nftPool);
vm.expectRevert(abi.encodeWithSelector(Demeter_CamelotFarm.NoData.selector));
Demeter_CamelotFarm(lockupFarm).onERC721Received(address(0), address(0), 0, "");
}
}

function test_onNFTHarvest_RevertWhen_notNftPool() public {
contract OnNFTHarvestTest is Demeter_CamelotFarmTest {
function test_RevertWhen_notNftPool() public {
vm.expectRevert(abi.encodeWithSelector(Demeter_CamelotFarm.NotAllowed.selector));
Demeter_CamelotFarm(lockupFarm).onNFTHarvest(address(0), address(0), 742, 1, 1);
}
Expand All @@ -177,7 +216,9 @@ contract Demeter_CamelotFarmTest is BaseFarmTest {
bool harvested = Demeter_CamelotFarm(lockupFarm).onNFTHarvest(address(0), user, 742, 1, 1);
assertEq(harvested, true);
}
}

contract ClaimPoolRewardsTest is Demeter_CamelotFarmTest {
function test_claimPoolRewards_RevertWhen_FarmIsClosed()
public
depositSetup(nonLockupFarm, false)
Expand Down
84 changes: 51 additions & 33 deletions test/e20-farms/BaseE20Farm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ abstract contract BaseE20FarmTest is BaseFarmTest {
function getPoolAddress() public virtual returns (address);
}

abstract contract E20FarmDepositTest is BaseE20FarmTest {
function test_E20FarmDeposit() public useKnownActor(user) {
address farm;
bool lockup;
for (uint8 j; j < 2; ++j) {
lockup = j == 0 ? true : false;
farm = lockup ? lockupFarm : nonLockupFarm;
address poolAddress = getPoolAddress();
uint256 amt = 1e3 * 10 ** ERC20(poolAddress).decimals();
deal(poolAddress, currentActor, amt);
ERC20(poolAddress).approve(address(farm), amt);
uint256 usrBalanceBefore = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceBefore = ERC20(poolAddress).balanceOf(farm);
if (!lockup) {
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 0);
} else {
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 0);
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 1);
}
vm.expectEmit(address(farm));
emit Deposited(BaseFarm(farm).totalDeposits() + 1, currentActor, lockup, amt);
BaseE20Farm(farm).deposit(amt, lockup);
uint256 usrBalanceAfter = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceAfter = ERC20(poolAddress).balanceOf(farm);
assertEq(usrBalanceAfter, usrBalanceBefore - amt);
assertEq(farmBalanceAfter, farmBalanceBefore + amt);
}
}
}

abstract contract IncreaseDepositTest is BaseE20FarmTest {
event DepositIncreased(uint256 indexed depositId, uint256 liquidity);

Expand All @@ -29,10 +62,10 @@ abstract contract IncreaseDepositTest is BaseE20FarmTest {
BaseE20Farm(lockupFarm).increaseDeposit(DEPOSIT_ID, amt);
}

function testFuzz_RevertWhen_farmIsClosed(uint256 amt) public depositSetup(lockupFarm, true) useKnownActor(user) {
function test_IncreaseDeposit_RevertWhen_farmIsClosed() public depositSetup(lockupFarm, true) useKnownActor(user) {
address poolAddress = getPoolAddress();

vm.assume(amt > 100 * 10 ** ERC20(poolAddress).decimals() && amt <= 1000 * 10 ** ERC20(poolAddress).decimals());
uint256 amt = 100 * 10 ** ERC20(poolAddress).decimals();

deal(poolAddress, currentActor, amt);
ERC20(poolAddress).approve(address(lockupFarm), amt);
Expand All @@ -42,13 +75,9 @@ abstract contract IncreaseDepositTest is BaseE20FarmTest {
BaseE20Farm(lockupFarm).increaseDeposit(DEPOSIT_ID, amt);
}

function testFuzz_RevertWhen_depositInCoolDown(uint256 amt)
public
depositSetup(lockupFarm, true)
useKnownActor(user)
{
function test_RevertWhen_depositInCoolDown() public depositSetup(lockupFarm, true) useKnownActor(user) {
address poolAddress = getPoolAddress();
vm.assume(amt > 100 * 10 ** ERC20(poolAddress).decimals() && amt <= 1000 * 10 ** ERC20(poolAddress).decimals());
uint256 amt = 100 * 10 ** ERC20(poolAddress).decimals();
BaseE20Farm(lockupFarm).initiateCooldown(DEPOSIT_ID);
skip(86400 * 2);
deal(poolAddress, currentActor, amt);
Expand All @@ -57,32 +86,21 @@ abstract contract IncreaseDepositTest is BaseE20FarmTest {
BaseE20Farm(lockupFarm).increaseDeposit(DEPOSIT_ID, amt);
}

function testFuzz_lockupFarm(uint256 amt) public depositSetup(lockupFarm, true) useKnownActor(user) {
address poolAddress = getPoolAddress();
vm.assume(amt > 100 * 10 ** ERC20(poolAddress).decimals() && amt <= 1000 * 10 ** ERC20(poolAddress).decimals());

deal(poolAddress, currentActor, amt);
uint256 usrBalanceBefore = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceBefore = ERC20(poolAddress).balanceOf(lockupFarm);
ERC20(poolAddress).approve(address(lockupFarm), amt);
BaseE20Farm(lockupFarm).increaseDeposit(DEPOSIT_ID, amt);
uint256 usrBalanceAfter = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceAfter = ERC20(poolAddress).balanceOf(lockupFarm);
assertEq(usrBalanceAfter, usrBalanceBefore - amt);
assertEq(farmBalanceAfter, farmBalanceBefore + amt);
}

function testFuzz_nonLockupFarm(uint256 amt) public depositSetup(nonLockupFarm, false) useKnownActor(user) {
function testFuzz_IncreaseDepositTest(bool lockup, uint256 amt) public {
address farm;
farm = lockup ? lockupFarm : nonLockupFarm;
depositSetupFn(farm, lockup);
vm.startPrank(user);
address poolAddress = getPoolAddress();
vm.assume(amt > 100 * 10 ** ERC20(poolAddress).decimals() && amt <= 1000 * 10 ** ERC20(poolAddress).decimals());

deal(poolAddress, currentActor, amt);
uint256 usrBalanceBefore = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceBefore = ERC20(poolAddress).balanceOf(nonLockupFarm);
ERC20(poolAddress).approve(address(nonLockupFarm), amt);
BaseE20Farm(nonLockupFarm).increaseDeposit(DEPOSIT_ID, amt);
uint256 usrBalanceAfter = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceAfter = ERC20(poolAddress).balanceOf(nonLockupFarm);
deal(poolAddress, user, amt);
uint256 usrBalanceBefore = ERC20(poolAddress).balanceOf(user);
uint256 farmBalanceBefore = ERC20(poolAddress).balanceOf(farm);
ERC20(poolAddress).approve(address(farm), amt);
BaseE20Farm(farm).increaseDeposit(DEPOSIT_ID, amt);
uint256 usrBalanceAfter = ERC20(poolAddress).balanceOf(user);
uint256 farmBalanceAfter = ERC20(poolAddress).balanceOf(farm);
assertEq(usrBalanceAfter, usrBalanceBefore - amt);
assertEq(farmBalanceAfter, farmBalanceBefore + amt);
}
Expand Down Expand Up @@ -139,12 +157,12 @@ abstract contract IncreaseDepositTest is BaseE20FarmTest {
abstract contract RecoverERC20E20FarmTest is BaseE20FarmTest {
event Transfer(address indexed from, address indexed to, uint256 value);

function test_recoverE20_LockupFarm_RevertWhen_CannotWithdrawRewardTokenOrFarmToken() public useKnownActor(owner) {
function test_RevertWhen_CannotWithdrawRewardTokenOrFarmToken() public useKnownActor(owner) {
vm.expectRevert(abi.encodeWithSelector(BaseE20Farm.CannotWithdrawRewardTokenOrFarmToken.selector));
BaseFarm(lockupFarm).recoverERC20(USDCe);
}

function test_recoverE20_LockupFarm_RevertWhen_CannotWithdrawZeroAmount() public useKnownActor(owner) {
function test_RevertWhen_CannotWithdrawZeroAmount() public useKnownActor(owner) {
vm.expectRevert(abi.encodeWithSelector(BaseFarm.CannotWithdrawZeroAmount.selector));
BaseFarm(lockupFarm).recoverERC20(USDT);
}
Expand Down
18 changes: 1 addition & 17 deletions test/e20-farms/balancer/BalancerFarm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ contract BalancerFarmTest is
UpdateCoolDownPeriodTest,
CloseFarmTest,
_SetupFarmTest,
E20FarmDepositTest,
IncreaseDepositTest,
DecreaseDepositTest,
RecoverERC20E20FarmTest,
Expand Down Expand Up @@ -135,24 +136,7 @@ contract BalancerFarmTest is
uint256 amt = baseAmt * 10 ** ERC20(poolAddress).decimals();
deal(poolAddress, currentActor, amt);
ERC20(poolAddress).approve(address(farm), amt);
uint256 usrBalanceBefore = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceBefore = ERC20(poolAddress).balanceOf(farm);
if (!locked) {
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 0);
} else {
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 0);
vm.expectEmit(address(farm));
emit PoolSubscribed(BaseFarm(farm).totalDeposits() + 1, 1);
}
vm.expectEmit(address(farm));
emit Deposited(BaseFarm(farm).totalDeposits() + 1, currentActor, locked, amt);
BaseE20Farm(farm).deposit(amt, locked);
uint256 usrBalanceAfter = ERC20(poolAddress).balanceOf(currentActor);
uint256 farmBalanceAfter = ERC20(poolAddress).balanceOf(farm);
assertEq(usrBalanceAfter, usrBalanceBefore - amt);
assertEq(farmBalanceAfter, farmBalanceBefore + amt);
return amt;
}

Expand Down
Loading
Loading