|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0; |
|
|
|
import "./IERC721.sol"; |
|
import "./IERC721Receiver.sol"; |
|
import "./IERC721Metadata.sol"; |
|
import "./Address.sol"; |
|
import "./Context.sol"; |
|
import "./Strings.sol"; |
|
import "./ERC165.sol"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
contract ERC721 is Context, ERC165, IERC721, IERC721Metadata { |
|
using Address for address; |
|
using Strings for uint256; |
|
|
|
|
|
string private _name; |
|
|
|
|
|
string private _symbol; |
|
|
|
|
|
mapping(uint256 => address) private _owners; |
|
|
|
|
|
mapping(address => uint256) private _balances; |
|
|
|
|
|
mapping(uint256 => address) private _tokenApprovals; |
|
|
|
|
|
mapping(address => mapping(address => bool)) private _operatorApprovals; |
|
|
|
|
|
|
|
|
|
constructor(string memory name_, string memory symbol_) { |
|
_name = name_; |
|
_symbol = symbol_; |
|
} |
|
|
|
|
|
|
|
|
|
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) { |
|
return |
|
interfaceId == type(IERC721).interfaceId || |
|
interfaceId == type(IERC721Metadata).interfaceId || |
|
super.supportsInterface(interfaceId); |
|
} |
|
|
|
|
|
|
|
|
|
function balanceOf(address owner) public view virtual override returns (uint256) { |
|
require(owner != address(0), "ERC721: address zero is not a valid owner"); |
|
return _balances[owner]; |
|
} |
|
|
|
|
|
|
|
|
|
function ownerOf(uint256 tokenId) public view virtual override returns (address) { |
|
address owner = _owners[tokenId]; |
|
require(owner != address(0), "ERC721: invalid token ID"); |
|
return owner; |
|
} |
|
|
|
|
|
|
|
|
|
function name() public view virtual override returns (string memory) { |
|
return _name; |
|
} |
|
|
|
|
|
|
|
|
|
function symbol() public view virtual override returns (string memory) { |
|
return _symbol; |
|
} |
|
|
|
|
|
|
|
|
|
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { |
|
_requireMinted(tokenId); |
|
|
|
string memory baseURI = _baseURI(); |
|
return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : ""; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function _baseURI() internal view virtual returns (string memory) { |
|
return ""; |
|
} |
|
|
|
|
|
|
|
|
|
function approve(address to, uint256 tokenId) public virtual override { |
|
address owner = ERC721.ownerOf(tokenId); |
|
require(to != owner, "ERC721: approval to current owner"); |
|
|
|
require( |
|
_msgSender() == owner || isApprovedForAll(owner, _msgSender()), |
|
"ERC721: approve caller is not token owner nor approved for all" |
|
); |
|
|
|
_approve(to, tokenId); |
|
} |
|
|
|
|
|
|
|
|
|
function getApproved(uint256 tokenId) public view virtual override returns (address) { |
|
_requireMinted(tokenId); |
|
|
|
return _tokenApprovals[tokenId]; |
|
} |
|
|
|
|
|
|
|
|
|
function setApprovalForAll(address operator, bool approved) public virtual override { |
|
_setApprovalForAll(_msgSender(), operator, approved); |
|
} |
|
|
|
|
|
|
|
|
|
function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) { |
|
return _operatorApprovals[owner][operator]; |
|
} |
|
|
|
|
|
|
|
|
|
function transferFrom( |
|
address from, |
|
address to, |
|
uint256 tokenId |
|
) public virtual override { |
|
|
|
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not token owner nor approved"); |
|
|
|
_transfer(from, to, tokenId); |
|
} |
|
|
|
|
|
|
|
|
|
function safeTransferFrom( |
|
address from, |
|
address to, |
|
uint256 tokenId |
|
) public virtual override { |
|
safeTransferFrom(from, to, tokenId, ""); |
|
} |
|
|
|
|
|
|
|
|
|
function safeTransferFrom( |
|
address from, |
|
address to, |
|
uint256 tokenId, |
|
bytes memory data |
|
) public virtual override { |
|
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not token owner nor approved"); |
|
_safeTransfer(from, to, tokenId, data); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _safeTransfer( |
|
address from, |
|
address to, |
|
uint256 tokenId, |
|
bytes memory data |
|
) internal virtual { |
|
_transfer(from, to, tokenId); |
|
require(_checkOnERC721Received(from, to, tokenId, data), "ERC721: transfer to non ERC721Receiver implementer"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _exists(uint256 tokenId) internal view virtual returns (bool) { |
|
return _owners[tokenId] != address(0); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) { |
|
address owner = ERC721.ownerOf(tokenId); |
|
return (spender == owner || isApprovedForAll(owner, spender) || getApproved(tokenId) == spender); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _safeMint(address to, uint256 tokenId) internal virtual { |
|
_safeMint(to, tokenId, ""); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function _safeMint( |
|
address to, |
|
uint256 tokenId, |
|
bytes memory data |
|
) internal virtual { |
|
_mint(to, tokenId); |
|
require( |
|
_checkOnERC721Received(address(0), to, tokenId, data), |
|
"ERC721: transfer to non ERC721Receiver implementer" |
|
); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _mint(address to, uint256 tokenId) internal virtual { |
|
require(to != address(0), "ERC721: mint to the zero address"); |
|
require(!_exists(tokenId), "ERC721: token already minted"); |
|
|
|
_beforeTokenTransfer(address(0), to, tokenId); |
|
|
|
_balances[to] += 1; |
|
_owners[tokenId] = to; |
|
|
|
emit Transfer(address(0), to, tokenId); |
|
|
|
_afterTokenTransfer(address(0), to, tokenId); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _burn(uint256 tokenId) internal virtual { |
|
address owner = ERC721.ownerOf(tokenId); |
|
|
|
_beforeTokenTransfer(owner, address(0), tokenId); |
|
|
|
|
|
_approve(address(0), tokenId); |
|
|
|
_balances[owner] -= 1; |
|
delete _owners[tokenId]; |
|
|
|
emit Transfer(owner, address(0), tokenId); |
|
|
|
_afterTokenTransfer(owner, address(0), tokenId); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _transfer( |
|
address from, |
|
address to, |
|
uint256 tokenId |
|
) internal virtual { |
|
require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner"); |
|
require(to != address(0), "ERC721: transfer to the zero address"); |
|
|
|
_beforeTokenTransfer(from, to, tokenId); |
|
|
|
|
|
_approve(address(0), tokenId); |
|
|
|
_balances[from] -= 1; |
|
_balances[to] += 1; |
|
_owners[tokenId] = to; |
|
|
|
emit Transfer(from, to, tokenId); |
|
|
|
_afterTokenTransfer(from, to, tokenId); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function _approve(address to, uint256 tokenId) internal virtual { |
|
_tokenApprovals[tokenId] = to; |
|
emit Approval(ERC721.ownerOf(tokenId), to, tokenId); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function _setApprovalForAll( |
|
address owner, |
|
address operator, |
|
bool approved |
|
) internal virtual { |
|
require(owner != operator, "ERC721: approve to caller"); |
|
_operatorApprovals[owner][operator] = approved; |
|
emit ApprovalForAll(owner, operator, approved); |
|
} |
|
|
|
|
|
|
|
|
|
function _requireMinted(uint256 tokenId) internal view virtual { |
|
require(_exists(tokenId), "ERC721: invalid token ID"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _checkOnERC721Received( |
|
address from, |
|
address to, |
|
uint256 tokenId, |
|
bytes memory data |
|
) private returns (bool) { |
|
if (to.isContract()) { |
|
try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, data) returns (bytes4 retval) { |
|
return retval == IERC721Receiver.onERC721Received.selector; |
|
} catch (bytes memory reason) { |
|
if (reason.length == 0) { |
|
revert("ERC721: transfer to non ERC721Receiver implementer"); |
|
} else { |
|
|
|
assembly { |
|
revert(add(32, reason), mload(reason)) |
|
} |
|
} |
|
} |
|
} else { |
|
return true; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _beforeTokenTransfer( |
|
address from, |
|
address to, |
|
uint256 tokenId |
|
) internal virtual {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _afterTokenTransfer( |
|
address from, |
|
address to, |
|
uint256 tokenId |
|
) internal virtual {} |
|
} |
|
|