Skip to main content

DiamondUpgradeMod

Upgrade diamond with add, replace, and remove functions

Key Features
  • Manages facet additions, replacements, and removals within a diamond.
  • Supports optional delegatecall for state modification or initialization during upgrades.
  • Emits events for all function changes (DiamondFunctionAdded, DiamondFunctionReplaced, DiamondFunctionRemoved).
  • Includes specific errors to prevent invalid upgrade operations.
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 functions to upgrade a diamond's facet implementations. It allows adding new functions, replacing existing ones, and removing functions entirely, all managed through diamond storage. The upgradeDiamond function orchestrates these changes, optionally performing a delegatecall for state modifications or initialization.


Storage

DiamondStorage

storage-location: erc8042:erc8109.diamond

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

FacetAndPosition

Data stored for each function selector Facet address of function selector Position of selector in the 'bytes4[] selectors' array

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

addFunctions

function addFunctions(address _facet, bytes4[] calldata _functionSelectors) ;

Parameters:

PropertyTypeDescription
_facetaddress-
_functionSelectorsbytes4[]-

getDiamondStorage

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

removeFunctions

function removeFunctions(bytes4[] calldata _functionSelectors) ;

Parameters:

PropertyTypeDescription
_functionSelectorsbytes4[]-

replaceFunctions

function replaceFunctions(address _facet, bytes4[] calldata _functionSelectors) ;

Parameters:

PropertyTypeDescription
_facetaddress-
_functionSelectorsbytes4[]-

upgradeDiamond

Upgrade the diamond by adding, replacing, or removing functions. - _addFunctions maps new selectors to their facet implementations. - _replaceFunctions updates existing selectors to new facet addresses. - _removeFunctions removes selectors from the diamond. Functions added first, then replaced, then removed. These events are emitted to record changes to functions: - DiamondFunctionAdded - DiamondFunctionReplaced - DiamondFunctionRemoved If _delegate is non-zero, the diamond performs a delegatecall to _delegate using _functionCall. The DiamondDelegateCall event is emitted. The delegatecall is done to alter a diamond's state or to initialize, modify, or remove state after an upgrade. However, if _delegate is zero, no delegatecall is made and no DiamondDelegateCall event is emitted. If _tag is non-zero or if _metadata.length > 0 then the DiamondMetadata event is emitted.

function upgradeDiamond(
FacetFunctions[] calldata _addFunctions,
FacetFunctions[] calldata _replaceFunctions,
bytes4[] calldata _removeFunctions,
address _delegate,
bytes calldata _functionCall,
bytes32 _tag,
bytes calldata _metadata
) ;

Parameters:

PropertyTypeDescription
_addFunctionsFacetFunctions[]Selectors to add, grouped by facet.
_replaceFunctionsFacetFunctions[]Selectors to replace, grouped by facet.
_removeFunctionsbytes4[]Selectors to remove.
_delegateaddressOptional contract to delegatecall (zero address to skip).
_functionCallbytesOptional calldata to execute on _delegate.
_tagbytes32Optional arbitrary metadata, such as release version.
_metadatabytesOptional arbitrary data.

Events

Errors

Best Practices

Best Practice
  • Ensure the diamond's access control mechanism permits the caller to execute upgrade functions.
  • Verify that _addFunctions, _replaceFunctions, and _removeFunctions do not conflict with immutable functions.
  • Handle the DelegateCallReverted error if a delegatecall is performed and reverts.

Integration Notes

Shared Storage

This module interacts with diamond storage at DIAMOND_STORAGE_POSITION, identified by keccak256("erc8109.diamond"). The DiamondStorage struct, though empty in this definition, serves as the root for diamond state. Functions added, replaced, or removed by this module directly modify the diamond's function selector-to-facet mapping, making these changes immediately visible to all facets interacting with the diamond proxy.

Was this helpful?
Last updated: