Skip to main content

Python NEAR contract build/deploy tool

Project description

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

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

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

near_py_tool-0.1.28.tar.gz (8.6 MB view details)

Uploaded Source

Built Distribution

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

near_py_tool-0.1.28-py3-none-any.whl (11.8 MB view details)

Uploaded Python 3

File details

Details for the file near_py_tool-0.1.28.tar.gz.

File metadata

  • Download URL: near_py_tool-0.1.28.tar.gz
  • Upload date:
  • Size: 8.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.4

File hashes

Hashes for near_py_tool-0.1.28.tar.gz
Algorithm Hash digest
SHA256 89cc4251a298315dfa73e5b320bcc10dda0dbf63d509300e2b0fd218e03b3482
MD5 1e73723abaef763876583bf67ac870b5
BLAKE2b-256 5b757e2dfa140db222f80413491ece5ca738c6e1962801b492ad8d8304e3772d

See more details on using hashes here.

File details

Details for the file near_py_tool-0.1.28-py3-none-any.whl.

File metadata

File hashes

Hashes for near_py_tool-0.1.28-py3-none-any.whl
Algorithm Hash digest
SHA256 add19c2dcaaf1a8d03857985d553c491c5b4b998d9b7b04a5ab047745e824aec
MD5 358dd641d55170b0566cb3c522034b51
BLAKE2b-256 baca1921a8fc1f91107fc4b5ff339c1b0dc733b3a234de4923a070848e05c6c7

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