Skip to main content

DiamondUpgradeFacet

Manage diamond upgrades and facet additions

Key Features
  • Manages diamond functional surface area through facet composition.
  • Supports adding, replacing, and removing functions via selector mapping.
  • Enables diamond upgradeability by allowing facet deployments and reconfigurations.
  • Facilitates metadata updates associated with diamond state changes.

Overview

This facet provides core functionality for upgrading diamond proxies by adding, replacing, and removing facets. It allows for delegate calls and metadata updates, integrating directly with the diamond storage pattern. Developers use this facet to manage the diamond's functional surface area and upgrade its components.


Storage

OwnerStorage

Definition
struct OwnerStorage {
address owner;
}

FacetAndPosition

Definition
struct FacetAndPosition {
address facet;
uint32 position;
}

DiamondStorage

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

FacetFunctions

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

State Variables

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

Functions

upgradeDiamond


Function Changes:


DelegateCall:


Metadata:

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
) external;

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
  • Initialize diamond storage ownership using the owner field if applicable.
  • Ensure that selectors for adding or replacing functions do not conflict with immutable functions.
  • Verify that facet bytecode exists at the provided address before attempting to add or replace functions.

Security Considerations

Security

All state-modifying functions (addFunctions, replaceFunctions, removeFunctions, Metadata) must be protected by appropriate access control, typically owner-only, to prevent unauthorized modifications. The Metadata function emits an event, ensuring transparency for metadata changes. Input validation is crucial to prevent errors like NoSelectorsProvidedForFacet, NoBytecodeAtAddress, or conflicts with immutable functions.

Was this helpful?
Last updated: