Quick Start – Your First Contract

For this quick ‘how-to’, we will be using Jupyter Notebook. Jupyter is a great tool for Python programmers to develop and explore in as they combine the high feedback of a REPL with the presentation and saving of a program.

If you are a Python programmer, chances are you already have Jupyter installed. If not, follow this guide to get started. After that, just start the notebook:

jupyter notebook

Import the Client #

Contracting has a super high level client that allows you to develop smart contracts without any knowledge of the underlying mechanics of the execution system. This makes it perfect for new comers to the library.

from contracting.client import ContractingClient
client = ContractingClient()

If initializing the client hangs, that means your database is not running and Contracting can’t connect to where it stores data.

Hello, World! #

The following will be our first smart contract. Recreate it in your notebook.

def hello_world():
    def hello():
        return 'World!'

    def add(a: int, b: int):
        return private_add(a, b)

    def private_add(a, b):
        return a + b

Off the bat, notice three things:

  1. The smart contract is a closure (a method inside of a method).
    • This is so you can test and develop in any Python IDE you prefer (PyCharm, VSCode, Sublime Text, etc) and push the contract live manually when you are done.
  2. There is an @export decorator.
    • This is to denote that other people can submit a transaction to this method and it will be executed.
  3. The add method has type annotations but the private_add does not.
    • All exported methods must specify the types for their arguments. Private methods do not.
>> ['hello_world', 'submission']

If the 'hello_world' contract now appears in the returned list, you’ve successfully submitted your first smart contract. Congrats!

NOTE: For submitting closures, the name of the contract is automatically taken from the name of the closure. def my_func(): becomes my_func in the smart contract state space, etc. You can override this by passing the name argument into submit.

Updated on February 3, 2021