From 35d8bb556c57b7b97af4e1eccd31177273ef615c Mon Sep 17 00:00:00 2001 From: shuhei tanaka Date: Fri, 11 Oct 2024 18:03:10 +0700 Subject: [PATCH] add custom error --- src/CEP.sol | 28 +++++++++------------------- src/Evaluation.sol | 24 +++++++++--------------- src/ProjectRegistry.sol | 10 ++++------ 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/src/CEP.sol b/src/CEP.sol index aad7a67..ccb4854 100644 --- a/src/CEP.sol +++ b/src/CEP.sol @@ -89,10 +89,10 @@ contract CEP is AccessControl, Errors { ) external { - if (_proposor != msg.sender) revert UNAUTHORIZED(); - if (_proposor == address(0)) revert ZERO_ADDRESS(); - if (_contributors.length == 0) revert INVALID(); - if (_amount == 0) revert INVALID(); + require(_proposor == msg.sender, UNAUTHORIZED()); + require(_proposor != address(0), ZERO_ADDRESS()); + require(_contributors.length > 0, INVALID()); + require(_amount > 0, INVALID()); Evaluation evaluation = Evaluation(_evaluation); EvaluationPool memory pool = evaluations[evaluation.getPoolId()]; @@ -175,7 +175,7 @@ contract CEP is AccessControl, Errors { evaluation = _createEvaluation(_profileId, _owner, _contributors); - if (address(evaluation) == address(0)) revert ZERO_ADDRESS(); + require(address(evaluation) != address(0), ZERO_ADDRESS()); EvaluationPool memory pool = EvaluationPool({ profileId: _profileId, @@ -190,16 +190,17 @@ contract CEP is AccessControl, Errors { _setRoleAdmin(POOL_CONTRIBUTOR_ROLE, POOL_MANAGER_ROLE); - if (evaluation.getPoolId() != 0) revert ALREADY_INITIALIZED(); + require(evaluation.getPoolId() == 0, ALREADY_INITIALIZED()); evaluation.initialize(poolId); - if (evaluation.getPoolId() != poolId || address(evaluation.getCep()) != address(this)) revert MISMATCH(); + require(evaluation.getPoolId() == poolId, MISMATCH()); + require(address(evaluation.getCep()) == address(this), MISMATCH()); for (uint256 i = 0; i < _contributors.length; i++) { address contributor = _contributors[i]; - if (contributor == address(0)) revert ZERO_ADDRESS(); + require(contributor != address(0), ZERO_ADDRESS()); _grantRole(POOL_CONTRIBUTOR_ROLE, contributor); } @@ -227,17 +228,6 @@ contract CEP is AccessControl, Errors { return evaluationAddress; } - function fundPool(uint256 _poolId, uint256 _amount) external payable { - if (_amount == 0 || _amount != msg.value) revert INVALID(); - _fundPool(); - - emit PoolFunded(_poolId, _amount); - } - - function _fundPool() internal pure { - // TODO: implement the fundPool function - } - function attest( bytes32 profileId, address[] memory contributors, diff --git a/src/Evaluation.sol b/src/Evaluation.sol index bb9e20e..69bfad9 100644 --- a/src/Evaluation.sol +++ b/src/Evaluation.sol @@ -31,10 +31,11 @@ contract Evaluation is AccessControl, Errors { } governor = address(cep.governor()); owner = _owner; + _grantRole(DEFAULT_ADMIN_ROLE, _owner); } modifier onlyCep() { - _checkCep(); + require(_checkCep() == true, UNAUTHORIZED()); _; } @@ -77,10 +78,9 @@ contract Evaluation is AccessControl, Errors { return poolId; } - function _checkCep() internal view { - if (msg.sender != address(cep)) { - revert UNAUTHORIZED(); - } + function _checkCep() internal view returns (bool) { + require(msg.sender == address(cep), UNAUTHORIZED()); + return true; } function checkContributor(address _contributor) external view returns (bool isContributor) { @@ -89,11 +89,8 @@ contract Evaluation is AccessControl, Errors { } function _checkContributor(address _contributor) internal view returns (bool) { - if (!hasRole(CONTRIBUTOR_ROLE, _contributor)) { - revert UNAUTHORIZED(); - } else { - return true; - } + require(hasRole(CONTRIBUTOR_ROLE, _contributor), UNAUTHORIZED()); + return true; } function checkOwner(address caller) external view returns (bool) { @@ -101,10 +98,7 @@ contract Evaluation is AccessControl, Errors { } function _checkOwner(address caller) internal view returns (bool) { - if (caller != owner) { - revert UNAUTHORIZED(); - } else { - return true; - } + require(hasRole(DEFAULT_ADMIN_ROLE, caller), UNAUTHORIZED()); + return true; } } diff --git a/src/ProjectRegistry.sol b/src/ProjectRegistry.sol index fadf0dc..2ee7498 100644 --- a/src/ProjectRegistry.sol +++ b/src/ProjectRegistry.sol @@ -23,7 +23,7 @@ contract ProjectRegistry is Initializable, AccessControlUpgradeable, Errors { function initialize(address _owner) external initializer { // Make sure the owner is not 'address(0)' - if (_owner == address(0)) revert ZERO_ADDRESS(); + require(_owner != address(0), ZERO_ADDRESS()); // Grant the role to the owner _grantRole(CEP_OWNER, _owner); } @@ -42,7 +42,7 @@ contract ProjectRegistry is Initializable, AccessControlUpgradeable, Errors { bytes32 profileId = _generateProfileId(_nonce, _owner); // Make sure the owner is not the zero address - if (_owner == address(0)) revert ZERO_ADDRESS(); + require(_owner != address(0), ZERO_ADDRESS()); // Create a new Profile instance, also generates the anchor address Profile memory profile = @@ -54,15 +54,13 @@ contract ProjectRegistry is Initializable, AccessControlUpgradeable, Errors { uint256 memberLength = _members.length; // Only profile owner can add members - if (memberLength > 0 && _owner != msg.sender) { - revert UNAUTHORIZED(); - } + require(memberLength > 0 && _owner == msg.sender, UNAUTHORIZED()); for (uint256 i; i < memberLength;) { address member = _members[i]; // Will revert if any of the addresses are a zero address - if (member == address(0)) revert ZERO_ADDRESS(); + require(member != address(0), ZERO_ADDRESS()); // Grant the role to the member and emit the event for each member _grantRole(profileId, member);