# ivelinweb/jaga architecture

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

- Interactive diagram: https://gitdiagram.com/ivelinweb/jaga
- Repository: https://github.com/ivelinweb/jaga

## Overview

##### Project type and purpose
- This is a full-stack decentralized insurance dApp monorepo:
- Frontend: Next.js 14 app for user interactions (buy coverage, stake, file claims).
- Indexer: Ponder-powered GraphQL API to listen for on-chain events and serve data.
- Smart-contracts: Foundry project defining InsuranceManager, JagaStake, DAOGovernance, ClaimManager, MorphoReinvest.

##### File structure
- Top-level folders:
- frontend/: Next.js 14, TypeScript, React hooks, viem/wagmi clients, Apollo GraphQL.
- indexer/: Ponder config, event listeners, generated GraphQL schema, Hono HTTP server.
- smart-contracts/: Solidity contracts, tests, scripts, forge config.
- Key patterns:
- Frontend “src/app” follows Next.js routing & Server Actions.
- “hooks” call JSON-RPC for transactions; “components” render UI.
- Indexer listens to emitted events and populates on-chain tables in a DB.
- Smart-contracts emit events for indexing, and implement business logic on Hedera EVM.
- Config & deployment:
- frontend/next.config.js, Tailwind, tsconfig.
- indexer/ponder.config.ts, .env for DATABASE_SCHEMA.
- smart-contracts/foundry.toml, remappings, scripts for deployment.

##### README for additional insights
1) Staking & indexing
2) Premium payment & revenue split
3) Claims governance & payout
- Tech stack callouts: Next.js, Ponder, Foundry, Hedera EVM.

##### Detailed component breakdown (as many as possible)
- Under Frontend:
- UI layer: pages, components, theme-provider.
- Hooks: useStake, useInsuranceManager, useDAOGovernance, useChat.
- GraphQL client: Apollo.
- MCP (Jagabot): ChatInterface, MCP client code.
- Under Indexer:
- Ponder event listeners for each contract.
- On-chain tables: stakes, reward_claims, policies, claims.
- GraphQL / SQL endpoints.
- Under Hedera EVM:
- InsuranceManager, JagaStake, JagaToken, DAOGovernance, ClaimManager, MorphoReinvest.
- External tokens: USDC, Morpho Vault.
- Database:
- Schema “app” managed in indexer/.env.
- Tables for each event type.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layers
    subgraph "Frontend Layer" 
        style FrontendLayer fill:#D0E8FF,stroke:#3399FF
        Browser["Browser + Wallet"]:::client
        FrontendApp["Next.js Frontend"]:::frontend
        Browser -->|"UI Interactions & tx signatures"| FrontendApp

        subgraph "Frontend Application"
            UIComponents["UI Components"]:::frontend
            CampaignModule["Campaign Module"]:::frontend
            CoverageModule["Coverage Module"]:::frontend
            EarnModule["Earn (Staking) Module"]:::frontend
            JagabotModule["Jagabot (MCP Chat) Module"]:::frontend
            DAOModule["DAO Governance Module"]:::frontend
            VaultModule["Vault Module"]:::frontend
            DashboardPage["Dashboard Page"]:::frontend
            Layout["Application Layout"]:::frontend
            ThemeUI["Theme & UI Library"]:::frontend
            Hooks["React Hooks"]:::frontend
            GraphQLClient["Apollo GraphQL Client"]:::frontend

            FrontendApp --> UIComponents
            FrontendApp --> CampaignModule
            FrontendApp --> CoverageModule
            FrontendApp --> EarnModule
            FrontendApp --> JagabotModule
            FrontendApp --> DAOModule
            FrontendApp --> VaultModule
            FrontendApp --> DashboardPage
            FrontendApp --> Layout
            FrontendApp --> ThemeUI
            FrontendApp --> Hooks
            FrontendApp --> GraphQLClient
        end

        FrontendApp -->|"JSON-RPC Calls"| HederaNode
        FrontendApp -->|"GraphQL Queries"| IndexerAPI
    end

    subgraph "Indexer Layer"
        style IndexerLayer fill:#D0E8FF,stroke:#3399FF
        PonderConfig["Ponder Config"]:::indexer
        SchemaDef["Ponder Schema Definitions"]:::indexer
        Listeners["Event Listeners"]:::indexer
        GraphQLServer["GraphQL API Server"]:::indexer

        PonderConfig --> Listeners
        SchemaDef --> Listeners
        Listeners -->|"-- events -->"| DB
        Listeners --> GraphQLServer
        GraphQLServer -->|HTTP/GraphQL| FrontendApp

        IndexerAPI["Indexer API"]:::indexer
        GraphQLServer --> IndexerAPI
    end

    DB["Database (Postgres)"]:::database

    subgraph "On-Chain Layer"
        style OnChainLayer fill:#D0FFCC,stroke:#33AA33
        HederaNode["Hedera EVM Node"]:::onchain
        subgraph Contracts
            InsuranceManager["InsuranceManager"]:::contract
            JagaStake["JagaStake"]:::contract
            DAOGovernance["DAOGovernance"]:::contract
            ClaimManager["ClaimManager"]:::contract
            MorphoReinvest["MorphoReinvest"]:::contract
            USDC["USDC Token"]:::contract
            MorphoVault["Morpho Vault"]:::contract
        end
        HederaNode --> Contracts
        Contracts -->|-- emits events -->| Listeners
    end

    %% Click Events - Frontend
    click FrontendApp "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app"
    click UIComponents "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app/components"
    click CampaignModule "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app/campaign"
    click CoverageModule "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app/coverage"
    click EarnModule "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app/earn"
    click JagabotModule "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app/jagabot"
    click DAOModule "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app/jagadao"
    click VaultModule "https://github.com/ivelinweb/jaga/tree/main/frontend/src/app/vault"
    click DashboardPage "https://github.com/ivelinweb/jaga/blob/main/frontend/src/app/dashboard/page.tsx"
    click Layout "https://github.com/ivelinweb/jaga/blob/main/frontend/src/app/layout.tsx"
    click ThemeUI "https://github.com/ivelinweb/jaga/tree/main/frontend/src/components/ui"
    click Hooks "https://github.com/ivelinweb/jaga/tree/main/frontend/src/hooks"
    click GraphQLClient "https://github.com/ivelinweb/jaga/blob/main/frontend/src/lib/graphql.ts"
    click PonderConfig "https://github.com/ivelinweb/jaga/blob/main/indexer/ponder.config.ts"
    click SchemaDef "https://github.com/ivelinweb/jaga/blob/main/indexer/ponder.schema.ts"
    click Listeners "https://github.com/ivelinweb/jaga/blob/main/indexer/src/index.ts"
    click GraphQLServer "https://github.com/ivelinweb/jaga/blob/main/indexer/src/api/index.ts"

    %% Click Events - Smart Contracts
    click Contracts "https://github.com/ivelinweb/jaga/tree/main/smart-contracts/src"
    click SchemaDef "https://github.com/ivelinweb/jaga/blob/main/indexer/ponder.schema.ts"
    click PonderConfig "https://github.com/ivelinweb/jaga/blob/main/indexer/ponder.config.ts"
    click Listeners "https://github.com/ivelinweb/jaga/blob/main/indexer/src/index.ts"
    click GraphQLServer "https://github.com/ivelinweb/jaga/blob/main/indexer/src/api/index.ts"
    click DB "https://github.com/ivelinweb/jaga/blob/main/indexer/.env"

    %% Styles
    classDef client fill:#FFF2CC,stroke:#E6B800
    classDef frontend fill:#CCE5FF,stroke:#3399FF
    classDef indexer fill:#CCE5FF,stroke:#3399FF
    classDef database fill:#FFF5CC,stroke:#FFCC00
    classDef onchain fill:#E6FFCC,stroke:#66CC33
    classDef contract fill:#DFFFD0,stroke:#33AA33
```
