Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathandiep committed Jun 17, 2024
1 parent df6bd9a commit 3e7ab4b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 91 deletions.
58 changes: 17 additions & 41 deletions test/Quest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
string QUEST_ID = "QUEST_ID";
uint16 QUEST_FEE = 2000; // 20%
uint256 CLAIM_FEE = 999;
uint256 REFERRAL_REWARD_FEE = 500; // 5%
uint16 REFERRAL_REWARD_FEE = 250; // 2.5%
address protocolFeeRecipient = makeAddr("protocolFeeRecipient");
address questFactoryMock;
Quest quest;
Expand All @@ -37,7 +37,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
string constant DEFAULT_ERC20_SYMBOL = "RTC";

function setUp() public {
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT_IN_WEI, QUEST_FEE);
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT_IN_WEI, QUEST_FEE, REFERRAL_REWARD_FEE);
rewardTokenAddress = address(
new SampleERC20(
DEFAULT_ERC20_NAME,
Expand All @@ -58,8 +58,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
REWARD_AMOUNT_IN_WEI,
QUEST_ID,
QUEST_FEE,
protocolFeeRecipient,
REFERRAL_REWARD_FEE
protocolFeeRecipient
);
// Transfer all tokens to quest
vm.prank(admin);
Expand Down Expand Up @@ -97,8 +96,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
REWARD_AMOUNT_IN_WEI,
QUEST_ID,
QUEST_FEE,
protocolFeeRecipient,
REFERRAL_REWARD_FEE
protocolFeeRecipient
);
}

Expand All @@ -115,26 +113,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
REWARD_AMOUNT_IN_WEI,
QUEST_ID,
QUEST_FEE,
protocolFeeRecipient,
REFERRAL_REWARD_FEE
);
}

function test_RevertIf_initialize_ReferralRewardFeeTooHigh() public {
address payable questAddress = payable(address(new Quest()).cloneDeterministic(keccak256(abi.encodePacked(msg.sender, "SALT"))));
quest = Quest(questAddress);
vm.prank(questFactoryMock);
vm.expectRevert(abi.encodeWithSelector(ReferralRewardFeeTooHigh.selector));
quest.initialize(
rewardTokenAddress,
END_TIME,
START_TIME,
TOTAL_PARTICIPANTS,
REWARD_AMOUNT_IN_WEI,
QUEST_ID,
QUEST_FEE,
protocolFeeRecipient,
600
protocolFeeRecipient
);
}

Expand Down Expand Up @@ -197,7 +176,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
currentTime = bound(currentTime, startTime, endTime - 1);
rewardAmountInWei = bound(rewardAmountInWei, 1, REWARD_AMOUNT_IN_WEI * REWARD_AMOUNT_IN_WEI);
// Setup a reward token with fuzzed rewardAmountInWei
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, rewardAmountInWei, QUEST_FEE);
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, rewardAmountInWei, QUEST_FEE, REFERRAL_REWARD_FEE);
rewardTokenAddress = address(
new SampleERC20(
DEFAULT_ERC20_NAME,
Expand All @@ -221,8 +200,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
rewardAmountInWei,
QUEST_ID,
QUEST_FEE,
protocolFeeRecipient,
REFERRAL_REWARD_FEE
protocolFeeRecipient
);
// Transfer all tokens to quest
vm.prank(admin);
Expand Down Expand Up @@ -268,7 +246,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
// simulate ETH from TOTAL_PARTICIPANTS claims
vm.deal(address(quest), (CLAIM_FEE * TOTAL_PARTICIPANTS * 2) / 3);

uint256 totalFees = calculateTotalFees(TOTAL_PARTICIPANTS, REWARD_AMOUNT_IN_WEI, QUEST_FEE) / 2;
uint256 totalFees = calculateTotalProtocolFees(TOTAL_PARTICIPANTS, REWARD_AMOUNT_IN_WEI, QUEST_FEE) / 2;
uint256 questBalance = SampleERC20(rewardTokenAddress).balanceOf(address(quest));
uint256 questBalanceMinusFees = questBalance - totalFees;

Expand Down Expand Up @@ -322,15 +300,15 @@ contract TestQuest is Test, TestUtils, Errors, Events {
CLAIM REFERRAL FEES
//////////////////////////////////////////////////////////////*/

function test_fuzz_claimReferralFees(uint96 timestamp, uint256 participants, uint256 referralRewardFee) public {
function test_fuzz_claimReferralFees(uint96 timestamp, uint256 participants) public {
timestamp = uint96(bound(timestamp, START_TIME+10, END_TIME));
participants = bound(participants, 1, TOTAL_PARTICIPANTS);
referralRewardFee = bound(referralRewardFee, 1, REFERRAL_REWARD_FEE);
// referralRewardFee = bound(referralRewardFee, 1, REFERRAL_REWARD_FEE);


vm.startPrank(admin);
// Transfer the appropriate amount of Reward tokens to the quest based on fuzzed participants
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(participants, REWARD_AMOUNT_IN_WEI, QUEST_FEE);
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(participants, REWARD_AMOUNT_IN_WEI, QUEST_FEE, REFERRAL_REWARD_FEE);
rewardTokenAddress = address(
new SampleERC20(
DEFAULT_ERC20_NAME,
Expand All @@ -352,8 +330,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
REWARD_AMOUNT_IN_WEI,
QUEST_ID,
QUEST_FEE,
protocolFeeRecipient,
referralRewardFee
protocolFeeRecipient
);

vm.startPrank(admin);
Expand Down Expand Up @@ -432,15 +409,15 @@ contract TestQuest is Test, TestUtils, Errors, Events {
);
}

function test_fuzz_claimReferralFees_withdrawAfterClaim(uint96 timestamp, uint256 participants, uint256 referralRewardFee) public {
function test_fuzz_claimReferralFees_withdrawAfterClaim(uint96 timestamp, uint256 participants) public {
timestamp = uint96(bound(timestamp, START_TIME+10, END_TIME));
participants = bound(participants, 1, TOTAL_PARTICIPANTS);
referralRewardFee = bound(referralRewardFee, 1, REFERRAL_REWARD_FEE);
// referralRewardFee = bound(referralRewardFee, 1, REFERRAL_REWARD_FEE);


vm.startPrank(admin);
// Transfer the appropriate amount of Reward tokens to the quest based on fuzzed participants
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(participants, REWARD_AMOUNT_IN_WEI, QUEST_FEE);
defaultTotalRewardsPlusFee = calculateTotalRewardsPlusFee(participants, REWARD_AMOUNT_IN_WEI, QUEST_FEE, REFERRAL_REWARD_FEE);
rewardTokenAddress = address(
new SampleERC20(
DEFAULT_ERC20_NAME,
Expand All @@ -462,8 +439,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
REWARD_AMOUNT_IN_WEI,
QUEST_ID,
QUEST_FEE,
protocolFeeRecipient,
referralRewardFee
protocolFeeRecipient
);

vm.startPrank(admin);
Expand Down Expand Up @@ -586,7 +562,7 @@ contract TestQuest is Test, TestUtils, Errors, Events {
function test_maxProtocolReward() public {
assertEq(
quest.maxProtocolReward(),
calculateTotalFees(TOTAL_PARTICIPANTS, REWARD_AMOUNT_IN_WEI, QUEST_FEE),
calculateTotalProtocolFees(TOTAL_PARTICIPANTS, REWARD_AMOUNT_IN_WEI, QUEST_FEE),
"maxProtocolReward should be correct"
);
}
Expand Down
11 changes: 5 additions & 6 deletions test/QuestClaimable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contract TestQuestClaimable is Test, Errors, Events, TestUtils {
uint256 REWARD_AMOUNT = 10;
uint16 QUEST_FEE = 2000;
uint256 MINT_FEE = 100;
uint16 REFERRAL_REWARD_FEE = 250;
address protocolFeeRecipient = makeAddr("protocolFeeRecipient");
address questCreator = makeAddr(("questCreator"));
address participant = makeAddr(("participant"));
Expand Down Expand Up @@ -71,7 +72,7 @@ contract TestQuestClaimable is Test, Errors, Events, TestUtils {
//////////////////////////////////////////////////////////////*/
function test_claim_with_referrer() public {
vm.startPrank(questCreator);
sampleERC20.approve(address(questFactory), calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT, QUEST_FEE));
sampleERC20.approve(address(questFactory), calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT, QUEST_FEE, REFERRAL_REWARD_FEE));
address questAddress = questFactory.createERC20Quest(
101,
address(sampleERC20),
Expand All @@ -82,8 +83,7 @@ contract TestQuestClaimable is Test, Errors, Events, TestUtils {
"550e8400-e29b-41d4-a716-446655440000",
"actionType",
"questName",
"projectName",
500
"projectName"
);

vm.warp(START_TIME + 1);
Expand All @@ -108,7 +108,7 @@ contract TestQuestClaimable is Test, Errors, Events, TestUtils {
function test_claim_without_referrer() public {
referrer = address(0);
vm.startPrank(questCreator);
sampleERC20.approve(address(questFactory), calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT, QUEST_FEE));
sampleERC20.approve(address(questFactory), calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT, QUEST_FEE, REFERRAL_REWARD_FEE));
address questAddress = questFactory.createERC20Quest(
101,
address(sampleERC20),
Expand All @@ -119,8 +119,7 @@ contract TestQuestClaimable is Test, Errors, Events, TestUtils {
"550e8400-e29b-41d4-a716-446655440000",
"actionType",
"questName",
"projectName",
500
"projectName"
);

vm.warp(START_TIME + 1);
Expand Down
Loading

0 comments on commit 3e7ab4b

Please sign in to comment.