# var-meta-tech/solana-c-sdk architecture

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

- Interactive diagram: https://gitdiagram.com/var-meta-tech/solana-c-sdk
- Repository: https://github.com/var-meta-tech/solana-c-sdk

## Overview

##### Project type and purpose
- This is an open‑source SDK/tool that exposes Solana blockchain functionality (wallet, token, smart‑contract, and account operations) written in Rust, with a C FFI layer. It enables C programs to call into Rust code and interact with the Solana network.

##### File structure
- Top‑level build and config:
- Cargo.toml/Cargo.lock – Rust package metadata
- build.sh & generate.sh – scripts to compile the Rust library and generate C interfaces from Anchor IDL
- cbindgen.toml – config for generating C headers from Rust code
- Code directories:
- src/ – Rust source modules (client.rs, lib.rs, main.rs, smart_contract.rs, token.rs, wallet.rs)
- header/ – generated C interface files (solana_sdk.h, anchor_counter_interface.c, etc.)
- Helpers and tests:
- generate_c_interface.c – helper for IDL‑to‑C binding
- test/ – C test driver (test.c, test.sh, JSON fixtures)
- anchor_counter.json – sample Anchor IDL
- Other artifacts:
- wallet_keypair*.json – sample keypairs used by tests

##### README for additional insights
- Four core feature areas with corresponding Rust modules and exposed C functions:
- Wallet management (new_keypair, load_wallet, get_address)
- Token operations (transfer_sol, transfer_spl, mint_spl, get_balance)
- Smart contract integration (send_generic_transaction_c, initialize_account_c)
- Account data operations (get_account_data_c, get_mint_info)
- Build/test workflow: build.sh → produces a C‑compatible shared library and headers → test.sh runs C examples
- IDL‑based interface generation via generate.sh → header/anchor_counter_interface.c

a. Main components
1. Rust Core Library
– Modules: Client, Wallet, Token, Smart Contract, Account
– Exposes public FFI functions via #[no_mangle]
2. C FFI Layer
– Generated headers (solana_sdk.h, anchor_counter_interface.c)
– cbindgen & generate_c_interface
3. C Application / Test Harness
– test.c, any downstream C program including solana_sdk.h
4. Build & Generation Tools
– build.sh (Cargo build → shared library)
– generate.sh (IDL JSON → C interface)
5. External Dependencies
– Solana RPC network (devnet/mainnet RPC endpoints)
– Anchor IDL files
b. Relationships and interactions
– C FFI Layer → Rust Core Library (FFI boundary)
– Rust Core Library → Solana RPC (HTTP/JSON RPC calls)
– generate.sh → cbindgen → header/anchor_counter_interface.c (IDL‑based codegen)
– build.sh → Cargo → shared‑library artifact (.so/.dll/.dylib) used by C
c. Architectural patterns & design principles
– Modular Rust design: separate modules for each domain (wallet, token, contract)
– FFI boundary clearly separated via generated C headers
– Code‑generation pattern for Anchor IDL to C bindings
– Shell‑script–based build pipeline (simple CI plugin)
d. Technologies & frameworks
– Rust (cargo, solana‑client, solana‑sdk, spl‑token, borsh, serde)
– cbindgen for header generation
– C (gcc/clang) for test apps and FFI consumers
– Solana CLI for network setup/deployment
– Anchor (for smart‑contract IDL files)

##### Diagramming recommendations
- Blue rectangles for Rust modules
- Green rectangles for generated C headers/FFI layer
- Grey parallelogram for build scripts
- Yellow rectangles for C application/test harness
- C app → C FFI
- C FFI → Rust core
- Rust core → Solana RPC
- generate.sh → cbindgen → header files
- build.sh → Cargo build → .so/.dll
- “Build & Codegen Pipeline” vs. “Runtime Components”

## Diagram (Mermaid)

```mermaid
flowchart TB
  subgraph "Build & Codegen Pipeline"
    direction TB
    BS[/"build.sh"/]:::script
    GS[/"generate.sh"/]:::script
    GCIc["generate_c_interface.c"]:::script
    GI["generate_c_interface"]:::script
    CBT["cbindgen.toml"]:::script
    CT["Cargo.toml"]:::config
    CLK["Cargo.lock"]:::config
  end

  BS -->|"runs Cargo build"| Cargo["Cargo Build"]:::script
  GS -->|"uses IDL"| AIDL[(anchor_counter.json)]:::external
  GS --> GCIc
  GCIc --> GI
  GI --> CBT
  GI --> HSSI["solana_sdk.h"]:::ffi
  GI --> HACI["anchor_counter_interface.c"]:::ffi

  subgraph "Rust Core Library"
    direction TB
    Lib["src/lib.rs"]:::rust
    CL["src/client.rs"]:::rust
    WL["src/wallet.rs"]:::rust
    TL["src/token.rs"]:::rust
    SL["src/smart_contract.rs"]:::rust
    ML["src/main.rs"]:::rust
  end

  Cargo --> Lib
  Cargo --> CL
  Cargo --> WL
  Cargo --> TL
  Cargo --> SL
  Cargo --> ML

  subgraph "C FFI Layer"
    direction TB
    HSSI
    HACI
  end

  subgraph "C Application / Test Harness"
    direction TB
    TC["test/test.c"]:::capp
    TS["test/test.sh"]:::capp
    TJ["test/test.json"]:::capp
    KP1["wallet_keypair.json"]:::capp
    KP2["wallet_keypair_mint.json"]:::capp
    KP3["wallet_keypair_recipient.json"]:::capp
    KP4["wallet_keypair_recipient2.json"]:::capp
  end

  subgraph "External Dependencies"
    direction TB
    RPC[(Solana RPC)]:::external
  end

  TC -->|"includes header"| HSSI
  TC -->|"includes header"| HACI
  TS --> TC
  TC -->|"links to library"| Lib
  HSSI -->|"FFI call"| Lib
  SL -->|"calls RPC"| RPC

  click Lib "https://github.com/var-meta-tech/solana-c-sdk/blob/main/src/lib.rs"
  click CL "https://github.com/var-meta-tech/solana-c-sdk/blob/main/src/client.rs"
  click WL "https://github.com/var-meta-tech/solana-c-sdk/blob/main/src/wallet.rs"
  click TL "https://github.com/var-meta-tech/solana-c-sdk/blob/main/src/token.rs"
  click SL "https://github.com/var-meta-tech/solana-c-sdk/blob/main/src/smart_contract.rs"
  click ML "https://github.com/var-meta-tech/solana-c-sdk/blob/main/src/main.rs"
  click HSSI "https://github.com/var-meta-tech/solana-c-sdk/blob/main/header/solana_sdk.h"
  click HACI "https://github.com/var-meta-tech/solana-c-sdk/blob/main/header/anchor_counter_interface.c"
  click CBT "https://github.com/var-meta-tech/solana-c-sdk/blob/main/cbindgen.toml"
  click BS "https://github.com/var-meta-tech/solana-c-sdk/blob/main/build.sh"
  click GS "https://github.com/var-meta-tech/solana-c-sdk/blob/main/generate.sh"
  click GCIc "https://github.com/var-meta-tech/solana-c-sdk/blob/main/generate_c_interface.c"
  click GI "https://github.com/var-meta-tech/solana-c-sdk/tree/main/generate_c_interface"
  click CT "https://github.com/var-meta-tech/solana-c-sdk/blob/main/Cargo.toml"
  click CLK "https://github.com/var-meta-tech/solana-c-sdk/blob/main/Cargo.lock"
  click TC "https://github.com/var-meta-tech/solana-c-sdk/blob/main/test/test.c"
  click TS "https://github.com/var-meta-tech/solana-c-sdk/blob/main/test/test.sh"
  click TJ "https://github.com/var-meta-tech/solana-c-sdk/blob/main/test/test.json"
  click AIDL "https://github.com/var-meta-tech/solana-c-sdk/blob/main/anchor_counter.json"

  classDef rust fill:#bbeeff,stroke:#1f77b4,color:#1f77b4
  classDef ffi fill:#ccffcc,stroke:#2ca02c,color:#2ca02c
  classDef external fill:#ffcc66,stroke:#ff7f0e,color:#ff7f0e
  classDef script fill:#dddddd,stroke:#888888,color:#888888
  classDef capp fill:#ffffb3,stroke:#dbdb08,color:#dbdb08
  classDef config fill:#f2f2f2,stroke:#aaaaaa,color:#666666
```
