// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; /// @notice ERC-721 token receiver interface (EIP-721). interface IERC721Receiver { function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4); } /// @notice Minimal, dependency-free ERC-721 + ERC-721 Metadata + ERC-165. /// @dev No owner, no burn, no pause, no royalties, no upgrade path. Minting is internal only. /// Custom errors follow the ERC-6093 names so wallets and explorers decode them. abstract contract ERC721 { event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); event ApprovalForAll(address indexed owner, address indexed operator, bool approved); error ERC721InvalidOwner(address owner); error ERC721NonexistentToken(uint256 tokenId); error ERC721IncorrectOwner(address sender, uint256 tokenId, address owner); error ERC721InvalidSender(address sender); error ERC721InvalidReceiver(address receiver); error ERC721InsufficientApproval(address operator, uint256 tokenId); error ERC721InvalidApprover(address approver); error ERC721InvalidOperator(address operator); mapping(uint256 tokenId => address) private _owners; mapping(address owner => uint256) private _balances; mapping(uint256 tokenId => address) private _tokenApprovals; mapping(address owner => mapping(address operator => bool)) private _operatorApprovals; function name() public view virtual returns (string memory); function symbol() public view virtual returns (string memory); function tokenURI(uint256 tokenId) public view virtual returns (string memory); function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) { return interfaceId == 0x01ffc9a7 // ERC-165 || interfaceId == 0x80ac58cd // ERC-721 || interfaceId == 0x5b5e139f; // ERC-721 Metadata } function balanceOf(address owner) public view virtual returns (uint256) { if (owner == address(0)) revert ERC721InvalidOwner(address(0)); return _balances[owner]; } function ownerOf(uint256 tokenId) public view virtual returns (address) { return _requireOwned(tokenId); } function getApproved(uint256 tokenId) public view virtual returns (address) { _requireOwned(tokenId); return _tokenApprovals[tokenId]; } function isApprovedForAll(address owner, address operator) public view virtual returns (bool) { return _operatorApprovals[owner][operator]; } function approve(address to, uint256 tokenId) public virtual { address owner = _requireOwned(tokenId); if (msg.sender != owner && !_operatorApprovals[owner][msg.sender]) { revert ERC721InvalidApprover(msg.sender); } _tokenApprovals[tokenId] = to; emit Approval(owner, to, tokenId); } function setApprovalForAll(address operator, bool approved) public virtual { if (operator == address(0)) revert ERC721InvalidOperator(operator); _operatorApprovals[msg.sender][operator] = approved; emit ApprovalForAll(msg.sender, operator, approved); } function transferFrom(address from, address to, uint256 tokenId) public virtual { if (to == address(0)) revert ERC721InvalidReceiver(address(0)); address owner = _requireOwned(tokenId); if (owner != from) revert ERC721IncorrectOwner(from, tokenId, owner); if (msg.sender != owner && !_operatorApprovals[owner][msg.sender] && _tokenApprovals[tokenId] != msg.sender) { revert ERC721InsufficientApproval(msg.sender, tokenId); } delete _tokenApprovals[tokenId]; unchecked { _balances[from] -= 1; _balances[to] += 1; } _owners[tokenId] = to; emit Transfer(from, to, tokenId); } function safeTransferFrom(address from, address to, uint256 tokenId) public virtual { safeTransferFrom(from, to, tokenId, ""); } function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual { transferFrom(from, to, tokenId); _checkOnERC721Received(from, to, tokenId, data); } function _ownerOf(uint256 tokenId) internal view virtual returns (address) { return _owners[tokenId]; } function _requireOwned(uint256 tokenId) internal view returns (address owner) { owner = _owners[tokenId]; if (owner == address(0)) revert ERC721NonexistentToken(tokenId); } /// @dev Plain mint: does not call the receiver, so a contribution can never be blocked or /// re-entered by the contributor's contract. tokenId must be fresh. function _mint(address to, uint256 tokenId) internal virtual { if (to == address(0)) revert ERC721InvalidReceiver(address(0)); if (_owners[tokenId] != address(0)) revert ERC721InvalidSender(address(0)); unchecked { _balances[to] += 1; } _owners[tokenId] = to; emit Transfer(address(0), to, tokenId); } function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory data) private { if (to.code.length == 0) return; try IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, data) returns (bytes4 retval) { if (retval != IERC721Receiver.onERC721Received.selector) revert ERC721InvalidReceiver(to); } catch (bytes memory reason) { if (reason.length == 0) revert ERC721InvalidReceiver(to); assembly ("memory-safe") { revert(add(32, reason), mload(reason)) } } } }