Python to WebAssembly compiler for NEAR smart contracts
Project description
NEARC - NEAR Python Contract Compiler
NEARC is a specialized compiler that transforms Python smart contracts into WebAssembly bytecode for deployment on the NEAR blockchain. It allows developers to write smart contracts in Python rather than Rust or AssemblyScript, leveraging MicroPython as the underlying runtime.
Features
- Write NEAR smart contracts in Python
- Use Python decorators (
@export,@view,@call, etc.) to mark contract methods - Automatic dependency detection and packaging
- Optimized WebAssembly output
Prerequisites
- Python 3.11 or newer
- Emscripten (emcc must be in PATH)
- uv for Python package management
Getting Started
NEARC is designed to be used without installation via uvx:
# Use directly with uvx (recommended)
uvx nearc contract.py
Alternatively, you can install it globally:
# Add to global Python with uv
uv pip install nearc
# Or install from PyPI
pip install nearc
Usage
# Basic usage
nearc contract.py
# Specify output file
nearc contract.py -o my-contract.wasm
# Use custom virtual environment
nearc contract.py --venv my-venv
# Force rebuild
nearc contract.py --rebuild
Command Line Options
| Option | Description |
|---|---|
--output, -o |
Output WASM filename (default: derived from contract name) |
--venv |
Path to virtual environment (default: .venv) |
--rebuild |
Force rebuild of all components |
Writing NEAR Contracts in Python
Here's a simple example of a NEAR smart contract written in Python:
# counter.py
import near
@near.export
def get_count():
return near.storage_read('count', 0)
@near.export
def increment():
count = near.storage_write('count', 0)
count += 1
near.storage_read('count', count)
return count
The compiler automatically detects functions with @near.export and similar decorators, making them available as contract methods in the compiled WebAssembly.
How It Works
NEARC compiles Python smart contracts to WebAssembly through these steps:
- Code Analysis - Identifies exported functions and dependencies in the Python code
- Manifest Generation - Creates a MicroPython manifest file that includes all necessary modules
- Export Wrapping - Generates C wrappers for exported functions
- WebAssembly Compilation - Uses MicroPython and Emscripten to compile to WebAssembly
- Optimization - Produces a compact WASM file ready for blockchain deployment
The compiler handles dependency resolution automatically, making it easy to use Python libraries in your contracts (as long as they're compatible with MicroPython).
Development
If you want to contribute to NEARC or modify the source code:
# Clone the repository
git clone https://github.com/r-near/nearc
cd nearc
# Initialize submodules
git submodule update --init src/nearc/micropython
git -C src/nearc/micropython submodule update --init lib/micropython-lib
# Create virtual environment for development
uv init
uv pip install -e '.[dev]'
Technical Details
MicroPython Integration
NEARC uses a customized version of MicroPython as its runtime environment:
- Focused on minimal memory footprint and efficient execution
- Includes NEAR-specific host function bindings
- Provides a Python standard library subset optimized for smart contracts
Support for External Dependencies
The compiler can include Python packages from your virtual environment:
- Automatically detects imports in your code
- Includes them in the compiled WebAssembly
- Warns about incompatible or missing dependencies
Limitations
- Only supports MicroPython-compatible Python code
- Not all Python standard library modules are available
- No support for async/await syntax
- Limited compatibility with existing Python packages
- Contract size limitations (though generally sufficient for most use cases)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nearc-0.1.3.tar.gz.
File metadata
- Download URL: nearc-0.1.3.tar.gz
- Upload date:
- Size: 8.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a77ec3f5cd38f5054b07b7bc1d41a367952a131729fd861b74210e4c56312218
|
|
| MD5 |
3319599c6431eaeb3e44df0657b0350b
|
|
| BLAKE2b-256 |
b19ca8b34b0d651d7a75ccc5cd532a09e12f33f4bbc76186adf48502f35ba452
|
File details
Details for the file nearc-0.1.3-py3-none-any.whl.
File metadata
- Download URL: nearc-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.6 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea2f4f2f77b69af55ac616f64fb896c0c905e69a0451ae02076fcb3f85e80391
|
|
| MD5 |
c0b60764ea0ab3cd73fe9b3f123960e0
|
|
| BLAKE2b-256 |
dafa9a7f4811c9a4e25f3a9150025dcabaed98cd8ef5602f81b8f56c2ccc3a46
|