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.
safeTransferFrom(address from, address to, uint256 tokenId)
public
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)
public
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
.
ERC721
approve(address to, uint256 tokenId)
public
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
public
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)
public
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
public
Tells whether an operator is approved by a given owner.
transferFrom(address from, address to, uint256 tokenId)
public
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)
public
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)
public
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)
internal
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
_isApprovedOrOwner(address spender, uint256 tokenId) → bool
internal
Returns whether the given spender can transfer a given token ID.
_safeMint(address to, uint256 tokenId)
internal
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
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
Internal function to mint a new token. Reverts if the given token ID already exists.
_burn(address owner, uint256 tokenId)
internal
Internal function to burn a specific token.
Reverts if the token does not exist.
Deprecated, use _burn
instead.
_burn(uint256 tokenId)
internal
Internal function to burn a specific token. Reverts if the token does not exist.
_transferFrom(address from, address to, uint256 tokenId)
internal
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
Internal function to invoke IERC721Receiver.onERC721Received
on a target address.
The call is not executed if the target address is not a contract.
This is an internal detail of the ERC721
contract and its use is deprecated.
ERC721Metadata
tokenURI(uint256 tokenId) → string
external
Returns the URI for a given token ID. May return an empty string.
If the token’s URI is non-empty and a base URI was set (via
_setBaseURI
), it will be added to the token ID’s URI as a prefix.
Reverts if the token ID does not exist.
_setTokenURI(uint256 tokenId, string _tokenURI)
internal
Internal function to set the token URI for a given token.
Reverts if the token ID does not exist.
if all token IDs share a prefix (e.g. if your URIs look like
http://api.myproject.com/token/<id> ), use _setBaseURI to store
it and save gas.
|
_setBaseURI(string baseURI)
internal
Internal function to set the base URI for all token IDs. It is
automatically added as a prefix to the value returned in tokenURI
.
Available since v2.5.0.
baseURI() → string
external
Returns the base URI set via _setBaseURI
. This will be
automatically added as a preffix in tokenURI
to each token’s URI, when
they are non-empty.
Available since v2.5.0.
ERC721Enumerable
tokenOfOwnerByIndex(address owner, uint256 index) → uint256
public
Gets the token ID at a given index of the tokens list of the requested owner.
tokenByIndex(uint256 index) → uint256
public
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
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
Internal function to mint a new token. Reverts if the given token ID already exists.
_burn(address owner, uint256 tokenId)
internal
Internal function to burn a specific token.
Reverts if the token does not exist.
Deprecated, use ERC721._burn
instead.
ERC721Full
This implementation includes all the required and some optional functionality of the ERC721 standard Moreover, it includes approve all functionality using operator terminology.
IERC721Receiver
Interface for any contract that wants to support safeTransfers from ERC721 asset contracts.
onERC721Received(address operator, address from, uint256 tokenId, bytes data) → bytes4
public
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.
ERC721MetadataMintable
ERC721 minting logic with metadata.
ERC721Burnable
ERC721 Token that can be irreversibly burned (destroyed).