# xmtp/subgraphs architecture

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

- Interactive diagram: https://gitdiagram.com/xmtp/subgraphs
- Repository: https://github.com/xmtp/subgraphs

## Overview

##### Project Type and Purpose
- Purpose:
- Listen to on-chain events emitted by XMTP and settlement-chain contracts.
- Transform and store indexed data as GraphQL entities.

##### High-Level Architecture Overview
a. settlement-chain-contracts (Base chain)
b. appchain-contracts (XMTP Appchain)
- Shared configuration and tooling at the root (networks.json, .env, package.json).
- Developer workflow: Graph CLI → codegen → build → deploy → Alchemy.

##### Main Components
A. Blockchains
- Base chain (settlement-chain)
- XMTP Appchain
B. Smart Contracts
- Each on-chain contract ABI (in abis/).
- Events emitted at runtime.
C. The Graph Indexing Engine
- AssemblyScript mapping handlers (src/mappings).
- GraphQL schema (schema.graphql).
D. Storage & Entity Layer
- The Graph’s internal entity store (auto-generated).
E. GraphQL API
F. Developer Tooling & Configuration
- Graph CLI (codegen, build).
- networks.json (RPC endpoints, chain IDs).
- .env (Alchemy deploy key, version label).
- package.json scripts (npm run codegen, build, deploy).

##### Relationships & Data Flow
1. Smart contracts emit events on-chain.
2. The Graph Node (or hosted service) reads blocks via RPC URLs (from networks.json).
3. For each event:
- Mapping functions (AssemblyScript) transform events into entities.
- Entities are persisted in The Graph’s store.
4. Clients query the resulting data via GraphQL endpoints managed by Alchemy.
5. Developer triggers codegen/build/deploy using Graph CLI and npm scripts.

##### Architectural Patterns & Design Principles
- Monorepo tooling: shared dependencies and scripts for consistent workflows.
- Event-driven data ingestion and transformation.
- Infrastructure as code: networks.json and environment variables control deployments.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Blockchains
    subgraph "Blockchains"
        direction TB
        BaseChain["Base Chain"]:::blockchain
        Appchain["XMTP Appchain"]:::blockchain
    end

    %% Smart Contracts
    subgraph "Smart Contracts"
        direction TB
        SC_Base["Settlement Contracts"]:::contract
        SC_App["Appchain Contracts"]:::contract
    end

    %% The Graph Node / Hosted Service
    GraphNode(["The Graph Node / Hosted Service"]):::service

    %% Subgraph Pipelines
    subgraph "Settlement-Chain Subgraph" 
        direction TB
        Manifest_Base["subgraph.yaml"]:::indexer
        ABIs_Base["abis/"]:::indexer
        Mappings_Base["src/mappings"]:::indexer
        Schema_Base["schema.graphql"]:::indexer
    end

    subgraph "Appchain Subgraph"
        direction TB
        Manifest_App["subgraph.yaml"]:::indexer
        ABIs_App["abis/"]:::indexer
        Mappings_App["src/mappings"]:::indexer
        Schema_App["schema.graphql"]:::indexer
    end

    %% Entity Store
    Store(["Entity Store (The Graph)"]):::store

    %% GraphQL API Endpoints
    API["GraphQL API (Alchemy)"]:::api

    %% Developer Tools & Config
    subgraph "Developer Tools & Config"
        direction TB
        Networks["networks.json"]:::dev
        Env[".env.example"]:::dev
        Package["package.json"]:::dev
        Lock["package-lock.json"]:::dev
        CI[".github/"]:::dev
        Readme["README.md"]:::dev
        License["LICENSE"]:::dev
        Gitignore[".gitignore"]:::dev
        CLI["Graph CLI"]:::dev
        Scripts["npm scripts"]:::dev
    end

    %% Data Flow
    BaseChain -->|"event logs"| SC_Base
    Appchain -->|"event logs"| SC_App
    SC_Base -->|"RPC calls"| GraphNode
    SC_App -->|"RPC calls"| GraphNode

    GraphNode -->|"uses manifest & abis"| Manifest_Base
    GraphNode -->|"uses manifest & abis"| Manifest_App
    Manifest_Base --> Mappings_Base
    Manifest_App --> Mappings_App
    Mappings_Base --> Store
    Mappings_App --> Store

    Networks -->|"RPC config"| Manifest_Base
    Networks -->|"RPC config"| Manifest_App
    Env -->|"DEPLOY_KEY & VERSION"| Manifest_Base
    Env -->|"DEPLOY_KEY & VERSION"| Manifest_App

    Store -->|"GraphQL responses"| API
    API -->|"GraphQL queries"| Clients[("Clients")] 

    CLI --> Scripts
    Scripts --> Manifest_Base
    Scripts --> Manifest_App
    Package --> Scripts
    Lock --> Package
    CI --> Scripts
    Readme --> CI
    License --> Readme
    Gitignore --> CI

    %% Click Events
    click settlement-chain-contracts/ "https://github.com/xmtp/subgraphs/tree/main/settlement-chain-contracts/"
    click Manifest_Base "https://github.com/xmtp/subgraphs/blob/main/settlement-chain-contracts/subgraph.yaml"
    click Schema_Base "https://github.com/xmtp/subgraphs/blob/main/settlement-chain-contracts/schema.graphql"
    click appchain-contracts/ "https://github.com/xmtp/subgraphs/tree/main/appchain-contracts/"
    click Manifest_App "https://github.com/xmtp/subgraphs/blob/main/appchain-contracts/subgraph.yaml"
    click Schema_App "https://github.com/xmtp/subgraphs/blob/main/appchain-contracts/schema.graphql"
    click Networks "https://github.com/xmtp/subgraphs/blob/main/networks.json"
    click Env "https://github.com/xmtp/subgraphs/blob/main/.env.example"
    click Package "https://github.com/xmtp/subgraphs/blob/main/package.json"
    click Lock "https://github.com/xmtp/subgraphs/blob/main/package-lock.json"
    click CI "https://github.com/xmtp/subgraphs/tree/main/.github/"
    click Readme "https://github.com/xmtp/subgraphs/blob/main/README.md"
    click License "https://github.com/xmtp/subgraphs/tree/main/LICENSE"
    click Gitignore "https://github.com/xmtp/subgraphs/blob/main/.gitignore"

    %% Styles
    classDef blockchain fill:#D0E8FF,stroke:#3399FF,color:#000;
    classDef contract fill:#E0F3FF,stroke:#66B2FF,color:#000;
    classDef service fill:#FFF2CC,stroke:#FFCC00,color:#000;
    classDef indexer fill:#FFF4E5,stroke:#FFCC80,color:#000;
    classDef store fill:#E6FFEA,stroke:#33CC66,color:#000;
    classDef api fill:#FFE6CC,stroke:#FF9933,color:#000;
    classDef dev fill:#F2F2F2,stroke:#CCCCCC,color:#000;
```
