# ton-studio/ton-etl architecture

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

- Interactive diagram: https://gitdiagram.com/ton-studio/ton-etl
- Repository: https://github.com/ton-studio/ton-etl

## Overview

##### Project Type and Purpose
- The project is an indexing and decoding pipeline for the TON blockchain.
- Its goal is to ingest raw blockchain data (using a TON node and an indexer worker), store the data in a Postgres database, capture changes via Debezium into Kafka topics, process events with specialized parsers, and then export the processed data to an AWS S3-backed Data Lake (using AVRO file formatting).
- The README outlines components such as the TON node/indexer, DB setup, Debezium/Kafka integration, parser pipelines, and data lake exports.

##### File Structure for Architectural Clues
- Top-level directories:
- “database”: Contains configuration (e.g., docker-compose.yml, createdb.sql) for orchestrating the Postgres DB, Debezium, and Kafka.
- “indexer”: Contains docker-compose for running the TON index-worker component.
- “parser”: Extensive parser code modules, Docker configuration, and models for handling multiple event types (e.g., NFTs, jettons, swaps, gas trades, etc.).
- “datalake”: Contains scripts (main.py, generate_schema.py) and Dockerfiles for exporting processed data to an S3 Data Lake using AVRO serialized files.
- Other notable files:
- .github/workflows for CI/CD tests.
- Multiple docker-compose files that highlight the containerized deployment structure.

##### Core Components
a. TON Node + Indexer
- External TON Node (as per official documentation) connected to the TON indexer.
- TON indexer worker (from ton-index-worker) that ingests and indexes blockchain data.
b. Postgres Database
- Stores raw blockchain data (block, transactions, messages, jetton, NFT tables, etc.).
- Includes multiple schemas such as “public” for raw data, “parsed” for processed outputs, and “prices” for market metrics.
c. Change Data Capture with Debezium
- A Debezium connector (configured to capture changes in the Postgres DB) responsible for publishing change events.
d. Kafka Topics
- Kafka acts as the event streaming backbone.
- Change events from the database are sent to Kafka topics.
e. Parser Modules
- A suite of parsers (found in the “parser” directory) that subscribe to specific Kafka topics.
- They transform raw messages into domain-specific events (such as NFT events, jetton events, swaps, etc.) and update the parsed tables.
- They may also support recovery and state rebuilding functionalities.
f. Datalake Exporter
- Listens to specific Kafka topics.
- Exports processed data to an external destination – AWS S3 – using AVRO serialization.
- Relies on schema discovery (using scripts like generate_schema.py and schemas located in the datalake folder).

##### Relationships and Data Flow
- Data flows from the TON Node to the Indexer, which writes data to the Postgres DB.
- Debezium monitors the Postgres DB for changes and publishes the changes into Kafka topics.
- Parsers consume Kafka messages, process and transform the data, and then store the results (either back into the DB or forwarding them to separate services).
- Parallelly, the Datalake Exporter consumes topics from Kafka and sends data to S3.
- Deployment and orchestration are containerized using multiple docker-compose configurations (for database, indexer, parser, and data lake).

##### Architectural Patterns and Technologies
- Microservice-like modularization: Each component (indexer, parser, datalake exporter) is standalone.
- Event-driven architecture: Decouples components via Kafka topics and Debezium’s change data capture.
- Containerization: Each section is built and deployed using Docker (Dockerfiles and multiple docker-compose files).
- Data serialization: AVRO is used for exporting data to the data lake, showing an emphasis on schema management.
- Observability: Monitoring practices (like JMX exporter for Kafka) are part of the architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Ingestion Layer
    subgraph "Data Ingestion Layer"
        tonNode["TON Node"]:::ingestion
        tonIndexer["TON Indexer Worker"]:::ingestion
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        postgresDB["Postgres Database"]:::persistence
    end

    %% Event Streaming Layer
    subgraph "Event Streaming Layer"
        debezium["Debezium Connector"]:::streaming
        kafka["Kafka Topics"]:::streaming
    end

    %% Data Processing Layer
    subgraph "Data Processing Layer"
        parser["Parser Modules"]:::processing
    end

    %% Data Export Layer
    subgraph "Data Export Layer"
        datalake["Datalake Exporter"]:::export
        awsS3["AWS S3 Data Lake"]:::export
    end

    %% Connections
    tonNode -->|"feeds"| tonIndexer
    tonIndexer -->|"writes_to"| postgresDB
    postgresDB -->|"changes_captured_by"| debezium
    debezium -->|"publishes_to"| kafka
    kafka -->|"consumed_by"| parser
    kafka -->|"consumed_by"| datalake
    datalake -->|"exports_to"| awsS3

    %% Click Events
    click tonIndexer "https://github.com/ton-studio/ton-etl/tree/main/indexer/ton-index-worker"
    click postgresDB "https://github.com/ton-studio/ton-etl/blob/main/database/createdb.sql"
    click debezium "https://github.com/ton-studio/ton-etl/blob/main/database/docker-compose.yml"
    click kafka "https://github.com/ton-studio/ton-etl/blob/main/datalake/topics.py"
    click parser "https://github.com/ton-studio/ton-etl/tree/main/parser/"
    click datalake "https://github.com/ton-studio/ton-etl/blob/main/datalake/main.py"

    %% Styles
    classDef ingestion fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef persistence fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef streaming fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef processing fill:#DDA0DD,stroke:#000,stroke-width:2px;
    classDef export fill:#FFFF99,stroke:#000,stroke-width:2px;
```
