Skip to content

CityOfZion/python-smart-contract-workshop

Repository files navigation

Links

If you have any issues or ideas for improvements, please leave your feedback on the GitHub repository and on the NEO Discord.

Steps in the workshop

  1. Setup neo-python and a neo-privatenet Docker container optionally with neoscan
  2. First smart contract, just printing "Hello World": 1-print.py
    • Learn using neo-python's build command with the test argument
    • Test differences between Log and Notify
  3. First smart contract using print, Runtime.Log and Runtime.Notify: 2-print-and-notify.py
    • Learn using neo-python's build command with the test argument
    • Test differences between Log and Notify
  4. Basic smart contract using storage: 3-storage.py
    • Storage is one of the key components of most smart contracts
    • Everything is handled as bytes
    • Learn about debugstorage on/off/reset
  5. Check out Dictionary support and neo.Runtime.Serialize
  6. A domain registration smart contract: 4-domain.py
    • users can query, register, transfer and delete domains
    • important concept: checking of ownership
  7. NEX ICO template: https://github.com/neonexchange/neo-ico-template

Note: Inside neo-python's prompt.py you need to run config sc-events on to see any kind of notifications of the examples!

Recommended System Setup

Linux or Mac is recommended, and you need Python 3.6+. If you are using Windows, either setup a VM or use the Linux Subsystem (see also here for more infos).

Clone neo-python and setup everything as described in the README. Then create a symlink of this workshop folder to neo-python/sc, which makes it easier to import, build and execute the smart contracts in this workshop.

Always work with a private network with this Docker image: https://hub.docker.com/r/cityofzion/neo-privatenet You can also easily run the private network with neoscan - just use this Docker compose file:

$ wget https://raw.githubusercontent.com/slipo/neo-scan-docker/master/docker-compose.yml -O docker-compose-neoscan.yml
$ docker-compose -f docker-compose-neoscan.yml up

See here for more information.

Quickstart

# Clone the workshop repository
git clone https://github.com/CityOfZion/python-smart-contract-workshop.git
cd python-smart-contract-workshop

# Pull the Docker image
docker pull cityofzion/neo-privatenet

# Start a private network
docker run --rm -d --name neo-privatenet -p 20333-20336:20333-20336/tcp -p 30333-30336:30333-30336/tcp cityofzion/neo-privatenet

# Download the private network wallet
wget https://s3.amazonaws.com/neo-experiments/neo-privnet.wallet

# Create a Python 3.6 virtual environment and activate it
python3.6 -m venv venv
. venv/bin/activate

# Install neo-python
pip install neo-python

# Remove any old private chain database
rm -rf ~/.neopython/Chains/privnet*

# Start neo-python connected to the private net (-p), showing sc events (-v)
np-prompt -p -v

Typical method signatures

# These two are just examples for playing around and experimenting:
def Main():
def Main(operation):

# This is how most real smart contracts look like:
def Main(operation, args):

See also: parameter & return value types

Often used imports

from boa.interop.Neo.Runtime import Log, Notify
from boa.interop.Neo.Storage import Get, Put, GetContext
from boa.interop.Neo.Runtime import GetTrigger,CheckWitness
from boa.builtins import concat, list, range, take, substr

Often used build commands

neo> build sc/1-print.py test 07 05 True False
neo> build sc/2-print-and-notify.py test 07 05 True False
neo> build sc/3-storage.py test 07 05 True False
neo> build sc/4-domain.py test 0710 05 True False query ["test.com"]

Useful code snippets

Timestamps

You can get the last block timestamp from the blockchain with this code.

def now():
    height = GetHeight()
    current_block = GetHeader(height)
    return current_block.Timestamp

Might not work with neo-boa 0.2.2, downgrade to 0.2.1 (see also CityOfZion/neo-boa#35).

Random numbers

See https://medium.com/proof-of-working/coz-first-dapps-competition-dapp-review-3a6b284afaef#414c

About

Workshop for programming smart contracts with Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages