# snapshot-labs/snapshot-hub architecture

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

- Interactive diagram: https://gitdiagram.com/snapshot-labs/snapshot-hub
- Repository: https://github.com/snapshot-labs/snapshot-hub

## Overview

##### Project Type and Purpose
- This is a backend service (“hub”) for the Snapshot network. Its primary responsibilities are to store data in a MySQL database, serve APIs (including GraphQL endpoints) for querying and processing various operations (such as proposals, votes, spaces, etc.), and to forward and counter-sign messages using a stored private key.
- The README confirms that the service exposes its API endpoints (e.g., at /api) and requires configuration for database connection strings (HUB_DATABASE_URL and SEQ_DATABASE_URL) as well as a private key (RELAYER_PK) for signing messages.

##### File Structure Insights
- The top-level files (Dockerfile, docker-compose.yml, etc.) and various configuration files (e.g., .env.example, tsconfig.json, jest.config.ts) indicate that the project is built with Node.js and TypeScript and is containerized.
- The src directory is the heart of the application:
- src/index.ts is the likely entry point.
- The src/api.ts and src/eip4824.ts files suggest HTTP API-related logic and some specialized protocol or key functionality.
- The src/graphql directory is highly detailed and broken down into examples, helpers, and multiple operations. This points to a well-modularized GraphQL API that handles different domains (users, votes, proposals, spaces, etc.).
- The helpers under src/helpers (e.g., mysql, rateLimit, metrics, log) provide utility functions for database connectivity, rate limiting, logging and other cross-cutting concerns.
- The test directory, with end-to-end tests and setup/teardown scripts, reinforces that there’s an emphasis on ensuring the integrity of the system via comprehensive automated testing.

##### Main Components
- API Server:
- Main API entry point (src/index.ts) that initiates the server.
- GraphQL Module:
- Break out the GraphQL service logic into its own component, showing that it aggregates various operations (aliases, follows, leaderboards, proposals, votes, etc.). Point out that these operations represent the business logic for interacting with different data types within the Snapshot ecosystem.
- Database:
- Two distinct database connections: one for the main hub data (HUB_DATABASE_URL) and one for messages or sequencing (SEQ_DATABASE_URL).
- That schema initialization is driven by the SQL script in src/helpers/schema.sql.
- Message Signing Component:
- A component that utilizes the RELAYER_PK (private key) for counter-signing accepted messages, ensuring integrity and authenticity.
- External Communication:
- Since the hub forwards messages to peers on the Snapshot network, include an “external peers” component. This can be depicted as outbound interfaces that handle messaging (and possibly logging or error reporting, hinted by the Sentry release workflow).
- Auxiliary Services:

##### Relationships and Data Flow
- How incoming API requests (HTTP/GraphQL) are received by the API Server.
- The API server interacting with the GraphQL module to process operations.
- Data being read from and written to the MySQL databases.
- Outbound messages being forwarded to external Snapshot network peers.
- The signing process that uses the relayer’s private key before messages are sent out.
- Dependencies between the API layer, the GraphQL layer, and the helper modules (for tasks like logging, metrics, and rate-limiting).

##### Incorporation of Architectural Patterns
- Modular Architecture: Clearly separate concerns by isolating the API layer, GraphQL operations, database access, and cryptographic signing.
- Configuration & Environment Management: Note the use of environment variables (from .env) to configure the databases and key management.
- Testing and Continuous Integration: Include a section or a note indicating that there is an extensive testing suite (located in the test directory) and automated workflows in GitHub for linting, testing, and Docker image publishing.

## Diagram (Mermaid)

```mermaid
graph TD
    Client["Client Request"]:::external
    A["API Server (src/index.ts)"]:::api
    API_Endpoints["API Endpoints (src/api.ts)"]:::api
    GraphQL["GraphQL Module (src/graphql)"]:::service
    MySQL_Helper["MySQL Helper (src/helpers/mysql.ts)"]:::helper
    Schema["Schema Script (src/helpers/schema.sql)"]:::helper
    Hub_DB["MySQL Hub Database"]:::database
    Seq_DB["MySQL Sequence Database"]:::database
    MessageSigner["Message Signing (src/eip4824.ts)"]:::security
    ExternalPeers["External Snapshot Peers"]:::external
    CICD["CI/CD Workflows (.github/workflows)"]:::aux
    Testing["Testing Suite (test)"]:::aux

    Client -->|"sendsRequest"| A
    A -->|"routesTo"| API_Endpoints
    API_Endpoints -->|"invokesOperations"| GraphQL
    GraphQL -->|"queriesWrites"| Hub_DB
    GraphQL -->|"queriesWrites"| Seq_DB
    GraphQL -->|"usesHelper"| MySQL_Helper
    MySQL_Helper -->|"initializesSchema"| Schema
    GraphQL -->|"preparesMessage"| MessageSigner
    MessageSigner -->|"forwardsSigned"| ExternalPeers
    CICD -->|"deploys"| A
    Testing -->|"validates"| A

    click A "https://github.com/snapshot-labs/snapshot-hub/blob/master/src/index.ts"
    click API_Endpoints "https://github.com/snapshot-labs/snapshot-hub/blob/master/src/api.ts"
    click GraphQL "https://github.com/snapshot-labs/snapshot-hub/tree/master/src/graphql"
    click MySQL_Helper "https://github.com/snapshot-labs/snapshot-hub/blob/master/src/helpers/mysql.ts"
    click Schema "https://github.com/snapshot-labs/snapshot-hub/blob/master/src/helpers/schema.sql"
    click MessageSigner "https://github.com/snapshot-labs/snapshot-hub/blob/master/src/eip4824.ts"
    click CICD "https://github.com/snapshot-labs/snapshot-hub/tree/master/.github/workflows"
    click Testing "https://github.com/snapshot-labs/snapshot-hub/tree/master/test"

    classDef api fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef service fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef database fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef security fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef aux fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef helper fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
```
