Diamond Module
This module provides core internal functions and storage management for diamond proxies.
- Provides functions for diamond proxy operations.
- Manages diamond storage using a dedicated storage slot (
"erc8153.diamond"). - Supports facet registration and retrieval through internal mechanisms.
Storage
State Variables
| Property | Type | Description |
|---|---|---|
DIAMOND_STORAGE_POSITION | bytes32 | Storage slot position for the diamond.
Value: keccak256("erc8153.diamond") |
Diamond Storage
Functions
getDiamondStorage
Returns:
| Property | Type | Description |
|---|---|---|
s | DiamondStorage | The DiamondStorage struct in storage. |
addFacets
Registers one or more facets to a diamond. For each facet, selectors are discovered by calling exportSelectors(). Each selector is then mapped to the facet in diamond storage. Reverts if any selector is already registered on the diamond.
Parameters:
| Property | Type | Description |
|---|---|---|
_facets | address[] | The facet addresses to add. Each must implement IFacet and return selectors from exportSelectors(). |
importSelectors
Retrieves the function selectors exposed by a facet by calling its exportSelectors(). Validates the returned ABI-encoded bytes (offset, length, and that the payload length is a multiple of 4) and returns the packed selectors without copying (zero-copy decode).
Used internally by addFacets.
Parameters:
| Property | Type | Description |
|---|---|---|
_facet | address | Contract address implementing exportSelectors() that returns ABI-encoded bytes of 4-byte function selectors. |
Returns:
| Property | Type | Description |
|---|---|---|
selectors | bytes | Packed 4-byte function selectors (length is a multiple of 4). Same memory layout as returned by exportSelectors(); may point into the staticcall return buffer. |
at
Returns the 4-byte function selector at the given index in a packed bytes array of selectors (e.g. from importSelectors or other selector packing).
Parameters:
| Property | Type | Description |
|---|---|---|
selectors | bytes | Packed array of 4-byte function selectors (length must be a multiple of 4). |
index | uint256 | Zero-based index of the selector to read (0 = first selector, 1 = second, etc.). |
Returns:
| Property | Type | Description |
|---|---|---|
selector | bytes4 | The function selector at the given index. |
Events
Errors
Best Practices
- Ensure that facet registration functions (like
addFacetsandimportSelectors) are called only during diamond initialization or controlled upgrade processes. - Verify that the
DiamondStoragestruct is correctly defined and that any new fields are added at the end to maintain storage compatibility. - Handle custom errors such as
CannotAddFunctionToDiamondThatAlreadyExistsandNoSelectorsForFacetto ensure robust error management.
Integration Notes
This module interacts directly with the diamond's shared storage at the DIAMOND_STORAGE_POSITION, which is identified by keccak256("erc8153.diamond").
All functions within this module read from and write to this shared storage. Changes to the facetList or other storage elements are immediately visible to any facet that accesses the same storage slot.