Skip to main content

Minimal build/deploy tool for Python NEAR smart contracts

This is a build/deploy tool for Python NEAR smart contracts for https://github.com/near/devtools/issues/2

Python source files are compiled into the WASM binary via MicroPython and Emscripten and then deployed via near-cli-rs tool.

near-py-tool CLI is modelled after cargo-near

Dependencies

near-py-tool expects the following dependencies installed:

  • Python>=3.9
  • essential build tools like make and C compiler
  • Emscripten for compiling Python into WASM via MicroPython (can be installed automatically by near-py-tool, if desired). Version 4.0.0 or above is recommended as there are issues with building some of the contracts on 3.x. Automatic installation via emsdk is offered if emcc is absent from PATH
  • near-cli-rs for deployment and other NEAR Protocol interactions

Getting started

  • install near-py-tool via pip install near-py-tool
  • run near-py-tool new test-project to create a minimal Python smart contract project
  • cd ./test-project
  • run near-py-tool build to produce a standalone WASM file
  • run near-py-tool create-dev-account to create a testnet account if you don't have one already
  • run near-py-tool deploy to deploy the smart contract to testnet or mainnet

Examples

Please see the test suite for various low-level NEAR API usage examples:

External examples:

Platform support

Currenly Linux, MacOS and Windows (native/MSYS2/WSL) platforms are supported. Native Windows support is implemented via an automatically installed MSYS2 local copy which is utilized for MicroPython/WASM contract builds

Python library support / compatibility

Most of the MicroPython standard library is included and should be functional where applicable to WASM runtime environment.

External Python package are supported as long as they don't require native compiled code to work. near-py-tool will download any packages referenced via pyproject.toml and will try to compile them into the WASM binary alongside the main contract.py file.

Unneeded modules from the MicroPython stdlib can be excluded from the build by adding the following section to pyproject.toml: [tool.near-py-tool] exclude-micropython-stdlib-packages = [...]

Please be aware that there is no full compatibility to CPython yet since we use MicroPython as a runtime

MessagePack serialization

near-py-tool provides an built-in msgpack module, which provides fast and space-efficient MessagePack serialization of arbitrary Python data, including aritrary-precision integers (implemented in C via cmp library)

Basic interface is equivalent to the Python msgpack module:

def packb(o: object) -> bytes
def unpackb(b: bytes) -> object

Arbitrary-precision integers are stored as a MessagePack extension type 81; this is not portable outside of the WASM contract runtime environment, but useful for saving large number like account balances within the contract persisten state

NEAR WASM ABI support

Everything from https://github.com/near/near-sdk-rs/blob/master/near-sys/src/lib.rs should be available via near module, for example:

  • near.input() retrieves contract input as bytes
  • near.value_return(value) returns a value (str or bytes) from the contract
  • near.log_utf8(message) logs a message (str)

Contract methods to be exported from the WASM binary should be decorated with @near.export

See the NEAR-ABI.md for a complete list of available methods and their type signatures.

Benchmarks

Here are some gas cost benchmark results which allow comparison to Rust and JS

Stats for similar Rust/JS contracts are available here

In general, gas costs for this Python implementation lie in between the Rust and JS values, which makes Python smart contracts practical for most use cases

Contributing

We are in the process of restructuring this into separate Python SDK / nearc compiler / test suite repositories as part of https://github.com/near/devtools/issues/3, so future contributions should be directed towards the new repositories as they take shape

Metadata

Release files for near-py-tool 0.1.29

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for near-py-tool 0.1.29
File Size Uploaded
near_py_tool-0.1.29.tar.gz 8.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for near-py-tool 0.1.29
File Interpreter ABI Platform
near_py_tool-0.1.29-py3-none-any.whl Python 3 none any Details

Total release size: 20.4 MB

Release files / near_py_tool-0.1.29.tar.gz

Download URL near_py_tool-0.1.29.tar.gz
Size 8.6 MB
Tags Source
SHA-256 checksum
How to use checksums
6e7377c42b81f3f7d027a5d5690e12b900f3409e23ef8acb0b777279d14e4d99
BLAKE2b-256 checksum
How to use checksums
610c0361c7f5ef0fd1e94783f430a1d5c64b24e3b1bc8d61d572ce5c7ced9bc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.4

Release files / near_py_tool-0.1.29-py3-none-any.whl

Download URL near_py_tool-0.1.29-py3-none-any.whl
Size 11.8 MB
Tags Python 3
SHA-256 checksum
How to use checksums
541d42162b60eb855fcd9aac5af5739f16a8e10975bdbaad044555f2bcd22d87
BLAKE2b-256 checksum
How to use checksums
022b44dc4f760d6defbc48456dcd629b05913636b79ab2749ae4de34f1befe80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.4

Release history Release notifications | RSS feed

This release

0.1.29 This release

2 release files

0.1.26

2 release files

0.1.25

2 release files

0.1.24

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page