DEV Community


How to Share Functions Between Facets of a Diamond

mudgen profile image Nick Mudge ・2 min read

There are a number of ways to share or reuse functions between facets of an Ethereum diamond. The best way I have found is to write internal functions in Solidity libraries and call those functions in facets.

Only internal functions actually used from a library are added to the bytecode of a facet so it is possible to have large Solidity libraries that are utilized across facets without adding too much to the bytecode of facets that use them.

It is possible to use diamond storage and/or AppStorage in Solidity libraries which makes libraries even more useful and powerful.

Checkout Aavegotchi's libraries which are used across facets.

In addition to using internal functions in Solidity libraries EIP-2535 Diamonds describes a number of other ways functions can be shared or reused between facets:

  1. Copy internal function code in one facet to the other facet.
  2. Put common internal functions in a contract that is inherited by multiple facets.
  3. A type safe way to call an external function defined in another facet is to do this: MyOtherFacet(this).myFunction(arg1, arg2)
  4. A more gas-efficient way to call an external function defined in another facet is to use delegatecall. Here is an example of doing that:

    DiamondStorage storage ds = diamondStorage();
    bytes4 functionSelector = bytes4(keccak256("myFunction(uint256)"));
    // get facet address of function
    address facet = ds.selectorToFacet[functionSelector];
    bytes memory myFunctionCall = abi.encodeWithSelector(functionSelector, 4);
    (bool success, uint result) = address(facet).delegatecall(myFunctionCall);
    require(success, "myFunction failed");
  5. Instead of calling an external function defined in another facet you can instead create an internal function version of the external function. Add the internal version of the function to the facet that needs to use it.

Discussion (0)

Editor guide