templates.storeValue

 1# Store Value - Example for illustrative purposes only.
 2
 3import smartpy as sp
 4
 5
 6@sp.module
 7def main():
 8    class StoreValue(sp.Contract):
 9        def __init__(self, value):
10            self.data.storedValue = value
11
12        @sp.entrypoint
13        def replace(self, params):
14            self.data.storedValue = params.value
15
16        @sp.entrypoint
17        def double(self):
18            self.data.storedValue *= 2
19
20        @sp.entrypoint
21        def divide(self, params):
22            assert params.divisor > 5
23            self.data.storedValue /= params.divisor
24
25
26if "main" in __name__:
27
28    @sp.add_test()
29    def test():
30        scenario = sp.test_scenario("StoreValue", main)
31        c1 = main.StoreValue(12)
32        scenario.h1("Store Value")
33        scenario += c1
34        c1.replace(value=15)
35        scenario.p("Some computation").show(c1.data.storedValue * 12)
36        c1.replace(value=25)
37        c1.double()
38        c1.divide(
39            divisor=2, _valid=False, _exception="Assert failure: params.divisor > 5"
40        )
41        scenario.verify(c1.data.storedValue == 50)
42        c1.divide(divisor=6)
43        scenario.verify(c1.data.storedValue == 8)