Skip to content

Commit 04ec461

Browse files
authored
[chore] custome errors (#57)
1 parent 81f1de0 commit 04ec461

File tree

6 files changed

+48
-14
lines changed

6 files changed

+48
-14
lines changed

contracts/errors/Errors.sol

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pragma solidity ^0.8.0;
2+
3+
interface IImmutableERC721Errors {
4+
/// @dev Caller tried to mint an already burned token
5+
error IImmutableERC721TokenAlreadyBurned(uint256 tokenId);
6+
7+
/// @dev Caller tried to mint an already burned token
8+
error IImmutableERC721SendingToZerothAddress();
9+
10+
/// @dev Caller tried to mint an already burned token
11+
error IImmutableERC721MismatchedTransferLengths();
12+
}
13+
14+
interface RoyaltyEnforcementErrors {
15+
/// @dev Caller tried to mint an already burned token
16+
error RoyaltyEnforcementDoesNotImplementRequiredInterface();
17+
}

contracts/royalty-enforcement/RoyaltyEnforced.sol

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import "./IRoyaltyAllowlist.sol";
77
// Access Control
88
import "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
99

10-
abstract contract RoyaltyEnforced is AccessControlEnumerable {
10+
// Errors
11+
import {RoyaltyEnforcementErrors} from "../errors/Errors.sol";
12+
13+
abstract contract RoyaltyEnforced is
14+
AccessControlEnumerable,
15+
RoyaltyEnforcementErrors
16+
{
1117
/// ===== Errors =====
1218

1319
/// @dev Error thrown when calling address is not Allowlisted
@@ -50,12 +56,13 @@ abstract contract RoyaltyEnforced is AccessControlEnumerable {
5056
function setRoyaltyAllowlistRegistry(
5157
address _royaltyAllowlist
5258
) public onlyRole(DEFAULT_ADMIN_ROLE) {
53-
require(
54-
IERC165(_royaltyAllowlist).supportsInterface(
59+
if (
60+
!IERC165(_royaltyAllowlist).supportsInterface(
5561
type(IRoyaltyAllowlist).interfaceId
56-
),
57-
"contract does not implement IRoyaltyAllowlist"
58-
);
62+
)
63+
) {
64+
revert RoyaltyEnforcementDoesNotImplementRequiredInterface();
65+
}
5966

6067
emit RoyaltyAllowlistRegistryUpdated(
6168
address(royaltyAllowlist),

contracts/token/erc721/abstract/ImmutableERC721Base.sol

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import "../../../royalty-enforcement/RoyaltyEnforced.sol";
1212
// Utils
1313
import "@openzeppelin/contracts/utils/Counters.sol";
1414

15+
// Errors
16+
import {IImmutableERC721Errors} from "../../../errors/Errors.sol";
17+
1518
/*
1619
ImmutableERC721Base is an abstract contract that offers minimum preset functionality without
1720
an opinionated form of minting. This contract is intended to be inherited and implement it's
@@ -21,7 +24,8 @@ import "@openzeppelin/contracts/utils/Counters.sol";
2124
abstract contract ImmutableERC721Base is
2225
RoyaltyEnforced,
2326
ERC721Burnable,
24-
ERC2981
27+
ERC2981,
28+
IImmutableERC721Errors
2529
{
2630
/// ===== State Variables =====
2731

@@ -206,7 +210,9 @@ abstract contract ImmutableERC721Base is
206210

207211
/// @dev mints specified token ids to specified address
208212
function _batchMint(IDMint memory mintRequest) internal {
209-
require(mintRequest.to != address(0), "Address is zero");
213+
if (mintRequest.to != address(0)) {
214+
revert IImmutableERC721SendingToZerothAddress();
215+
}
210216
for (uint256 j = 0; j < mintRequest.tokenIds.length; j++) {
211217
_mint(mintRequest.to, mintRequest.tokenIds[j]);
212218
}
@@ -215,7 +221,9 @@ abstract contract ImmutableERC721Base is
215221

216222
/// @dev mints specified token ids to specified address
217223
function _safeBatchMint(IDMint memory mintRequest) internal {
218-
require(mintRequest.to != address(0), "Address is zero");
224+
if (mintRequest.to == address(0)) {
225+
revert IImmutableERC721SendingToZerothAddress();
226+
}
219227
for (uint256 j; j < mintRequest.tokenIds.length; j++) {
220228
_safeMint(mintRequest.to, mintRequest.tokenIds[j]);
221229
}
@@ -225,15 +233,15 @@ abstract contract ImmutableERC721Base is
225233
/// @dev mints specified token id to specified address
226234
function _mint(address to, uint256 tokenId) internal override(ERC721) {
227235
if (_burnedTokens[tokenId]) {
228-
revert("ERC721: token already burned");
236+
revert IImmutableERC721TokenAlreadyBurned(tokenId);
229237
}
230238
super._mint(to, tokenId);
231239
}
232240

233241
/// @dev mints specified token id to specified address
234242
function _safeMint(address to, uint256 tokenId) internal override(ERC721) {
235243
if (_burnedTokens[tokenId]) {
236-
revert("ERC721: token already burned");
244+
revert IImmutableERC721TokenAlreadyBurned(tokenId);
237245
}
238246
super._safeMint(to, tokenId);
239247
}

contracts/token/erc721/preset/ImmutableERC721PermissionedMintable.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ contract ImmutableERC721PermissionedMintable is ImmutableERC721Base {
108108
/// @dev Allows owner or operator to transfer a batch of tokens
109109
function safeTransferFromBatch(TransferRequest calldata tr) external {
110110
if (tr.tokenIds.length != tr.tos.length) {
111-
revert("number of token ids not the same as number of receivers");
111+
revert IImmutableERC721MismatchedTransferLengths();
112112
}
113113

114114
for (uint i = 0; i < tr.tokenIds.length; i++) {

test/royalty-enforcement/AllowlistERC721TransfersApprovals.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ describe("Allowlisted ERC721 Transfers", function () {
7474

7575
await expect(
7676
erc721.connect(owner).setRoyaltyAllowlistRegistry(erc721Two.address)
77-
).to.be.revertedWith("contract does not implement IRoyaltyAllowlist");
77+
).to.be.revertedWith(
78+
"RoyaltyEnforcementDoesNotImplementRequiredInterface()"
79+
);
7880
});
7981

8082
it("Should not allow a non-admin to access the function to update the registry", async function () {

test/token/erc721/ImmutableERC721PermissionedMintable.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe("Immutable ERC721 Permissioned Mintable Test Cases", function () {
131131
const mintRequests = [{ to: user.address, tokenIds: [1, 2] }];
132132
await expect(
133133
erc721.connect(minter).safeMintBatch(mintRequests)
134-
).to.be.revertedWith("ERC721: token already burned");
134+
).to.be.revertedWith("IImmutableERC721TokenAlreadyBurned(1)");
135135
});
136136
});
137137

0 commit comments

Comments
 (0)