From 5037dde85780b866f035600bb7328b152e7f74bf Mon Sep 17 00:00:00 2001 From: huyhuynh3103 Date: Tue, 16 Apr 2024 10:50:50 +0700 Subject: [PATCH] chore: use custom error instead --- src/upgradeable/ERC721CommonUpgradeable.sol | 11 +++++++---- ...esetMinterPauserAutoIdCustomizedUpgradeable.sol | 14 +++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/upgradeable/ERC721CommonUpgradeable.sol b/src/upgradeable/ERC721CommonUpgradeable.sol index 545f042..56e5690 100644 --- a/src/upgradeable/ERC721CommonUpgradeable.sol +++ b/src/upgradeable/ERC721CommonUpgradeable.sol @@ -10,6 +10,9 @@ abstract contract ERC721CommonUpgradeable is ERC721PresetMinterPauserAutoIdCustomizedUpgradeable, IERC721State { + error ErrInvalidArrayLength(); + error ErrNonExistentToken(); + constructor() { _disableInitializers(); } @@ -18,12 +21,12 @@ abstract contract ERC721CommonUpgradeable is * @inheritdoc IERC721State */ function stateOf(uint256 _tokenId) external view virtual override returns (bytes memory) { - require(_exists(_tokenId), "ERC721Common: query for non-existent token"); + if (!_exists(_tokenId)) revert ErrNonExistentToken(); return abi.encodePacked(ownerOf(_tokenId), nonces[_tokenId], _tokenId); } /** - * @dev Override `ERC721-_baseURI`. + * @dev Override `ERC721Upgradeable-_baseURI`. */ function _baseURI() internal @@ -49,7 +52,7 @@ abstract contract ERC721CommonUpgradeable is } /** - * @dev Override `ERC721PresetMinterPauserAutoIdCustomized-_beforeTokenTransfer`. + * @dev Override `ERC721PresetMinterPauserAutoIdCustomizedUpgradeable-_beforeTokenTransfer`. */ function _beforeTokenTransfer(address _from, address _to, uint256 _firstTokenId, uint256 _batchSize) internal @@ -76,7 +79,7 @@ abstract contract ERC721CommonUpgradeable is onlyRole(MINTER_ROLE) returns (uint256[] memory _tokenIds) { - require(_recipients.length > 0, "ERC721Common: invalid array lengths"); + if (_recipients.length == 0) revert ErrInvalidArrayLength(); _tokenIds = new uint256[](_recipients.length); for (uint256 _i = 0; _i < _recipients.length; _i++) { diff --git a/src/upgradeable/ERC721PresetMinterPauserAutoIdCustomizedUpgradeable.sol b/src/upgradeable/ERC721PresetMinterPauserAutoIdCustomizedUpgradeable.sol index 3087f07..7790e5f 100644 --- a/src/upgradeable/ERC721PresetMinterPauserAutoIdCustomizedUpgradeable.sol +++ b/src/upgradeable/ERC721PresetMinterPauserAutoIdCustomizedUpgradeable.sol @@ -37,6 +37,8 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is ERC721BurnableUpgradeable, ERC721PausableUpgradeable { + error ErrUnauthorizedAccount(address account, bytes32 neededRole); + using CountersUpgradeable for CountersUpgradeable.Counter; bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); @@ -95,7 +97,9 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is * - the caller must have the `MINTER_ROLE`. */ function mint(address to) public virtual returns (uint256 tokenId) { - require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint"); + address sender = _msgSender(); + if (!hasRole(MINTER_ROLE, sender)) revert ErrUnauthorizedAccount(sender, MINTER_ROLE); + tokenId = _mintFor(to); } @@ -109,7 +113,9 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is * - the caller must have the `PAUSER_ROLE`. */ function pause() public virtual { - require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause"); + address sender = _msgSender(); + if (!hasRole(PAUSER_ROLE, sender)) revert ErrUnauthorizedAccount(sender, PAUSER_ROLE); + _pause(); } @@ -123,7 +129,9 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is * - the caller must have the `PAUSER_ROLE`. */ function unpause() public virtual { - require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to unpause"); + address sender = _msgSender(); + if (!hasRole(PAUSER_ROLE, sender)) revert ErrUnauthorizedAccount(sender, PAUSER_ROLE); + _unpause(); }