Introducing SmartPyBasic, a simple CLI to build Tezos smart contract in Python
This document is now outdated. Please refer to our reference manual and installation instructions.
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.
This document is now outdated. Please refer to our reference manual and installation instructions.
Installation
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
smartmlbasic.js # A node JavaScript file to interact with SmartML
smartpybasic.py # A helper library
SmartPy.sh # A helper shell-script
SmartPy.sh provides several useful entry points to the library.
Dependencies
SmartPyBasic depends on python3 and node.js.
A demo script
There is one demo script in SmartPyBasic: scripts/demo.py
.
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
# or
# ~/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)
@sp.entryPoint
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
spb.compileContract(contract,
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:
sp.if
, sp.else
, sp.for
, and sp.while
.
In pure Python, for example with python3
, these are not allowed.
They are currently replaced by the following constructions:
with sp.ifBlock(condition):
...with sp.elseBlock():
...with sp.forBlock('x', l) as x:
...with sp.whileBlock(condition):
...
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.
Going further
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.