Lesson 1

Welcome to SmartPy and Tezos Programming

In conclusion, this lesson has introduced you to the Tezos blockchain and SmartPy, the smart contract language for Tezos. You've also written, understood, and tested your first SmartPy contract. This is just the beginning - there's so much more to learn and do with SmartPy and Tezos.

Introduction to Blockchain, Tezos, and SmartPy

Blockchain Basics

Before we can fully understand Tezos and SmartPy, we must first understand the technology that underpins it all: the blockchain. A blockchain is a chain of blocks, with each block containing a list of transactions. Blockchain technology provides a decentralized database, or “digital ledger”, of transactions that all network participants can see. Its architecture guarantees that every transaction is unique, and once it’s recorded in a database, it’s impossible to change.

Introduction to Tezos

Tezos is one such blockchain platform. What sets Tezos apart from many other blockchain platforms, such as Bitcoin or Ethereum, is its emphasis on “self-amendment”, which allows the protocol to upgrade itself without the need for a hard fork. This is a significant benefit as it makes the protocol adaptable and future-proof.

Tezos also offers a platform for smart contracts, which are self-executing contracts with the agreement between buyer and seller directly written into lines of code. This ability to manage and verify digital agreements offers a broad range of potential applications, from financial services to supply chains to decentralized applications (DApps). Read more on Tezos: What Is Tezos? All You Need to Know About XTZ

SmartPy: Smart Contract Language for Tezos

To create smart contracts on Tezos, we use a language called SmartPy. SmartPy is a Python library to develop smart contracts for the Tezos blockchain. It is an intuitive and effective language for expressing contracts and their associated test scenarios.

The most notable feature of SmartPy is its integration with Python, which is one of the world’s most popular and fastest-growing programming languages. If you’re already familiar with Python, you will find SmartPy quite easy to pick up.

Getting Started with SmartPy and Your First Contract

Accessing the SmartPy IDE

SmartPy includes a full-featured Integrated Development Environment (IDE) accessible from your web browser. Go to the SmartPy IDE to start writing your first smart contract.

Writing Your First Contract

In the SmartPy IDE, you will find an editing window where you can input your contract code. Let’s start by writing a basic contract. Copy and paste the following code into the SmartPy editor:

Python
import smartpy as sp

# A SmartPy module
@sp.module
def main():
    # A class of contracts
    class MyContract(sp.Contract):
        def __init__(self, myParameter1, myParameter2):
            self.data.myParameter1 = myParameter1
            self.data.myParameter2 = myParameter2

        # An entrypoint, i.e., a message receiver
        # (contracts react to messages)
        @sp.entrypoint
        def myEntryPoint(self, params):
            assert self.data.myParameter1 <= 123
            self.data.myParameter1 += params

Understanding the Contract

This contract code has several important parts:

  • import smartpy as sp - This imports the SmartPy library so we can use it to write our contract.
  • @sp.module - This decorator tells the SmartPy interpreter that this function will contain a SmartPy contract.
  • class MyContract(sp.Contract): - Here we define a new class (a blueprint for creating new objects) for our contract. It inherits from the sp.Contract superclass, which provides many useful functionalities for handling the contract’s state and defining its behavior.
  • self.data.myParameter1 = myParameter1 and self.data.myParameter2 = myParameter2 - Here we’re defining the initial state of our contract. These two parameters will be passed to the contract when it’s deployed to the blockchain.
  • @sp.entrypoint - This decorator tells the interpreter that the following function (myEntryPoint) is an entry point of our contract. Entry points are how we interact with our contract once it’s been deployed.
  • self.data.myParameter1 += params - This line of code increments myParameter1 by the amount passed to myEntryPoint.

Testing the Contract

A vital part of writing contracts is testing them thoroughly. Testing in SmartPy is integrated into the development process. Here’s how you can add tests to the contract:

Python
# Tests
@sp.add_test(name="Welcome")
def test():
    # We define a test scenario, together with some outputs and checks
    # The scenario takes the module as a parameter
    scenario = sp.test_scenario(main)
    scenario.h1("Welcome")

    # We first define a contract and add it to the scenario
    c1 = main.MyContract(12, 123)
    scenario += c1

    # And call some of its entrypoints
    c1.myEntryPoint(12)
    c1.myEntryPoint(13)
    c1.myEntryPoint(14)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50).run(valid=False)  # this is expected to fail

    # Finally, we check its final storage
    scenario.verify(c1.data.myParameter1 == 151)

    # We can define another contract using the current state of c1
    c2 = main.MyContract(1, c1.data.myParameter1)
    scenario += c2
    scenario.verify(c2.data.myParameter2 == 151)

Running the Contract and Tests

Your full contract now should be like this:

Python
import smartpy as sp

# This is the SmartPy editor.
# You can experiment with SmartPy by loading a template.
# (in the Commands menu above this editor)
#
# A typical SmartPy program has the following form:


# A SmartPy module
@sp.module
def main():
    # A class of contracts
    class MyContract(sp.Contract):
        def __init__(self, myParameter1, myParameter2):
            self.data.myParameter1 = myParameter1
            self.data.myParameter2 = myParameter2

        # An entrypoint, i.e., a message receiver
        # (contracts react to messages)
        @sp.entrypoint
        def myEntryPoint(self, params):
            assert self.data.myParameter1 <= 123
            self.data.myParameter1 += params


# Tests
@sp.add_test(name="Welcome")
def test():
    # We define a test scenario, together with some outputs and checks
    # The scenario takes the module as a parameter
    scenario = sp.test_scenario(main)
    scenario.h1("Welcome")

    # We first define a contract and add it to the scenario
    c1 = main.MyContract(12, 123)
    scenario += c1

    # And call some of its entrypoints
    c1.myEntryPoint(12)
    c1.myEntryPoint(13)
    c1.myEntryPoint(14)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50).run(valid=False)  # this is expected to fail

    # Finally, we check its final storage
    scenario.verify(c1.data.myParameter1 == 151)

    # We can define another contract using the current state of c1
    c2 = main.MyContract(1, c1.data.myParameter1)
    scenario += c2
    scenario.verify(c2.data.myParameter2 == 151)

Once you’ve written your contract and tests, you can run them directly in the IDE. Click the “Run” button in the top right corner of the IDE. The IDE will compile your contract, run the tests, and display the output along with a detailed explanation of each operation and state change.

In conclusion, this lesson has introduced you to the Tezos blockchain and SmartPy, the smart contract language for Tezos. You’ve also written, understood, and tested your first SmartPy contract. This is just the beginning - there’s so much more to learn and do with SmartPy and Tezos. We hope you’re excited to continue on this journey with us in the upcoming lessons!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent Gate Learn.
Catalog
Lesson 1

Welcome to SmartPy and Tezos Programming

In conclusion, this lesson has introduced you to the Tezos blockchain and SmartPy, the smart contract language for Tezos. You've also written, understood, and tested your first SmartPy contract. This is just the beginning - there's so much more to learn and do with SmartPy and Tezos.

Introduction to Blockchain, Tezos, and SmartPy

Blockchain Basics

Before we can fully understand Tezos and SmartPy, we must first understand the technology that underpins it all: the blockchain. A blockchain is a chain of blocks, with each block containing a list of transactions. Blockchain technology provides a decentralized database, or “digital ledger”, of transactions that all network participants can see. Its architecture guarantees that every transaction is unique, and once it’s recorded in a database, it’s impossible to change.

Introduction to Tezos

Tezos is one such blockchain platform. What sets Tezos apart from many other blockchain platforms, such as Bitcoin or Ethereum, is its emphasis on “self-amendment”, which allows the protocol to upgrade itself without the need for a hard fork. This is a significant benefit as it makes the protocol adaptable and future-proof.

Tezos also offers a platform for smart contracts, which are self-executing contracts with the agreement between buyer and seller directly written into lines of code. This ability to manage and verify digital agreements offers a broad range of potential applications, from financial services to supply chains to decentralized applications (DApps). Read more on Tezos: What Is Tezos? All You Need to Know About XTZ

SmartPy: Smart Contract Language for Tezos

To create smart contracts on Tezos, we use a language called SmartPy. SmartPy is a Python library to develop smart contracts for the Tezos blockchain. It is an intuitive and effective language for expressing contracts and their associated test scenarios.

The most notable feature of SmartPy is its integration with Python, which is one of the world’s most popular and fastest-growing programming languages. If you’re already familiar with Python, you will find SmartPy quite easy to pick up.

Getting Started with SmartPy and Your First Contract

Accessing the SmartPy IDE

SmartPy includes a full-featured Integrated Development Environment (IDE) accessible from your web browser. Go to the SmartPy IDE to start writing your first smart contract.

Writing Your First Contract

In the SmartPy IDE, you will find an editing window where you can input your contract code. Let’s start by writing a basic contract. Copy and paste the following code into the SmartPy editor:

Python
import smartpy as sp

# A SmartPy module
@sp.module
def main():
    # A class of contracts
    class MyContract(sp.Contract):
        def __init__(self, myParameter1, myParameter2):
            self.data.myParameter1 = myParameter1
            self.data.myParameter2 = myParameter2

        # An entrypoint, i.e., a message receiver
        # (contracts react to messages)
        @sp.entrypoint
        def myEntryPoint(self, params):
            assert self.data.myParameter1 <= 123
            self.data.myParameter1 += params

Understanding the Contract

This contract code has several important parts:

  • import smartpy as sp - This imports the SmartPy library so we can use it to write our contract.
  • @sp.module - This decorator tells the SmartPy interpreter that this function will contain a SmartPy contract.
  • class MyContract(sp.Contract): - Here we define a new class (a blueprint for creating new objects) for our contract. It inherits from the sp.Contract superclass, which provides many useful functionalities for handling the contract’s state and defining its behavior.
  • self.data.myParameter1 = myParameter1 and self.data.myParameter2 = myParameter2 - Here we’re defining the initial state of our contract. These two parameters will be passed to the contract when it’s deployed to the blockchain.
  • @sp.entrypoint - This decorator tells the interpreter that the following function (myEntryPoint) is an entry point of our contract. Entry points are how we interact with our contract once it’s been deployed.
  • self.data.myParameter1 += params - This line of code increments myParameter1 by the amount passed to myEntryPoint.

Testing the Contract

A vital part of writing contracts is testing them thoroughly. Testing in SmartPy is integrated into the development process. Here’s how you can add tests to the contract:

Python
# Tests
@sp.add_test(name="Welcome")
def test():
    # We define a test scenario, together with some outputs and checks
    # The scenario takes the module as a parameter
    scenario = sp.test_scenario(main)
    scenario.h1("Welcome")

    # We first define a contract and add it to the scenario
    c1 = main.MyContract(12, 123)
    scenario += c1

    # And call some of its entrypoints
    c1.myEntryPoint(12)
    c1.myEntryPoint(13)
    c1.myEntryPoint(14)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50).run(valid=False)  # this is expected to fail

    # Finally, we check its final storage
    scenario.verify(c1.data.myParameter1 == 151)

    # We can define another contract using the current state of c1
    c2 = main.MyContract(1, c1.data.myParameter1)
    scenario += c2
    scenario.verify(c2.data.myParameter2 == 151)

Running the Contract and Tests

Your full contract now should be like this:

Python
import smartpy as sp

# This is the SmartPy editor.
# You can experiment with SmartPy by loading a template.
# (in the Commands menu above this editor)
#
# A typical SmartPy program has the following form:


# A SmartPy module
@sp.module
def main():
    # A class of contracts
    class MyContract(sp.Contract):
        def __init__(self, myParameter1, myParameter2):
            self.data.myParameter1 = myParameter1
            self.data.myParameter2 = myParameter2

        # An entrypoint, i.e., a message receiver
        # (contracts react to messages)
        @sp.entrypoint
        def myEntryPoint(self, params):
            assert self.data.myParameter1 <= 123
            self.data.myParameter1 += params


# Tests
@sp.add_test(name="Welcome")
def test():
    # We define a test scenario, together with some outputs and checks
    # The scenario takes the module as a parameter
    scenario = sp.test_scenario(main)
    scenario.h1("Welcome")

    # We first define a contract and add it to the scenario
    c1 = main.MyContract(12, 123)
    scenario += c1

    # And call some of its entrypoints
    c1.myEntryPoint(12)
    c1.myEntryPoint(13)
    c1.myEntryPoint(14)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50)
    c1.myEntryPoint(50).run(valid=False)  # this is expected to fail

    # Finally, we check its final storage
    scenario.verify(c1.data.myParameter1 == 151)

    # We can define another contract using the current state of c1
    c2 = main.MyContract(1, c1.data.myParameter1)
    scenario += c2
    scenario.verify(c2.data.myParameter2 == 151)

Once you’ve written your contract and tests, you can run them directly in the IDE. Click the “Run” button in the top right corner of the IDE. The IDE will compile your contract, run the tests, and display the output along with a detailed explanation of each operation and state change.

In conclusion, this lesson has introduced you to the Tezos blockchain and SmartPy, the smart contract language for Tezos. You’ve also written, understood, and tested your first SmartPy contract. This is just the beginning - there’s so much more to learn and do with SmartPy and Tezos. We hope you’re excited to continue on this journey with us in the upcoming lessons!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent Gate Learn.