Lightweight Diamond
Installaion
NPM
npm install @coinmeca/lightweight-diamond -D
Yarn
yarn add @coinmeca/lightweight-diamond -D
Overview
This project was created to make it easier to use the diamond pattern through abstract implementation and inheritance.
Folder Structure
Functions are spread out from the one single contract as facets in the diamond pattern. Categorizing contracts with just types such as 'interfaces' or 'libraries' would make a project more complex. So in this case suggest to use like a grouping like a family according to their function's same point of features. This hierarchy and its folder structure follow:
─ contracts
└─ services
├─ service1
│ ├─ `Service.sol` // Diamond contract
│ ├─ `IService.sol` // Interface that combine with all of facet's functions for diamond contract
│ ├─ `Data.sol` // Data storage for diamond contract
│ ├─ shared // Shared functions between facets
│ │ ├─ `Modifers.sol` // Modifier functions for facets
│ │ ├─ `Events.sol` // Events for facets
│ │ └─ `Internals.sol` // Shared functions as internal for facets
│ └─ facets // Facets
│ ├─ `Facet1.sol`
│ ├─ `Facet2.sol`
│ └─ `Facet3.sol`
│
├─ service2
:
modules
: The modules
folder is similar to node_modules
. This is a folder that contains template contracts with ready-made functions that we need to refer to and use through importing. The light-weight version of the Diamond contract we use is located here.
Concept
This example was created to help implement diamond patterns easily. Also, if you want to provide clear addresses to separate the touchpoints with which users will interact, you can implement a diamond pattern that shares functionality but has different data storage.
In this example repository, facet management and state value management storage are used separately. Storage for diamond or facet management is created through DiamondContractManger
, which is only used for adding and managing facets. The unique state value of a specific diamond is managed by Data.sol.
DiamondContract
: It is an abstracted contract with a constructor and fallback and receive functions to use the Diamond pattern. It has a separateDiamondContractManger
.DiamondContractManager
: It has storage just for facet management along with the function to manage facets and owners included in the contract.DiamondFacade
: This is an abstraction contract provided for the creation of a diamond contract. It does not hold any storage or facets itself and acts as a relay that only holds 'fallback'.
Inheriting and Implementation
This implementation simplifies building the diamond pattern through contract inheritance, as shown below:
import {DiamondContract} from "@coinmeca/lightweight-diamond/DiamondContract.sol";
contract Service is DiamondContract