Write smart contracts for Neo3 in Python
Made by COZ.IO
- Overview
- Quickstart
- Docs
- Reference Examples
- Python Supported Features
- Neo Python Suite Projects
- Opening a New Issue
- License
Neo3-Boa is a tool for creating Neo Smart Contracts using Python. It compiles .py
files to .nef
and .manisfest.json
formats for usage in the Neo Virtual Machine which is used to execute contracts on the Neo Blockchain.
Neo-boa is part of the Neo Python Framework, aimed to allow the full development of dApps using Python alone.
We want Python developers to feel comfortable when trying neo3-boa for the first time. It should look and behave like regular Python. For this reason we decided to avoid adding new keywords, but use decorators and helper functions instead.
In the real world, simply coding a smart contract is not enough. Developers need to debug, deploy and invoke it. Therefore, it’s important for this tool to be part of a bigger Python framework. To help the developers and avoid a bad user experience, we need to use logs and inform errors with details.
We need to ensure that the code works as expected, and the only way to do that is to run our tests against the official Neo 3 VM. Neo repository already contains a class called TestEngine that is capable of running tests using C# smart-contracts. It will be adjusted to support compiled smart-contracts.
Create a product that is easy to maintain and upgrade. Use Unit tests, typed and documented code to ensure its maintainability.
The diagram bellow shows the basic building blocks of the Neo3-Boa project.
Installation requires Python 3.7 or later.
On Linux / Mac OS:
$ python3 -m venv venv
$ source venv/bin/activate
On Windows:
$ python3 -m venv venv
$ venv\Scripts\activate.bat
$ pip install neo3-boa
If neo3-boa is not available via pip, you can run it from source.
$ git clone https://github.com/CityOfZion/neo3-boa.git
$ pip install wheel
$ pip install -e .
$ neo3-boa path/to/your/file.py
Note: When resolving compilation errors it is recommended to resolve the first reported error and try to compile again. An error can have a cascading effect and throw more errors all caused by the first.
from boa3.boa3 import Boa3
Boa3.compile_and_save('path/to/your/file.py')
Neo3-boa is compatible with the Neo Debugger. Debugger launch configuration example:
{
//Launch configuration example for Neo3-boa.
//Make sure you compile your smart-contract before you try to debug it.
"version": "0.2.0",
"configurations": [
{
"name": "example.nef",
"type": "neo-contract",
"request": "launch",
"program": "${workspaceFolder}\\example.nef",
"operation": "main",
"args": [],
"storage": [],
"runtime": {
"witnesses": {
"check-result": true
}
}
}
]
}
Clone neo-devpack-dotnet project and compile the TestEngine.
Note: Until neo-devpack-dotnet#365 is approved by Neo, you need to clone neo-devpack-dotnet from simplitech:test-engine-executable branch
$ git clone https://github.com/simplitech/neo-devpack-dotnet.git -b v3.0.3
$ dotnet build ./neo-devpack-dotnet/src/Neo.TestEngine/Neo.TestEngine.csproj
Go into the neo-devpack-dotnet, pull and recompile.
${path-to-folder}/neo-devpack-dotnet git pull
${path-to-folder}/neo-devpack-dotnet dotnet build ./src/Neo.TestEngine/Neo.TestEngine.csproj
Create a Python Script, import the TestEngine class, and define a function to test your smart contract. In this function
you'll need to call the method run()
. Its parameters are the path of the compiled smart contract, the smart
contract's method, and the arguments if necessary. Then assert your result to see if it's correct.
Your Python Script should look something like this:
from boa3_test.tests.test_classes.testengine import TestEngine
from boa3.neo.smart_contract.VoidType import VoidType
def test_hello_world_main():
root_folder = '{path-to-test-engine-folder}'
path = '%s/boa3_test/examples/HelloWorld.nef' % root_folder
engine = TestEngine(root_folder)
result = engine.run(path, 'Main')
assert result is VoidType
You can read the docs here. Please check our examples for reference.
For an extensive collection of examples:
Install neo3-boa
and the TestEngine
and run the following command
Note: If you didn't install TestEngine in neo3-boa's root folder, you need to change the value of
TEST_ENGINE_DIRECTORY
in this file
python -m unittest discover boa3_test
Status | Release | Converts | Example Code | Contract Example Test |
✅ | v0.3 | Local variable declarations and assignments |
|
List of examples |
✅ | v0.3 | Global variable declarations and assignments |
|
List of examples |
✅ | v0.4 | Global keyword |
|
List of examples |
✅ | v0.3 | Arithmetic operations |
|
List of examples |
✅ | v0.8 | Arithmetic operations |
|
List of examples |
🔜 | backlog | Arithmetic operations |
|
|
✅ | v0.3 | Arithmetic augmented assignment operators |
|
List of examples |
✅ | v0.8 | Arithmetic augmented assignment operators |
|
List of examples |
🔜 | backlog | Arithmetic augmented assignment operators |
|
|
✅ | v0.3 | Relational operations |
|
List of examples |
✅ | v0.8.3 | Relational operations |
|
List of examples |
✅ | v0.3 | Bitwise operations |
|
List of examples |
✅ | v0.8.3 | Bitwise augmented assignment operators |
|
List of examples |
✅ | v0.3 | Boolean logic operations |
|
List of examples |
✅ | v0.3 | Tuple type |
|
List of examples |
✅ | v0.3 | List type |
|
List of examples |
✅ | v0.4 | List type |
|
List of examples |
✅ | v0.7 | List type |
|
List of examples |
✅ | v0.3 | Dict type |
|
List of examples |
🔜 | backlog | Set type |
|
|
✅ | v0.3 | Bytes type |
|
List of examples |
✅ | v0.3 | Bytearray type |
|
List of examples |
✅ | v0.8.2 | Optional type |
|
List of examples |
✅ | v0.6.1 | Union type |
|
List of examples |
✅ | v0.3 | While statement |
|
List of examples |
✅ | v0.3 | If, elif, else statements |
|
List of examples |
✅ | v0.3 | For statement |
|
List of examples |
✅ | v0.3 | Function call |
|
List of examples |
✅ | v0.3 | Built in function |
|
List of examples |
✅ | v0.4 | Built in function |
|
List of examples |
✅ | v0.7 | Built in function |
|
List of examples |
✅ | v0.8 | Built in function |
|
List of examples |
✅ | v0.8.1 | Built in function |
|
List of examples |
✅ | v0.8.3 | Built in function |
|
List of examples |
🔜 | backlog | Built in function |
|
|
✅ | v0.3 | Multiple expressions in the same line |
|
List of examples |
✅ | v0.4 | Chained assignment |
|
|
✅ | v0.3 | Sequence slicing |
|
List of examples |
🔜 | backlog | Sequence slicing |
|
|
✅ | v0.3 | Assert |
|
List of examples |
✅ | v0.4 | Try except |
|
List of examples |
✅ | v0.5 | Try except with finally |
|
List of examples |
✅ | v0.4 | Continue, break | ||
🔜 | backlog | Pass | ||
✅ | v0.3 | Import | Support to boa3.builtin packages. |
List of examples |
✅ | v0.8.3 | Import | Support to user created modules. | List of examples |
🔜 | next release | Class |
|
- neo3-boa: Python smart contracts' compiler.
- neo3-mamba: Python SDK for interacting with neo.
- Open a new issue if you encounter a problem.
- Pull requests are welcome. New features, writing tests and documentation are all needed.
- Open-source Apache 2.0.