templates.test_view_inheritance

 1# Store Value - Example for illustrative purposes only.
 2
 3import smartpy as sp
 4
 5
 6@sp.module
 7def main():
 8    class Onchain_views(sp.Contract):
 9        def __init__(self):
10            self.data.storedValue = 0
11
12        @sp.onchain_view()
13        def get_value(self):
14            return self.data.storedValue
15
16    class StoreValue(Onchain_views):
17        def __init__(self, value):
18            Onchain_views.__init__(self)
19            self.data.storedValue = value
20
21        @sp.entrypoint
22        def replace(self, params):
23            self.data.storedValue = params.value
24
25    class StoreValue2(Onchain_views):
26        def __init__(self, value):
27            # self.verbose = True
28            Onchain_views.__init__(self)
29            self.data.storedValue = value
30
31        @sp.entrypoint
32        def add(self, params):
33            self.data.storedValue += params.value
34
35    class StoreValue3(StoreValue2):
36        def __init__(self, value):
37            StoreValue2.__init__(self, value)
38
39        @sp.onchain_view()
40        def new_name(self):
41            return self.data.storedValue
42
43
44if "main" in __name__:
45
46    @sp.add_test()
47    def test():
48        sc = sp.test_scenario("StoreValue", main)
49        c1 = main.StoreValue(12)
50        sc += c1
51        c2 = main.StoreValue2(14)
52        sc += c2
53        c3 = main.StoreValue3(14)
54        sc += c3
55        sc.verify(sp.View(c2, "get_value")() == 14)
56        sc.verify(sp.View(c1, "get_value")() == 12)
57        sc.verify(sp.View(c3, "get_value")() == 14)