Diamond Inspect Facet
It lets you inspect the diamond’s routing table and retrieve the registered facet list.
- 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
| Property | Type | Description |
|---|---|---|
DIAMOND_STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc8153.diamond")) |
Diamond Storage
Facet
Used as a return type for the facets function.
Functions
facetAddress
Gets the facet address that handles the given selector. If facet is not found return address(0).
Parameters:
| Property | Type | Description |
|---|---|---|
_functionSelector | bytes4 | The function selector. |
Returns:
| Property | Type | Description |
|---|---|---|
facet | address | The 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.
Parameters:
| Property | Type | Description |
|---|---|---|
_facet | address | Facet contract address. Must implement exportSelectors() (packed bytes4 selectors). |
Returns:
| Property | Type | Description |
|---|---|---|
facetSelectors | bytes4[] | 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.
Returns:
| Property | Type | Description |
|---|---|---|
allFacets | address[] | Facet addresses in the diamond's internal traversal order. |
facets
Returns the facet address and function selectors of all facets in the diamond.
Returns:
| Property | Type | Description |
|---|---|---|
facetsAndSelectors | Facet[] | 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)callsfacet.exportSelectors()and unpacks the packed bytes into abytes4[].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
facetAddressto determine which facet handles a specific function selector. - Utilize
facetsandfacetFunctionSelectorsfor 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.