Skip to main content

ERC-721 Data Facet

ERC-721 token data retrieval functions for diamonds

Key Features
  • Exposes external view functions for ERC-721 token data.
  • Accesses shared diamond storage via getStorage internal function.
  • Supports ERC-721 data querying patterns like ownerOf, balanceOf, getApproved, and isApprovedForAll.
  • Provides exportSelectors for diamond facet discovery.

Overview

This facet provides external view functions for retrieving ERC-721 token ownership and approval data within a diamond. It accesses shared diamond storage to provide these details, enabling other facets or external contracts to query token information without direct storage access. Developers integrate this facet to expose standard ERC-721 data queries through the diamond proxy interface.

Storage

ERC721Storage

Definition
struct ERC721Storage {
mapping(uint256 tokenId => address owner) ownerOf;
mapping(address owner => uint256 balance) balanceOf;
mapping(address owner => mapping(address operator => bool approved)) isApprovedForAll;
mapping(uint256 tokenId => address approved) approved;
}

State Variables

PropertyTypeDescription
STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("erc721"))

Functions

balanceOf

Returns the number of tokens owned by a given address.

function balanceOf(address _owner) external view returns (uint256);

Parameters:

PropertyTypeDescription
_owneraddressThe address to query the balance of.

Returns:

PropertyTypeDescription
-uint256The balance (number of tokens) owned by _owner.

ownerOf

Returns the owner of a given token ID.

function ownerOf(uint256 _tokenId) public view returns (address);

Parameters:

PropertyTypeDescription
_tokenIduint256The token ID to query.

Returns:

PropertyTypeDescription
-addressThe address of the token owner.

getApproved

Returns the approved address for a given token ID.

function getApproved(uint256 _tokenId) external view returns (address);

Parameters:

PropertyTypeDescription
_tokenIduint256The token ID to query the approval of.

Returns:

PropertyTypeDescription
-addressThe approved address for the token.

isApprovedForAll

Returns true if an operator is approved to manage all of an owner's assets.

function isApprovedForAll(address _owner, address _operator) external view returns (bool);

Parameters:

PropertyTypeDescription
_owneraddressThe token owner.
_operatoraddressThe operator address.

Returns:

PropertyTypeDescription
-boolTrue if the operator is approved for all tokens of the owner.

exportSelectors

Exports the function selectors of the ERC721DataFacet This function is use as a selector discovery mechanism for diamonds

function exportSelectors() external pure returns (bytes memory);

Returns:

PropertyTypeDescription
-bytesselectors The exported function selectors of the ERC721DataFacet

Errors

Best Practices

Best Practice
  • Ensure the ERC721DataFacet is correctly registered with the diamond's facet registry.
  • Always call functions like ownerOf and balanceOf through the diamond proxy address.
  • Verify that the ERC721Storage struct is correctly initialized and populated by other facets before querying.

Security Considerations

Security

This facet primarily exposes view functions and does not perform state modifications. Ensure that the underlying ERC721Storage is managed securely by other facets. The ownerOf and balanceOf functions revert with ERC721NonexistentToken if the token ID does not exist, and ownerOf reverts with ERC721InvalidOwner if the owner address is zero. Input validation for token IDs and addresses is handled by the facet.

Was this helpful?
Last updated:

Newsletter

Get notified about releases, feature announcements, and technical deep-dives on building smart contracts with Compose.

No spam. Unsubscribe anytime.