Skip to main content

Diamond Module

Internal functions and storage for diamond proxy functionality.

Key Features
  • Exposes internal functions for diamond proxy operations.
  • Manages diamond storage using a dedicated storage slot (DIAMOND_STORAGE_POSITION).
  • Supports facet registration and retrieval through internal mechanisms.
  • No external dependencies, ensuring minimal on-chain footprint.
Module Usage

This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.

Overview

This module provides core internal functions and storage management for diamond proxies. It enables facets to interact with shared diamond storage and manage facet registrations. Changes made through this module are immediately visible to all facets utilizing the diamond storage pattern.

Storage

DiamondStorage

storage-location: erc8042:erc8153.diamond

Definition
struct DiamondStorage {
mapping(bytes4 functionSelector => FacetNode) facetNodes;
FacetList facetList;
}

FacetList

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

FacetNode

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

State Variables

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

Functions

addFacets

function addFacets(address[] memory _facets) ;

Parameters:

PropertyTypeDescription
_facetsaddress[]-

at

function at(bytes memory selectors, uint256 index) pure returns (bytes4 selector);

Parameters:

PropertyTypeDescription
selectorsbytes-
indexuint256-

diamondFallback

Find facet for function that is called and execute the function if a facet is found and return any value.

function diamondFallback() ;

getDiamondStorage

function getDiamondStorage() pure returns (DiamondStorage storage s);

importSelectors

function importSelectors(address _facet) view returns (bytes memory selectors);

Parameters:

PropertyTypeDescription
_facetaddress-

Events

Errors

Best Practices

Best Practice
  • Ensure that facet registration functions (like addFacets and importSelectors) are called only during diamond initialization or controlled upgrade processes.
  • Verify that the DiamondStorage struct is correctly defined and that any new fields are added at the end to maintain storage compatibility.
  • Handle custom errors such as CannotAddFunctionToDiamondThatAlreadyExists and NoSelectorsForFacet to ensure robust error management.

Integration Notes

Shared Storage

This module interacts directly with the diamond's shared storage at the DIAMOND_STORAGE_POSITION, which is identified by keccak256("erc8153.diamond"). The DiamondStorage struct, which includes a FacetList field, resides here. 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.

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.