Skip to main content

ExampleDiamond

Initializes diamond with facets and owner

Key Features
  • Registers facets and their function selectors on diamond deployment.
  • Establishes the initial owner of the diamond contract.
  • Enables correct call routing through the diamond proxy pattern.

Overview

This constructor initializes a diamond contract by registering facets and their function selectors. It also sets the initial owner of the diamond. This setup ensures that the diamond proxy can correctly route calls to the appropriate facet implementation.


Storage

Functions

constructor

Struct to hold facet address and its function selectors. struct FacetFunctions { address facet; bytes4[] selectors; } Initializes the diamond contract with facets, owner and other data. Adds all provided facets to the diamond's function selector mapping and sets the contract owner. Each facet in the array will have its function selectors registered to enable delegatecall routing.

constructor(DiamondMod.FacetFunctions[] memory _facets, address _diamondOwner) ;

Parameters:

PropertyTypeDescription
_facetsDiamondMod.FacetFunctions[]Array of facet addresses and their corresponding function selectors to add to the diamond.
_diamondOwneraddressAddress that will be set as the owner of the diamond contract.

fallback

fallback() external payable;

receive

receive() external payable;

Best Practices

Best Practice
  • Ensure all facets intended for the diamond are correctly registered with their selectors during initialization.
  • Set the diamond owner with a secure, multi-signature wallet or a dedicated governance contract.
  • Verify that the provided facet addresses are deployed and verified.

Security Considerations

Security

The constructor is critical for setting up the diamond's functionality. Ensure that only trusted addresses are provided as facet implementations and that the owner is set to a secure address. Validate that all function selectors are correctly mapped to their respective facets to prevent unexpected behavior.

Was this helpful?
Last updated: