Introducing SmartPyBasic, a simple CLI to build Tezos smart contract in Python
SmartPy is a Python library, accessible in an online editor SmartPy.io, that comes together with its SmartML backend. It enables developers to define and analyse smart contracts. These smart contracts are compiled into Michelson, the Tezos virtual machine.
This is not the absolute go-to solution to use SmartPy in order to define smart contract on Tezos using Python. Users may want to automate non-regression tests, use their own editors, version control, etc.
We can now use a very simple yet effective tool called SmartPyBasic that brings the SmartPy to Michelson compilation to the desktop.
This is very preliminary and is proposed here to the community due to popular demand.
SmartPyBasic can be installed in a directory, say
~, by typing in a terminal:
sh <(curl -s https://SmartPy.io/SmartPyBasic/SmartPy.sh) local-install ~
It installs a few files:
~/SmartPyBasic/scripts # A list of demo scripts (here, only demo.py)
smartpy.py # The SmartPy library
smartpyio.py # A helper module used by SmartPy, mostly in browser
smartmljs.bc.js # The SmartML library
browser.py # A mock module to simulate a browser
smartpybasic.py # A helper library
SmartPy.sh # A helper shell-script
SmartPy.sh provides several useful entry points to the library.
SmartPyBasic depends on python3 and node.js.
A demo script
There is one demo script in SmartPyBasic:
Its code is the following:
## Copyright 2019 Smart Chain Arena LLC. ### To run this script, we need to setup a PYTHONPATH to the
# SmartPyBasic directory.# If the SmartPyBasic directory is ~/SmartPyBasic, then
# PYTHONPATH=~/SmartPyBasic python3 demo.py
# ~/SmartPyBasic/SmartPy.sh run demo.py
# should work.import smartpy as spclass MyContract(sp.Contract):
def __init__(self, myParameter1, myParameter2):
self.init(myParameter1 = myParameter1,
myParameter2 = myParameter2)
def myEntryPoint(self, params):
sp.verify(self.data.myParameter1 <= 123)
self.data.myParameter1 += params# We evaluate a contract with parameters.
contract = MyContract(12, 13)# We need to export the compile the contract.
# It can be done with the following.
import smartpybasic as spb
targetBaseFilename = /tmp/myContractDemo")print("Contract compiled in /tmp/myContractDemoCode.tz")
Direct use with Python
There are several possibilities, the simple one is to call
python3 on a script after properly setting the PYTHONPATH.
For example, if we installed SmartPyBasic inside
~, we can do:
PYTHONPATH=~/SmartPyBasic python3 ~/SmartPyBasic/scripts/demo.py
ls -ltr /tmp/myContractDemo*
We can also do
~/SmartPyBasic/SmartPy.sh run ~/SmartPyBasic/scripts/demo.py
ls -ltr /tmp/myContractDemo*
It then shows:
-rw-r--r-- ... myContractDemoExpression.smlse
-rw-r--r-- ... myContractDemoStorage.tz
-rw-r--r-- ... myContractDemoCode.tz
These three files are:
.. .smlse: an internal expression between SmartPy and SmartML, kept for the record but not directly useful.
.. .Storage.tz: the Michelson representation of the storage.
.. .Code.tz: the Michelson representation of the code.
The run command
SmartPy is a Python library that also uses some “syntactic sugar”.
In the SmartPy.io editor, one can write:
In pure Python, for example with
python3, these are not allowed.
They are currently replaced by the following constructions:
...with sp.forBlock('x', l) as x:
We can use the
run argument of
SmartPy.sh to recover the syntactic sugar.
~/SmartPyBasic/SmartPy.sh run <mycontract.py>
The local-compile command
Another useful command is
local-compile. It is similar ton
run except that it takes a few extra arguments to call a class defined in a Python script.
~/SmartPyBasic/SmartPy.sh local-compile https://SmartPy.io/demo/templates/tictactoe.py "TicTacToe()" /tmp/tictactoe
It is also useful to directly call templates from SmartPy.io/demo for example.
SmartPyBasic is already quite useful but it is still early stage. We will provide in the future better Python integration in the desktop, better testing framework, etc.