Skip to main content

Mycelium Python-to-Soroban-WASM compiler

Project description

Mycelium Python-to-Soroban Compiler

The Mycelium Compiler (mycelium-compiler) is a high-performance Python AST parser and transpilation engine that converts Python-DSL smart contracts into highly optimized, secure WebAssembly (WASM) binaries for the Stellar/Soroban virtual machine.


๐Ÿ—๏ธ Compiler Architecture

The compilation process is structured into four main phases:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Python DSL  โ”‚ โ”€โ”€โ”€> โ”‚  AST Parsing   โ”‚ โ”€โ”€โ”€> โ”‚ Type Validation โ”‚ โ”€โ”€โ”€> โ”‚ Transpilationโ”‚
โ”‚  (Source)    โ”‚      โ”‚  (parser.py)   โ”‚      โ”‚ (validator.py)  โ”‚      โ”‚ (codegen/)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                                              โ”‚
                                                                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Soroban WASM โ”‚ <โ”€โ”€โ”€ โ”‚   Rust Cargo   โ”‚ <โ”€โ”€โ”€ โ”‚   Stellar CLI   โ”‚ <โ”€โ”€โ”€ โ”‚  Rust Code  โ”‚
โ”‚   (Binary)   โ”‚      โ”‚     Build      โ”‚      โ”‚   Compilation   โ”‚      โ”‚ (src/lib.rs)โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

1. AST Parsing (parser.py)

  • Reads the Python source file and converts it into a Python Abstract Syntax Tree (AST) using Python's native ast library.
  • Extracts module-level constants, @contract definitions, storage variables, and contract function schemas.
  • Parses auxiliary classes representing custom structs, events, interfaces, and constant-based enums.

2. Type & AST Validation (validator.py)

  • Ensures that all types specified in the Python contract strictly conform to Soroban-compatible primitives and collection types.
  • Asserts that all function signatures, return types, and storage variables are valid.
  • Supported Primitives: int, str, bytes, bool, Symbol, i32, i64, i128, u32, u64, Address, U256, U128, U64, U32, I128, I32, Bool, Env.
  • Supported Collections: Map[K, V], Vec[T], Bytes[N], DynArray[T, N], list, tuple.

3. Rust Code Generation (codegen/)

  • Storage Type Inference (inferrer.py): Traverses function logic to infer the types of local variables and on-chain storage states to construct statically typed Rust equivalents.
  • Transpiler (transpiler.py & core.py): Translates Python statements, loops, branches, assignments, and expressions into clean, memory-safe, idiomatic Soroban Rust code.
  • Features:
    • Automatically handles local variable pre-declaration.
    • Implements storage read/write virtualization (mapping self.balances[key] to Soroban persistent/instance storage access).
    • Injects contextual state wrappers (e.g., msg_sender.require_auth(), block sequences, and transaction timestamps).

4. Compilation & Bootstrapping (core.py)

  • Emits temporary Cargo workspaces with optimal release settings:
    • opt-level = "z" (optimized for minimal WASM size).
    • overflow-checks = true.
    • Link-Time Optimization (lto = true) and single-unit compilation.
  • Stellar CLI Bootstrapper: Checks for stellar in the system path. If not found, it automatically downloads the certified stellar-cli executable for your specific platform/architecture.
  • Compiles the Rust intermediate file into a .wasm file.

๐Ÿš€ Installation & CLI Usage

Install the compiler package:

pip install mycelium-compiler

Compile a Python contract source file to WASM:

mycelium compile my_contract.py -o build/my_contract.wasm

Script Execution (Python API)

You can also compile contracts programmatically inside Python scripts:

from mycelium_compiler.main import compile_file

compile_file("my_contract.py", "build/my_contract.wasm")

๐Ÿ“ DSL Contract Example

The compiler translates Python files looking like this:

from mycelium import contract, external, view, U64, Address, Map

@contract
class TokenCounter:
    # On-chain state variables
    balances: Map[Address, U64]
    owner: Address

    @external
    def initialize(self, owner: Address):
        self.owner = owner

    @external
    def mint(self, to: Address, amount: U64):
        # Implicitly requires auth from the owner (under the hood)
        if msg_sender != self.owner:
            panic("Unauthorized")
        
        current = self.balances.get(to, 0)
        self.balances[to] = current + amount

    @view
    def get_balance(self, account: Address) -> U64:
        return self.balances.get(account, 0)

๐Ÿ› ๏ธ Sandbox Fallback Execution

When running inside cloud sandboxes or serverless backend instances (e.g., Render or AWS Lambda) where Docker is restricted, the compiler features a native python runner fallback that executes the cargo compiler in-process, bypassing the container requirement while enforcing standard resource safety limits.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mycelium_compiler-0.1.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mycelium_compiler-0.1.0-py3-none-any.whl (37.9 kB view details)

Uploaded Python 3

File details

Details for the file mycelium_compiler-0.1.0.tar.gz.

File metadata

  • Download URL: mycelium_compiler-0.1.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for mycelium_compiler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dbac2bb5ef068324c8e2b0abb5cda2d990af2d673f267c460b6be06da3dc1c7b
MD5 37453a1466126cf32c1c3881b09be6a4
BLAKE2b-256 0bbb49324194ff038cb6999ca0f7d62ad5ba9d2b96bacf8ea13b216dbb15e9d4

See more details on using hashes here.

File details

Details for the file mycelium_compiler-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mycelium_compiler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 73f26026c3e205b8a58802f9f0b7085da7713fa59f27e7b3551664a1be182bf0
MD5 80654f709929d07b828cf76004ff588e
BLAKE2b-256 2566d7c0376a9098e2ea4d1cd55c7c6cdf5dc6eb7f2186f937579e7957f98ea4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page