ERC721

Reference of interfaces, presets, and utilities related to ERC721 contracts.

For an overview of ERC721, read our ERC721 guide.

Core

IERC721

use openzeppelin::token::erc721::interface::IERC721;

Interface of the IERC721 standard as defined in EIP721.

0x33eb2f84c309543403fd69f0d0f363781ef06ef6faeb0131ff16ea3175bd943

Functions

balance_of(account: ContractAddress) → u256 external

Returns the number of NFTs owned by account.

owner_of(token_id: u256) → ContractAddress external

Returns the owner address of token_id.

safe_transfer_from(from: ContractAddress, to: ContractAddress, token_id: u256, data: Span<felt252>) external

Transfer ownership of token_id from from to to, checking first that to is aware of the ERC721 protocol to prevent tokens being locked forever. For information regarding how contracts communicate their awareness of the ERC721 protocol, see Receiving Tokens.

Emits a Transfer event.

transfer_from(from: ContractAddress, to: ContractAddress, token_id: u256) external

Transfer ownership of token_id from from to to.

Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721 transfers or else they may be permanently lost. Usage of IERC721::safe_transfer_from prevents loss, though the caller must understand this adds an external call which potentially creates a reentrancy vulnerability.

Emits a Transfer event.

approve(to: ContractAddress, token_id: u256) external

Change or reaffirm the approved address for an NFT.

Emits an Approval event.

set_approval_for_all(operator: ContractAddress, approved: bool) external

Enable or disable approval for operator to manage all of the caller’s assets.

Emits an Approval event.

get_approved(token_id: u256) -> u256 external

Returns the address approved for token_id.

is_approved_for_all(owner: ContractAddress, operator: ContractAddress) -> bool external

Query if operator is an authorized operator for owner.

Events

Approval(owner: ContractAddress, approved: ContractAddress, token_id: u256) event

Emitted when owner enables approved to manage the token_id token.

ApprovalForAll(owner: ContractAddress, operator: ContractAddress, approved: bool) event

Emitted when owner enables approved to manage the token_id token.

Transfer(from: ContractAddress, to: ContractAddress, token_id: u256) event

Emitted when token_id token is transferred from from to to.

IERC721Metadata

use openzeppelin::token::erc721::interface::IERC721Metadata;

Interface for the optional metadata functions in EIP721.

0x6069a70848f907fa57668ba1875164eb4dcee693952468581406d131081bbd

Functions

name() -> felt252 external

Returns the NFT name.

symbol() -> felt252 external

Returns the NFT ticker symbol.

token_uri(token_id: u256) -> felt252 external

Returns the Uniform Resource Identifier (URI) as a short string for the token_id token. If the URI is not set for token_id, the return value will be 0.

ERC721Component

use openzeppelin::token::erc721::ERC721Component;

ERC721 component implementing IERC721 and IERC721Metadata.

Implementing SRC5Component is a requirement for this component to be implemented.

Embeddable functions

balance_of(self: @ContractState, account: ContractAddress) → u256 external

owner_of(self: @ContractState, token_id: u256) → ContractAddress external

Requirements:

  • token_id exists.

safe_transfer_from(ref self: ContractState, from: ContractAddress, to: ContractAddress, token_id: u256, data: Span<felt252>) external

Requirements:

  • Caller is either approved or the token_id owner.

  • to is not the zero address.

  • from is not the zero address.

  • token_id exists.

  • to is either an account contract or supports the IERC721Receiver interface.

transfer_from(ref self: ContractState, from: ContractAddress, to: ContractAddress, token_id: u256) external

Requirements:

  • Caller either approved or the token_id owner.

  • to is not the zero address.

  • from is not the zero address.

  • token_id exists.

approve(ref self: ContractState, to: ContractAddress, token_id: u256) external

Requirements:

  • The caller is either an approved operator or the token_id owner.

  • to cannot be the token owner or the zero address.

  • token_id exists.

set_approval_for_all(ref self: ContractState, operator: ContractAddress, approved: bool) external

Requirements:

  • operator cannot be the caller.

get_approved(self: @ContractState, token_id: u256) -> u256 external

Requirements:

  • token_id exists.

is_approved_for_all(self: @ContractState, owner: ContractAddress, operator: ContractAddress) -> bool external

name(self: @ContractState) -> felt252 external

symbol(self: @ContractState) -> felt252 external

token_uri(self: @ContractState, token_id: u256) -> felt252 external

camelCase Support

balanceOf(self: @ContractState, account: ContractAddress) -> u256 external

ownerOf(self: @ContractState, tokenId: u256) -> ContractAddress external

safeTransferFrom(ref self: ContractState, from: ContractAddress, to: ContractAddress, tokenId: u256, data: Span<felt252>) external

transferFrom(ref self: ContractState, from: ContractAddress, to: ContractAddress, tokenId: u256) external

setApprovalForAll(ref self: ContractState, operator: ContractAddress, approved: bool) external

getApproved(self: @ContractState, tokenId: u256) -> ContractAddress external

isApprovedForAll(self: @ContractState, owner: ContractAddress, operator: ContractAddress) -> bool external

tokenURI(self: @ContractState, tokenId: u256) -> felt252 external

Internal functions

initializer(ref self: ContractState, name_: felt252, symbol_: felt252) internal

Initializes the contract by setting the token name and symbol. This should be used inside the contract’s constructor.

_owner_of(self: @ContractState, token_id: felt252) -> ContractAddress internal

Internal function that returns the owner address of token_id. This function will panic if the token does not exist.

_exists(self: @ContractState, token_id: u256) -> bool internal

Internal function that returns whether token_id exists.

Tokens start existing when they are minted (_mint), and stop existing when they are burned (_burn).

_is_approved_or_owner(ref self: ContractState, spender: ContractAddress, token_id: u256) -> bool internal

Internal function that returns whether spender is allowed to manage token_id.

Requirements:

  • token_id exists.

_approve(ref self: ContractState, to: ContractAddress, token_id: u256) internal

Internal function that changes or reaffirms the approved address for an NFT.

Emits an Approval event.

Requirements:

  • token_id exists.

  • to is not the current token owner.

_set_approval_for_all(ref self: ContractState, owner: ContractAddress, operator: ContractAddress, approved: bool) internal

Internal function that enables or disables approval for operator to manage all of the owner assets.

Emits an Approval event.

Requirements:

  • operator cannot be the caller.

_mint(ref self: ContractState, to: ContractAddress, token_id: u256) internal

Internal function that mints token_id and transfers it to to.

Emits an Transfer event.

Requirements:

  • to is not the zero address.

  • token_id does not already exist.

_transfer(ref self: ContractState, from: ContractAddress, to: ContractAddress, token_id: u256) internal

Internal function that transfers token_id from from to to.

Emits an Transfer event.

Requirements:

  • to is not the zero address.

  • from is the token owner.

  • token_id exists.

_burn(ref self: ContractState, token_id: u256) internal

Internal function that destroys token_id. The approval is cleared when the token is burned. This internal function does not check if the sender is authorized to operate on the token.

Emits an Transfer event.

Requirements:

token_id exists.

_safe_mint(ref self: ContractState, to: ContractAddress, token_id: u256, data: Span<felt252>) internal

Internal function that mints token_id and transfers it to to. If to is not an account contract, to must support IERC721Receiver; otherwise, the transaction will fail.

Emits an Transfer event.

Requirements:

  • token_id does not already exist.

  • to is either an account contract or supports the IERC721Receiver interface.

_safe_transfer(ref self: ContractState, from: ContractAddress, to: ContractAddress, token_id: u256, data: Span<felt252>) internal

Internal function that transfers token_id 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 does not include permissions but can be useful for instances like implementing alternative mechanisms to perform signature-based token transfers.

Emits an Transfer event.

Requirements:

  • to cannot be the zero address.

  • from must be the token owner.

  • token_id exists.

  • to either is an account contract or supports the IERC721Receiver interface.

_set_token_uri(ref self: ContractState, token_id: u256, token_uri: felt252) internal

Internal function that sets the token_uri of token_id.

Requirements:

  • token_id exists.

Events

Approval(owner: ContractAddress, approved: ContractAddress, token_id: u256) event

See Approval.

ApprovalForAll(owner: ContractAddress, operator: ContractAddress, approved: bool) event

Transfer(from: ContractAddress, to: ContractAddress, token_id: u256) event

See Transfer.

IERC721Receiver

use openzeppelin::token::erc721::interface::IERC721Receiver;

Interface for contracts that support receiving safe_transfer_from transfers.

0x3a0dff5f70d80458ad14ae37bb182a728e3c8cdda0402a5daa86620bdf910bc

Functions

on_erc721_received(operator: ContractAddress, from: ContractAddress, token_id: u256, data Span<felt252>) -> felt252 external

Whenever an IERC721 token_id token is transferred to this non-account contract via IERC721::safe_transfer_from by operator from from, this function is called.

Presets

ERC721

use openzeppelin::presets::ERC721;

Basic ERC721 contract leveraging ERC721Component.

0x05e5a302b02eca41819fe263420eb8dc96bfb9770a90f55847c4c1337b551635

Embedded Implementations

Constructor

constructor(ref self: ContractState, name: felt252, symbol: felt252, recipient: ContractAddress, token_ids: Span<u256>, token_uris: Span<felt252>) constructor

Sets the name and symbol.

Mints token_ids tokens with each corresponding URI from token_uris to recipient.