الدرس رقم 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. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.
الكتالوج
الدرس رقم 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. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.