Module providing the internal functions to extend the diamond's upgrade functionality by adding, replacing, or removing facets.
Key Features
Manages facet lifecycle (add, replace, remove) within a diamond.
Updates selector routing so subsequent calls dispatch to the new facet.
Use this module in your project
You can use modules to wrap around you own project logic while using the default Compose building blocks. Modules provides all the necessary internal helpers for maximum integration
Storage follows the diamond slot layout in this file; any code using the same STORAGE_POSITION or related positions reads and writes shared state.
Upgrade the diamond by adding, replacing, and/or removing facets.
Execution order:
1
Add Facets
Attach new facet contracts and register their function selectors.
2
Replace Facets
Swap existing selectors to point to updated facet implementations.
3
Remove Facets
Unregister selectors that are no longer needed in the diamond.
Then, if _delegate != address(0), the diamond performs a delegatecall with _delegateCalldata and emits DiamondDelegateCall.
functionupgradeDiamond(
address[]calldata _addFacets,
FacetReplacement[]calldata _replaceFacets,
address[]calldata _removeFacets,
address _delegate,
bytescalldata _delegateCalldata,
bytes32 _tag,
bytescalldata _metadata
)external;
Parameters:
Property
Type
Description
_addFacets
address[]
Facet addresses to add
_replaceFacets
FacetReplacement[]
(oldFacet, newFacet) pairs to replace
_removeFacets
address[]
Facet addresses to remove
_delegate
address
Optional contract to delegatecall (address(0) to skip)
_delegateCalldata
bytes
Optional calldata to execute on _delegate
_tag
bytes32
Optional arbitrary metadata, such as release version
_metadata
bytes
Optional arbitrary metadata
Facet Requirement
Facets must implement exportSelectors() in order to make their selectors discoverable by diamonds.
Only the exported selectors will be added to the diamond.
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).
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.
Thrown by importSelectors when the staticcall to _facet.exportSelectors() fails.
Signature:
error ExportSelectorsCallFailed(address _facet);
Thrown by importSelectors when the facet returns data that is not valid ABI-encoded bytes (e.g. wrong offset, length not a multiple of 4, or length exceeds payload).
Signature:
error IncorrectSelectorsEncoding(address _facet);
Thrown by importSelectors when the facet address has no code (e.g. EOA or uninitialized contract).
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 made through upgradeDiamond, addFacets, replaceFacets, and removeFacets are immediately visible to the diamond and all facet that access the shared storage.