Skip to content

Commit 8959088

Browse files
authored
Custom errors, etc (#619)
* custom error for extensions * contract publisher * legacy contracts * custom error for CurrencyTransferLib * efficient hash function MerkleProof * Pack without forwarder * fix test * fix tests * fix btt tests * extension tests * extension tests * test pack * more btt * more tests * drop prebuilt tests * fix lint
1 parent effd208 commit 8959088

File tree

81 files changed

+1049
-741
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1049
-741
lines changed

contracts/extension/BatchMintMetadata.sol

+21-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ pragma solidity ^0.8.0;
1111
*/
1212

1313
contract BatchMintMetadata {
14+
/// @dev Invalid index for batch
15+
error BatchMintInvalidBatchId(uint256 index);
16+
17+
/// @dev Invalid token
18+
error BatchMintInvalidTokenId(uint256 tokenId);
19+
20+
/// @dev Metadata frozen
21+
error BatchMintMetadataFrozen(uint256 batchId);
22+
1423
/// @dev Largest tokenId of each batch of tokens with the same baseURI + 1 {ex: batchId 100 at position 0 includes tokens 0-99}
1524
uint256[] private batchIds;
1625

@@ -46,7 +55,7 @@ contract BatchMintMetadata {
4655
*/
4756
function getBatchIdAtIndex(uint256 _index) public view returns (uint256) {
4857
if (_index >= getBaseURICount()) {
49-
revert("Invalid index");
58+
revert BatchMintInvalidBatchId(_index);
5059
}
5160
return batchIds[_index];
5261
}
@@ -65,7 +74,7 @@ contract BatchMintMetadata {
6574
}
6675
}
6776

68-
revert("Invalid tokenId");
77+
revert BatchMintInvalidTokenId(_tokenId);
6978
}
7079

7180
/// @dev Returns the baseURI for a token. The intended metadata URI for the token is baseURI + tokenId.
@@ -78,7 +87,8 @@ contract BatchMintMetadata {
7887
return baseURI[indices[i]];
7988
}
8089
}
81-
revert("Invalid tokenId");
90+
91+
revert BatchMintInvalidTokenId(_tokenId);
8292
}
8393

8494
/// @dev returns the starting tokenId of a given batchId.
@@ -94,20 +104,25 @@ contract BatchMintMetadata {
94104
return 0;
95105
}
96106
}
97-
revert("Invalid batchId");
107+
108+
revert BatchMintInvalidBatchId(_batchID);
98109
}
99110

100111
/// @dev Sets the base URI for the batch of tokens with the given batchId.
101112
function _setBaseURI(uint256 _batchId, string memory _baseURI) internal {
102-
require(!batchFrozen[_batchId], "Batch frozen");
113+
if (batchFrozen[_batchId]) {
114+
revert BatchMintMetadataFrozen(_batchId);
115+
}
103116
baseURI[_batchId] = _baseURI;
104117
emit BatchMetadataUpdate(_getBatchStartId(_batchId), _batchId);
105118
}
106119

107120
/// @dev Freezes the base URI for the batch of tokens with the given batchId.
108121
function _freezeBaseURI(uint256 _batchId) internal {
109122
string memory baseURIForBatch = baseURI[_batchId];
110-
require(bytes(baseURIForBatch).length > 0, "Invalid batch");
123+
if (bytes(baseURIForBatch).length == 0) {
124+
revert BatchMintInvalidBatchId(_batchId);
125+
}
111126
batchFrozen[_batchId] = true;
112127
emit MetadataFrozen();
113128
}

contracts/extension/ContractMetadata.sol

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import "./interface/IContractMetadata.sol";
1313
*/
1414

1515
abstract contract ContractMetadata is IContractMetadata {
16+
/// @dev The sender is not authorized to perform the action
17+
error ContractMetadataUnauthorized();
18+
1619
/// @notice Returns the contract metadata URI.
1720
string public override contractURI;
1821

@@ -26,7 +29,7 @@ abstract contract ContractMetadata is IContractMetadata {
2629
*/
2730
function setContractURI(string memory _uri) external override {
2831
if (!_canSetContractURI()) {
29-
revert("Not authorized");
32+
revert ContractMetadataUnauthorized();
3033
}
3134

3235
_setupContractURI(_uri);

contracts/extension/DelayedReveal.sol

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import "./interface/IDelayedReveal.sol";
1212
*/
1313

1414
abstract contract DelayedReveal is IDelayedReveal {
15+
/// @dev The contract doesn't have any url to be delayed revealed
16+
error DelayedRevealNothingToReveal();
17+
18+
/// @dev The result of the returned an incorrect hash
19+
error DelayedRevealIncorrectResultHash(bytes32 expected, bytes32 actual);
20+
1521
/// @dev Mapping from tokenId of a batch of tokens => to delayed reveal data.
1622
mapping(uint256 => bytes) public encryptedData;
1723

@@ -34,14 +40,19 @@ abstract contract DelayedReveal is IDelayedReveal {
3440
function getRevealURI(uint256 _batchId, bytes calldata _key) public view returns (string memory revealedURI) {
3541
bytes memory data = encryptedData[_batchId];
3642
if (data.length == 0) {
37-
revert("Nothing to reveal");
43+
revert DelayedRevealNothingToReveal();
3844
}
3945

4046
(bytes memory encryptedURI, bytes32 provenanceHash) = abi.decode(data, (bytes, bytes32));
4147

4248
revealedURI = string(encryptDecrypt(encryptedURI, _key));
4349

44-
require(keccak256(abi.encodePacked(revealedURI, _key, block.chainid)) == provenanceHash, "Incorrect key");
50+
if (keccak256(abi.encodePacked(revealedURI, _key, block.chainid)) != provenanceHash) {
51+
revert DelayedRevealIncorrectResultHash(
52+
provenanceHash,
53+
keccak256(abi.encodePacked(revealedURI, _key, block.chainid))
54+
);
55+
}
4556
}
4657

4758
/**

contracts/extension/Drop.sol

+37-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ import "./interface/IDrop.sol";
77
import "../lib/MerkleProof.sol";
88

99
abstract contract Drop is IDrop {
10+
/// @dev The sender is not authorized to perform the action
11+
error DropUnauthorized();
12+
13+
/// @dev Exceeded the max token total supply
14+
error DropExceedMaxSupply();
15+
16+
/// @dev No active claim condition
17+
error DropNoActiveCondition();
18+
19+
/// @dev Claim condition invalid currency or price
20+
error DropClaimInvalidTokenPrice(
21+
address expectedCurrency,
22+
uint256 expectedPricePerToken,
23+
address actualCurrency,
24+
uint256 actualExpectedPricePerToken
25+
);
26+
27+
/// @dev Claim condition exceeded limit
28+
error DropClaimExceedLimit(uint256 expected, uint256 actual);
29+
30+
/// @dev Claim condition exceeded max supply
31+
error DropClaimExceedMaxSupply(uint256 expected, uint256 actual);
32+
33+
/// @dev Claim condition not started yet
34+
error DropClaimNotStarted(uint256 expected, uint256 actual);
35+
1036
/*///////////////////////////////////////////////////////////////
1137
State variables
1238
//////////////////////////////////////////////////////////////*/
@@ -54,7 +80,7 @@ abstract contract Drop is IDrop {
5480
bool _resetClaimEligibility
5581
) external virtual override {
5682
if (!_canSetClaimConditions()) {
57-
revert("Not authorized");
83+
revert DropUnauthorized();
5884
}
5985

6086
uint256 existingStartIndex = claimCondition.currentStartId;
@@ -81,7 +107,7 @@ abstract contract Drop is IDrop {
81107

82108
uint256 supplyClaimedAlready = claimCondition.conditions[newStartIndex + i].supplyClaimed;
83109
if (supplyClaimedAlready > _conditions[i].maxClaimableSupply) {
84-
revert("max supply claimed");
110+
revert DropExceedMaxSupply();
85111
}
86112

87113
claimCondition.conditions[newStartIndex + i] = _conditions[i];
@@ -163,18 +189,22 @@ abstract contract Drop is IDrop {
163189
uint256 supplyClaimedByWallet = claimCondition.supplyClaimedByWallet[_conditionId][_claimer];
164190

165191
if (_currency != claimCurrency || _pricePerToken != claimPrice) {
166-
revert("!PriceOrCurrency");
192+
revert DropClaimInvalidTokenPrice(_currency, _pricePerToken, claimCurrency, claimPrice);
167193
}
168194

169195
if (_quantity == 0 || (_quantity + supplyClaimedByWallet > claimLimit)) {
170-
revert("!Qty");
196+
revert DropClaimExceedLimit(claimLimit, _quantity + supplyClaimedByWallet);
171197
}
198+
172199
if (currentClaimPhase.supplyClaimed + _quantity > currentClaimPhase.maxClaimableSupply) {
173-
revert("!MaxSupply");
200+
revert DropClaimExceedMaxSupply(
201+
currentClaimPhase.maxClaimableSupply,
202+
currentClaimPhase.supplyClaimed + _quantity
203+
);
174204
}
175205

176206
if (currentClaimPhase.startTimestamp > block.timestamp) {
177-
revert("cant claim yet");
207+
revert DropClaimNotStarted(currentClaimPhase.startTimestamp, block.timestamp);
178208
}
179209
}
180210

@@ -186,7 +216,7 @@ abstract contract Drop is IDrop {
186216
}
187217
}
188218

189-
revert("!CONDITION.");
219+
revert DropNoActiveCondition();
190220
}
191221

192222
/// @dev Returns the claim condition at the given uid.

contracts/extension/Drop1155.sol

+36-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ import "./interface/IDrop1155.sol";
77
import "../lib/MerkleProof.sol";
88

99
abstract contract Drop1155 is IDrop1155 {
10+
/// @dev The sender is not authorized to perform the action
11+
error DropUnauthorized();
12+
13+
/// @dev Exceeded the max token total supply
14+
error DropExceedMaxSupply();
15+
16+
/// @dev No active claim condition
17+
error DropNoActiveCondition();
18+
19+
/// @dev Claim condition invalid currency or price
20+
error DropClaimInvalidTokenPrice(
21+
address expectedCurrency,
22+
uint256 expectedPricePerToken,
23+
address actualCurrency,
24+
uint256 actualExpectedPricePerToken
25+
);
26+
27+
/// @dev Claim condition exceeded limit
28+
error DropClaimExceedLimit(uint256 expected, uint256 actual);
29+
30+
/// @dev Claim condition exceeded max supply
31+
error DropClaimExceedMaxSupply(uint256 expected, uint256 actual);
32+
33+
/// @dev Claim condition not started yet
34+
error DropClaimNotStarted(uint256 expected, uint256 actual);
35+
1036
/*///////////////////////////////////////////////////////////////
1137
State variables
1238
//////////////////////////////////////////////////////////////*/
@@ -64,7 +90,7 @@ abstract contract Drop1155 is IDrop1155 {
6490
bool _resetClaimEligibility
6591
) external virtual override {
6692
if (!_canSetClaimConditions()) {
67-
revert("Not authorized");
93+
revert DropUnauthorized();
6894
}
6995
ClaimConditionList storage conditionList = claimCondition[_tokenId];
7096
uint256 existingStartIndex = conditionList.currentStartId;
@@ -91,7 +117,7 @@ abstract contract Drop1155 is IDrop1155 {
91117

92118
uint256 supplyClaimedAlready = conditionList.conditions[newStartIndex + i].supplyClaimed;
93119
if (supplyClaimedAlready > _conditions[i].maxClaimableSupply) {
94-
revert("max supply claimed");
120+
revert DropExceedMaxSupply();
95121
}
96122

97123
conditionList.conditions[newStartIndex + i] = _conditions[i];
@@ -174,19 +200,22 @@ abstract contract Drop1155 is IDrop1155 {
174200
uint256 supplyClaimedByWallet = claimCondition[_tokenId].supplyClaimedByWallet[_conditionId][_claimer];
175201

176202
if (_currency != claimCurrency || _pricePerToken != claimPrice) {
177-
revert("!PriceOrCurrency");
203+
revert DropClaimInvalidTokenPrice(_currency, _pricePerToken, claimCurrency, claimPrice);
178204
}
179205

180206
if (_quantity == 0 || (_quantity + supplyClaimedByWallet > claimLimit)) {
181-
revert("!Qty");
207+
revert DropClaimExceedLimit(claimLimit, _quantity + supplyClaimedByWallet);
182208
}
183209

184210
if (currentClaimPhase.supplyClaimed + _quantity > currentClaimPhase.maxClaimableSupply) {
185-
revert("!MaxSupply");
211+
revert DropClaimExceedMaxSupply(
212+
currentClaimPhase.maxClaimableSupply,
213+
currentClaimPhase.supplyClaimed + _quantity
214+
);
186215
}
187216

188217
if (currentClaimPhase.startTimestamp > block.timestamp) {
189-
revert("cant claim yet");
218+
revert DropClaimNotStarted(currentClaimPhase.startTimestamp, block.timestamp);
190219
}
191220
}
192221

@@ -199,7 +228,7 @@ abstract contract Drop1155 is IDrop1155 {
199228
}
200229
}
201230

202-
revert("!CONDITION.");
231+
revert DropNoActiveCondition();
203232
}
204233

205234
/// @dev Returns the claim condition at the given uid.

contracts/extension/DropSinglePhase.sol

+35-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ import "./interface/IDropSinglePhase.sol";
77
import "../lib/MerkleProof.sol";
88

99
abstract contract DropSinglePhase is IDropSinglePhase {
10+
/// @dev The sender is not authorized to perform the action
11+
error DropUnauthorized();
12+
13+
/// @dev Exceeded the max token total supply
14+
error DropExceedMaxSupply();
15+
16+
/// @dev No active claim condition
17+
error DropNoActiveCondition();
18+
19+
/// @dev Claim condition invalid currency or price
20+
error DropClaimInvalidTokenPrice(
21+
address expectedCurrency,
22+
uint256 expectedPricePerToken,
23+
address actualCurrency,
24+
uint256 actualExpectedPricePerToken
25+
);
26+
27+
/// @dev Claim condition exceeded limit
28+
error DropClaimExceedLimit(uint256 expected, uint256 actual);
29+
30+
/// @dev Claim condition exceeded max supply
31+
error DropClaimExceedMaxSupply(uint256 expected, uint256 actual);
32+
33+
/// @dev Claim condition not started yet
34+
error DropClaimNotStarted(uint256 expected, uint256 actual);
35+
1036
/*///////////////////////////////////////////////////////////////
1137
State variables
1238
//////////////////////////////////////////////////////////////*/
@@ -63,7 +89,7 @@ abstract contract DropSinglePhase is IDropSinglePhase {
6389
/// @dev Lets a contract admin set claim conditions.
6490
function setClaimConditions(ClaimCondition calldata _condition, bool _resetClaimEligibility) external override {
6591
if (!_canSetClaimConditions()) {
66-
revert("Not authorized");
92+
revert DropUnauthorized();
6793
}
6894

6995
bytes32 targetConditionId = conditionId;
@@ -75,7 +101,7 @@ abstract contract DropSinglePhase is IDropSinglePhase {
75101
}
76102

77103
if (supplyClaimedAlready > _condition.maxClaimableSupply) {
78-
revert("max supply claimed");
104+
revert DropExceedMaxSupply();
79105
}
80106

81107
claimCondition = ClaimCondition({
@@ -140,19 +166,22 @@ abstract contract DropSinglePhase is IDropSinglePhase {
140166
uint256 _supplyClaimedByWallet = supplyClaimedByWallet[conditionId][_claimer];
141167

142168
if (_currency != claimCurrency || _pricePerToken != claimPrice) {
143-
revert("!PriceOrCurrency");
169+
revert DropClaimInvalidTokenPrice(_currency, _pricePerToken, claimCurrency, claimPrice);
144170
}
145171

146172
if (_quantity == 0 || (_quantity + _supplyClaimedByWallet > claimLimit)) {
147-
revert("!Qty");
173+
revert DropClaimExceedLimit(claimLimit, _quantity + _supplyClaimedByWallet);
148174
}
149175

150176
if (currentClaimPhase.supplyClaimed + _quantity > currentClaimPhase.maxClaimableSupply) {
151-
revert("!MaxSupply");
177+
revert DropClaimExceedMaxSupply(
178+
currentClaimPhase.maxClaimableSupply,
179+
currentClaimPhase.supplyClaimed + _quantity
180+
);
152181
}
153182

154183
if (currentClaimPhase.startTimestamp > block.timestamp) {
155-
revert("cant claim yet");
184+
revert DropClaimNotStarted(currentClaimPhase.startTimestamp, block.timestamp);
156185
}
157186
}
158187

0 commit comments

Comments
 (0)