# naizo01/harmonia_protocol architecture

> Architecture diagram of the GitHub repository naizo01/harmonia_protocol, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/naizo01/harmonia_protocol
- Repository: https://github.com/naizo01/harmonia_protocol

## Overview

##### Project Overview and Main Components
- The Harmonia Protocol is a blockchain-based system that intervenes in decentralized exchange (DEX) fee structures. It relies on on-chain smart contracts to adjust fees dynamically, while integrating with external services such as Lit Protocol and Web3Auth for secure social logins (via Discord) and attestation verification.
- The project is organized into three main parts:
a. Smart Contracts (located in the “contracts” directory): These Solidity contracts are responsible for handling dynamic fee adjustments, attestations, and interactions with oracles (Pyth) as well as Uniswap V4 hooks.
b. Frontend (in the “nextjs” directory): A Next.js application that provides the user interface for interacting with the protocol. It includes components, hooks, and utility functions to integrate with blockchain features, authentication (Web3Auth & Lit Protocol), and token swap components.
c. Backend / Sign Module (in the “sign” folder): An API server (built with Express) in charge of generating and verifying attestations (called “Sign Attestation”) along with helper libraries and scripts. It handles encryption and the connection between Discord membership and on-chain records.

##### Analyzing the File Structure
- The “contracts” directory: Contains multiple deployment scripts, environment examples, a structured folder for deployments (with subdirectories for different networks and components like ATTESTER, DYNAMICFEE, etc.), integration with Forge, and external libraries (like OpenZeppelin). This setup indicates a thorough segregation of contracts and environment-specific deployments.
- The “nextjs” directory: Holds the full front-end stack, including components (UI elements, dialogs, hooks, contexts for handling Lit and attestation logic), configuration files, and connection utilities. It is structured to incorporate blockchain data, authentication via external protocols, and UI/UX features.
- The “sign” directory: Hosts an Express server (with middleware, routes, and tests) and example scripts revealing its use for managing attestations. It also includes cryptographic utility libraries.

##### Relationships and Data Flow
- The End-User interacts with the Next.js frontend, which is responsible for:
- Initiating blockchain transactions (like token swaps or fee adjustments) via Uniswap V4 hooks.
- Handling authentication through Web3Auth and Lit Protocol (which abstracts Discord login and secure, encrypted attestations).
- The Next.js client communicates with the backend “sign” service when it needs to create or validate attestations. This backend uses its crypto utilities and Express routes to generate signatures or fetch schema/attestation data.
- On the blockchain, smart contracts deployed via the “contracts” directory receive transactions (for dynamic fee adjustments, attestation verifications, etc.) and interact with external oracles (like Pyth) for price feeds.
- External Services:
- Discord: Used for community membership validation.
- Lit Protocol and Web3Auth: Facilitate secure social logins and encryption.
- Uniswap V4 & Pyth: Provide core DeFi functions and real-time price information.

##### Summary of Inclusions
- Clear labels for each component: “Smart Contracts (with submodules),” “Frontend (Next.js),” and “Backend (Express Sign Service)”.
- From the user (via Frontend) to authentication services (Web3Auth and Lit Protocol).
- From Frontend to Backend for attestation services.
- From both Frontend/Backend to Smart Contracts for DEX transactions.
- From Smart Contracts to external services like Pyth (price feed) and Uniswap V4.
- Annotation with relevant protocols (Lit Protocol, Web3Auth, Uniswap V4, Pyth, Discord) and deployment details (network-specific deployments under “contracts/deployments”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    subgraph "User Layer"
        EU("End User"):::user
    end

    %% Frontend Layer
    subgraph "Frontend (Next.js)"
        FE("Next.js Application"):::frontend
        FEComponents("UI Components"):::frontend
        FEHooks("Hooks & Utilities"):::frontend
    end

    %% Backend Layer
    subgraph "Backend API (Express Sign Service)"
        BE("Express Server"):::backend
        BERoutes("Attestation Routes & Crypto Utilities"):::backend
    end

    %% Smart Contracts Layer
    subgraph "Smart Contracts"
        SC("Core Contracts"):::smart
        Attester("ATTESTER Contract"):::smart
        DynamicFee("DYNAMICFEE Contract"):::smart
        Pyth("PYTH Oracle Integration"):::smart
        Whitelist("WHITELIST Contract"):::smart
    end

    %% External Services
    subgraph "External Services"
        Lit("Lit Protocol"):::external
        Web3Auth("Web3Auth"):::external
        Discord("Discord"):::external
        Uniswap("Uniswap V4 Hooks"):::external
    end

    %% Relationships & Data Flow
    EU -->|"interacts"| FE
    FE -->|"renders"| FEComponents
    FE -->|"executes"| FEHooks
    FE -->|"attestation_request"| BE
    FE -->|"blockchain_txn"| SC
    BE -->|"attestation_validation"| SC

    %% Internal Smart Contracts Relationships
    SC -->|"manages"| Attester
    SC -->|"handles"| DynamicFee
    SC -->|"integrates_with"| Pyth
    SC -->|"utilizes"| Whitelist
    DynamicFee -->|"executes"| Uniswap
    Attester -->|"verifies"| Pyth

    %% External Integrations for Authentication
    FE -->|"loginToken"| Lit
    FE -->|"loginToken"| Web3Auth
    Web3Auth -->|"membership"| Discord

    %% Styles
    classDef user fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef smart fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#721c24,stroke-width:2px;

    %% Click Events for Component Mappings
    click SC "https://github.com/naizo01/harmonia_protocol/tree/main/contracts/"
    click Attester "https://github.com/naizo01/harmonia_protocol/tree/main/contracts/deployments/ATTESTER"
    click DynamicFee "https://github.com/naizo01/harmonia_protocol/tree/main/contracts/deployments/DYNAMICFEE"
    click Pyth "https://github.com/naizo01/harmonia_protocol/tree/main/contracts/deployments/PYTH"
    click Whitelist "https://github.com/naizo01/harmonia_protocol/tree/main/contracts/deployments/WHITELIST"
    click FE "https://github.com/naizo01/harmonia_protocol/tree/main/nextjs/"
    click FEComponents "https://github.com/naizo01/harmonia_protocol/tree/main/nextjs/components/"
    click FEHooks "https://github.com/naizo01/harmonia_protocol/tree/main/nextjs/hooks/"
    click BE "https://github.com/naizo01/harmonia_protocol/tree/main/sign/express/src"
```
