ERC 721

This set of interfaces, contracts, and utilities are all related to the ERC721 Non-Fungible Token Standard.

For a walkthrough on how to create an ERC721 token read our ERC721 guide.

The EIP consists of three interfaces, found here as IERC721, IERC721Metadata, and IERC721Enumerable. Only the first one is required in a contract to be ERC721 compliant.

Each interface is implemented separately in ERC721, ERC721Metadata, and ERC721Enumerable. You can choose the subset of functionality you would like to support in your token by combining the desired subset through inheritance.

The fully featured token implementing all three interfaces is prepackaged as ERC721Full.

Additionally, IERC721Receiver can be used to prevent tokens from becoming forever locked in contracts. Imagine sending an in-game item to an exchange address that can’t send it back!. When using safeTransferFrom, the token contract checks to see that the receiver is an IERC721Receiver, which implies that it knows how to handle ERC721 tokens. If you’re writing a contract that needs to receive ERC721 tokens, you’ll want to include this interface.

Finally, some custom extensions are also included:

  • ERC721Mintable — like the ERC20 version, this allows certain addresses to mint new tokens

  • ERC721Pausable — like the ERC20 version, this allows addresses to freeze transfers of tokens

This page is incomplete. We’re working to improve it for the next release. Stay tuned!

Core

IERC721

Required interface of an ERC721 compliant contract.

balanceOf(address owner) → uint256 balance

Returns the number of NFTs in `owner’s account.

ownerOf(uint256 tokenId) → address owner

Returns the owner of the NFT specified by tokenId.

safeTransferFrom(address from, address to, uint256 tokenId)

Transfers a specific NFT (tokenId) from one account (from) to another (to).

Requirements: - from, to cannot be zero. - tokenId must be owned by from. - If the caller is not from, it must be have been allowed to move this NFT by either approve or setApprovalForAll.

transferFrom(address from, address to, uint256 tokenId)

Transfers a specific NFT (tokenId) from one account (from) to another (to).

Requirements: - If the caller is not from, it must be approved to move this NFT by either approve or setApprovalForAll.

approve(address to, uint256 tokenId)

getApproved(uint256 tokenId) → address operator

setApprovalForAll(address operator, bool _approved)

isApprovedForAll(address owner, address operator) → bool

safeTransferFrom(address from, address to, uint256 tokenId, bytes data)

Transfer(address from, address to, uint256 tokenId)

Approval(address owner, address approved, uint256 tokenId)

ApprovalForAll(address owner, address operator, bool approved)

ERC721

constructor()

balanceOf(address owner) → uint256

Gets the balance of the specified address.

ownerOf(uint256 tokenId) → address

Gets the owner of the specified token ID.

approve(address to, uint256 tokenId)

Approves another address to transfer the given token ID The zero address indicates there is no approved address. There can only be one approved address per token at a given time. Can only be called by the token owner or an approved operator.

getApproved(uint256 tokenId) → address

Gets the approved address for a token ID, or zero if no address set Reverts if the token ID does not exist.

setApprovalForAll(address to, bool approved)

Sets or unsets the approval of a given operator An operator is allowed to transfer all tokens of the sender on their behalf.

isApprovedForAll(address owner, address operator) → bool

Tells whether an operator is approved by a given owner.

transferFrom(address from, address to, uint256 tokenId)

Transfers the ownership of a given token ID to another address. Usage of this method is discouraged, use safeTransferFrom whenever possible. Requires the msg.sender to be the owner, approved, or operator.

safeTransferFrom(address from, address to, uint256 tokenId)

Safely transfers the ownership of a given token ID to another address If the target address is a contract, it must implement IERC721Receiver.onERC721Received, which is called upon a safe transfer, and return the magic value bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); otherwise, the transfer is reverted. Requires the msg.sender to be the owner, approved, or operator

safeTransferFrom(address from, address to, uint256 tokenId, bytes _data)

Safely transfers the ownership of a given token ID to another address If the target address is a contract, it must implement IERC721Receiver.onERC721Received, which is called upon a safe transfer, and return the magic value bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); otherwise, the transfer is reverted. Requires the _msgSender() to be the owner, approved, or operator

_safeTransferFrom(address from, address to, uint256 tokenId, bytes _data)

Safely transfers the ownership of a given token ID to another address If the target address is a contract, it must implement onERC721Received, which is called upon a safe transfer, and return the magic value bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); otherwise, the transfer is reverted. Requires the msg.sender to be the owner, approved, or operator

_exists(uint256 tokenId) → bool

Returns whether the specified token exists.

_isApprovedOrOwner(address spender, uint256 tokenId) → bool

Returns whether the given spender can transfer a given token ID.

_safeMint(address to, uint256 tokenId)

Internal function to safely mint a new token. Reverts if the given token ID already exists. If the target address is a contract, it must implement onERC721Received, which is called upon a safe transfer, and return the magic value bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); otherwise, the transfer is reverted.

_safeMint(address to, uint256 tokenId, bytes _data)

Internal function to safely mint a new token. Reverts if the given token ID already exists. If the target address is a contract, it must implement onERC721Received, which is called upon a safe transfer, and return the magic value bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); otherwise, the transfer is reverted.

_mint(address to, uint256 tokenId)

Internal function to mint a new token. Reverts if the given token ID already exists.

_burn(address owner, uint256 tokenId)

Internal function to burn a specific token. Reverts if the token does not exist. Deprecated, use _burn instead.

_burn(uint256 tokenId)

Internal function to burn a specific token. Reverts if the token does not exist.

_transferFrom(address from, address to, uint256 tokenId)

Internal function to transfer ownership of a given token ID to another address. As opposed to transferFrom, this imposes no restrictions on msg.sender.

_checkOnERC721Received(address from, address to, uint256 tokenId, bytes _data) → bool

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

This function is deprecated.

ERC721Metadata

constructor(string name, string symbol)

Constructor function

name() → string

Gets the token name.

symbol() → string

Gets the token symbol.

tokenURI(uint256 tokenId) → string

Returns an URI for a given token ID. Throws if the token ID does not exist. May return an empty string.

_setTokenURI(uint256 tokenId, string uri)

Internal function to set the token URI for a given token. Reverts if the token ID does not exist.

_burn(address owner, uint256 tokenId)

Internal function to burn a specific token. Reverts if the token does not exist. Deprecated, use _burn(uint256) instead.

ERC721Enumerable

constructor()

Constructor function.

tokenOfOwnerByIndex(address owner, uint256 index) → uint256

Gets the token ID at a given index of the tokens list of the requested owner.

totalSupply() → uint256

Gets the total amount of tokens stored by the contract.

tokenByIndex(uint256 index) → uint256

Gets the token ID at a given index of all the tokens in this contract Reverts if the index is greater or equal to the total number of tokens.

_transferFrom(address from, address to, uint256 tokenId)

Internal function to transfer ownership of a given token ID to another address. As opposed to transferFrom, this imposes no restrictions on msg.sender.

_mint(address to, uint256 tokenId)

Internal function to mint a new token. Reverts if the given token ID already exists.

_burn(address owner, uint256 tokenId)

Internal function to burn a specific token. Reverts if the token does not exist. Deprecated, use ERC721._burn instead.

_tokensOfOwner(address owner) → uint256[]

Gets the list of token IDs of the requested owner.

ERC721Full

This implementation includes all the required and some optional functionality of the ERC721 standard Moreover, it includes approve all functionality using operator terminology.

constructor(string name, string symbol)

IERC721Receiver

Interface for any contract that wants to support safeTransfers from ERC721 asset contracts.

onERC721Received(address operator, address from, uint256 tokenId, bytes data) → bytes4

The ERC721 smart contract calls this function on the recipient after a IERC721.safeTransferFrom. This function MUST return the function selector, otherwise the caller will revert the transaction. The selector to be returned can be obtained as this.onERC721Received.selector. This function MAY throw to revert and reject the transfer. Note: the ERC721 contract address is always the message sender.

Extensions

ERC721Mintable

ERC721 minting logic.

Modifiers

mint(address to, uint256 tokenId) → bool

Function to mint tokens.

safeMint(address to, uint256 tokenId) → bool

Function to safely mint tokens.

safeMint(address to, uint256 tokenId, bytes _data) → bool

Function to safely mint tokens.

ERC721MetadataMintable

ERC721 minting logic with metadata.

Modifiers

mintWithTokenURI(address to, uint256 tokenId, string tokenURI) → bool

Function to mint tokens.

ERC721Pausable

ERC721 modified with pausable transfers.

approve(address to, uint256 tokenId)

setApprovalForAll(address to, bool approved)

_transferFrom(address from, address to, uint256 tokenId)

Convenience

ERC721Holder

onERC721Received(address, address, uint256, bytes) → bytes4