レッスン4

FIFO Contract Creation

In this lesson, we'll delve into a more complex example of SmartPy contracts: a First In, First Out (FIFO) queue. FIFO is a method of organizing and manipulating a data buffer, where the oldest (first) entry or 'head' of the queue, is processed first.

Theory

In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed.

Source: Invostopedia

In the context of smart contracts, implementing a FIFO queue can be useful for many scenarios, such as a fair queueing system where everyone gets served (or processed) in the order they come in.

Practical

Let’s go ahead and write a FIFO contract. The main operations for our contract will be push, to add elements to the queue, and pop, to remove elements from the queue.

The contract stores the queue in a list in its storage, and every push operation appends an element to the end of the list, while every pop operation removes an element from the beginning of the list.

Here’s how the contract might look:

Python
import smartpy as sp


@sp.module
def main():
    # The Fifo class defines a simple contract that handles push and pop instructions
    # on a first-in first-out basis.

    class SimpleFifo(sp.Contract):
        def __init__(self):
            self.data.first = 0
            self.data.last = -1
            self.data.saved = {}

        @sp.entrypoint
        def pop(self):
            assert self.data.first < self.data.last
            del self.data.saved[self.data.first]
            self.data.first += 1

        @sp.entrypoint
        def push(self, element):
            self.data.last += 1
            self.data.saved[self.data.last] = element

        @sp.onchain_view
        def head(self):
            return self.data.saved[self.data.first]


if "templates" not in __name__:

    @sp.add_test(name="Fifo")
    def test():
        scenario = sp.test_scenario(main)
        scenario.h1("Simple Fifo Contract")
        c1 = main.SimpleFifo()
        scenario += c1
        c1.push(4)
        c1.push(5)
        c1.push(6)
        c1.push(7)
        c1.pop()
        scenario.verify(sp.View(c1, "head")() == 5)

To test the FIFO contract:

Step 1: Copy the contract code and paste it into the SmartPy IDE.

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

Step 3: Check the right side of the IDE to see the simulation results. You’ll see the state of the contract storage after each operation.

Step 4: Experiment by changing the order of operations or adding new operations.

You’ve now learned how to create a FIFO contract on the Tezos blockchain! In the next lesson, we’ll go further with recursive views, a powerful feature of SmartPy that lets contracts call their own views. Keep exploring and happy coding!

免責事項
* 暗号資産投資には重大なリスクが伴います。注意して進めてください。このコースは投資アドバイスを目的としたものではありません。
※ このコースはGate Learnに参加しているメンバーが作成したものです。作成者が共有した意見はGate Learnを代表するものではありません。
カタログ
レッスン4

FIFO Contract Creation

In this lesson, we'll delve into a more complex example of SmartPy contracts: a First In, First Out (FIFO) queue. FIFO is a method of organizing and manipulating a data buffer, where the oldest (first) entry or 'head' of the queue, is processed first.

Theory

In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed.

Source: Invostopedia

In the context of smart contracts, implementing a FIFO queue can be useful for many scenarios, such as a fair queueing system where everyone gets served (or processed) in the order they come in.

Practical

Let’s go ahead and write a FIFO contract. The main operations for our contract will be push, to add elements to the queue, and pop, to remove elements from the queue.

The contract stores the queue in a list in its storage, and every push operation appends an element to the end of the list, while every pop operation removes an element from the beginning of the list.

Here’s how the contract might look:

Python
import smartpy as sp


@sp.module
def main():
    # The Fifo class defines a simple contract that handles push and pop instructions
    # on a first-in first-out basis.

    class SimpleFifo(sp.Contract):
        def __init__(self):
            self.data.first = 0
            self.data.last = -1
            self.data.saved = {}

        @sp.entrypoint
        def pop(self):
            assert self.data.first < self.data.last
            del self.data.saved[self.data.first]
            self.data.first += 1

        @sp.entrypoint
        def push(self, element):
            self.data.last += 1
            self.data.saved[self.data.last] = element

        @sp.onchain_view
        def head(self):
            return self.data.saved[self.data.first]


if "templates" not in __name__:

    @sp.add_test(name="Fifo")
    def test():
        scenario = sp.test_scenario(main)
        scenario.h1("Simple Fifo Contract")
        c1 = main.SimpleFifo()
        scenario += c1
        c1.push(4)
        c1.push(5)
        c1.push(6)
        c1.push(7)
        c1.pop()
        scenario.verify(sp.View(c1, "head")() == 5)

To test the FIFO contract:

Step 1: Copy the contract code and paste it into the SmartPy IDE.

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

Step 3: Check the right side of the IDE to see the simulation results. You’ll see the state of the contract storage after each operation.

Step 4: Experiment by changing the order of operations or adding new operations.

You’ve now learned how to create a FIFO contract on the Tezos blockchain! In the next lesson, we’ll go further with recursive views, a powerful feature of SmartPy that lets contracts call their own views. Keep exploring and happy coding!

免責事項
* 暗号資産投資には重大なリスクが伴います。注意して進めてください。このコースは投資アドバイスを目的としたものではありません。
※ このコースはGate Learnに参加しているメンバーが作成したものです。作成者が共有した意見はGate Learnを代表するものではありません。