|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import { ERC721A } from "../eip/ERC721A.sol"; |
| 5 | + |
| 6 | +import "../extension/ContractMetadata.sol"; |
| 7 | +import "../extension/Multicall.sol"; |
| 8 | +import "../extension/Ownable.sol"; |
| 9 | +import "../extension/Royalty.sol"; |
| 10 | +import "../extension/BatchMintMetadata.sol"; |
| 11 | + |
| 12 | +import "../lib/TWStrings.sol"; |
| 13 | + |
| 14 | +/** |
| 15 | + * The `ERC721Base` smart contract implements the ERC721 NFT standard, along with the ERC721A optimization to the standard. |
| 16 | + * It includes the following additions to standard ERC721 logic: |
| 17 | + * |
| 18 | + * - Ability to mint NFTs via the provided `mint` function. |
| 19 | + * |
| 20 | + * - Contract metadata for royalty support on platforms such as OpenSea that use |
| 21 | + * off-chain information to distribute roaylties. |
| 22 | + * |
| 23 | + * - Ownership of the contract, with the ability to restrict certain functions to |
| 24 | + * only be called by the contract's owner. |
| 25 | + * |
| 26 | + * - Multicall capability to perform multiple actions atomically |
| 27 | + * |
| 28 | + * - EIP 2981 compliance for royalty support on NFT marketplaces. |
| 29 | + */ |
| 30 | + |
| 31 | +contract ERC721Base is ERC721A, ContractMetadata, Multicall, Ownable, Royalty, BatchMintMetadata { |
| 32 | + using TWStrings for uint256; |
| 33 | + |
| 34 | + /*////////////////////////////////////////////////////////////// |
| 35 | + Mappings |
| 36 | + //////////////////////////////////////////////////////////////*/ |
| 37 | + |
| 38 | + mapping(uint256 => string) private fullURI; |
| 39 | + |
| 40 | + /*////////////////////////////////////////////////////////////// |
| 41 | + Constructor |
| 42 | + //////////////////////////////////////////////////////////////*/ |
| 43 | + |
| 44 | + constructor( |
| 45 | + string memory _name, |
| 46 | + string memory _symbol, |
| 47 | + address _royaltyRecipient, |
| 48 | + uint128 _royaltyBps |
| 49 | + ) ERC721A(_name, _symbol) { |
| 50 | + _setupOwner(msg.sender); |
| 51 | + _setupDefaultRoyaltyInfo(_royaltyRecipient, _royaltyBps); |
| 52 | + } |
| 53 | + |
| 54 | + /*////////////////////////////////////////////////////////////// |
| 55 | + ERC165 Logic |
| 56 | + //////////////////////////////////////////////////////////////*/ |
| 57 | + |
| 58 | + /// @dev See ERC165: https://eips.ethereum.org/EIPS/eip-165 |
| 59 | + function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721A, IERC165) returns (bool) { |
| 60 | + return |
| 61 | + interfaceId == 0x01ffc9a7 || // ERC165 Interface ID for ERC165 |
| 62 | + interfaceId == 0x80ac58cd || // ERC165 Interface ID for ERC721 |
| 63 | + interfaceId == 0x5b5e139f || // ERC165 Interface ID for ERC721Metadata |
| 64 | + interfaceId == type(IERC2981).interfaceId; // ERC165 ID for ERC2981 |
| 65 | + } |
| 66 | + |
| 67 | + /*////////////////////////////////////////////////////////////// |
| 68 | + Overriden ERC721 logic |
| 69 | + //////////////////////////////////////////////////////////////*/ |
| 70 | + |
| 71 | + /** |
| 72 | + * @notice Returns the metadata URI for an NFT. |
| 73 | + * @dev See `BatchMintMetadata` for handling of metadata in this contract. |
| 74 | + * |
| 75 | + * @param _tokenId The tokenId of an NFT. |
| 76 | + */ |
| 77 | + function tokenURI(uint256 _tokenId) public view virtual override returns (string memory) { |
| 78 | + string memory fullUriForToken = fullURI[_tokenId]; |
| 79 | + if (bytes(fullUriForToken).length > 0) { |
| 80 | + return fullUriForToken; |
| 81 | + } |
| 82 | + |
| 83 | + string memory batchUri = getBaseURI(_tokenId); |
| 84 | + return string(abi.encodePacked(batchUri, _tokenId.toString())); |
| 85 | + } |
| 86 | + |
| 87 | + /*////////////////////////////////////////////////////////////// |
| 88 | + Minting logic |
| 89 | + //////////////////////////////////////////////////////////////*/ |
| 90 | + |
| 91 | + /** |
| 92 | + * @notice Lets an authorized address mint an NFT to a recipient. |
| 93 | + * @dev The logic in the `_canMint` function determines whether the caller is authorized to mint NFTs. |
| 94 | + * |
| 95 | + * @param _to The recipient of the NFT to mint. |
| 96 | + * @param _tokenURI The full metadata URI for the NFT minted. |
| 97 | + */ |
| 98 | + function mintTo(address _to, string memory _tokenURI) public virtual { |
| 99 | + require(_canMint(), "Not authorized to mint."); |
| 100 | + fullURI[nextTokenIdToMint()] = _tokenURI; |
| 101 | + _safeMint(_to, 1, ""); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @notice Lets an authorized address mint multiple NFTs at once to a recipient. |
| 106 | + * @dev The logic in the `_canMint` function determines whether the caller is authorized to mint NFTs. |
| 107 | + * |
| 108 | + * @param _to The recipient of the NFT to mint. |
| 109 | + * @param _quantity The number of NFTs to mint. |
| 110 | + * @param _baseURI The baseURI for the `n` number of NFTs minted. The metadata for each NFT is `baseURI/tokenId` |
| 111 | + * @param _data Additional data to pass along during the minting of the NFT. |
| 112 | + */ |
| 113 | + function batchMintTo( |
| 114 | + address _to, |
| 115 | + uint256 _quantity, |
| 116 | + string memory _baseURI, |
| 117 | + bytes memory _data |
| 118 | + ) public virtual { |
| 119 | + require(_canMint(), "Not authorized to mint."); |
| 120 | + _batchMintMetadata(nextTokenIdToMint(), _quantity, _baseURI); |
| 121 | + _safeMint(_to, _quantity, _data); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @notice Lets an owner or approved operator burn the NFT of the given tokenId. |
| 126 | + * @dev ERC721A's `_burn(uint256,bool)` internally checks for token approvals. |
| 127 | + * |
| 128 | + * @param _tokenId The tokenId of the NFT to burn. |
| 129 | + */ |
| 130 | + function burn(uint256 _tokenId) external virtual { |
| 131 | + _burn(_tokenId, true); |
| 132 | + } |
| 133 | + |
| 134 | + /*////////////////////////////////////////////////////////////// |
| 135 | + Public getters |
| 136 | + //////////////////////////////////////////////////////////////*/ |
| 137 | + |
| 138 | + /// @notice The tokenId assigned to the next new NFT to be minted. |
| 139 | + function nextTokenIdToMint() public view virtual returns (uint256) { |
| 140 | + return _currentIndex; |
| 141 | + } |
| 142 | + |
| 143 | + /*////////////////////////////////////////////////////////////// |
| 144 | + Internal (overrideable) functions |
| 145 | + //////////////////////////////////////////////////////////////*/ |
| 146 | + |
| 147 | + /// @dev Returns whether contract metadata can be set in the given execution context. |
| 148 | + function _canSetContractURI() internal view virtual override returns (bool) { |
| 149 | + return msg.sender == owner(); |
| 150 | + } |
| 151 | + |
| 152 | + /// @dev Returns whether a token can be minted in the given execution context. |
| 153 | + function _canMint() internal view virtual returns (bool) { |
| 154 | + return msg.sender == owner(); |
| 155 | + } |
| 156 | + |
| 157 | + /// @dev Returns whether owner can be set in the given execution context. |
| 158 | + function _canSetOwner() internal view virtual override returns (bool) { |
| 159 | + return msg.sender == owner(); |
| 160 | + } |
| 161 | + |
| 162 | + /// @dev Returns whether royalty info can be set in the given execution context. |
| 163 | + function _canSetRoyaltyInfo() internal view virtual override returns (bool) { |
| 164 | + return msg.sender == owner(); |
| 165 | + } |
| 166 | +} |
0 commit comments