# eshan-sharma/chainpot architecture

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

- Interactive diagram: https://gitdiagram.com/eshan-sharma/chainpot
- Repository: https://github.com/eshan-sharma/chainpot

## Overview

##### Project type and purpose
- This is a full-stack decentralized application (“dApp”) combining a modern web front end (Next.js + TailwindCSS), a TypeScript/Node.js back end (Next.js API routes + tRPC + Prisma + PostgreSQL), and a suite of Solidity smart contracts (built with Foundry).
- Purpose: Users deposit into escrow, earn yield via Compound v3, and compete in an auction or lottery (Pyth Oracle randomness) to claim the yield. Access is invite-only and governed by on-chain modules.

##### File structure
- Top-level folders:
- prisma/ (database schema for InviteCode, User, Pool, etc.)
- smart-contract/ (Foundry project, modules: Escrow, Auction Engine, Lottery Engine, Member Account Manager, Compound Integrator)
- src/ (Next.js application)
– src/app (React pages, Layout, API routes for NextAuth & tRPC)
– src/server (tRPC routers, auth config, Prisma client)
– src/trpc (client hooks)
- Config/build files: next.config.js, tsconfig.json, start-database.sh
- Smart-contract sub-folders for tests, scripts, and forge configuration.

##### README for additional insights
- Features list clarifying on-chain modules and off-chain services
- Tech-stack table highlights key frameworks (Foundry, Compound v3, Pyth Entropy, NextAuth/Reclaim, Wagmi/RainbowKit).

a. Main components:
- User & Wallet (RainbowKit + Wagmi)
- Frontend (Next.js + TailwindCSS)
- Authentication layer (NextAuth + Reclaim Protocol)
- API & Business logic (tRPC routers in Next.js API routes)
- Database (Prisma → PostgreSQL)
- Smart Contracts (Escrow, Member Manager, Auction Engine, Lottery Engine, Compound Integrator)
- External services (Compound v3, Pyth Entropy Oracle)
- Blockchain network (Ethereum or testnet)

b. Relationships & interactions:
1. User connects wallet via RainbowKit/Wagmi → interacts directly with smart contracts for deposits/withdrawals.
2. Frontend calls NextAuth endpoints to sign in/up off-chain.
3. Frontend uses tRPC client to fetch pool/user data and invite codes from the backend.
4. Backend (tRPC routers) reads/writes via Prisma to PostgreSQL.
5. Backend monitors contract events (e.g., cycle end) and may trigger on-chain transactions (e.g., reset, award yield).
6. Smart contracts call out to Compound v3 for yield and to Pyth Oracle for randomness.

c. Architectural patterns & design principles:
- Separation of concerns: UI vs. backend API vs. on-chain
- Modular smart contracts: one contract per domain (Escrow, Auction, Lottery, Member)
- Event-driven flow: backend reacts to blockchain events
- Type-safe client-server communication via tRPC

d. Key technologies/frameworks:
- Frontend: Next.js, TailwindCSS, Wagmi, RainbowKit
- Auth: NextAuth, Reclaim Protocol
- API: Next.js API routes + tRPC
- DB: Prisma ORM + PostgreSQL
- Smart contracts: Solidity (Foundry)
- Yield: Compound v3
- Randomness: Pyth Entropy

##### Elements
- Clear labels for each component (e.g., “NextAuth,” “tRPC Router: post.ts,” “Prisma Client,” “Escrow Contract”)
- Gray hexagons for smart contracts

##### Detail without over-splitting
- Prisma model’s role (InviteCode, User, Pool) as part of the DB layer.
- How Foundry tests/scripts fit into the smart-contract CI pipeline (optional annotation).
- Event listeners in the server that react to on-chain events.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Off-chain Frontend
    subgraph "Off-chain Frontend"
        direction TB
        UI["Next.js UI"]:::frontend
        Layout["Layout.tsx"]:::frontend
        Page["Page.tsx"]:::frontend
        PostComp["post.tsx"]:::frontend
        Wallet["RainbowKit/Wagmi"]:::frontend
        UI --> Layout
        UI --> Page
        UI --> PostComp
        Layout --> Wallet
    end

    %% Server/API Layer
    subgraph "Server/API Layer"
        direction TB
        NextAuthRoute["NextAuth API Route"]:::server
        AuthConfig["auth/config.ts"]:::server
        AuthIndex["auth/index.ts"]:::server
        TRPCRoute["tRPC API Route"]:::server
        TRPCSetup["trpc.ts"]:::server
        TRPCRouter["post.ts Router"]:::server
        TRPCRoot["root.ts"]:::server
        DBClient["Prisma Client (db.ts)"]:::server
        NextAuthRoute --> AuthConfig
        NextAuthRoute --> AuthIndex
        UI --> NextAuthRoute
        UI --> TRPCRoute
        TRPCRoute --> TRPCSetup
        TRPCRoute --> TRPCRouter
        TRPCSetup --> TRPCRoot
        TRPCRouter --> TRPCRoot
        TRPCRoot --> DBClient
    end

    %% Database Layer
    subgraph "Database" 
        direction TB
        Schema["schema.prisma"]:::db
        InviteCodeModel["inviteCode.prisma"]:::db
        UserModel["user.prisma"]:::db
        PoolModel["pool.prisma"]:::db
        PoolMemberModel["poolMember.prisma"]:::db
        AuctionRoundModel["auctionRound.prisma"]:::db
        BidModel["bid.prisma"]:::db
        LotteryRoundModel["lotterRound.prisma"]:::db
        WinnerModel["winner.prisma"]:::db
        DBClient --> Schema
        Schema --> InviteCodeModel
        Schema --> UserModel
        Schema --> PoolModel
        Schema --> PoolMemberModel
        Schema --> AuctionRoundModel
        Schema --> BidModel
        Schema --> LotteryRoundModel
        Schema --> WinnerModel
    end

    %% Blockchain/Smart Contracts
    subgraph "Blockchain / Smart Contracts"
        direction TB
        ContractsSrc["Contracts (src/)"]:::contracts
        ForgeConfig["foundry.toml"]:::contracts
        ForgeStdLib["forge-std"]:::contracts
        Scripts["script/"]:::contracts
        Tests["test/"]:::contracts
        Compose["Member, Escrow, Auction, Lottery, Compound Integrator"]:::contracts
        ContractsSrc --> Compose
        ContractsSrc --> ForgeConfig
        ContractsSrc --> ForgeStdLib
        ContractsSrc --> Scripts
        ContractsSrc --> Tests
        Wallet --> ContractsSrc
        TRPCRoot --> Tests
    end

    %% External Services
    subgraph "External Services"
        direction TB
        Compound["Compound v3"]:::external
        Pyth["Pyth Entropy Oracle"]:::external
        ContractsSrc --> Compound
        ContractsSrc --> Pyth
    end

    %% Event-driven interactions
    ContractsSrc -->|"Events"| TRPCRoot
    TRPCRoot -->|"Trigger tx"| ContractsSrc

    %% User interactions
    User["User"]:::actor
    User --> UI
    User --> Wallet

    %% Connections
    DBClient -->|"Queries"| DBClient
    Portfolio["PostgreSQL"]:::db
    DBClient --> Portfolio

    %% Click Events
    click Layout "https://github.com/eshan-sharma/chainpot/blob/main/src/app/layout.tsx"
    click Page "https://github.com/eshan-sharma/chainpot/blob/main/src/app/page.tsx"
    click PostComp "https://github.com/eshan-sharma/chainpot/blob/main/src/app/_components/post.tsx"
    click Wallet "https://github.com/eshan-sharma/chainpot/blob/main/src/app/layout.tsx"
    click NextAuthRoute "https://github.com/eshan-sharma/chainpot/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click AuthConfig "https://github.com/eshan-sharma/chainpot/blob/main/src/server/auth/config.ts"
    click AuthIndex "https://github.com/eshan-sharma/chainpot/blob/main/src/server/auth/index.ts"
    click TRPCRoute "https://github.com/eshan-sharma/chainpot/blob/main/src/app/api/trpc/[trpc]/route.ts"
    click TRPCSetup "https://github.com/eshan-sharma/chainpot/blob/main/src/server/api/trpc.ts"
    click TRPCRouter "https://github.com/eshan-sharma/chainpot/blob/main/src/server/api/routers/post.ts"
    click TRPCRoot "https://github.com/eshan-sharma/chainpot/blob/main/src/server/api/root.ts"
    click DBClient "https://github.com/eshan-sharma/chainpot/blob/main/src/server/db.ts"
    click Schema "https://github.com/eshan-sharma/chainpot/blob/main/prisma/schema.prisma"
    click InviteCodeModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/inviteCode.prisma"
    click UserModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/user.prisma"
    click PoolModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/pool.prisma"
    click PoolMemberModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/poolMember.prisma"
    click AuctionRoundModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/auctionRound.prisma"
    click BidModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/bid.prisma"
    click LotteryRoundModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/lotterRound.prisma"
    click WinnerModel "https://github.com/eshan-sharma/chainpot/blob/main/prisma/winner.prisma"
    click ContractsSrc "https://github.com/eshan-sharma/chainpot/tree/main/smart-contract/src"
    click ForgeConfig "https://github.com/eshan-sharma/chainpot/blob/main/smart-contract/foundry.toml"
    click ForgeStdLib "https://github.com/eshan-sharma/chainpot/tree/main/smart-contract/lib/forge-std"
    click Scripts "https://github.com/eshan-sharma/chainpot/tree/main/smart-contract/script"
    click Tests "https://github.com/eshan-sharma/chainpot/tree/main/smart-contract/test"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,stroke-width:1px
    classDef server fill:#D6F5D6,stroke:#1a8f13,stroke-width:1px
    classDef db fill:#FFE8A1,stroke:#c48700,stroke-width:1px,stroke-dasharray: 2 2
    classDef contracts fill:#E0E0E0,stroke:#636363,stroke-width:1px
    classDef external fill:#F5D6E0,stroke:#c12f5e,stroke-width:1px,stroke-dasharray: 2 2
    classDef actor fill:#FFFFFF,stroke:#000000,stroke-width:1px,stroke-dasharray: 2 2
```
