# nik123-py/chainmorph architecture

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

- Interactive diagram: https://gitdiagram.com/nik123-py/chainmorph
- Repository: https://github.com/nik123-py/chainmorph

## Overview

##### Main Components
- Frontend: Built using Next.js with React, Tailwind CSS, and several UI components and hooks. Look at the “app” directory, “components”, “hooks”, “providers”, and related assets.
- Backend: An Express.js API located in the “backend” directory, structured with controllers, routes, models, middleware, and validations.
- Database: MongoDB (accessed via Mongoose) that handles smart contract data, user information, security issues, optimizations, and notifications.
- External Services:
- Blockchain integration via Ethereum wallet authentication (using Ethers.js, Web3.js).
- API connections to Infura (for blockchain interactions), Etherscan (for contract verification), and OpenAI (for AI-powered analysis).
- Real-time updates using Socket.io (available on both backend and frontend).

##### Out Interactions and Relationships
- The Frontend:
- Serves as a client interface for users. It has pages for contracts, security, optimizations, and settings.
- It invokes API endpoints from the backend for operations such as authentication, smart contract monitoring, retrieving analyses, and notifications.
- It interacts with external blockchain providers for Ethereum wallet authentication and contract interactions.
- The Backend:
- Exposes RESTful routes (documented in the “backend/src/routes” folder) that are consumed by the frontend.
- Utilizes controllers (in “backend/src/controllers”) to process incoming requests and respond appropriately.
- Uses middleware for authentication (JWT), error handling, and data validations.
- Communicates with the MongoDB database via Mongoose models (listed in “backend/src/models”).
- External Integration:
- REST API calls and Socket.io enable real-time communication.
- Blockchain data is fetched and verified using Infura, Etherscan, and wallet providers.
- Optional AI functionalities are integrated through OpenAI’s API to provide optimization suggestions.

##### Architectural Patterns and Technologies
- MVC Pattern: Used in the backend where the controllers, models, and routes are well-organized.
- Component-Based Design: Evident in the React frontend with a clear separation of UI elements, hooks, and providers.
- Real-time Communication: Leveraging Socket.io for real-time notifications/updates between client and server.
- Security and Authentication: Using JWT and Ethereum wallet authentication.
- API-Driven Integration: Strong external API usage (Infura, Etherscan, OpenAI).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    User["End User"]:::external

    %% Frontend Layer
    subgraph "Frontend"
        direction TB
        %% Next.js Pages
        pageContracts["Contracts Page"]:::frontend
        pageSecurity["Security Page"]:::frontend
        pageOptimizations["Optimizations Page"]:::frontend
        pageSettings["Settings Page"]:::frontend

        %% UI Components
        UIComponents["UI Components"]:::frontend
        UIComponentsUI["UI Components (UI)"]:::frontend

        %% React Hooks
        hookAuth["use-auth.tsx"]:::frontend
        hookMobile["use-mobile.tsx"]:::frontend
        hookToast["use-toast.ts"]:::frontend

        %% Providers
        provAuth["Auth Provider"]:::frontend
        provTheme["Theme Provider"]:::frontend
        provWeb3["Web3 Provider"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend"
        direction TB
        %% Controllers
        subgraph "Controllers"
            direction TB
            ctrlAuth["Auth Controller"]:::backend
            ctrlContract["Contract Controller"]:::backend
            ctrlNotification["Notification Controller"]:::backend
            ctrlOptimization["Optimization Controller"]:::backend
            ctrlSecurity["Security Controller"]:::backend
            ctrlUser["User Controller"]:::backend
        end

        %% Routes
        subgraph "Routes"
            direction TB
            routeAuth["Auth Routes"]:::backend
            routeContract["Contract Routes"]:::backend
            routeNotification["Notification Routes"]:::backend
            routeOptimization["Optimization Routes"]:::backend
            routeSecurity["Security Routes"]:::backend
            routeUser["User Routes"]:::backend
        end

        %% Middleware
        subgraph "Middleware"
            direction TB
            mwAuth["Auth Middleware"]:::backend
            mwError["Error Middleware"]:::backend
            mwValidation["Validation Middleware"]:::backend
        end

        %% Validations
        subgraph "Validations"
            direction TB
            valAuth["Auth Validation"]:::backend
            valContract["Contract Validation"]:::backend
            valOptimization["Optimization Validation"]:::backend
            valSecurity["Security Validation"]:::backend
            valUser["User Validation"]:::backend
        end

        %% Entry Point
        beIndex["API Entry Point"]:::backend
    end

    %% Database Layer
    dbMongo["MongoDB (Mongoose)"]:::database

    %% External Services
    subgraph "External Services"
        direction TB
        extEthers["Infura & Etherscan"]:::external
        extOpenAI["OpenAI API"]:::external
        extSocket["Socket.io Server"]:::external

        %% Utility Modules
        libApi["lib/api.ts"]:::utility
        libBrowser["lib/browser-utils.ts"]:::utility
        libUtils["lib/utils.ts"]:::utility
    end

    %% Relationships
    User -->|"Interacts"| pageContracts
    User -->|"Interacts"| pageSecurity
    User -->|"Interacts"| pageOptimizations
    User -->|"Interacts"| pageSettings

    %% Frontend Internal Connections
    pageContracts -->|"RendersUI"| UIComponents
    pageSecurity -->|"RendersUI"| UIComponents
    pageOptimizations -->|"RendersUI"| UIComponents
    pageSettings -->|"RendersUI"| UIComponents

    UIComponents -->|"Uses"| hookAuth
    UIComponents -->|"Uses"| hookMobile
    UIComponents -->|"Uses"| hookToast

    UIComponents -->|"WrappedBy"| provAuth
    UIComponents -->|"WrappedBy"| provTheme
    UIComponents -->|"WrappedBy"| provWeb3

    %% Frontend to Backend
    pageContracts -->|"APIRequest/JWTAuth"| routeContract
    pageSecurity  -->|"APIRequest/JWTAuth"| routeSecurity
    pageOptimizations -->|"APIRequest/JWTAuth"| routeOptimization
    pageSettings -->|"APIRequest/JWTAuth"| routeAuth

    %% Backend Internal Connections
    routeAuth -->|"Calls"| ctrlAuth
    routeContract -->|"Calls"| ctrlContract
    routeNotification -->|"Calls"| ctrlNotification
    routeOptimization -->|"Calls"| ctrlOptimization
    routeSecurity -->|"Calls"| ctrlSecurity
    routeUser -->|"Calls"| ctrlUser

    ctrlAuth -->|"Validates"| mwAuth
    ctrlContract -->|"Validates"| mwValidation
    ctrlNotification -->|"Validates"| mwError

    ctrlAuth -->|"Uses"| valAuth
    ctrlContract -->|"Uses"| valContract
    ctrlOptimization -->|"Uses"| valOptimization
    ctrlSecurity -->|"Uses"| valSecurity
    ctrlUser -->|"Uses"| valUser

    %% Backend to Database
    ctrlContract -->|"CRUD"| dbMongo
    ctrlNotification -->|"CRUD"| dbMongo
    ctrlOptimization -->|"CRUD"| dbMongo
    ctrlSecurity -->|"CRUD"| dbMongo
    ctrlUser -->|"CRUD"| dbMongo

    %% Real-time Communication
    pageContracts ---|"RealTime"| extSocket
    pageSecurity ---|"RealTime"| extSocket
    beIndex ---|"RealTime"| extSocket

    %% External Service Integrations
    provWeb3 ---|"WalletAuth"| extEthers
    ctrlContract ---|"BlockchainVerify"| extEthers
    ctrlOptimization ---|"AIPoweredAnalysis"| extOpenAI

    %% Utility Connections
    provWeb3 ---|"Utilizes"| libApi
    UIComponents ---|"Helper"| libBrowser
    ctrlNotification ---|"Helper"| libUtils

    %% Backend Entry Point connection
    beIndex --> routeAuth
    beIndex --> routeContract
    beIndex --> routeNotification
    beIndex --> routeOptimization
    beIndex --> routeSecurity
    beIndex --> routeUser

    %% Click Events for Frontend
    click pageContracts "https://github.com/nik123-py/chainmorph/blob/main/app/contracts/page.tsx"
    click pageSecurity "https://github.com/nik123-py/chainmorph/blob/main/app/security/page.tsx"
    click pageOptimizations "https://github.com/nik123-py/chainmorph/blob/main/app/optimizations/page.tsx"
    click pageSettings "https://github.com/nik123-py/chainmorph/blob/main/app/settings/page.tsx"
    click UIComponents "https://github.com/nik123-py/chainmorph/tree/main/components"
    click UIComponentsUI "https://github.com/nik123-py/chainmorph/tree/main/components/ui"
    click hookAuth "https://github.com/nik123-py/chainmorph/blob/main/hooks/use-auth.tsx"
    click hookMobile "https://github.com/nik123-py/chainmorph/blob/main/hooks/use-mobile.tsx"
    click hookToast "https://github.com/nik123-py/chainmorph/blob/main/hooks/use-toast.ts"
    click provAuth "https://github.com/nik123-py/chainmorph/blob/main/providers/auth-provider.tsx"
    click provTheme "https://github.com/nik123-py/chainmorph/blob/main/providers/theme-provider.tsx"
    click provWeb3 "https://github.com/nik123-py/chainmorph/blob/main/providers/web3-provider.tsx"

    %% Click Events for Backend
    click ctrlAuth "https://github.com/nik123-py/chainmorph/blob/main/backend/src/controllers/auth.controller.js"
    click ctrlContract "https://github.com/nik123-py/chainmorph/blob/main/backend/src/controllers/contract.controller.js"
    click ctrlNotification "https://github.com/nik123-py/chainmorph/blob/main/backend/src/controllers/notification.controller.js"
    click ctrlOptimization "https://github.com/nik123-py/chainmorph/blob/main/backend/src/controllers/optimization.controller.js"
    click ctrlSecurity "https://github.com/nik123-py/chainmorph/blob/main/backend/src/controllers/security.controller.js"
    click ctrlUser "https://github.com/nik123-py/chainmorph/blob/main/backend/src/controllers/user.controller.js"

    click routeAuth "https://github.com/nik123-py/chainmorph/blob/main/backend/src/routes/auth.routes.js"
    click routeContract "https://github.com/nik123-py/chainmorph/blob/main/backend/src/routes/contract.routes.js"
    click routeNotification "https://github.com/nik123-py/chainmorph/blob/main/backend/src/routes/notification.routes.js"
    click routeOptimization "https://github.com/nik123-py/chainmorph/blob/main/backend/src/routes/optimization.routes.js"
    click routeSecurity "https://github.com/nik123-py/chainmorph/blob/main/backend/src/routes/security.routes.js"
    click routeUser "https://github.com/nik123-py/chainmorph/blob/main/backend/src/routes/user.routes.js"

    click mwAuth "https://github.com/nik123-py/chainmorph/blob/main/backend/src/middleware/auth.middleware.js"
    click mwError "https://github.com/nik123-py/chainmorph/blob/main/backend/src/middleware/error.middleware.js"
    click mwValidation "https://github.com/nik123-py/chainmorph/blob/main/backend/src/middleware/validation.middleware.js"

    click valAuth "https://github.com/nik123-py/chainmorph/blob/main/backend/src/validations/auth.validation.js"
    click valContract "https://github.com/nik123-py/chainmorph/blob/main/backend/src/validations/contract.validation.js"
    click valOptimization "https://github.com/nik123-py/chainmorph/blob/main/backend/src/validations/optimization.validation.js"
    click valSecurity "https://github.com/nik123-py/chainmorph/blob/main/backend/src/validations/security.validation.js"
    click valUser "https://github.com/nik123-py/chainmorph/blob/main/backend/src/validations/user.validation.js"

    click beIndex "https://github.com/nik123-py/chainmorph/blob/main/backend/src/index.js"

    %% Click Events for Database Models
    click dbMongo "https://github.com/nik123-py/chainmorph/tree/main/backend/src/models"

    %% Click Events for External Utilities
    click libApi "https://github.com/nik123-py/chainmorph/blob/main/lib/api.ts"
    click libBrowser "https://github.com/nik123-py/chainmorph/blob/main/lib/browser-utils.ts"
    click libUtils "https://github.com/nik123-py/chainmorph/blob/main/lib/utils.ts"

    %% Styles
    classDef frontend fill:#bbdefb,stroke:#1e88e5,stroke-width:2px;
    classDef backend fill:#c8e6c9,stroke:#43a047,stroke-width:2px;
    classDef database fill:#ffe082,stroke:#f9a825,stroke-width:2px;
    classDef external fill:#ffcdd2,stroke:#e53935,stroke-width:2px;
    classDef utility fill:#e1bee7,stroke:#8e24aa,stroke-width:2px;
```
