In this article, you’ll be learning to write smart contract using SmartPy. Smart contracts are programs stored on the blockchain that is executed when a predefined condition on code is met. The key difference between smart contracts and normal code is that smart contracts interact with the blockchain, enabling transactions in a fully decentralized and trustless environment.
There are several high-level languages for writing smart contracts on Tezos, but Michelson is a domain-specific language on Tezos for writing smart contracts. Smartpy and Ligo are the most popular languages developers use, but the code written in these high-level languages is ultimately compiled to Michelson.
In this article, you’ll be using SmartPy to write a smart contract.
This article is the 2nd part of our series on Tezos, read the 1st part Introduction to Tezos here.
Table of contents What is SmartPy?SmartPy is an intuitive and powerful smart contract development platform for Tezos. Furthermore, it is based on python; so, if you have prior experience with python, it will be useful; otherwise, you can refer here.
The contract will store the user data (name, gender, and score), and there will be two entry points:
Go to SmartPy IDE and follow the tutorial below:
Step to write smart contract using SmartPy First Step: Initialize contract storageLet’s get started with importing the Smartpy library.
import smartpy as spIn Smartpy, a contract is defined by a class that inherits from sp.Contract.
class User(sp.Contract): def __init__(self): passThe first step in creating a contract is defining its storage. The storage can be interpreted as a database in web2.0. The data is stored on storage, and it defines the state of the contract.
You have to define it using self.init() function inside the constructor.
In this contract, you have to create a map() for storing data of all the users.
import smartpy as sp class User(sp.Contract): # Constructor def __init__(self): self.init(userMap = sp.map()) Second Step: Create entry pointThe next step is to create an entry point. An entry point can be invoked from outside and can change the contract’s storage. For creating an entry point, it has to be decorated with @sp.entry_point decorator.
In this contract, you have to create two entry points:
In this entry point, you have to store a record of name , gender and score in the userMap The data is passed in param object. The contract storage is accessed using self.data
This entry point is updating the user’s score by adding the incoming score to the existing score of the user.
Third Step: Write tests for smart contract on SmartPyOnce you are done writing the entry points, the next step is to write some tests for the operations mentioned above in the respective entry points. Tests are important to ensure if the contract is working fine before deploying it to the testnet.
The complete smart contract can be found here.
Fourth Step: Run the smart contract on SmartPyCongratulations! you are done with writing your smart contract. Next, run it to view the test results.
On clicking the Run button, it displays the test output on the right as shown in the image below:
This was the second article in the series of four articles listed below:
Since now you have a basic understanding of what is Tezos blockchain and writing smart contracts using SmartPy, in the next part, you’ll be learning to deploy and explore smart contracts.
All Rights Reserved. Copyright , Central Coast Communications, Inc.