-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(contracts): add advanced ts tests
- Loading branch information
Showing
11 changed files
with
1,782 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/contracts/contracts/src/test/advanced/AdvancedERC721Checker.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.27; | ||
|
||
import {AdvancedChecker} from "../../AdvancedChecker.sol"; | ||
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | ||
|
||
/** | ||
* @title AdvancedERC721Checker | ||
* @notice Implements advanced checks for ERC721 token requirements. | ||
* @dev Extends AdvancedChecker to provide three-phase validation: | ||
* - Pre-check: Basic token ownership verification. | ||
* - Main check: Token balance threshold validation. | ||
* - Post-check: Special token ID range verification. | ||
*/ | ||
contract AdvancedERC721Checker is AdvancedChecker { | ||
IERC721 public immutable NFT; | ||
/// @notice Minimum token balance required for main check. | ||
uint256 public immutable MIN_BALANCE; | ||
/// @notice Minimum token ID allowed for post-check validation. | ||
uint256 public immutable MIN_TOKEN_ID; | ||
/// @notice Maximum token ID allowed for post-check validation. | ||
uint256 public immutable MAX_TOKEN_ID; | ||
|
||
constructor( | ||
IERC721 _nft, | ||
uint256 _minBalance, | ||
uint256 _minTokenId, | ||
uint256 _maxTokenId, | ||
bool _skipPre, | ||
bool _skipPost, | ||
bool _allowMultipleMain | ||
) AdvancedChecker(_skipPre, _skipPost, _allowMultipleMain) { | ||
NFT = _nft; | ||
MIN_BALANCE = _minBalance; | ||
MIN_TOKEN_ID = _minTokenId; | ||
MAX_TOKEN_ID = _maxTokenId; | ||
} | ||
|
||
/** | ||
* @notice Pre-check verifies basic token ownership. | ||
* @dev Validates if the subject owns the specific tokenId provided in evidence. | ||
* @param subject Address to check ownership for. | ||
* @param evidence Encoded uint256 tokenId. | ||
* @return True if subject owns the token, false otherwise. | ||
*/ | ||
function _checkPre(address subject, bytes memory evidence) internal view override returns (bool) { | ||
super._checkPre(subject, evidence); | ||
|
||
uint256 tokenId = abi.decode(evidence, (uint256)); | ||
return NFT.ownerOf(tokenId) == subject; | ||
} | ||
|
||
/** | ||
* @notice Main check verifies minimum token balance. | ||
* @dev Validates if the subject holds at least MIN_BALANCE tokens. | ||
* @param subject Address to check balance for. | ||
* @param evidence Not used in this check. | ||
* @return True if subject meets minimum balance requirement. | ||
*/ | ||
function _checkMain(address subject, bytes memory evidence) internal view override returns (bool) { | ||
super._checkMain(subject, evidence); | ||
|
||
return NFT.balanceOf(subject) >= MIN_BALANCE; | ||
} | ||
|
||
/** | ||
* @notice Post-check verifies ownership of a token within specific ID range. | ||
* @dev Validates if subject owns a token with ID between MIN_TOKEN_ID and MAX_TOKEN_ID. | ||
* @param subject Address to check ownership for. | ||
* @param evidence Encoded uint256 tokenId. | ||
* @return True if subject owns a token in valid range. | ||
*/ | ||
function _checkPost(address subject, bytes memory evidence) internal view override returns (bool) { | ||
super._checkPost(subject, evidence); | ||
|
||
uint256 tokenId = abi.decode(evidence, (uint256)); | ||
return tokenId >= MIN_TOKEN_ID && tokenId <= MAX_TOKEN_ID && NFT.ownerOf(tokenId) == subject; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/contracts/contracts/src/test/advanced/AdvancedERC721Policy.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.27; | ||
|
||
import {AdvancedPolicy} from "../../AdvancedPolicy.sol"; | ||
import {AdvancedERC721Checker} from "./AdvancedERC721Checker.sol"; | ||
|
||
/** | ||
* @title AdvancedERC721Policy | ||
* @notice Policy contract implementing three-phase validation for ERC721 tokens. | ||
* @dev Extends AdvancedPolicy to enforce ERC721-specific checks through AdvancedERC721Checker. | ||
*/ | ||
contract AdvancedERC721Policy is AdvancedPolicy { | ||
/// @notice Reference to the ERC721 checker contract implementing validation logic. | ||
AdvancedERC721Checker public immutable CHECKER; | ||
|
||
/// @param _checker Address of the AdvancedERC721Checker contract. | ||
constructor(AdvancedERC721Checker _checker) AdvancedPolicy(_checker) { | ||
CHECKER = _checker; | ||
} | ||
|
||
/// @notice Returns the trait identifier for this policy. | ||
/// @return String identifying this as an ERC721-based policy. | ||
function trait() external pure returns (string memory) { | ||
return "AdvancedERC721"; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/contracts/contracts/src/test/advanced/AdvancedVoting.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.27; | ||
|
||
import {AdvancedPolicy} from "../../AdvancedPolicy.sol"; | ||
import {Check} from "../../interfaces/IAdvancedPolicy.sol"; | ||
|
||
/** | ||
* @title AdvancedVoting | ||
* @notice Voting contract with three-phase validation and NFT rewards. | ||
* @dev Uses pre-check for registration, main check for voting, and post-check for claiming NFT rewards. | ||
*/ | ||
contract AdvancedVoting { | ||
event Registered(address voter); | ||
event Voted(address voter, uint8 option); | ||
event RewardClaimed(address voter, uint256 rewardId); | ||
|
||
error NotRegistered(); | ||
error NotVoted(); | ||
error AlreadyClaimed(); | ||
error InvalidOption(); | ||
error NotOwnerOfReward(); | ||
|
||
/// @notice Policy contract handling validation checks. | ||
AdvancedPolicy public immutable POLICY; | ||
|
||
/// @notice Tracks vote counts for each option. | ||
mapping(uint8 => uint256) public voteCounts; | ||
|
||
constructor(AdvancedPolicy _policy) { | ||
POLICY = _policy; | ||
} | ||
|
||
/** | ||
* @notice Register to participate in voting. | ||
* @dev Validates NFT ownership through pre-check. | ||
* @param tokenId Token ID to verify ownership. | ||
*/ | ||
function register(uint256 tokenId) external { | ||
bytes memory evidence = abi.encode(tokenId); | ||
|
||
POLICY.enforce(msg.sender, evidence, Check.PRE); | ||
|
||
emit Registered(msg.sender); | ||
} | ||
|
||
/** | ||
* @notice Cast vote after verifying registration. | ||
* @dev Requires pre-check completion and validates voting power. | ||
* @param option Voting option (0 or 1). | ||
*/ | ||
function vote(uint8 option) external { | ||
(bool pre, , ) = POLICY.enforced(address(this), msg.sender); | ||
|
||
if (!pre) revert NotRegistered(); | ||
if (option >= 2) revert InvalidOption(); | ||
|
||
bytes memory evidence = abi.encode(option); | ||
POLICY.enforce(msg.sender, evidence, Check.MAIN); | ||
|
||
unchecked { | ||
voteCounts[option]++; | ||
} | ||
|
||
emit Voted(msg.sender, option); | ||
} | ||
|
||
/** | ||
* @notice Claim NFT reward after voting. | ||
* @dev Validates voting participation and transfers reward NFT. | ||
* @param rewardId NFT ID to be claimed as reward. | ||
*/ | ||
function reward(uint256 rewardId) external { | ||
(bool pre, uint8 main, bool post) = POLICY.enforced(address(this), msg.sender); | ||
|
||
if (!pre) revert NotRegistered(); | ||
if (main == 0) revert NotVoted(); | ||
if (post) revert AlreadyClaimed(); | ||
|
||
bytes memory evidence = abi.encode(rewardId); | ||
POLICY.enforce(msg.sender, evidence, Check.POST); | ||
|
||
emit RewardClaimed(msg.sender, rewardId); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/contracts/contracts/src/test/wrappers/AdvancedERC721CheckerHarness.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.27; | ||
|
||
import {AdvancedERC721Checker} from "../advanced/AdvancedERC721Checker.sol"; | ||
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | ||
import {Check} from "../../interfaces/IAdvancedChecker.sol"; | ||
|
||
// This contract is a harness for testing the AdvancedERC721Checker contract. | ||
// Deploy this contract and call its methods to test the internal methods of AdvancedERC721Checker. | ||
contract AdvancedERC721CheckerHarness is AdvancedERC721Checker { | ||
constructor( | ||
IERC721 _nft, | ||
uint256 _minBalance, | ||
uint256 _minTokenId, | ||
uint256 _maxTokenId, | ||
bool _skipPre, | ||
bool _skipPost, | ||
bool _allowMultipleMain | ||
) AdvancedERC721Checker(_nft, _minBalance, _minTokenId, _maxTokenId, _skipPre, _skipPost, _allowMultipleMain) {} | ||
|
||
/// @notice Exposes the internal `_check` method for testing purposes. | ||
/// @param subject The address to be checked. | ||
/// @param evidence The data associated with the check. | ||
/// @param checkType The type of check to perform (PRE, MAIN, POST). | ||
function exposed__check(address subject, bytes calldata evidence, Check checkType) public view returns (bool) { | ||
return _check(subject, evidence, checkType); | ||
} | ||
|
||
/// @notice Exposes the internal `_checkPre` method for testing purposes. | ||
/// @param subject The address to be checked. | ||
/// @param evidence The data associated with the check. | ||
function exposed__checkPre(address subject, bytes calldata evidence) public view returns (bool) { | ||
return _checkPre(subject, evidence); | ||
} | ||
|
||
/// @notice Exposes the internal `_checkMain` method for testing purposes. | ||
/// @param subject The address to be checked. | ||
/// @param evidence The data associated with the check. | ||
function exposed__checkMain(address subject, bytes calldata evidence) public view returns (bool) { | ||
return _checkMain(subject, evidence); | ||
} | ||
|
||
/// @notice Exposes the internal `_checkPost` method for testing purposes. | ||
/// @param subject The address to be checked. | ||
/// @param evidence The data associated with the check. | ||
function exposed__checkPost(address subject, bytes calldata evidence) public view returns (bool) { | ||
return _checkPost(subject, evidence); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/contracts/contracts/src/test/wrappers/AdvancedERC721PolicyHarness.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.27; | ||
|
||
import {AdvancedERC721Policy} from "../advanced/AdvancedERC721Policy.sol"; | ||
import {AdvancedERC721Checker} from "../advanced/AdvancedERC721Checker.sol"; | ||
import {Check} from "../../interfaces/IAdvancedChecker.sol"; | ||
|
||
// This contract is a harness for testing the AdvancedERC721Policy contract. | ||
// Deploy this contract and call its methods to test the internal methods of AdvancedERC721Policy. | ||
contract AdvancedERC721PolicyHarness is AdvancedERC721Policy { | ||
constructor(AdvancedERC721Checker _checker) AdvancedERC721Policy(_checker) {} | ||
|
||
/// @notice Exposes the internal `_enforce` method for testing purposes. | ||
/// @param subject The address of those who have successfully enforced the check. | ||
/// @param evidence Additional data required for the check (e.g., encoded token identifier). | ||
/// @param checkType The type of the check to be enforced for the subject with the given data. | ||
function exposed__enforce(address subject, bytes calldata evidence, Check checkType) internal onlyTarget { | ||
_enforce(subject, evidence, checkType); | ||
} | ||
} |
Oops, something went wrong.