Skip to main content

ERC721

ERC721​

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}.

constructor​

constructor(string name_, string symbol_) internal

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

supportsInterface​

function supportsInterface(bytes4 interfaceId) public view virtual returns (bool)

See {IERC165-supportsInterface}.

balanceOf​

function balanceOf(address owner) public view virtual returns (uint256)

See {IERC721-balanceOf}.

ownerOf​

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

See {IERC721-ownerOf}.

name​

function name() public view virtual returns (string)

See {IERC721Metadata-name}.

symbol​

function symbol() public view virtual returns (string)

See {IERC721Metadata-symbol}.

tokenURI​

function tokenURI(uint256 tokenId) public view virtual returns (string)

See {IERC721Metadata-tokenURI}.

_baseURI​

function _baseURI() internal view virtual returns (string)

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.

approve​

function approve(address to, uint256 tokenId) public virtual

See {IERC721-approve}.

getApproved​

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

See {IERC721-getApproved}.

setApprovalForAll​

function setApprovalForAll(address operator, bool approved) public virtual

See {IERC721-setApprovalForAll}.

isApprovedForAll​

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

See {IERC721-isApprovedForAll}.

transferFrom​

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

See {IERC721-transferFrom}.

safeTransferFrom​

function safeTransferFrom(address from, address to, uint256 tokenId) public

See {IERC721-safeTransferFrom}.

safeTransferFrom​

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

See {IERC721-safeTransferFrom}.

_ownerOf​

function _ownerOf(uint256 tokenId) internal view virtual returns (address)

_Returns the owner of the tokenId. Does NOT revert if token doesn't exist

IMPORTANT: Any overrides to this function that add ownership of tokens not tracked by the core ERC721 logic MUST be matched with the use of {increaseBalance} to keep balances consistent with ownership. The invariant to preserve is that for any address a the value returned by balanceOf(a) must be equal to the number of tokens such that _ownerOf(tokenId) is a.

_getApproved​

function _getApproved(uint256 tokenId) internal view virtual returns (address)

Returns the approved address for tokenId. Returns 0 if tokenId is not minted.

_isAuthorized​

function _isAuthorized(address owner, address spender, uint256 tokenId) internal view virtual returns (bool)

_Returns whether spender is allowed to manage owner's tokens, or tokenId in particular (ignoring whether it is owned by owner).

WARNING: This function assumes that owner is the actual owner of tokenId and does not verify this assumption._

_checkAuthorized​

function _checkAuthorized(address owner, address spender, uint256 tokenId) internal view virtual

_Checks if spender can operate on tokenId, assuming the provided owner is the actual owner. Reverts if spender does not have approval from the provided owner for the given token or for all its assets the spender for the specific tokenId.

WARNING: This function assumes that owner is the actual owner of tokenId and does not verify this assumption._

_increaseBalance​

function _increaseBalance(address account, uint128 value) internal virtual

_Unsafe write access to the balances, used by extensions that "mint" tokens using an {ownerOf} override.

NOTE: the value is limited to type(uint128).max. This protect against _balance overflow. It is unrealistic that a uint256 would ever overflow from increments when these increments are bounded to uint128 values.

WARNING: Increasing an account's balance using this function tends to be paired with an override of the {ownerOf} function to resolve the ownership of the corresponding tokens so that balances and ownership remain consistent with one another.

_update​

function _update(address to, uint256 tokenId, address auth) internal virtual returns (address)

_Transfers tokenId from its current owner to to, or alternatively mints (or burns) if the current owner (or to) is the zero address. Returns the owner of the tokenId before the update.

The auth argument is optional. If the value passed is non 0, then this function will check that auth is either the owner of the token, or approved to operate on the token (by the owner).

Emits a {Transfer} event.

NOTE: If overriding this function in a way that tracks balances, see also {increaseBalance}.

_mint​

function _mint(address to, uint256 tokenId) internal

_Mints tokenId and transfers it to to.

WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible

Requirements:

  • tokenId must not exist.
  • to cannot be the zero address.

Emits a {Transfer} event._

_safeMint​

function _safeMint(address to, uint256 tokenId) internal

_Mints tokenId, transfers it to to and checks for to acceptance.

Requirements:

  • tokenId must not exist.
  • If to refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.

Emits a {Transfer} event._

_safeMint​

function _safeMint(address to, uint256 tokenId, bytes data) internal virtual

Same as {xref-ERC721-_safeMint-address-uint256-}[_safeMint], with an additional data parameter which is forwarded in {IERC721Receiver-onERC721Received} to contract recipients.

_burn​

function _burn(uint256 tokenId) internal

_Destroys tokenId. The approval is cleared when the token is burned. This is an internal function that does not check if the sender is authorized to operate on the token.

Requirements:

  • tokenId must exist.

Emits a {Transfer} event._

_transfer​

function _transfer(address from, address to, uint256 tokenId) internal

_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._

_safeTransfer​

function _safeTransfer(address from, address to, uint256 tokenId) internal

_Safely transfers tokenId token from from to to, checking that contract recipients are aware of the ERC721 standard 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 like {safeTransferFrom} in the sense that it invokes {IERC721Receiver-onERC721Received} on the receiver, and can be used to e.g. implement alternative mechanisms to perform token transfer, such as signature-based.

Requirements:

  • tokenId token must exist and be owned by from.
  • to cannot be the zero address.
  • from cannot be the zero address.
  • If to refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.

Emits a {Transfer} event._

_safeTransfer​

function _safeTransfer(address from, address to, uint256 tokenId, bytes data) internal virtual

Same as {xref-ERC721-_safeTransfer-address-address-uint256-}[_safeTransfer], with an additional data parameter which is forwarded in {IERC721Receiver-onERC721Received} to contract recipients.

_approve​

function _approve(address to, uint256 tokenId, address auth) internal

_Approve to to operate on tokenId

The auth argument is optional. If the value passed is non 0, then this function will check that auth is either the owner of the token, or approved to operate on all tokens held by this owner.

Emits an {Approval} event.

Overrides to this logic should be done to the variant with an additional bool emitEvent argument._

_approve​

function _approve(address to, uint256 tokenId, address auth, bool emitEvent) internal virtual

Variant of _approve with an optional flag to enable or disable the {Approval} event. The event is not emitted in the context of transfers.

_setApprovalForAll​

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

_Approve operator to operate on all of owner tokens

Requirements:

  • operator can't be the address zero.

Emits an {ApprovalForAll} event._

_requireOwned​

function _requireOwned(uint256 tokenId) internal view returns (address)

_Reverts if the tokenId doesn't have a current owner (it hasn't been minted, or it has been burned). Returns the owner.

Overrides to ownership logic should be done to {ownerOf}.

_setOwner​

function _setOwner(uint256 tokenId, address owner) internal virtual

Override this function to set owner