Урок 1

SmartPy和Tezos編程入門

本課程介紹了Tezos區塊鏈及其智能合約語言SmartPy。我們編寫、學習併測試了自己的第一個SmartPy合約。這隻是一個開始,關於SmartPy和Tezos,還有很多需要學習和探索的東西。

區塊鏈、Tezos和SmartPy介紹

1.1 區塊鏈基礎

在全麵了解Tezos和SmartPy之前,我們必鬚先了解支撐所有這些技術的底層技術——區塊鏈。區塊鏈是由衆多區塊組成的鏈式結構,每個區塊包含一個交易列錶。區塊鏈技術提供了一個去中心化的交易數據庫,即“數字分類賬”,對所有網絡參與者可見。它的架構保證每筆交易都是唯一的,一旦記録在數據庫中,便無法更改。

1.2 Tezos介紹

Tezos是一個典型的區塊鏈平颱,它與衆多其他區塊鏈平颱(如比特幣或以太坊)的區別在於其強調“自我修正”,允許協議在不進行硬分叉的情況下進行升級。這是一個重大優勢,使協議具有適應性且不會過時。

Tezos還提供了一個智能合約平颱。智能合約是一種自動執行的合約,買賣雙方之間的協議直接通過代碼編寫。這種管理和驗證數字協議的能力使Tezos在諸多領域具有潛在應用,包括金融服務、供應鏈、去中心化應用(DApp)等。拓展:Tezos公鏈是什麽?爲何它在NFT方麵的建樹值得關註

1.3 SmartPy:Tezos的智能合約語言

要在Tezos上創建智能合約,我們使用一種叫做SmartPy的語言。SmartPy是一個用於開髮Tezos區塊鏈智能合約的Python庫。它是一種直觀有效的語言,用於體現合約及相關的測試場景。

SmartPy最顯著的特點是它與Python的整合,Python是世界上使用最廣且髮展最快的編程語言之一。如果你對Python已經有一定了解,那麽學習SmartPy會相對容易。

用SmartPy創建你的第一份合約

訪問SmartPy IDE

SmartPy包含一個功能齊全的集成開髮環境(IDE),可通過瀏覽器訪問。進入SmartPy IDE後,便可以開始編寫你的第一個智能合約了。

編寫智能合約

在SmartPy IDE中,找到編輯窗口,輸入合約代碼。我們首先來編寫一個基本的合約。覆製以下代碼併粘貼到SmartPy編輯器中:

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

合約解析

該合約代碼包含以下幾個重要部分:

  • import smartpy as sp- 此代碼將導入SmartPy庫,我們可以用它來編寫合約。
  • @sp.module- 此裝飾器告知SmartPy解釋器,這個函數將包含一個SmartPy合約。
  • class MyContract(sp.Contract):- 在這裡,我們爲合約定義一個新類(用於創建新對象的藍圖)。它繼承自sp.Contract超類,該超類提供了許多有用的功能,用於處理合約的狀態併定義其行爲。
  • self.data.myParameter1 = myParameter1self.data.myParameter2 = myParameter2- 這裡我們定義了合約的初始狀態。這兩個參數將在部署到區塊鏈時傳遞給合約。
  • @sp.entrypoint- 此裝飾器告知解釋器,函數myEntryPoint是合約的入口點。入口點是我們部署合約後與合約交互的方式。
  • self.data.myParameter1 += params- 此行代碼將myParameter1的值增加一定數量,此數量是傳給myEntryPoint的量。

合約測試

編寫合約的一個重要內容是對其進行徹底的測試。在SmartPy中,測試與開髮過程相融合。使用以下代碼將測試添加到合約中:

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)

合約運行

編寫好的完整合約代碼應如下所示:

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)

編寫好合約和測試代碼後,可以直接在IDE中運行它們。單擊IDE右上角的“Run”按鈕。IDE將對合約進行編譯,運行測試,併顯示輸出結果以及每個操作和狀態變化的詳細説明。

總的來説,本課程介紹了Tezos區塊鏈及其智能合約語言SmartPy。我們編寫、學習併測試了自己的第一個SmartPy合約。這隻是一個開始,關於SmartPy和Tezos,還有很多需要學習和探索的東西。不要走開,繼續跟我們進行這場探索之旅吧!

Отказ от ответственности
* Криптоинвестирование сопряжено со значительными рисками. Будьте осторожны. Курс не является инвестиционным советом.
* Курс создан автором, который присоединился к Gate Learn. Мнение автора может не совпадать с мнением Gate Learn.
Каталог
Урок 1

SmartPy和Tezos編程入門

本課程介紹了Tezos區塊鏈及其智能合約語言SmartPy。我們編寫、學習併測試了自己的第一個SmartPy合約。這隻是一個開始,關於SmartPy和Tezos,還有很多需要學習和探索的東西。

區塊鏈、Tezos和SmartPy介紹

1.1 區塊鏈基礎

在全麵了解Tezos和SmartPy之前,我們必鬚先了解支撐所有這些技術的底層技術——區塊鏈。區塊鏈是由衆多區塊組成的鏈式結構,每個區塊包含一個交易列錶。區塊鏈技術提供了一個去中心化的交易數據庫,即“數字分類賬”,對所有網絡參與者可見。它的架構保證每筆交易都是唯一的,一旦記録在數據庫中,便無法更改。

1.2 Tezos介紹

Tezos是一個典型的區塊鏈平颱,它與衆多其他區塊鏈平颱(如比特幣或以太坊)的區別在於其強調“自我修正”,允許協議在不進行硬分叉的情況下進行升級。這是一個重大優勢,使協議具有適應性且不會過時。

Tezos還提供了一個智能合約平颱。智能合約是一種自動執行的合約,買賣雙方之間的協議直接通過代碼編寫。這種管理和驗證數字協議的能力使Tezos在諸多領域具有潛在應用,包括金融服務、供應鏈、去中心化應用(DApp)等。拓展:Tezos公鏈是什麽?爲何它在NFT方麵的建樹值得關註

1.3 SmartPy:Tezos的智能合約語言

要在Tezos上創建智能合約,我們使用一種叫做SmartPy的語言。SmartPy是一個用於開髮Tezos區塊鏈智能合約的Python庫。它是一種直觀有效的語言,用於體現合約及相關的測試場景。

SmartPy最顯著的特點是它與Python的整合,Python是世界上使用最廣且髮展最快的編程語言之一。如果你對Python已經有一定了解,那麽學習SmartPy會相對容易。

用SmartPy創建你的第一份合約

訪問SmartPy IDE

SmartPy包含一個功能齊全的集成開髮環境(IDE),可通過瀏覽器訪問。進入SmartPy IDE後,便可以開始編寫你的第一個智能合約了。

編寫智能合約

在SmartPy IDE中,找到編輯窗口,輸入合約代碼。我們首先來編寫一個基本的合約。覆製以下代碼併粘貼到SmartPy編輯器中:

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

合約解析

該合約代碼包含以下幾個重要部分:

  • import smartpy as sp- 此代碼將導入SmartPy庫,我們可以用它來編寫合約。
  • @sp.module- 此裝飾器告知SmartPy解釋器,這個函數將包含一個SmartPy合約。
  • class MyContract(sp.Contract):- 在這裡,我們爲合約定義一個新類(用於創建新對象的藍圖)。它繼承自sp.Contract超類,該超類提供了許多有用的功能,用於處理合約的狀態併定義其行爲。
  • self.data.myParameter1 = myParameter1self.data.myParameter2 = myParameter2- 這裡我們定義了合約的初始狀態。這兩個參數將在部署到區塊鏈時傳遞給合約。
  • @sp.entrypoint- 此裝飾器告知解釋器,函數myEntryPoint是合約的入口點。入口點是我們部署合約後與合約交互的方式。
  • self.data.myParameter1 += params- 此行代碼將myParameter1的值增加一定數量,此數量是傳給myEntryPoint的量。

合約測試

編寫合約的一個重要內容是對其進行徹底的測試。在SmartPy中,測試與開髮過程相融合。使用以下代碼將測試添加到合約中:

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)

合約運行

編寫好的完整合約代碼應如下所示:

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)

編寫好合約和測試代碼後,可以直接在IDE中運行它們。單擊IDE右上角的“Run”按鈕。IDE將對合約進行編譯,運行測試,併顯示輸出結果以及每個操作和狀態變化的詳細説明。

總的來説,本課程介紹了Tezos區塊鏈及其智能合約語言SmartPy。我們編寫、學習併測試了自己的第一個SmartPy合約。這隻是一個開始,關於SmartPy和Tezos,還有很多需要學習和探索的東西。不要走開,繼續跟我們進行這場探索之旅吧!

Отказ от ответственности
* Криптоинвестирование сопряжено со значительными рисками. Будьте осторожны. Курс не является инвестиционным советом.
* Курс создан автором, который присоединился к Gate Learn. Мнение автора может не совпадать с мнением Gate Learn.