Installation
This chapter aims to prepare you and your working environment to be ready to code, test, and compile your smart contracts.
You can code locally on your favorite IDE or use the official SmartPy online editor, with added interesting features to facilitate development.
Write, test, and compile from the Online Editor
The SmartPy online editor is not just a simple text editor for smart contracts. It comes with a built-in simulation suite, which provides powerful testing tools for developers.
FIGURE 1: Smartpy.io Online EditorTo familiarize yourself with SmartPy, you can choose among numerous existing examples of smart contracts, by going to the Templates tab.
FIGURE 2: Smartpy.io Online Editor TemplatesOn the left screen of the online editor, you can:
- Write your code
- Run and test your code
Once you run your code, you can visualize the result on the right screen, which contains:
- Your inlined python code
- Your generated Michelson contract and storage (also available in JSON format)
- A summary of your test scenario and the generated Michelson parameter code
- An interface to deploy your Michelson contract on a testnet or the mainnet
Write, test and, compile from your local IDE
In the following chapters, we will use the online editor. But in this section, we will still provide the basics for using SmartPy on a local IDE.
Prerequisite
Create a virtual environment
A virtual environment is a self-contained Python installation, separated from the global Python installation. It contains its own modules. Hence, it is most useful when a specific module version is needed without affecting the other modules. Run this command to create a virtual environment:
python3 -m venv /path/to/env
Activate the environment
By default, your OS uses the python interpreter from /usr/bin/python
. Once a virtual environment is created, it has to be activated in order to be used by the OS. You can activate your virtual environment by running:
source /path/to/env/bin/activate
The name of the environment should appear at the beginning of the command line. It can be deactivated by running:
(venv) $ deactivate
Install the command line interface
To install the SmartPy CLI, run:
sh <(curl -s https://smartpy.io/cli/install.sh)
Compile SmartPy Contracts or Expressions
Use the SmartPy.sh
script to compile a SmartPy smart contract:
~/smartpy-cli/SmartPy.sh compile <contract.py> <output-directory>
Compilation produces multiple possible outputs including:
- Michelson code for contract and storage
- JSON code for contract and storage
It is referring to Michelson source code in JSON representation.
Execute the tests of your SmartPy Script
Use the SmartPy.sh
script to run the tests:
~/smartpy-cli/SmartPy.sh test <contract.py> <output-directory>
This leads to multiple outputs: types of storage values and entrypoint parameters, generated Michelson code, pretty-printed scenario, etc.
Conclusion
Installing an environment for programming with SmartPy is easy to do. You just need Python and the necessary libraries. Or you can simply start coding in the online editor.
References
[1] https://smartpy.io/reference.html#_command_line_interface