# dedotdev/dedot architecture

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

- Interactive diagram: https://gitdiagram.com/dedotdev/dedot
- Repository: https://github.com/dedotdev/dedot

## Overview

##### Main components
- Core Client & API: The “packages/api” and “packages/dedot” modules serve as the central interface for interacting with blockchain networks. They handle JSON-RPC calls, state queries, runtime API calls, subscription events (block chain head, transactions) and provide the main client classes (e.g., DedotClient, Substrate client interfaces).
- Providers: Located in “packages/providers”, these modules implement the actual transport layers (such as WsProvider for WebSocket-based connections and the Smoldot provider for light client functionality). They abstract away connection details and error handling.
- Codecs & Types: “packages/codecs” and “packages/types” are responsible for decoding/encoding scale-codec data coming from a Substrate chain. They represent extrinsic, metadata, and other low-level binary formats in a type-safe way.
- Code Generation: “packages/codegen” (including the “typink” submodule) automatically produces strongly-typed definitions (like chain types, contract types, runtime API definitions) by processing on-chain metadata. This allows Dedot to provide precise types and API suggestions.
- Contracts: “packages/contracts” supplies a full-featured, type-safe framework for interacting with ink! smart contracts. It includes executors to dispatch contract calls and deployers to handle contract instantiation.
- Runtime Specs & Chaintypes: “packages/runtime-specs” and “packages/chaintypes” provide runtime-specific constants, errors, events, and API details that are unique to each blockchain network.
- CLI: “packages/cli” gives command-line interfaces for interacting with Dedot (for example, executing commands for contract metadata processing or chain type generation).
- Utilities: “packages/utils” offers shared helper functions (hashing, formatting, address manipulation, queueing, etc.) that support many of the above layers.

##### Component Interactions and Data Flows
- When a user calls an API method (e.g., querying an account balance, subscribing to new block events), the Dedot client forwards the request to the appropriate Provider, which handles the network communication.
- If the data involves runtime calls or contract execution, the Contracts and Runtime-Specs modules participate in constructing call parameters and interpreting responses.
- The Codegen package sits in the development tools layer. It reads on-chain metadata and writes out strongly typed definitions that are then consumed by the API and Contracts packages.
- The CLI is a separate external interface that primarily calls into Dedot’s libraries (or related functions) for various administrative and utility operations.

##### Architectural Patterns and Design Principles
- Modular/Monorepo Architecture: The separation into packages (API, CLI, Codecs, etc.) allows independent evolution of each module while maintaining a coherent overall system through Lerna (as indicated by lerna.json).
- Layered Abstraction: There is a clear separation between low-level functionalities (e.g., data encoding/decoding, direct JSON-RPC calls via Providers) and higher-level abstractions (e.g., DedotClient, typed Contract APIs). This keeps the system both flexible (allowing custom providers) and safe (with strong TypeScript types).
- Tree-Shakable & Lightweight: The emphasis on “small bundle size” and removing heavy dependencies is also depicted by separating concerns and only including what a project really needs.

##### Putting It All Together
- Around it, connect the “Providers” to represent connectivity mechanisms.
- “CLI”, “Code Generation”, “Storage”, and “Utilities” as peripheral components that support development, testing, and enhanced functionality.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "External Environment"
        A["Blockchain Network"]:::external
    end

    subgraph "Core Modules"
        B["Dedot Client/API"]:::core
        C["Providers"]:::core
        D["Codecs & Types"]:::core
        E["Runtime Specs & Chaintypes"]:::core
        F["Contracts"]:::core
    end

    subgraph "Supporting Modules"
        G["Code Generation"]:::support
        H["CLI"]:::support
        I["Shape & Storage"]:::support
        J["Utilities"]:::support
    end

    %% Core interactions
    B -->|"JSON-RPC call"| A
    A -->|"JSON-RPC response"| B
    B -->|"send-request"| C
    C -->|"raw-data"| D
    D -->|"decoded-data"| B
    B -->|"runtime-calls"| E
    B -->|"contract-exec"| F

    %% Supporting interactions
    G -->|"generated-types"| B
    G -->|"generated-types"| F
    H -->|"API-command"| B
    I -->|"validate-structure"| D
    J -->|"helper-utils"| B
    J -->|"support"| C
    J -->|"data-format"| D

    %% Click Events for Core Modules & Supporting Modules
    click B "https://github.com/dedotdev/dedot/blob/main/packages/api/src/client/DedotClient.ts"
    click B "https://github.com/dedotdev/dedot/blob/main/packages/dedot/src/index.ts"

    click C "https://github.com/dedotdev/dedot/tree/main/packages/providers/"
    click C "https://github.com/dedotdev/dedot/blob/main/packages/providers/src/ws/WsProvider.ts"
    click C "https://github.com/dedotdev/dedot/blob/main/packages/providers/src/smoldot/SmoldotProvider.ts"

    click D "https://github.com/dedotdev/dedot/tree/main/packages/codecs/"
    click D "https://github.com/dedotdev/dedot/tree/main/packages/types/"

    click G "https://github.com/dedotdev/dedot/tree/main/packages/codegen/"
    click G "https://github.com/dedotdev/dedot/tree/main/packages/codegen/src/typink/"

    click F "https://github.com/dedotdev/dedot/tree/main/packages/contracts/"
    click F "https://github.com/dedotdev/dedot/blob/main/packages/contracts/src/Contract.ts"

    click E "https://github.com/dedotdev/dedot/tree/main/packages/runtime-specs/"
    click E "https://github.com/dedotdev/dedot/tree/main/packages/api/src/chaintypes/"

    click I "https://github.com/dedotdev/dedot/tree/main/packages/shape/"
    click I "https://github.com/dedotdev/dedot/tree/main/packages/storage/"

    click H "https://github.com/dedotdev/dedot/tree/main/packages/cli/"

    click J "https://github.com/dedotdev/dedot/tree/main/packages/utils/"
    click J "https://github.com/dedotdev/dedot/tree/main/packages/dedot/src/utils/"

    %% Styles
    classDef external fill:#fcc,stroke:#333,stroke-width:2px;
    classDef core fill:#ccf,stroke:#333,stroke-width:2px;
    classDef support fill:#cfc,stroke:#333,stroke-width:2px;
```
