# dapplink-labs/multichain-sync-account architecture

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

- Interactive diagram: https://gitdiagram.com/dapplink-labs/multichain-sync-account
- Repository: https://github.com/dapplink-labs/multichain-sync-account

## Overview

##### Project Type and Purpose
- The project is clearly a backend middleware solution for centralized wallet management. Its primary goal is to synchronize transactions across multiple blockchain networks, acting as part of the DappLink one-click wallet deployment solution.
- The README and file tree indicate that it handles wallet synchronization by fetching, processing, and persisting multi-chain data, while also providing APIs (via RPC and protobuf) and background job processing.

##### Analyzing the File Structure
- Top-level directories like cmd, common, config, database, notifier, rpcclient, services, and worker provide key insights into the architecture.
- The cmd directory (with multichain-sync) contains the main entry point of the application.
- The common directory holds utility packages (bigint, cliapp, clock, retry, etc.) that enforce consistency across the project – these are lower-level libraries and helpers.
- The database directory manages data persistence and includes separate files for addresses, balances, deposits, withdrawals, and even dynamic table handling, suggesting a rich data model.
- The notifier directory indicates that the system includes mechanisms to alert or trigger external actions when certain events (like transaction changes) occur.
- The rpcclient and protobuf directories reveal that the system integrates with external blockchain services via gRPC and protocol buffers, managing remote calls and data serialization.
- The worker directory contains logic for processing deposits, withdrawals, and synchronization tasks – this appears to be the background job processing mechanism.
- Miscellaneous directories (flags, migrations, sh) show that automated builds, environment configuration, and database migration scripts are well-integrated into the project.

a. Main Components to Include:
- Entry Point / CLI Application: Represent the multichain-sync command under the cmd directory that acts as the orchestrator of the system.
- Common Library/Core Utilities: Group of shared utilities (clock, retry, tasks, etc.) that all other components use.
- Configuration Layer: Handles environment and custom configuration from the config directory.
- Database Layer: Acts as persistent storage, holding blockchain-related data (addresses, balances, blocks, deposits, withdrawals, tokens). Show connections to database management found in the database and migrations directories.
- RPC Client / External Blockchain APIs: Models the integration with external chains using rpcclient and protobuf definitions. This should also show how chain accounts and their communication are structured.
- Notifier: Represents the mechanism for sending alerts or fallback notifications (see notifier directory).
- Worker Processes: Background job execution for handling deposits, withdrawals, and overall synchronization – the logic found in the worker directory.
- Services: Additional business/logic layer that perhaps orchestrates higher-level operations and interacts with both the RPC and database layers.

b. Relationships and Interactions:
- The CLI entry point (cmd/multichain-sync) initializes configuration, loads common utilities, and kicks off worker processes.
- Worker processes interact with the database layer to read/write transaction data as well as with the RPC client for fetching chain data.
- The notifier component listens to events from services and/or workers and communicates externally if required.
- RPC client components act as intermediaries between the middleware and external blockchain networks.
- The common utilities underlie almost every module, ensuring standardization in logging, error handling, and operations (e.g., retry strategies, time management).

c. Architectural Patterns and Design Principles:
- Layered Architecture: Separation into entry point, utilities, business services, data layer, and external integrations.
- Separation of Concerns: Each component (RPC client, database, worker) handles its specific responsibility.
- Modularity: The system is designed in modular packages (for clocks, tasks, serializers, etc.) that are reusable and testable.
- Asynchronous/Background Processing: Indicated by the workers’ structure which process deposits and withdrawals outside of the main command flow.
- Use of gRPC and Protobuf for inter-service communication, showing strong typing and robust remote procedure calling.

d. Technologies, Frameworks, and Libraries:
- Go (version 1.22+ as indicated) is used throughout.
- gRPC and protobuf – evident from protobuf and rpcclient directories.
- SQL-based migrations and possible relational database usage (given the migrations and SQL scripts).
- Docker Compose for deployment orchestration.
- Various utility libraries for tasks like retries, caching, and time manipulation (see common/*).

##### Detailed Process Flow (Optional)
- Start at the CLI entry point which reads configuration and then instantiates background workers.
- Workers call into the RPC client to poll or push blockchain events.
- Data fetched is processed and then persisted by the Database layer.
- Errors or significant events can be recognized by the Notifier which may send alerts.
- Common packages like retry, clock, and slices support these operations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CLI["CLI/Entry Point"]:::entry
    CONFIG["Configuration Layer"]:::config
    CUTIL["Common Utilities"]:::util
    WORKER["Worker Processes"]:::worker
    SERVICES["Services"]:::service
    RPC["RPC Client/External Blockchain APIs"]:::external
    DB["Database Layer"]:::database
    NOTIF["Notifier"]:::notifier

    %% Primary Flow
    CLI -->|"loads"| CONFIG
    CLI -->|"loads"| CUTIL
    CLI -->|"triggers"| WORKER
    WORKER -->|"fetchesDataFrom"| RPC
    WORKER -->|"persistsDataTo"| DB
    WORKER -->|"notifies"| NOTIF
    SERVICES -->|"orchestrates"| DB
    SERVICES -->|"interactsWith"| RPC

    %% Supporting Utility Usage (dashed arrows)
    CONFIG -.-> CUTIL
    DB -.-> CUTIL
    RPC -.-> CUTIL
    WORKER -.-> CUTIL
    SERVICES -.-> CUTIL
    NOTIF -.-> CUTIL

    %% Click Events
    click CLI "https://github.com/dapplink-labs/multichain-sync-account/blob/main/cmd/multichain-sync/main.go"
    click CLI "https://github.com/dapplink-labs/multichain-sync-account/blob/main/cmd/multichain-sync/cli.go"
    click CUTIL "https://github.com/dapplink-labs/multichain-sync-account/tree/main/common"
    click CONFIG "https://github.com/dapplink-labs/multichain-sync-account/blob/main/config/config.go"
    click CONFIG "https://github.com/dapplink-labs/multichain-sync-account/blob/main/config/config_mock.go"
    click DB "https://github.com/dapplink-labs/multichain-sync-account/tree/main/database"
    click DB "https://github.com/dapplink-labs/multichain-sync-account/blob/main/migrations/00001_create_schema.sql"
    click RPC "https://github.com/dapplink-labs/multichain-sync-account/tree/main/rpcclient"
    click RPC "https://github.com/dapplink-labs/multichain-sync-account/tree/main/protobuf"
    click NOTIF "https://github.com/dapplink-labs/multichain-sync-account/tree/main/notifier"
    click WORKER "https://github.com/dapplink-labs/multichain-sync-account/tree/main/worker"
    click SERVICES "https://github.com/dapplink-labs/multichain-sync-account/tree/main/services"

    %% Styles
    classDef entry fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef util fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef config fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef database fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef external fill:#ffebcc,stroke:#333,stroke-width:2px;
    classDef notifier fill:#e6ccff,stroke:#333,stroke-width:2px;
    classDef worker fill:#d9edf7,stroke:#333,stroke-width:2px;
    classDef service fill:#f7d9e3,stroke:#333,stroke-width:2px;
```
