Skip to main content

ERC721

Git Source

Inherits: Context, ERC165, IERC721, IERC721Metadata

Doesn't track balances.

Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including the Metadata extension, but not including the Enumerable extension, which is available separately as ERC721Enumerable.

State Variables​

_name​

string private _name;

_symbol​

string private _symbol;

_owners​

mapping(uint256 => address) internal _owners;

_tokenApprovals​

mapping(uint256 => address) private _tokenApprovals;

_operatorApprovals​

mapping(address => mapping(address => bool)) private _operatorApprovals;

Functions​

_initialize​

Initializes the contract by setting a name and a symbol to the token collection.

function _initialize(string memory name_, string memory symbol_) internal;

supportsInterface​

See IERC165-supportsInterface.

function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool);

balanceOf​

Balance tracking to be overriden by childs

function balanceOf(address owner) external view virtual override returns (uint256 balance);

ownerOf​

See IERC721-ownerOf.

function ownerOf(uint256 tokenId) public view virtual override returns (address);

name​

See IERC721Metadata-name.

function name() public view virtual override returns (string memory);

symbol​

See IERC721Metadata-symbol.

function symbol() public view virtual override returns (string memory);

tokenURI​

See IERC721Metadata-tokenURI.

function tokenURI(uint256 tokenId) public view virtual override returns (string memory);

_baseURI​

Base URI for computing tokenURI. If set, the resulting URI for each token will be the concatenation of the baseURI and the tokenId. Empty by default, can be overridden in child contracts.

function _baseURI() internal view virtual returns (string memory);

approve​

See IERC721-approve.

function approve(address to, uint256 tokenId) public virtual override;

getApproved​

See IERC721-getApproved.

function getApproved(uint256 tokenId) public view virtual override returns (address);

setApprovalForAll​

See IERC721-setApprovalForAll.

function setApprovalForAll(address operator, bool approved) public virtual override;

isApprovedForAll​

See IERC721-isApprovedForAll.

function isApprovedForAll(address owner, address operator) public view virtual override returns (bool);

transferFrom​

See IERC721-transferFrom.

function transferFrom(address from, address to, uint256 tokenId) public virtual override;

safeTransferFrom​

See IERC721-safeTransferFrom.

function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override;

safeTransferFrom​

See IERC721-safeTransferFrom.

function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual override;

_safeTransfer​

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. data is additional data, it has no specified format and it is sent in call to to. This internal function is equivalent to safeTransferFrom, and can be used to e.g. implement alternative mechanisms to perform token transfer, such as signature-based. Requirements:

  • from cannot be the zero address.
  • to cannot be the zero address.
  • tokenId token must exist and be owned by from.
  • If to refers to a smart contract, it must implement IERC721Receiver-onERC721Received, which is called upon a safe transfer. Emits a Transfer event.
function _safeTransfer(address from, address to, uint256 tokenId, bytes memory data) internal virtual;

_exists​

Returns whether tokenId exists. Tokens can be managed by their owner or approved accounts via approve or setApprovalForAll. Tokens start existing when they are minted (_mint), and stop existing when they are burned (_burn).

function _exists(uint256 tokenId) internal view virtual returns (bool);

_isApprovedOrOwner​

Returns whether spender is allowed to manage tokenId. Requirements:

  • tokenId must exist.
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool);

_mint​

Mints tokenId and transfers it to to. Requirements:

  • tokenId must not exist.
  • to cannot be the zero address. Emits a Transfer event.
function _mint(address to, uint256 tokenId) internal virtual;

_burn​

Destroys tokenId. The approval is cleared when the token is burned. Requirements:

  • tokenId must exist. Emits a Transfer event.
function _burn(uint256 tokenId) internal virtual;

_transfer​

Transfers tokenId from from to to. As opposed to transferFrom, this imposes no restrictions on msg.sender. Requirements:

  • to cannot be the zero address.
  • tokenId token must be owned by from. Emits a Transfer event.
function _transfer(address from, address to, uint256 tokenId) internal virtual;

_approve​

Approve to to operate on tokenId Emits an Approval event.

function _approve(address to, uint256 tokenId) internal virtual;

_setApprovalForAll​

Approve operator to operate on all of owner tokens Emits an ApprovalForAll event.

function _setApprovalForAll(address owner, address operator, bool approved) internal virtual;

_requireMinted​

Reverts if the tokenId has not been minted yet.

function _requireMinted(uint256 tokenId) internal view virtual;

_checkOnERC721Received​

Internal function to invoke IERC721Receiver-onERC721Received on a target address. The call is not executed if the target address is not a contract.

function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory data) private returns (bool);

Parameters

NameTypeDescription
fromaddressaddress representing the previous owner of the given token ID
toaddresstarget address that will receive the tokens
tokenIduint256uint256 ID of the token to be transferred
databytesbytes optional data to send along with the call

Returns

NameTypeDescription
<none>boolbool whether the call correctly returned the expected magic value

_beforeTokenTransfer​

Hook that is called before any token transfer. This includes minting and burning. Calling conditions:

  • When from and to are both non-zero, from's tokenId will be transferred to to.
  • When from is zero, tokenId will be minted for to.
  • When to is zero, from's tokenId will be burned.
  • from and to are never both zero. To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual;

_afterTokenTransfer​

Hook that is called after any transfer of tokens. This includes minting and burning. Calling conditions:

  • when from and to are both non-zero.
  • from and to are never both zero. To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
function _afterTokenTransfer(address from, address to, uint256 tokenId) internal virtual;

Errors​

ALEADY_MINTED​

error ALEADY_MINTED();

APPROVE_TO_CALLER​

error APPROVE_TO_CALLER();

APPROVAL_TO_CURRENT_OWNER​

error APPROVAL_TO_CURRENT_OWNER();

CALLER_NOT_OWNER_OR_APPROVED​

error CALLER_NOT_OWNER_OR_APPROVED();

INVALID_TOKEN_ID​

error INVALID_TOKEN_ID();

INCORRECT_OWNER​

error INCORRECT_OWNER();

MINT_TO_ZERO​

error MINT_TO_ZERO();

TRANSFER_TO_NON_IMPLEMENTER​

error TRANSFER_TO_NON_IMPLEMENTER();

TRANSFER_TO_ZERO_ADDRESS​

error TRANSFER_TO_ZERO_ADDRESS();