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.
v0.4.0 — the compiler rejoins the unified version line (
0.2.0→0.4.0), matchingmycelium-sdk,mycelium-cli, and themycelium-stellarmetapackage.
Note for contract authors: per-address storage keys use the raw
Address— writestorage.set("stake:" + addr, value). The compiler maps"prefix:" + addrto a(Symbol, Address)tuple key automatically; do not wrap the address instr().
🏗️ 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
astlibrary. - Extracts module-level constants,
@contractdefinitions, 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
stellarin the system path. If not found, it automatically downloads the certifiedstellar-cliexecutable for your specific platform/architecture. - Compiles the Rust intermediate file into a
.wasmfile.
🚀 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.
Metadata
Release files for mycelium-compiler 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mycelium_compiler-0.5.1.tar.gz | 37.6 kB | Without provenance | Details for mycelium_compiler-0.5.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mycelium_compiler-0.5.1-py3-none-any.whl | Python 3 | none | any | Details for mycelium_compiler-0.5.1-py3-none-any.whl |
Total release size: 75.7 kB
Release files / mycelium_compiler-0.5.1.tar.gz
| Download URL | mycelium_compiler-0.5.1.tar.gz |
|---|---|
| Size | 37.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1a9b6c13486e0649acb2a6ca0334f455c9739b0e2e4ce5bff1734419bc954dbd
|
|
BLAKE2b-256 checksum How to use checksums |
a2ae2e3dd4fe8e35f57d42e629ab1a369fdf5d6ba535f1371c4e470e6309d82a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / mycelium_compiler-0.5.1-py3-none-any.whl
| Download URL | mycelium_compiler-0.5.1-py3-none-any.whl |
|---|---|
| Size | 38.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
36f6babb36dcf67ee3fb681558ef793fbc8dceb89982affc5684ecca1aed7ca8
|
|
BLAKE2b-256 checksum How to use checksums |
527af2f19477df496ec17a4a35de9c1cd9d90035549dbee24c4efb4d29c6087d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for mycelium-compiler 0.5.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.