templates.test_inner_invalid

 1# Inner operation is invalid, calling other contracts - Example for illustrative purposes only.
 2
 3import smartpy as sp
 4
 5
 6@sp.module
 7def main():
 8    class Test(sp.Contract):
 9        def __init__(self):
10            self.data.counter = 0
11
12        @sp.entrypoint
13        def run(self, n):
14            assert self.data.counter * n != 9
15            if n > 1:
16                self.data.counter += 1
17                sp.transfer(n - 1, sp.mutez(0), sp.self_entrypoint("run"))
18
19        @sp.entrypoint
20        def reset(self):
21            self.data.counter = 0
22
23        @sp.entrypoint
24        def compute(self, n):
25            sp.transfer(n, sp.mutez(0), sp.self_entrypoint("run"))
26
27
28@sp.add_test()
29def test():
30    scenario = sp.test_scenario("Test", main)
31    scenario.h1("Test template - Inter-Contract Calls")
32    test = main.Test()
33    scenario += test
34    for i in range(0, 20):
35        valid = i not in [6, 10]
36        test.reset()
37        test.compute(i, _valid=valid)
38        scenario.verify(test.data.counter == (max(0, i - 1) if valid else 0))