DiamondUpgradeMod
Upgrade diamond with add, replace, and remove functions
- Manages facet additions, replacements, and removals within a diamond.
- Supports optional
delegatecallfor state modification or initialization during upgrades. - Emits events for all function changes (
DiamondFunctionAdded,DiamondFunctionReplaced,DiamondFunctionRemoved). - Includes specific errors to prevent invalid upgrade operations.
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
FacetAndPosition
Data stored for each function selector Facet address of function selector Position of selector in the 'bytes4[] selectors' array
FacetFunctions
State Variables
| Property | Type | Description |
|---|---|---|
DIAMOND_STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc8109.diamond")) |
Functions
addFunctions
Parameters:
| Property | Type | Description |
|---|---|---|
_facet | address | - |
_functionSelectors | bytes4[] | - |
getDiamondStorage
removeFunctions
Parameters:
| Property | Type | Description |
|---|---|---|
_functionSelectors | bytes4[] | - |
replaceFunctions
Parameters:
| Property | Type | Description |
|---|---|---|
_facet | address | - |
_functionSelectors | bytes4[] | - |
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.
Parameters:
| Property | Type | Description |
|---|---|---|
_addFunctions | FacetFunctions[] | Selectors to add, grouped by facet. |
_replaceFunctions | FacetFunctions[] | Selectors to replace, grouped by facet. |
_removeFunctions | bytes4[] | Selectors to remove. |
_delegate | address | Optional contract to delegatecall (zero address to skip). |
_functionCall | bytes | Optional calldata to execute on _delegate. |
_tag | bytes32 | Optional arbitrary metadata, such as release version. |
_metadata | bytes | Optional arbitrary data. |
Events
Errors
Best Practices
- Ensure the diamond's access control mechanism permits the caller to execute upgrade functions.
- Verify that
_addFunctions,_replaceFunctions, and_removeFunctionsdo not conflict with immutable functions. - Handle the
DelegateCallRevertederror if adelegatecallis performed and reverts.
Integration Notes
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.