บทเรียนที่ 3

Building a Smart Contract Calculator

In this lesson, we will continue to expand your understanding of SmartPy and the way it interacts with the Tezos blockchain by creating a basic calculator. This contract will have functions for addition, subtraction, multiplication, and division.

Theory

Smart contracts on Tezos can have multiple entry points, which can be seen as methods or functions in object-oriented programming. Each of these entry points can have its own parameters and can interact with the contract’s storage. In our calculator contract, each mathematical operation will be an entry point.

It’s crucial to note that all modifications of the storage are recorded on the blockchain. As a result, the operations we perform are not transient like they would be in a regular calculator. Instead, they’re immutable and auditable operations on the Tezos blockchain.

Also, it’s worth remembering that since the Tezos blockchain is decentralized, all computations should be deterministic. This means operations like division might work slightly differently than you’re used to. For instance, division in Tezos contracts is integer division, so 3 divided by 2 would give 1, not 1.5.

Practical

Below is the contract code for the calculator. The Calculator contract stores the result of operations in its storage. Each entry point takes one parameter and performs the operation with the stored result and the input parameter.

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):
    assert x > 0, "Input must be greater than 0"

    self.data.result = 0  # Initialize the counter

    if x < 1:  # For the 0 < x < 1 interval
        y = x
        while y < 1:
            self.data.result -= 1  # Decrement the counter
            y *= 2  # Multiply y by 2 until y >= 1
    else:  # For x >= 1
        y = x
        while y >= 2:
            self.data.result += 1  # Increment the counter
            y /= 2  # Divide y by 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)

Let’s put this contract into action!

Step 1: Paste the contract code into the SmartPy IDE.

Step 2: Click on the Run button on the top-right to compile and simulate the contract.

Step 3: Observe the simulation results on the right side of the IDE. You can see the contract storage’s state after each operation, like multiply, add, square root, etc

Step 4: Feel free to modify the parameters for the operations and observe the changes in contract storage!

You’ve now built and interacted with a smart contract that performs basic calculator operations! In the next lesson, we’ll be looking at more advanced concepts like FIFO contract creation. Remember to keep exploring, and happy coding!

ข้อจำกัดความรับผิด
* การลงทุนคริปโตมีความเสี่ยงสูง โปรดดำเนินการด้วยความระมัดระวัง หลักสูตรนี้ไม่ได้มีไว้เพื่อเป็นคำแนะนำในการลงทุน
* หลักสูตรนี้สร้างขึ้นโดยผู้เขียนที่ได้เข้าร่วม Gate Learn ความคิดเห็นของผู้เขียนไม่ได้มาจาก Gate Learn
แคตตาล็อก
บทเรียนที่ 3

Building a Smart Contract Calculator

In this lesson, we will continue to expand your understanding of SmartPy and the way it interacts with the Tezos blockchain by creating a basic calculator. This contract will have functions for addition, subtraction, multiplication, and division.

Theory

Smart contracts on Tezos can have multiple entry points, which can be seen as methods or functions in object-oriented programming. Each of these entry points can have its own parameters and can interact with the contract’s storage. In our calculator contract, each mathematical operation will be an entry point.

It’s crucial to note that all modifications of the storage are recorded on the blockchain. As a result, the operations we perform are not transient like they would be in a regular calculator. Instead, they’re immutable and auditable operations on the Tezos blockchain.

Also, it’s worth remembering that since the Tezos blockchain is decentralized, all computations should be deterministic. This means operations like division might work slightly differently than you’re used to. For instance, division in Tezos contracts is integer division, so 3 divided by 2 would give 1, not 1.5.

Practical

Below is the contract code for the calculator. The Calculator contract stores the result of operations in its storage. Each entry point takes one parameter and performs the operation with the stored result and the input parameter.

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):
    assert x > 0, "Input must be greater than 0"

    self.data.result = 0  # Initialize the counter

    if x < 1:  # For the 0 < x < 1 interval
        y = x
        while y < 1:
            self.data.result -= 1  # Decrement the counter
            y *= 2  # Multiply y by 2 until y >= 1
    else:  # For x >= 1
        y = x
        while y >= 2:
            self.data.result += 1  # Increment the counter
            y /= 2  # Divide y by 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)

Let’s put this contract into action!

Step 1: Paste the contract code into the SmartPy IDE.

Step 2: Click on the Run button on the top-right to compile and simulate the contract.

Step 3: Observe the simulation results on the right side of the IDE. You can see the contract storage’s state after each operation, like multiply, add, square root, etc

Step 4: Feel free to modify the parameters for the operations and observe the changes in contract storage!

You’ve now built and interacted with a smart contract that performs basic calculator operations! In the next lesson, we’ll be looking at more advanced concepts like FIFO contract creation. Remember to keep exploring, and happy coding!

ข้อจำกัดความรับผิด
* การลงทุนคริปโตมีความเสี่ยงสูง โปรดดำเนินการด้วยความระมัดระวัง หลักสูตรนี้ไม่ได้มีไว้เพื่อเป็นคำแนะนำในการลงทุน
* หลักสูตรนี้สร้างขึ้นโดยผู้เขียนที่ได้เข้าร่วม Gate Learn ความคิดเห็นของผู้เขียนไม่ได้มาจาก Gate Learn