Skip to main content

DiamondMod

Manages facet additions and function dispatch for diamonds

Key Features
  • Internal functions for facet management and call dispatch.
  • Uses diamond storage pattern for centralized state.
  • No external dependencies, promoting composability.
  • Compatible with ERC-2535 diamond standard.
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 internal functions for managing facets and dispatching calls within a diamond proxy. It enables composability by allowing facets to be added and functions to be routed to the correct implementation. Changes to facet mappings are managed internally, ensuring consistent function resolution across all interacting facets.


Storage

DiamondStorage

storage-location: erc8042:erc8109.diamond

Definition
struct DiamondStorage {
mapping(bytes4 functionSelector => FacetAndPosition) facetAndPosition;
/**
* `selectors` contains all function selectors that can be called in the diamond.
*/
bytes4[] selectors;
}

FacetAndPosition

Definition
struct FacetAndPosition {
address facet;
uint32 position;
}

FacetFunctions

Definition
struct FacetFunctions {
address facet;
bytes4[] selectors;
}

State Variables

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

Functions

addFacets

Adds facets and their function selectors to the diamond. Only supports adding functions during diamond deployment.

function addFacets(FacetFunctions[] memory _facets) ;

Parameters:

PropertyTypeDescription
_facetsFacetFunctions[]-

diamondFallback

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

function diamondFallback() ;

getStorage

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

Events

Errors

Best Practices

Best Practice
  • Call addFacets only during initial diamond deployment to avoid conflicts.
  • Ensure that function selectors are unique across all facets before adding.
  • Handle FunctionNotFound errors when dispatching calls that do not map to any facet.

Integration Notes

Shared Storage

This module utilizes a dedicated storage position, DIAMOND_STORAGE_POSITION, identified by keccak256("erc8109.diamond"). The DiamondStorage struct, although empty in its definition, serves as a conceptual representation of the state managed at this position. Facets interact with this module's functions to register new facets and their associated function selectors, influencing the diamond's runtime behavior. Any updates to the facet mappings are immediately visible to all facets interacting with the diamond proxy.

Was this helpful?
Last updated: