Skip to main content

Diamond Inspect Facet

It lets you inspect the diamond’s routing table and retrieve the registered facet list.

Key Features
  • Query which facet a function selector is routed to.
  • Enumerate registered facets (and their exported selectors).
  • Read the diamond’s composition from shared diamond storage (DIAMOND_STORAGE_POSITION).

Storage

State Variables

PropertyTypeDescription
DIAMOND_STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("erc8153.diamond"))

Diamond Storage

Definition
/** storage-location: erc8042:erc8153.diamond */
struct DiamondStorage {
mapping(bytes4 functionSelector => FacetNode) facetNodes;
FacetList facetList;
}

struct FacetList {
bytes4 headFacetNodeId;
bytes4 tailFacetNodeId;
uint32 facetCount;
uint32 selectorCount;
}

struct FacetNode {
address facet;
bytes4 prevFacetNodeId;
bytes4 nextFacetNodeId;
}

Facet

Used as a return type for the facets function.

Definition
struct Facet {
address facet;
bytes4[] functionSelectors;
}

Functions

facetAddress

Gets the facet address that handles the given selector. If facet is not found return address(0).

function facetAddress(bytes4 _functionSelector) external view returns (address facet);

Parameters:

PropertyTypeDescription
_functionSelectorbytes4The function selector.

Returns:

PropertyTypeDescription
facetaddressThe facet address that handles the given selector. (If not found return address(0))

facetFunctionSelectors

Gets the function selectors exported by the given facet and returns them as bytes4[].

If the diamond is not currently routing the facet for the facet’s first exported selector, this function returns an empty array. If _facet does not implement exportSelectors() with the expected packing, the call will revert.

function facetFunctionSelectors(address _facet) external view returns (bytes4[] memory facetSelectors);

Parameters:

PropertyTypeDescription
_facetaddressFacet contract address. Must implement exportSelectors() (packed bytes4 selectors).

Returns:

PropertyTypeDescription
facetSelectorsbytes4[]Unpacked selectors returned by facet.exportSelectors(), or an empty array if the facet is not registered.

facetAddresses

Gets the facet addresses used by the diamond. If facetList.facetCount is 0, this returns an empty array.

function facetAddresses() external view returns (address[] memory allFacets);

Returns:

PropertyTypeDescription
allFacetsaddress[]Facet addresses in the diamond's internal traversal order.

facets

Returns the facet address and function selectors of all facets in the diamond.

function facets() external view returns (Facet[] memory facetsAndSelectors);

Returns:

PropertyTypeDescription
facetsAndSelectorsFacet[]An array of Facet structs containing each facet address and its function selectors.

Selector Packing Notes (exportSelectors())

exportSelectors() returns a packed bytes blob where each bytes4 selector is encoded into 4 consecutive bytes (a bytes.concat(sel1, sel2, ...) style packing).

DiamondInspectFacet follows that convention:

  • facetFunctionSelectors(facet) calls facet.exportSelectors() and unpacks the packed bytes into a bytes4[].
  • facets() does the same for every facet discovered in the diamond.

Best Practices

  • Integrate this facet to enable external inspection of diamond facet mappings (Useful for tooling and indexing).
  • Use facetAddress to determine which facet handles a specific function selector.
  • Utilize facets and facetFunctionSelectors for comprehensive diamond structure analysis.

Security Considerations

Most functions are view/pure and do not mutate state.

However, facetFunctionSelectors() and facets() perform external calls to IFacet(facet).exportSelectors(). If the provided address is not a valid IFacet implementation (or if exportSelectors() reverts / returns unexpected data), these calls can revert.

From on-chain code, avoid passing untrusted facet addresses.

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.