|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +/// @author thirdweb |
| 5 | + |
| 6 | +/** |
| 7 | + * @title Batch-mint Metadata |
| 8 | + * @notice The `BatchMintMetadata` is a contract extension for any base NFT contract. It lets the smart contract |
| 9 | + * using this extension set metadata for `n` number of NFTs all at once. This is enabled by storing a single |
| 10 | + * base URI for a batch of `n` NFTs, where the metadata for each NFT in a relevant batch is `baseURI/tokenId`. |
| 11 | + */ |
| 12 | + |
| 13 | +contract BatchMintMetadata_V1 { |
| 14 | + /// @dev Largest tokenId of each batch of tokens with the same baseURI. |
| 15 | + uint256[] private batchIds; |
| 16 | + |
| 17 | + /// @dev Mapping from id of a batch of tokens => to base URI for the respective batch of tokens. |
| 18 | + mapping(uint256 => string) private baseURI; |
| 19 | + |
| 20 | + /** |
| 21 | + * @notice Returns the count of batches of NFTs. |
| 22 | + * @dev Each batch of tokens has an in ID and an associated `baseURI`. |
| 23 | + * See {batchIds}. |
| 24 | + */ |
| 25 | + function getBaseURICount() public view returns (uint256) { |
| 26 | + return batchIds.length; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @notice Returns the ID for the batch of tokens at the given index. |
| 31 | + * @dev See {getBaseURICount}. |
| 32 | + * @param _index Index of the desired batch in batchIds array. |
| 33 | + */ |
| 34 | + function getBatchIdAtIndex(uint256 _index) public view returns (uint256) { |
| 35 | + if (_index >= getBaseURICount()) { |
| 36 | + revert("Invalid index"); |
| 37 | + } |
| 38 | + return batchIds[_index]; |
| 39 | + } |
| 40 | + |
| 41 | + /// @dev Returns the id for the batch of tokens the given tokenId belongs to. |
| 42 | + function _getBatchId(uint256 _tokenId) internal view returns (uint256 batchId, uint256 index) { |
| 43 | + uint256 numOfTokenBatches = getBaseURICount(); |
| 44 | + uint256[] memory indices = batchIds; |
| 45 | + |
| 46 | + for (uint256 i = 0; i < numOfTokenBatches; i += 1) { |
| 47 | + if (_tokenId < indices[i]) { |
| 48 | + index = i; |
| 49 | + batchId = indices[i]; |
| 50 | + |
| 51 | + return (batchId, index); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + revert("Invalid tokenId"); |
| 56 | + } |
| 57 | + |
| 58 | + /// @dev Returns the baseURI for a token. The intended metadata URI for the token is baseURI + tokenId. |
| 59 | + function _getBaseURI(uint256 _tokenId) internal view returns (string memory) { |
| 60 | + uint256 numOfTokenBatches = getBaseURICount(); |
| 61 | + uint256[] memory indices = batchIds; |
| 62 | + |
| 63 | + for (uint256 i = 0; i < numOfTokenBatches; i += 1) { |
| 64 | + if (_tokenId < indices[i]) { |
| 65 | + return baseURI[indices[i]]; |
| 66 | + } |
| 67 | + } |
| 68 | + revert("Invalid tokenId"); |
| 69 | + } |
| 70 | + |
| 71 | + /// @dev Sets the base URI for the batch of tokens with the given batchId. |
| 72 | + function _setBaseURI(uint256 _batchId, string memory _baseURI) internal { |
| 73 | + baseURI[_batchId] = _baseURI; |
| 74 | + } |
| 75 | + |
| 76 | + /// @dev Mints a batch of tokenIds and associates a common baseURI to all those Ids. |
| 77 | + function _batchMintMetadata( |
| 78 | + uint256 _startId, |
| 79 | + uint256 _amountToMint, |
| 80 | + string memory _baseURIForTokens |
| 81 | + ) internal returns (uint256 nextTokenIdToMint, uint256 batchId) { |
| 82 | + batchId = _startId + _amountToMint; |
| 83 | + nextTokenIdToMint = batchId; |
| 84 | + |
| 85 | + batchIds.push(batchId); |
| 86 | + |
| 87 | + baseURI[batchId] = _baseURIForTokens; |
| 88 | + } |
| 89 | +} |
0 commit comments