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:
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.
The following will be our first smart contract. Recreate it in your notebook.
def hello_world(): @export def hello(): return 'World!' @export 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:
- 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.
- There is an
- This is to denote that other people can submit a transaction to this method and it will be executed.
addmethod has type annotations but the
- All exported methods must specify the types for their arguments. Private methods do not.
>> ['hello_world', 'submission']
'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