Contract Source Code:
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC721/IERC721.sol)
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address from,
address to,
uint256 tokenId
) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool _approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "@openzeppelin/contracts-v4/token/ERC721/IERC721.sol";
interface ICoverNFT is IERC721 {
function isApprovedOrOwner(address spender, uint tokenId) external returns (bool);
function mint(address to) external returns (uint tokenId);
function changeOperator(address newOperator) external;
function changeNFTDescriptor(address newNFTDescriptor) external;
function totalSupply() external view returns (uint);
function name() external view returns (string memory);
error NotOperator();
error NotMinted();
error WrongFrom();
error InvalidRecipient();
error InvalidNewOperatorAddress();
error InvalidNewNFTDescriptorAddress();
error NotAuthorized();
error UnsafeRecipient();
error AlreadyMinted();
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
interface ICoverNFTDescriptor {
function tokenURI(uint tokenId) external view returns (string memory);
}
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.18;
import "../../interfaces/ICoverNFT.sol";
import "../../interfaces/ICoverNFTDescriptor.sol";
/// @dev Based on Solmate https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC721.sol
contract CoverNFT is ICoverNFT {
string public name;
string public symbol;
mapping(uint => address) internal _ownerOf;
mapping(address => uint) internal _balanceOf;
mapping(uint => address) public getApproved;
mapping(address => mapping(address => bool)) public isApprovedForAll;
uint96 internal _totalSupply;
address public operator;
address public nftDescriptor;
modifier onlyOperator {
if (msg.sender != operator) revert NotOperator();
_;
}
constructor(
string memory _name,
string memory _symbol,
address _operator,
address _nftDescriptor
) {
name = _name;
symbol = _symbol;
operator = _operator;
nftDescriptor = _nftDescriptor;
}
// operator functions
function changeOperator(address _newOperator) public onlyOperator {
if (_newOperator == address(0)) revert InvalidNewOperatorAddress();
operator = _newOperator;
}
function changeNFTDescriptor(address _newNFTDescriptor) public onlyOperator {
if (_newNFTDescriptor == address(0)) revert InvalidNewNFTDescriptorAddress();
nftDescriptor = _newNFTDescriptor;
}
// minting and supply
function mint(address to) external onlyOperator returns (uint id) {
if (to == address(0)) revert InvalidRecipient();
// counter overflow is incredibly unrealistic
unchecked {
id = ++_totalSupply;
_balanceOf[to]++;
}
_ownerOf[id] = to;
emit Transfer(address(0), to, id);
}
function totalSupply() public view returns (uint) {
return _totalSupply;
}
// ERC165
function supportsInterface(bytes4 interfaceId) public pure returns (bool) {
return
interfaceId == 0x01ffc9a7 || // ERC165 Interface ID for ERC165
interfaceId == 0x80ac58cd || // ERC165 Interface ID for ERC721
interfaceId == 0x5b5e139f; // ERC165 Interface ID for ERC721Metadata
}
// ERC721
function tokenURI(uint id) public view virtual returns (string memory uri) {
if (_ownerOf[id] == address(0)) revert NotMinted();
return ICoverNFTDescriptor(nftDescriptor).tokenURI(id);
}
function ownerOf(uint id) public view returns (address owner) {
if ((owner = _ownerOf[id]) == address(0)) revert NotMinted();
}
function balanceOf(address owner) public view returns (uint) {
if (owner == address(0)) revert NotMinted();
return _balanceOf[owner];
}
function approve(address spender, uint id) public {
address owner = _ownerOf[id];
if (msg.sender != owner && !isApprovedForAll[owner][msg.sender]) revert NotAuthorized();
getApproved[id] = spender;
emit Approval(owner, spender, id);
}
function setApprovalForAll(address spender, bool approved) public {
isApprovedForAll[msg.sender][spender] = approved;
emit ApprovalForAll(msg.sender, spender, approved);
}
/// @dev `ownerOf` and `getApproved` throw if the token doesn't exist as per ERC721 spec
/// @dev as a consequence this function will throw as well in that case
function isApprovedOrOwner(address spender, uint tokenId) external view returns (bool) {
address owner = ownerOf(tokenId);
return spender == owner || isApprovedForAll[owner][spender] || spender == getApproved[tokenId];
}
function transferFrom(address from, address to, uint id) public {
if (from != _ownerOf[id]) revert WrongFrom();
if (to == address(0)) revert InvalidRecipient();
if (msg.sender != from && !isApprovedForAll[from][msg.sender] && msg.sender != getApproved[id]) {
revert NotAuthorized();
}
// underflow of the sender's balance is impossible because we check for
// ownership above and the recipient's balance can't realistically overflow
unchecked {
_balanceOf[from]--;
_balanceOf[to]++;
}
_ownerOf[id] = to;
delete getApproved[id];
emit Transfer(from, to, id);
}
function safeTransferFrom(address from, address to, uint id) public {
transferFrom(from, to, id);
if (to.code.length != 0 && ERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, "") != ERC721TokenReceiver.onERC721Received.selector) {
revert UnsafeRecipient();
}
}
function safeTransferFrom(
address from,
address to,
uint id,
bytes calldata data
) public {
transferFrom(from, to, id);
if (to.code.length != 0 && ERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, data) != ERC721TokenReceiver.onERC721Received.selector) {
revert UnsafeRecipient();
}
}
}
/// @notice A generic interface for a contract which properly accepts ERC721 tokens.
/// @dev Based on (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC721.sol)
abstract contract ERC721TokenReceiver {
function onERC721Received(address, address, uint, bytes calldata) external virtual returns
(bytes4) {
return ERC721TokenReceiver.onERC721Received.selector;
}
}