# oraichain/wasmd architecture

> Architecture diagram of the GitHub repository oraichain/wasmd, 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/oraichain/wasmd
- Repository: https://github.com/oraichain/wasmd

## Overview

This project is Wasm Zone’s implementation of a Cosmos SDK-based blockchain that has native support for executing wasm smart contracts. It started as a fork of Gaia and incorporates an additional module (x/wasm) that enables smart contracts. In essence, the project is a full-fledged blockchain node application with its own command-line interface, consensus engine, module infrastructure, and integrated smart contract execution environment.

##### Main Components
- Node/Application Layer: The core of the blockchain (found in the “app” directory and “cmd/wasmd”). This includes the node’s initialization, configuration, command-line interfaces, and runtime flags.
- Modules:
- Core Cosmos SDK modules inherited from Gaia.
- The smart contract module “x/wasm” that adds CosmWasm capabilities.
- Additional modules like “x/tokenfactory” (see directory “x/tokenfactory”) for specialized functionalities.
- WASM Engine: The dependency “wasmvm” which is responsible for loading and executing smart contracts.
- API/CLI Components: Components for user interaction, transaction submission, and querying the blockchain state (visible in “cmd/config”, “cmd/keys.go”, and CLI commands under “x/wasm/client”).
- Database/State Storage: The underlying persistent state storage and genesis configuration (configured in “app/app.go” and related files) holding accounts, contracts, and proposals.
- Testing and Infrastructure: The extensive test suites (directories like “tests”, “benchmarks”, “scripts”, “docs”) that illustrate integration testing, migration, and chain orchestration processes.
- Integration with External Services: Docker-related files, CI configurations (.circleci, GitHub actions, etc.), and the protobuf definitions (in the “proto” directory) that facilitate integration with external tools and clients.

##### Relationships and Interactions
- The node/application layer (wasmd) is responsible for managing blockchain consensus and invoking module-specific logic (including x/wasm).
- The x/wasm module interacts with the wasmvm runtime to load, validate, and execute smart contracts, passing along encoded JSON messages (with support from the cosmos-sdk encoding layer).
- API/CLI interfaces enable users to submit transactions, query state, and trigger smart contract executions.
- The genesis configuration and runtime parameters set initial conditions and allow for upgrades (see “app/upgrades” and “INTEGRATION.md”), which creates dependencies between the main application and upgrade mechanisms.
- The testing suites simulate end-to-end scenarios including IBC, governance proposals, and interchain testing (in “tests/e2e”, “tests/ibctesting”, “tests/system”, etc.), thereby showing the interactions between on-chain modules and external clients.

##### Architectural Patterns and Design Principles
- Modularity: The design adheres to the typical Cosmos SDK architecture, where individual modules (like x/wasm, x/tokenfactory) plug into a common application.
- Plugin/Extension: The wasm execution environment (imported via wasmvm) acts as an extension point for contract execution.
- Separation of concerns: There is a clear distinction between the core blockchain logic, application state management, smart contract execution, and auxiliary tools/scripts.
- Integration through Protobuf: The use of protobuf files (located in the “proto” directory) ensures that components (internal modules and external clients) maintain a clear, versioned communication protocol.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Blockchain Core
    subgraph "Blockchain Core"
        CA("Core Application & Storage"):::blockchain
        Wasmd("CLI (wasmd)"):::blockchain
    end

    %% Modules
    subgraph "Modules"
        Cosmos("Cosmos SDK Modules"):::module
        WasmModule("Smart Contract Module (x/wasm)"):::module
        Tokenfactory("Tokenfactory Module"):::module
    end

    WasmEngine("WASM Engine (wasmvm)"):::module

    %% API/CLI Interfaces
    subgraph "API/CLI Interfaces"
        CLIConfig("CLI Config"):::apicli
        KeysCLI("Keys CLI"):::apicli
        WasmClient("Wasm Client"):::apicli
    end

    %% Testing & Infrastructure
    subgraph "Testing & Infrastructure"
        Tests("Testing Suite"):::testing
        Benchmarks("Benchmarking"):::testing
        DeploymentScripts("Deployment Scripts"):::testing
        Docs("Docs & Integration"):::testing
    end

    %% External Integration
    subgraph "External Integration"
        CI("CI Config"):::external
        Docker("Docker Deployment"):::external
        Protobuf("Protobuf Definitions"):::external
    end

    %% Connections between API/CLI and Core
    CLIConfig -->|"submits"| CA
    KeysCLI -->|"submits"| CA
    WasmClient -->|"submits"| CA

    %% Core Application invoking modules
    CA -->|"invokes"| Cosmos
    CA -->|"invokes"| WasmModule
    CA -->|"invokes"| Tokenfactory

    %% Module interactions
    WasmModule -->|"executes"| WasmEngine

    %% Testing and Infrastructure interacting with Core
    Tests ---|"tests"| CA
    Benchmarks --- CA
    DeploymentScripts --- CA
    Docs --- CA

    %% External Integration interacting with Core
    CI --- CA
    Docker --- CA
    Protobuf --- CA

    %% Click Events
    click CA "https://github.com/oraichain/wasmd/tree/main/app/"
    click Wasmd "https://github.com/oraichain/wasmd/tree/main/cmd/wasmd/"
    click WasmModule "https://github.com/oraichain/wasmd/tree/main/x/wasm/"
    click Tokenfactory "https://github.com/oraichain/wasmd/tree/main/x/tokenfactory/"
    click WasmEngine "https://github.com/oraichain/wasmd/tree/main/precompile/cosmwasm/"
    click CLIConfig "https://github.com/oraichain/wasmd/tree/main/cmd/config/"
    click KeysCLI "https://github.com/oraichain/wasmd/blob/main/cmd/keys.go"
    click WasmClient "https://github.com/oraichain/wasmd/tree/main/x/wasm/client/"
    click Tests "https://github.com/oraichain/wasmd/tree/main/tests/"
    click Benchmarks "https://github.com/oraichain/wasmd/tree/main/benchmarks/"
    click DeploymentScripts "https://github.com/oraichain/wasmd/tree/main/scripts/"
    click Docs "https://github.com/oraichain/wasmd/tree/main/docs/"
    click CI "https://github.com/oraichain/wasmd/tree/main/.circleci/"
    click Docker "https://github.com/oraichain/wasmd/tree/main/Dockerfile"
    click Protobuf "https://github.com/oraichain/wasmd/tree/main/proto/"

    %% Styles
    classDef blockchain fill:#bbf,stroke:#333,stroke-width:2px;
    classDef module fill:#b3e5fc,stroke:#333,stroke-width:2px;
    classDef apicli fill:#ffcdd2,stroke:#333,stroke-width:2px;
    classDef testing fill:#c8e6c9,stroke:#333,stroke-width:2px;
    classDef external fill:#fff9c4,stroke:#333,stroke-width:2px;
```
