# **WEEK 12 - Diamond Contracts: A Modular Approach to Upgradeable Smart Contracts**
This week, We learned alot around Diamond Contracts, an innovative design pattern for creating upgradeable and modular smart contracts in Solidity. Diamond contracts solve critical limitations in Ethereum, such as the 24KB contract size limit, while enabling seamless upgrades without storage migration or address changes.
## **How Diamond Contracts Work**
A diamond contract follows EIP-2535 and consists of three main components:
**Diamond (Proxy Contract)**
The main contract that acts as the entry point.
Stores a mapping of function selectors to facet addresses (like a router).
Delegates function calls to the appropriate facet.
**Facets (Modular Contracts)**
Smaller, specialized contracts that implement specific logic (e.g., UserFacet, PaymentFacet).
Each facet contains related functions but shares the same storage.
**Storage (Centralized Data Structure)**
A single storage layout (often using structs in a designated storage slot) ensures data consistency across upgrades.
Diamond contracts offer a powerful solution for building flexible, maintainable smart contracts. By separating logic into facets while maintaining a single storage layer, developers can iterate efficiently without compromising security or user experience.
One very important point we were made to understand is the fact that communciation between a daimond and its facet happens through **delegate calls**.
Delegate calls let a contract borrow another contract’s logic but use its own storage. In diamonds, the proxy uses delegatecall to forward functions to facets, ensuring upgrades don’t disrupt storage. This keeps data consistent while allowing modular code changes.
The folder structuring of a daimond can be a bit complex, heres what it looks like bellow.

Let me explain the basic architecture,
ased on the folder structure i attached above here’s a breakdown of what each subfolder typically holds in a Diamond contract architecture (focus on the contracts folder)
**Facets (contracts/facets/):** This folder contains Solidity files that define different facets (or modules) of the Diamond contract. Each facet is a separate smart contract that implements specific functions, allowing modular upgrades without replacing the entire contract.
**Interfaces (contracts/interfaces/):** Holds interface definitions for smart contracts, ensuring that different parts of the Diamond architecture can interact predictably.
**Libraries (contracts/libraries/)**: Contains reusable Solidity libraries that help with common operations in the Diamond contract.
**Main Diamond Contract (contracts/Diamond.sol):** The central contract that implements the Diamond Standard (EIP-2535). It delegates function calls to the appropriate facet based on a function selector lookup.
**Upgrade Initialization (contracts/upgradeInitializer.sol):** A special contract that handles initialization when upgrading the Diamond contract. Ensures storage structures are correctly set when a new facet is added.
The full implementation is here on my github, check it out: https://github.com/olathedev/Foundry-Hardhat-Diamonds