Boilerplate code using diamond library(Diamond + CreateX).
git clone https://github.com/JhChoy/diamond-boilerplate
cd diamond-boilerplate
forge install
forge compile
forge test
forge fmt
Define storages following EIP-1967 format in src/storages/ (See Count.sol for example)
Define Facets to be included in the Diamond App in src/facets/
NOTE: Only use storages defined in src/storages. Using conventional storage definitions may cause storage conflicts.
Define your app by inheriting from DiamondApp, like the example CounterApp.sol. As with Facets, be careful not to use conventional storage definitions.
Define interfaces in src/interfaces. You can easily define all interfaces for your app by inheriting from IDiamondApp and other facet interfaces, as shown in ICounterApp.
See below Counter.s.sol.
contract CounterScript is DiamondScript("CounterApp") {
...
function deploy() public broadcast {
facetNames.push("CounterFacet");
facetArgs.push("");
counter = ICounterApp(deploy(abi.encode(msg.sender), salt, facetNames, facetArgs, address(0), "").diamond);
}
function upgrade() public broadcast {
facetNames.push("CounterFacet");
facetArgs.push("");
upgrade(facetNames, facetArgs, address(0), "");
}
}- Inherit from
DiamondScriptand pass the"DiamondApp"contract name to the constructor. - Use
deployfunction to deploy diamond and facets by providing:args: Initialization arguments for the diamondsalt: Salt for deterministic address generationfacetNames: List of facet contract namesfacetArgs: List of encoded constructor arguments for each facetinitContract: Optional initialization contract addressinitData: Optional initialization data- The deployment will automatically save the addresses in
deployments/${diamondName}.${network}.json - All external functions in these Facets will be registered
- Upgrade
DiamondAppusingupgradefunction with:facetNames: List of facet contract namesfacetArgs: List of encoded constructor arguments for each facetinitContract: Optional initialization contract addressinitData: Optional initialization data- This requires an existing deployment file in
deployments/${diamondName}.${network}.json - The upgrade will automatically save the new addresses in the deployment file
- The app will be upgraded with all external functions from the specified Facets
- If a facet is not in the new list, its functions will be removed
- If a facet is updated, its functions will be replaced
- If a facet is new, its functions will be added
All contributions are welcome
MIT