Skip to content

Commit

Permalink
chore: use custom error instead
Browse files Browse the repository at this point in the history
  • Loading branch information
huyhuynh3103 committed Apr 16, 2024
1 parent 7b2f732 commit 5037dde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/upgradeable/ERC721CommonUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ abstract contract ERC721CommonUpgradeable is
ERC721PresetMinterPauserAutoIdCustomizedUpgradeable,
IERC721State
{
error ErrInvalidArrayLength();
error ErrNonExistentToken();

constructor() {
_disableInitializers();
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down

0 comments on commit 5037dde

Please sign in to comment.