Skip to content

Solidity API

LibDiamond

A library for implementing the Diamond pattern (EIP-2535)

This library provides core functionality for diamond proxies, including facet management and function selector handling

InValidFacetCutAction

solidity
error InValidFacetCutAction()

Error thrown when an invalid facet cut action is provided

NotDiamondOwner

solidity
error NotDiamondOwner()

Error thrown when the caller is not the diamond owner

NoSelectorsInFacet

solidity
error NoSelectorsInFacet()

Error thrown when no selectors are provided for a facet

NoZeroAddress

solidity
error NoZeroAddress()

Error thrown when the zero address is used

SelectorExists

solidity
error SelectorExists(bytes4 selector)

Error thrown when a selector already exists

Parameters

NameTypeDescription
selectorbytes4The existing selector

SameSelectorReplacement

solidity
error SameSelectorReplacement(bytes4 selector)

Error thrown when attempting to replace a selector with the same selector

Parameters

NameTypeDescription
selectorbytes4The selector being replaced

MustBeZeroAddress

solidity
error MustBeZeroAddress()

Error thrown when a non-zero address is expected

NoCode

solidity
error NoCode()

Error thrown when a contract has no code

NonExistentSelector

solidity
error NonExistentSelector(bytes4 selector)

Error thrown when a non-existent selector is used

Parameters

NameTypeDescription
selectorbytes4The non-existent selector

ImmutableFunction

solidity
error ImmutableFunction(bytes4 selector)

Error thrown when attempting to modify an immutable function

Parameters

NameTypeDescription
selectorbytes4The selector of the immutable function

NonEmptyCalldata

solidity
error NonEmptyCalldata()

Error thrown when calldata is expected to be empty

EmptyCalldata

solidity
error EmptyCalldata()

Error thrown when calldata is expected to be non-empty

InitCallFailed

solidity
error InitCallFailed()

Error thrown when an initialization call fails

DIAMOND_STORAGE_POSITION

solidity
bytes32 DIAMOND_STORAGE_POSITION

Unique storage position for diamond storage

FacetAddressAndPosition

Struct to store facet address and function selector position

Parameters

NameTypeDescription
solidity
struct FacetAddressAndPosition {
  address facetAddress;
  uint96 functionSelectorPosition;
}

FacetFunctionSelectors

Struct to store function selectors for a facet

Parameters

NameTypeDescription
solidity
struct FacetFunctionSelectors {
  bytes4[] functionSelectors;
  uint256 facetAddressPosition;
}

DiamondStorage

Main storage struct for the diamond

Stores all necessary information for the diamond proxy

solidity
struct DiamondStorage {
  mapping(bytes4 => struct LibDiamond.FacetAddressAndPosition) selectorToFacetAndPosition;
  mapping(address => struct LibDiamond.FacetFunctionSelectors) facetFunctionSelectors;
  address[] facetAddresses;
  mapping(bytes4 => bool) supportedInterfaces;
  mapping(string => bool) supportedFunctionInterfaces;
  address contractOwner;
  address diamondAddress;
}

diamondStorage

solidity
function diamondStorage() internal pure returns (struct LibDiamond.DiamondStorage ds)

Retrieves the diamond storage

Return Values

NameTypeDescription
dsstruct LibDiamond.DiamondStorageThe DiamondStorage struct

OwnershipTransferred

solidity
event OwnershipTransferred(address previousOwner, address newOwner)

Emitted when contract ownership is transferred

Parameters

NameTypeDescription
previousOwneraddressAddress of the previous owner
newOwneraddressAddress of the new owner

setContractOwner

solidity
function setContractOwner(address _newOwner) internal

Sets a new contract owner

Parameters

NameTypeDescription
_newOwneraddressAddress of the new owner

contractOwner

solidity
function contractOwner() internal view returns (address contractOwner_)

Retrieves the current contract owner

Return Values

NameTypeDescription
contractOwner_addressAddress of the current contract owner

enforceIsContractOwner

solidity
function enforceIsContractOwner() internal view

Ensures that the caller is the contract owner

Reverts if the caller is not the contract owner

diamondCut

solidity
function diamondCut(struct IDiamondCut.FacetCut[] _diamondCut, struct IPropsInitCall.InitCall[] _initCalls) internal

Internal function to perform a diamond cut

Parameters

NameTypeDescription
_diamondCutstruct IDiamondCut.FacetCut[]Array of FacetCut structs describing the changes
_initCallsstruct IPropsInitCall.InitCall[]Array of InitCall structs for initialization

executeExternalInitCalls

solidity
function executeExternalInitCalls(address _diamond, struct IPropsInitCall.InitCall[] _externalInitCalls) internal

modifySupportedInterfaces

solidity
function modifySupportedInterfaces(bytes4[] _addInterfaceIds, bytes4[] _removeInterfaceIds) internal

Modifies the supported interfaces

Parameters

NameTypeDescription
_addInterfaceIdsbytes4[]Array of interface IDs to add
_removeInterfaceIdsbytes4[]Array of interface IDs to remove

modifySupportedFunctionInterfaces

solidity
function modifySupportedFunctionInterfaces(string[] _addInterfaceIds, string[] _removeInterfaceIds) internal

Modifies the supported function interfaces

Parameters

NameTypeDescription
_addInterfaceIdsstring[]Array of function interface IDs to add
_removeInterfaceIdsstring[]Array of function interface IDs to remove

addFunctions

solidity
function addFunctions(address _facetAddress, bytes4[] _functionSelectors) internal

Adds new functions to a facet

Parameters

NameTypeDescription
_facetAddressaddressAddress of the facet
_functionSelectorsbytes4[]Array of function selectors to add

replaceFunctions

solidity
function replaceFunctions(address _facetAddress, bytes4[] _functionSelectors) internal

Replaces functions in a facet

Parameters

NameTypeDescription
_facetAddressaddressAddress of the facet
_functionSelectorsbytes4[]Array of function selectors to replace

removeFunctions

solidity
function removeFunctions(address _facetAddress, bytes4[] _functionSelectors) internal

Removes functions from a facet

Parameters

NameTypeDescription
_facetAddressaddressAddress of the facet (should be zero address)
_functionSelectorsbytes4[]Array of function selectors to remove

addFacet

solidity
function addFacet(struct LibDiamond.DiamondStorage ds, address _facetAddress) internal

Adds a new facet to the diamond

Parameters

NameTypeDescription
dsstruct LibDiamond.DiamondStorageDiamondStorage struct
_facetAddressaddressAddress of the new facet

addFunction

solidity
function addFunction(struct LibDiamond.DiamondStorage ds, bytes4 _selector, uint96 _selectorPosition, address _facetAddress) internal

Adds a function to a facet

Parameters

NameTypeDescription
dsstruct LibDiamond.DiamondStorageDiamondStorage struct
_selectorbytes4Function selector to add
_selectorPositionuint96Position of the selector in the functionSelectors array
_facetAddressaddressAddress of the facet

removeFunction

solidity
function removeFunction(struct LibDiamond.DiamondStorage ds, address _facetAddress, bytes4 _selector) internal

Removes a function from a facet

Parameters

NameTypeDescription
dsstruct LibDiamond.DiamondStorageDiamondStorage struct
_facetAddressaddressAddress of the facet
_selectorbytes4Function selector to remove

initializeDiamondCut

solidity
function initializeDiamondCut(struct IPropsInitCall.InitCall[] _initCalls) internal

Initializes the diamond cut

Parameters

NameTypeDescription
_initCallsstruct IPropsInitCall.InitCall[]Array of InitCall structs for initialization

bytesToHex

solidity
function bytesToHex(bytes data) internal pure returns (string)

Converts bytes to a hexadecimal string

Parameters

NameTypeDescription
databytesBytes to convert

Return Values

NameTypeDescription
[0]stringHexadecimal string representation of the input bytes

enforceHasContractCode

solidity
function enforceHasContractCode(address _contract) internal view

Ensures that a contract has code

Parameters

NameTypeDescription
_contractaddressAddress of the contract to check

createFunctionIdentifier

solidity
function createFunctionIdentifier(address _facetAddress, bytes4 _selector) internal pure returns (string)

Creates a unique function identifier

Parameters

NameTypeDescription
_facetAddressaddressAddress of the facet
_selectorbytes4Function selector

Return Values

NameTypeDescription
[0]stringUnique function identifier string

addressToString

solidity
function addressToString(address _addr) internal pure returns (string)

Converts an address to a string

Parameters

NameTypeDescription
_addraddressAddress to convert

Return Values

NameTypeDescription
[0]stringString representation of the address

bytes4ToHexString

solidity
function bytes4ToHexString(bytes4 _bytes) internal pure returns (string)

Converts bytes4 to a hexadecimal string

Parameters

NameTypeDescription
_bytesbytes4Bytes4 to convert

Return Values

NameTypeDescription
[0]stringHexadecimal string representation of the bytes4

toHexString

solidity
function toHexString(uint256 value, uint256 length) internal pure returns (string)

Converts a uint256 value to a hexadecimal string of specified length

Parameters

NameTypeDescription
valueuint256The uint256 value to convert
lengthuint256The desired length of the output string

Return Values

NameTypeDescription
[0]stringHexadecimal string representation of the input value