Playing with SmartPy Smart Contracts: Part. 1

Mathis Selvi
Tezos Israel
Published in
2 min readAug 24, 2020

SmartPy was designed for lowering the barrier of entry for smart contracts development. As you can most likely guess from the name, it is based on Python, which is, let’s be honest, not the hardest coding language to learn. Great news, right? It helps us developing Tezos smart contracts in an easier way.
In this article, we’re going to start using SmartPy.io to create a smart contract and play around with it. For this, we will be using their online editor, as it is very clean and gives you access to a lot of different features, which makes it easy to develop and interact with a smart contract.

Coding our first smart contract!

Let me say it right now: the smart contract we will be coding today won’t be too complicated. Indeed, our goal here is to learn how to use SmartPy to create a smart contract, right, but also to use their online editor and some of the features it offers (such as testing, for example).

Basically, our smart contract will be some sort of a very basic crowdfunding system. People will be able to send any amount to the contract, and the owner will then be able to “cash out” whenever he wants, which will transfer all the tez in the contract to his address.

If needed, here is the link to the full smart contract: here. It contains the tests which we will cover in the next part.

Anyway, let’s kick things off by adding our first line of code, which is importing the library SmartPy.

import smartpy as sp

With SmartPy, a contract is basically a class definition which inherits from sp.Contract, such as:

class BasicCrowdfunding(sp.Contract):

Here, our class is named BasicCrowdfunding. Our class then needs a __init__ constructor which will call self.init() in order to, as we can see from the name of the function, initialize all the fields we have in our contract’s storage.

def __init__(self, owner):
self.init(
admin = owner,
contributedAmount = sp.tez(0)
)

We’re initializing two fields: admin and contributedAmount. We assign owner, which we get from parameters, to admin. Basically, it contains the tz1 address of the contract’s owner. Then, we set contributedAmount to sp.tez(0), representing 0 tez. contributedAmount is here to track the amount of tez that was contributed to the crowdfunding (0 when we’re initiating the contract).

Before continuing, here is how it looks so far:

import smartpy as spclass BasicCrowdfunding(sp.Contract):
def __init__(self, owner):
self.init(
admin = owner,
contributedAmount = sp.tez(0)
)

We’re done with the first part. Now we need to add entry points so that we can call the contract from outside.

But, what are entry points?!

To read more click here.

--

--

Mathis Selvi
Tezos Israel

🛠️ Working on Tezos 👟 Fashion Enthusiast 💿 Music Lover