Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some cleanups
Browse files Browse the repository at this point in the history
waynehoover committed Nov 21, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ce73c87 commit 94ef5b8
Showing 5 changed files with 37 additions and 91 deletions.
2 changes: 1 addition & 1 deletion contracts/Quest.sol
Original file line number Diff line number Diff line change
@@ -168,7 +168,7 @@ contract Quest is ReentrancyGuardUpgradeable, PausableUpgradeable, Ownable, IQue
/// @param hash_ The hash of the message
/// @param signature_ The signature of the hash
function recoverSigner(bytes32 hash_, bytes calldata signature_) public view returns (address) {
return ECDSA.recover(ECDSA.toEthSignedMessageHash(hash_), signature_);
return ECDSA.recoverCalldata(ECDSA.toEthSignedMessageHash(hash_), signature_);
}

function claim(bytes calldata signature_, bytes calldata data_) external payable whenNotEnded {
2 changes: 1 addition & 1 deletion contracts/Quest1155.sol
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ contract Quest1155 is ERC1155Holder, ReentrancyGuardUpgradeable, PausableUpgrade
/// @param hash_ The hash of the message
/// @param signature_ The signature of the hash
function recoverSigner(bytes32 hash_, bytes calldata signature_) public view returns (address) {
return ECDSA.recover(ECDSA.toEthSignedMessageHash(hash_), signature_);
return ECDSA.recoverCalldata(ECDSA.toEthSignedMessageHash(hash_), signature_);
}

function claim(bytes calldata signature_, bytes calldata data_) external payable whenNotEnded {
42 changes: 1 addition & 41 deletions contracts/QuestFactory.sol
Original file line number Diff line number Diff line change
@@ -106,21 +106,9 @@ contract QuestFactory is Initializable, LegacyStorage, OwnableRoles, IQuestFacto

modifier claimChecks(ClaimData memory claimData_) {
Quest storage currentQuest = quests[claimData_.questId];
bytes memory hashc = abi.encodePacked(claimData_.claimer, claimData_.questId);
bytes32 encodedHash;

if (claimData_.ref != address(0)) {
hashc = abi.encodePacked(hashc, claimData_.ref);
}
if (bytes(claimData_.extraData).length > 0){
encodedHash = claimData_.hashBytes;
} else {
encodedHash = keccak256(hashc);
}

if (currentQuest.numberMinted + 1 > currentQuest.totalParticipants) revert OverMaxAllowedToMint();
if (currentQuest.addressMinted[claimData_.claimer]) revert AddressAlreadyMinted();
if (encodedHash != claimData_.hashBytes) revert InvalidHash();
if (recoverSigner(claimData_.hashBytes, claimData_.signature) != claimSignerAddress) revert AddressNotSigned();
_;
}
@@ -308,6 +296,7 @@ contract QuestFactory is Initializable, LegacyStorage, OwnableRoles, IQuestFacto
CLAIM
//////////////////////////////////////////////////////////////*/

/// TODO: Remove this function
/// @dev universal dynamic claim function
/// @param signature_ The signature of the data
/// @param data_ The data to decode for the claim
@@ -330,35 +319,6 @@ contract QuestFactory is Initializable, LegacyStorage, OwnableRoles, IQuestFacto
}
}

/// @dev universal claim function for all quest types
/// @param questId_ The id of the quest
/// @param hash_ The hash of the message
/// @param signature_ The signature of the hash
/// @param ref_ The referral address
function claim(string memory questId_, bytes32 hash_, bytes memory signature_, address ref_) external payable {
if (quests[questId_].questType.eq("erc1155")) {
claim1155RewardsRef(ClaimData(questId_, hash_, signature_, ref_, msg.sender, ""));
} else { // erc20, erc20Stream
claimRewardsRef(ClaimData(questId_, hash_, signature_, ref_, msg.sender, ""));
}
}

/// @dev claim rewards for a quest
/// @param questId_ The id of the quest
/// @param hash_ The hash of the message
/// @param signature_ The signature of the hash
function claim1155Rewards(string memory questId_, bytes32 hash_, bytes memory signature_) external payable {
claim1155RewardsRef(ClaimData(questId_, hash_, signature_, address(0), msg.sender, ""));
}

/// @dev claim rewards for a quest
/// @param questId_ The id of the quest
/// @param hash_ The hash of the message
/// @param signature_ The signature of the hash
function claimRewards(string memory questId_, bytes32 hash_, bytes memory signature_) external payable {
claimRewardsRef(ClaimData(questId_, hash_, signature_, address(0), msg.sender, ""));
}

/*//////////////////////////////////////////////////////////////
SET
//////////////////////////////////////////////////////////////*/
6 changes: 2 additions & 4 deletions contracts/interfaces/IQuestFactory.sol
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ interface IQuestFactory {
error AuthOwnerDiscountToken();
error Deprecated();
error Erc20QuestAddressNotSet();
error InvalidHash();
error InvalidMintFee();
error MsgValueLessThanQuestNFTFee();
error OverMaxAllowedToMint();
@@ -163,9 +162,8 @@ interface IQuestFactory {
// Update Functions

// Claim
function claim(string memory questId_, bytes32 hash_, bytes memory signature_, address ref_) external payable;
function claim1155Rewards(string memory questId_, bytes32 hash_, bytes memory signature_) external payable;
function claimRewards(string memory questId_, bytes32 hash_, bytes memory signature_) external payable;
function claim(bytes calldata signature_, bytes calldata data_) external payable;


// Create
function create1155QuestAndQueue(
76 changes: 32 additions & 44 deletions test/QuestFactory.t.sol
Original file line number Diff line number Diff line change
@@ -270,11 +270,13 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {
);

vm.warp(START_TIME + 1);
bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId", referrer));

bytes memory data = abi.encode(participant, referrer, "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, referrer);
questFactory.claim{value: MINT_FEE}(signature, data);

// 1155 reward
assertEq(sampleERC1155.balanceOf(participant, 1), 1, "particpiant erc1155 balance");
@@ -300,11 +302,12 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {
);

vm.warp(START_TIME + 1);
bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));
bytes memory data = abi.encode(participant, address(0), "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
questFactory.claim{value: MINT_FEE}(signature, data);

// 1155 reward
assertEq(sampleERC1155.balanceOf(participant, 1), 1, "particpiant erc1155 balance");
@@ -331,11 +334,13 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {

uint256 questCreatorBeforeBalance = questCreator.balance;
vm.warp(START_TIME + 1);
bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId", referrer));

bytes memory data = abi.encode(participant, referrer, "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, referrer);
questFactory.claim{value: MINT_FEE}(signature, data);

// erc20 reward
assertEq(sampleERC20.balanceOf(participant), REWARD_AMOUNT, "particpiant erc20 balance");
@@ -484,11 +489,14 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {

uint256 questCreatorBeforeBalance = questCreator.balance;
vm.warp(START_TIME + 1);
bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));


bytes memory data = abi.encode(participant, address(0), "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
questFactory.claim{value: MINT_FEE}(signature, data);

// erc20 reward
assertEq(sampleERC20.balanceOf(participant), REWARD_AMOUNT, "particpiant erc20 balance");
@@ -517,37 +525,13 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {
0
);

bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));
bytes memory data = abi.encode(participant, address(0), "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.expectRevert(abi.encodeWithSelector(QuestNotStarted.selector));
vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
}

function test_RevertIf_claim_InvalidHash() public{
vm.startPrank(owner);
questFactory.setRewardAllowlistAddress(address(sampleERC20), true);

vm.startPrank(questCreator);
sampleERC20.approve(address(questFactory), calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT, QUEST_FEE));
questFactory.createQuestAndQueue(
address(sampleERC20),
END_TIME,
START_TIME,
TOTAL_PARTICIPANTS,
REWARD_AMOUNT,
"questId",
"actionSpec",
0
);

vm.warp(START_TIME + 1);
bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.expectRevert(abi.encodeWithSelector(InvalidHash.selector));
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
questFactory.claim{value: MINT_FEE}(signature, data);
}

function test_RevertIf_claim_AddressAlreadyMinted() public{
@@ -568,14 +552,15 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {
);

vm.warp(START_TIME + 1);
bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));
bytes memory data = abi.encode(participant, address(0), "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
questFactory.claim{value: MINT_FEE}(signature, data);

vm.expectRevert(abi.encodeWithSelector(AddressAlreadyMinted.selector));
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
questFactory.claim{value: MINT_FEE}(signature, data);
}

function test_RevertIf_claim_QuestEnded() public{
@@ -595,13 +580,14 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {
0
);

bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));
bytes memory data = abi.encode(participant, address(0), "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.warp(END_TIME + 1);
vm.startPrank(participant);
vm.expectRevert(abi.encodeWithSelector(QuestEnded.selector));
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
questFactory.claim{value: MINT_FEE}(signature, data);
}

function test_RevertIf_claim_InvalidMintFee() public{
@@ -621,12 +607,13 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {
0
);

bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));
bytes memory data = abi.encode(participant, address(0), "questId", "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.startPrank(participant);
vm.expectRevert(abi.encodeWithSelector(InvalidMintFee.selector));
questFactory.claim{value: MINT_FEE -1}("questId", msgHash, signature, address(0));
questFactory.claim{value: MINT_FEE -1}(signature, data);
}

function test_fuzz_claim(string memory questId_, uint256 totalParticipants_, uint256 rewardAmount_) public{
@@ -654,11 +641,12 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {

uint256 questCreatorBeforeBalance = questCreator.balance;
vm.warp(START_TIME + 1);
bytes32 msgHash = keccak256(abi.encodePacked(participant, questId_, referrer));
bytes memory data = abi.encode(participant, referrer, questId_, "json");
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}(questId_, msgHash, signature, referrer);
questFactory.claim{value: MINT_FEE}(signature, data);

// erc20 reward
assertEq(sampleERC20.balanceOf(participant), rewardAmount_, "particpiant erc20 balance");

0 comments on commit 94ef5b8

Please sign in to comment.