# mudgen/diamond-1-hardhat architecture

> Architecture diagram of the GitHub repository mudgen/diamond-1-hardhat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mudgen/diamond-1-hardhat
- Repository: https://github.com/mudgen/diamond-1-hardhat

## Overview

##### Project type and purpose
- This is a blockchain / smart-contract reference implementation built with Hardhat.
- It implements the EIP-2535 “Diamond Standard,” allowing a single on-chain contract (“diamond”) to route calls to multiple modular facets (contracts).
- Its purpose is to provide boilerplate facets (DiamondCutFacet, DiamondLoupeFacet), library code (LibDiamond), deployment scripts, and tests, so that users can deploy and upgrade a diamond-style proxy contract.

##### File structure
Top-level:
- contracts/              ← Solidity sources
– Diamond.sol           ← the proxy entry point
– facets/               ← modular upgradeable logic contracts
· DiamondCutFacet.sol
· DiamondLoupeFacet.sol
· …other user facets
– interfaces/           ← facet and EIP interfaces
– libraries/            ← LibDiamond.sol (storage + internal diamondCut)
– upgradeInitializers/  ← optional state init contracts
- scripts/                ← deployment and helper scripts
– deploy.js
– libraries/diamond.js  ← helper for constructing cut arrays
- test/                   ← Mocha/Chai tests
– diamondTest.js
– cacheBugTest.js
- hardhat.config.js       ← Hardhat configuration
- package.json            ← JS dependencies & scripts
Patterns:
- Proxy pattern (Diamond.sol) delegating calls
- Library for shared storage and internal logic
- Facet modules loaded at deploy or upgrade time

##### README for additional insights
- Confirms EIP-2535 compliance, notes which loupe functions are gas-heavy vs. on-chain safe.
- Deployment instructions use Hardhat + scripts/deploy.js.
- Tests cover diamondCut/upgrade and loupe queries.
- Encourages copying facets/library into new diamonds.

a. Main components:
- Developer environment (Hardhat + scripts)
- Deployed Diamond contract (proxy)
- Facet contracts (DiamondCutFacet, DiamondLoupeFacet, plus any custom facets)
- LibDiamond library (internal code linked at compile-time)
- Ethereum network (testnet/mainnet)
- Off-chain clients (web3.js/ethers.js front-ends, tests)
b. Relationships & interactions:
- scripts/deploy.js → compiles & deploys Diamond.sol + facets → Ethereum network
- Diamond proxy forwards calls to facet addresses via delegatecall
- Facets implement logic; LibDiamond manages storage & diamondCut
- Off-chain clients call facets through the diamond address (using each facet’s ABI)
- Tests simulate contract calls & upgrades
c. Architectural patterns:
- Diamond proxy (multi-facet upgradeable proxy)
- Delegatecall routing table stored in diamond storage
- Single-transaction upgrades (cut + optional initializer)
d. Key technologies:
- Solidity (contracts)
- Hardhat (compile, test, deploy)
- Ethers.js or web3.js (client libraries)
- Mocha/Chai (tests)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Developer Tools" 
        direction TB
        HardhatConfig["hardhat.config.js"]:::offchain
        DeployScript["deploy.js"]:::offchain
        CutHelper["diamond.js"]:::offchain
        Package["package.json"]:::offchain
        subgraph "Tests"
            direction TB
            Test1["diamondTest.js"]:::offchain
            Test2["cacheBugTest.js"]:::offchain
        end
    end

    subgraph "Off-Chain Interface"
        direction TB
        Client["Web3/Ethers.js Client"]:::offchain
    end

    Ethereum["Ethereum Network"]:::network

    subgraph "On-Chain Contracts"
        direction TB
        subgraph "Diamond Proxy + Library"
            direction TB
            DiamondProxy["Diamond.sol\n(proxy entry point)"]:::onchain
            LibDiamond["LibDiamond.sol\n(storage & diamondCut)"]:::lib
        end
        subgraph "Facet Modules"
            direction TB
            DiamondCutFacet["DiamondCutFacet.sol\n(diamondCut function)"]:::onchain
            DiamondLoupeFacet["DiamondLoupeFacet.sol\n(loupe functions)"]:::onchain
            UserFacets["Other User Facets"]:::onchain
        end
        Interfaces["contracts/interfaces"]:::onchain
        Initializers["upgradeInitializers"]:::onchain
    end

    %% Deploy flow
    HardhatConfig --> DeployScript
    CutHelper --> DeployScript
    DeployScript -->|deploy| Ethereum
    Ethereum -->|deploys| DiamondProxy
    DeployScript -->|initial cut array| DiamondProxy

    %% Runtime interactions
    Client -->|calls functions| DiamondProxy
    Test1 -->|simulates calls/upgrades| DiamondProxy
    Test2 -->|simulates bug reproduction| DiamondProxy

    %% Proxy routing
    DiamondProxy -->|delegatecall| DiamondCutFacet
    DiamondProxy -->|delegatecall| DiamondLoupeFacet
    DiamondProxy -->|delegatecall| UserFacets
    DiamondProxy -->|uses storage| LibDiamond

    %% Dependencies
    DiamondProxy -->|imports| LibDiamond
    DiamondProxy -->|implements| Interfaces
    DiamondProxy -->|optionally init| Initializers

    classDef onchain fill:#e0f7fa,stroke:#006064,stroke-width:2px,shape:rect;
    classDef offchain fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px,shape:rect;
    classDef lib fill:#fff3e0,stroke:#ef6c00,stroke-width:2px,shape:rect;
    classDef network fill:#f3e5f5,stroke:#6a1b9a,stroke-width:2px,shape:round;

    click DiamondProxy "https://github.com/mudgen/diamond-1-hardhat/blob/main/contracts/Diamond.sol"
    click DiamondCutFacet "https://github.com/mudgen/diamond-1-hardhat/blob/main/contracts/facets/DiamondCutFacet.sol"
    click DiamondLoupeFacet "https://github.com/mudgen/diamond-1-hardhat/blob/main/contracts/facets/DiamondLoupeFacet.sol"
    click UserFacets "https://github.com/mudgen/diamond-1-hardhat/tree/main/contracts/facets"
    click Interfaces "https://github.com/mudgen/diamond-1-hardhat/tree/main/contracts/interfaces"
    click LibDiamond "https://github.com/mudgen/diamond-1-hardhat/blob/main/contracts/libraries/LibDiamond.sol"
    click Initializers "https://github.com/mudgen/diamond-1-hardhat/tree/main/contracts/upgradeInitializers"
    click DeployScript "https://github.com/mudgen/diamond-1-hardhat/blob/main/scripts/deploy.js"
    click CutHelper "https://github.com/mudgen/diamond-1-hardhat/blob/main/scripts/libraries/diamond.js"
    click HardhatConfig "https://github.com/mudgen/diamond-1-hardhat/blob/main/hardhat.config.js"
    click Test1 "https://github.com/mudgen/diamond-1-hardhat/blob/main/test/diamondTest.js"
    click Test2 "https://github.com/mudgen/diamond-1-hardhat/blob/main/test/cacheBugTest.js"
    click Package "https://github.com/mudgen/diamond-1-hardhat/blob/main/package.json"
```
