Diamond Upgrade Facet
Orchestrates upgrade functionality, enabling the addition, replacement, and removal of facets.
- Owner-gated upgrade entrypoint (ERC-173
owner). - Optional
delegatecallfor post-upgrade initialization/state migration. - Updates selector routing so subsequent calls dispatch to the new facet.
Storage
State Variables
| Property | Type | Description |
|---|---|---|
DIAMOND_STORAGE_POSITION | bytes32 | Diamond storage slot position in the proxy (Value: keccak256("erc8153.diamond")) |
OWNER_STORAGE_POSITION | bytes32 | Owner storage slot position in the proxy (Value: keccak256("erc173.owner")) |
Diamond Storage
Owner Storage
FacetReplacement
Functions
upgradeDiamond
Upgrade the diamond by adding, replacing, and/or removing facets.
Execution order:Then, if _delegate != address(0), the diamond performs a delegatecall with _delegateCalldata and emits DiamondDelegateCall.
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 |
Facets must implement exportSelectors() in order to make their selectors discoverable by diamonds.
Only the exported selectors will be added to the diamond.
interface IFacet {
function exportSelectors() external pure returns (bytes memory);
}
See Facet-Based Diamond EIP-8153 for more details.
Events
Errors
Best Practices
- Ensure all diamond upgrade operations are performed by authorized wallet. This facet is configure to use the unique owner, use the Upgrade Module to wrap around your own access control logic.
- Always verify facet logic contracts are immutable and trusted before using it inside a diamond.
- Ensure each facet’s
exportSelectors()returns a valid packed selector list in deterministic order. - Carefully audit any optional post-upgrade
delegatecall(delegate contract + calldata).
Security Considerations
TheupgradeDiamond function is critical: this function mutates the diamond’s selector routing and facet list.
Although this facet protected by Owner checks, it can optionally execute an unrestricted delegatecall after facet updates. Allowing changes to the diamond storage.
That being said, make sure to provide verified and trusted contract addresses to avoid any unwanted changes or vulnerabilities