Lição 3

构建智能合约计算器

在本课中,我们将继续学习SmartPy及其与Tezos区块链的交互方式,创建一个基本的计算器。该合约将具有加法、减法、乘法和除法功能。

理论

Tezos上的智能合约可以有多个入口点,入口点可以看作是面向对象编程中的方法或函数。每个入口点都可以有自己的参数,并且可以与合约的存储进行交互。在我们的计算器合约中,每个数学运算都将是一个入口点。

需要注意的是,对存储数据的任何修改都将记录在区块链上。因此,我们执行的运算不像常规计算器中那样只能短暂存储。在Tezos区块链上,这些运算是不可篡改且可审计的。

此外,需要注意的是,由于Tezos区块链是去中心化的,所有计算都应该是确定性的。因此,像除法这样的运算可能与你所熟悉的稍有不同。在Tezos合约中,除法是整数除法,因此3除以2将得到1,而不是1.5。

实操

下面是该计算器合约的代码。Calculator合约将运算结果保存在其存储空间中。每个入口点都将接收一个参数,并使用存储结果和输入参数执行运算。

Python
import smartpy as sp


@sp.module
def main():
    class Calculator(sp.Contract):
        def __init__(self):
            self.data.result = 0

        @sp.entrypoint
        def multiply(self, x, y):
            self.data.result = x * y

        @sp.entrypoint
        def add(self, x, y):
            self.data.result = x + y

        @sp.entrypoint
        def square(self, x):
            self.data.result = x * x

        @sp.entrypoint
        def squareRoot(self, x):
            assert x >= 0
            y = x
            while y * y > x:
                y = (x / y + y) / 2
            assert y * y <= x and x < (y + 1) * (y + 1)
            self.data.result = y

        @sp.entrypoint
        def factorial(self, x):
            self.data.result = 1
            for y in range(1, x + 1):
                self.data.result *= y

        @sp.entrypoint
        def log2(self, x):
            self.data.result = 0
            y = x
            while y > 1:
                self.data.result += 1
                y /= 2


if "templates" not in __name__:

    @sp.add_test(name="Calculator")
    def test():
        c1 = main.Calculator()
        scenario = sp.test_scenario(main)
        scenario.h1("Calculator")
        scenario += c1
        c1.multiply(x=2, y=5)
        c1.add(x=2, y=5)
        c1.add(x=2, y=5)
        c1.square(12)
        c1.squareRoot(0)
        c1.squareRoot(1234)
        c1.factorial(100)
        c1.log2(c1.data.result)
        scenario.verify(c1.data.result == 524)

我们来实际操作一下如何实现这份合约!

第一步:将合约代码粘贴到SmartPy IDE中。

第二步:点击右上角的Run按钮来编译并模拟合约。

第三步:观察IDE右侧的模拟结果。你可以看到每次运算(如乘法、加法、平方根等)后合约存储的状态。

第4步:任意修改运算参数并观察合约存储的变化!

现在,你已经学会了构建和运行计算器智能合约,该合约能够执行基本的运算!在下一课中,我们将学习更高级的概念,如FIFO合约创建。最后,再次勉励大家,继续探索,开启愉快的编程之旅!

Exclusão de responsabilidade
* O investimento em criptomoedas envolve riscos significativos. Prossiga com cuidado. O curso não pretende ser um conselho de investimento.
* O curso é criado pelo autor que se juntou ao Gate Learn. Qualquer opinião partilhada pelo autor não representa o Gate Learn.
Catálogo
Lição 3

构建智能合约计算器

在本课中,我们将继续学习SmartPy及其与Tezos区块链的交互方式,创建一个基本的计算器。该合约将具有加法、减法、乘法和除法功能。

理论

Tezos上的智能合约可以有多个入口点,入口点可以看作是面向对象编程中的方法或函数。每个入口点都可以有自己的参数,并且可以与合约的存储进行交互。在我们的计算器合约中,每个数学运算都将是一个入口点。

需要注意的是,对存储数据的任何修改都将记录在区块链上。因此,我们执行的运算不像常规计算器中那样只能短暂存储。在Tezos区块链上,这些运算是不可篡改且可审计的。

此外,需要注意的是,由于Tezos区块链是去中心化的,所有计算都应该是确定性的。因此,像除法这样的运算可能与你所熟悉的稍有不同。在Tezos合约中,除法是整数除法,因此3除以2将得到1,而不是1.5。

实操

下面是该计算器合约的代码。Calculator合约将运算结果保存在其存储空间中。每个入口点都将接收一个参数,并使用存储结果和输入参数执行运算。

Python
import smartpy as sp


@sp.module
def main():
    class Calculator(sp.Contract):
        def __init__(self):
            self.data.result = 0

        @sp.entrypoint
        def multiply(self, x, y):
            self.data.result = x * y

        @sp.entrypoint
        def add(self, x, y):
            self.data.result = x + y

        @sp.entrypoint
        def square(self, x):
            self.data.result = x * x

        @sp.entrypoint
        def squareRoot(self, x):
            assert x >= 0
            y = x
            while y * y > x:
                y = (x / y + y) / 2
            assert y * y <= x and x < (y + 1) * (y + 1)
            self.data.result = y

        @sp.entrypoint
        def factorial(self, x):
            self.data.result = 1
            for y in range(1, x + 1):
                self.data.result *= y

        @sp.entrypoint
        def log2(self, x):
            self.data.result = 0
            y = x
            while y > 1:
                self.data.result += 1
                y /= 2


if "templates" not in __name__:

    @sp.add_test(name="Calculator")
    def test():
        c1 = main.Calculator()
        scenario = sp.test_scenario(main)
        scenario.h1("Calculator")
        scenario += c1
        c1.multiply(x=2, y=5)
        c1.add(x=2, y=5)
        c1.add(x=2, y=5)
        c1.square(12)
        c1.squareRoot(0)
        c1.squareRoot(1234)
        c1.factorial(100)
        c1.log2(c1.data.result)
        scenario.verify(c1.data.result == 524)

我们来实际操作一下如何实现这份合约!

第一步:将合约代码粘贴到SmartPy IDE中。

第二步:点击右上角的Run按钮来编译并模拟合约。

第三步:观察IDE右侧的模拟结果。你可以看到每次运算(如乘法、加法、平方根等)后合约存储的状态。

第4步:任意修改运算参数并观察合约存储的变化!

现在,你已经学会了构建和运行计算器智能合约,该合约能够执行基本的运算!在下一课中,我们将学习更高级的概念,如FIFO合约创建。最后,再次勉励大家,继续探索,开启愉快的编程之旅!

Exclusão de responsabilidade
* O investimento em criptomoedas envolve riscos significativos. Prossiga com cuidado. O curso não pretende ser um conselho de investimento.
* O curso é criado pelo autor que se juntou ao Gate Learn. Qualquer opinião partilhada pelo autor não representa o Gate Learn.