# ownprotocol/own-smart-contracts architecture

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

- Interactive diagram: https://gitdiagram.com/ownprotocol/own-smart-contracts
- Repository: https://github.com/ownprotocol/own-smart-contracts

## Overview

##### Project Type and Purpose
- This is a blockchain dApp—a full‐stack project combining:
– A Next.js (T3 stack) client (“client/”)
– A Hardhat smart‐contract project (“contracts/”)
– Serverless API routes for signing presale arguments and user auth
- Purpose:
– Let users buy a token in a presale, stake it, and earn rewards on a testnet (Sepolia).

##### File Structure for Components
Frontend (“client/”)
- Framework: Next.js with React (app directory, pages, components)
- State & Data Fetch: React Query (providers, hooks), custom hooks (use-staking, use-presale, etc.)
- API layer: Next.js Route Handlers under src/app/api for:
– /api/contracts/get-signed-presale-args
– /api/users/auth/get-user
- UI Components: organized by feature (home, presale, staking, tokenomics, user-stake)
- Config & Providers: thirdweb client, chain‐connect provider, network‐switch, query‐client

Smart Contracts (“contracts/”)
- Hardhat project with Solidity contracts for OWN token, presale, staking, veOwn
- Deployment scripts (deploy/001_… to 005_…)
- Tests by feature (presale, stake, veOwn)
- Config: .env, hardhat.config.ts, .openzeppelin for network deployments

Shared / External
- Blockchain network: Sepolia testnet RPC
- Third-party services:
– thirdweb (contract abstraction + wallet connection)
– Wert (fiat-on-ramp)
– Sablier (planned vesting)

##### README for Architecture Clues
- Confirms two subprojects (client, contracts) and tech stacks (Bun, Vercel/pnpm)
- Notes on staking logic to be completed and vesting via Sablier

##### Main Components & Interactions to Illustrate
A. Client (Next.js App)
– Layers: React UI → Hooks/Services → Next.js API Routes → RPC Provider
– Providers: ThirdwebClientProvider, QueryClientProvider, ChainConnectProvider

B. API Layer (Serverless Functions)
– get-signed-presale-args: signs presale params for secure client purchase
– get-user: authenticates/returns user info (via JWT or session)

C. Blockchain Network
– RPC Endpoint (e.g., Infura/Alchemy/Sepolia node)
– Deployed Smart Contracts: OwnToken, Presale, Stake, VeOwn

D. Smart-Contract Project
– Hardhat scripts for deployment & verification
– Testing suite for each contract

E. External Services
– Wert: Fiat on-ramp integration
– Sablier: Vesting (future)

##### Architectural Patterns & Design Principles
- Separation of Concerns:
– UI vs. Data Fetch vs. API vs. Smart Contracts
- Provider Pattern (React Context)
- Hooks for business logic encapsulation
- Serverless functions as thin signing/auth layers
- EVM blockchain as stateful backend

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User
    User["User Browser"]:::external

    %% Client Layer
    subgraph "Client: Next.js (Bun)" 
        direction TB
        subgraph "Pages / App" 
            direction TB
            Layout["layout.tsx"] 
            Page["page.tsx"]
            Loading["loading.tsx"]
            NotFound["not-found.tsx"]
            Positions["positions/page.tsx"]
            PresalePage["presale/page.tsx"]
            StakingPage["staking/page.tsx"]
        end
        subgraph "Components" 
            direction TB
            UIComponents["client/src/components"] 
        end
        subgraph "Hooks / Services"
            direction TB
            HooksDir["client/src/hooks"]
            LoginAction["login.ts"]
        end
        subgraph "Providers & Config"
            direction TB
            ThirdwebProvider["thirdweb-client-provider.tsx"]
            QueryProvider["query-client-provider.tsx"]
            ChainProvider["chain-connect-provider.tsx"]
            NetworkSwitch["network-switch-provider.tsx"]
            ContractsConfig["contracts.ts"]
            WertConfig["wert-config.ts"]
            NextConfig["next.config.js"]
            TailwindConfig["tailwind.config.ts"]
        end
        subgraph "API Routes (Serverless)"
            direction TB
            PresaleAPI["get-signed-presale-args/route.ts"]
            AuthAPI["get-user/route.ts"]
        end
    end

    %% Smart-Contracts Layer
    subgraph "Smart-Contracts: Hardhat (Solidity)"
        direction TB
        subgraph "Contracts"
            direction TB
            ImplDir["contracts/implementations"]
            Interfaces["contracts/interfaces"]
            Mocks["contracts/mocks"]
        end
        subgraph "Deployment Scripts"
            direction TB
            Deploy1["001_own.ts"]
            Deploy2["002_veOwn.ts"]
            Deploy3["003_presale.ts"]
            Deploy4["004_stake.ts"]
            Deploy5["005_local_testing.ts"]
        end
        subgraph "Tests"
            direction TB
            OwnTest["own.test.ts"]
            PresaleTests["presale/..."]
            StakeTests["stake/..."]
            VeOwnTests["veOwn/..."]
            WertTest["wert.test.ts"]
        end
        subgraph "Config & Helpers"
            direction TB
            HardhatConfig["hardhat.config.ts"]
            EnvExample[".env.example"]
            ConstantsDir["constants"]
            HelpersDir["helpers"]
        end
    end

    %% Blockchain Network
    subgraph "Blockchain: Sepolia RPC & Contracts"
        direction TB
        RPCNode["Sepolia RPC Node"]:::blockchain
        OnChainContracts["Smart Contract State"]:::database
    end

    %% External Services
    subgraph "External Services"
        direction TB
        Wert["Wert (Fiat on-ramp)"]:::external
        Sablier["Sablier (Vesting)"]:::external
    end

    %% Connections
    User -->|"HTTP (React)"| Layout
    Layout -->|"renders UI"| UIComponents
    UIComponents --> HooksDir
    HooksDir -->|"calls"| PresaleAPI
    HooksDir -->|"calls"| AuthAPI
    UIComponents -->|"direct contract interactions"| ThirdwebProvider
    ThirdwebProvider -->|"JSON-RPC"| RPCNode
    RPCNode --> OnChainContracts
    PresaleAPI -->|"signing logic"| HooksDir

    %% Click Events
    click Layout "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/layout.tsx"
    click Page "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/page.tsx"
    click Loading "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/loading.tsx"
    click NotFound "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/not-found.tsx"
    click Positions "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/positions/page.tsx"
    click PresalePage "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/presale/page.tsx"
    click StakingPage "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/staking/page.tsx"
    click UIComponents "https://github.com/ownprotocol/own-smart-contracts/tree/main/client/src/components"
    click HooksDir "https://github.com/ownprotocol/own-smart-contracts/tree/main/client/src/hooks"
    click LoginAction "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/actions/login.ts"
    click PresaleAPI "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/api/contracts/get-signed-presale-args/route.ts"
    click AuthAPI "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/app/api/users/auth/get-user/route.ts"
    click ThirdwebProvider "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/providers/thirdweb-client-provider.tsx"
    click QueryProvider "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/providers/query-client-provider.tsx"
    click ChainProvider "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/providers/chain-connect-provider.tsx"
    click NetworkSwitch "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/providers/network-switch-provider.tsx"
    click ContractsConfig "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/config/contracts.ts"
    click WertConfig "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/src/config/wert-config.ts"
    click NextConfig "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/next.config.js"
    click TailwindConfig "https://github.com/ownprotocol/own-smart-contracts/blob/main/client/tailwind.config.ts"
    click ImplDir "https://github.com/ownprotocol/own-smart-contracts/tree/main/contracts/contracts/implementations"
    click Interfaces "https://github.com/ownprotocol/own-smart-contracts/tree/main/contracts/contracts/interfaces"
    click Mocks "https://github.com/ownprotocol/own-smart-contracts/tree/main/contracts/contracts/mocks"
    click Deploy1 "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/deploy/001_own.ts"
    click Deploy2 "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/deploy/002_veOwn.ts"
    click Deploy3 "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/deploy/003_presale.ts"
    click Deploy4 "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/deploy/004_stake.ts"
    click Deploy5 "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/deploy/005_local_testing.ts"
    click OwnTest "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/test/own.test.ts"
    click WertTest "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/test/wert.test.ts"
    click HardhatConfig "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/hardhat.config.ts"
    click EnvExample "https://github.com/ownprotocol/own-smart-contracts/blob/main/contracts/.env.example"
    click ConstantsDir "https://github.com/ownprotocol/own-smart-contracts/tree/main/contracts/constants"
    click HelpersDir "https://github.com/ownprotocol/own-smart-contracts/tree/main/contracts/helpers"

    %% Styles
    classDef external fill:#cccccc,stroke:#888,stroke-width:1px
    classDef blockchain fill:#d4edda,stroke:#2e7d32,stroke-width:1px
    classDef database fill:#c8e6c9,stroke:#2e7d32,stroke-width:1px
    class client fill:#bbdefb,stroke:#0d47a1,stroke-width:1px
    class smart-contracts fill:#ffe0b2,stroke:#ef6c00,stroke-width:1px
```
