Skip to content

Commit

Permalink
add custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
tnkshuuhei committed Oct 11, 2024
1 parent 6b127f7 commit 35d8bb5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
28 changes: 9 additions & 19 deletions src/CEP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 9 additions & 15 deletions src/Evaluation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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());
_;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -89,22 +89,16 @@ 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) {
return _checkOwner(caller);
}

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;
}
}
10 changes: 4 additions & 6 deletions src/ProjectRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 =
Expand All @@ -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);
Expand Down

0 comments on commit 35d8bb5

Please sign in to comment.