templates.fifo

 1# Fifo - Example for illustrative purposes only.
 2
 3import smartpy as sp
 4
 5
 6@sp.module
 7def main():
 8    # The Fifo class defines a simple contract that handles push and pop instructions
 9    # on a first-in first-out basis.
10
11    class SimpleFifo(sp.Contract):
12        def __init__(self):
13            self.data.first = 0
14            self.data.last = -1
15            self.data.saved = sp.cast({}, sp.map[sp.int, sp.int])
16
17        @sp.entrypoint
18        def pop(self):
19            assert self.data.first < self.data.last
20            del self.data.saved[self.data.first]
21            self.data.first += 1
22
23        @sp.entrypoint
24        def push(self, element):
25            self.data.last += 1
26            self.data.saved[self.data.last] = element
27
28        @sp.onchain_view
29        def head(self):
30            return self.data.saved[self.data.first]
31
32
33if "main" in __name__:
34
35    @sp.add_test()
36    def test():
37        scenario = sp.test_scenario("Fifo", main)
38        scenario.h1("Simple Fifo Contract")
39        c1 = main.SimpleFifo()
40        scenario += c1
41        c1.push(4)
42        c1.push(5)
43        c1.push(6)
44        c1.push(7)
45        c1.pop()
46        # FIXME scenario.verify(sp.View(c1, "head")() == 5)