templates.test_operation_order

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    class Called(sp.Contract):
 7        def __init__(self):
 8            self.data.calls = []
 9
10        @sp.entrypoint
11        def call(self, v):
12            sp.cast(v, sp.int)
13            self.data.calls.push(v)
14
15        @sp.entrypoint
16        def reset(self):
17            self.data.calls = []
18
19    class MyContract(sp.Contract):
20        def __init__(self):
21            pass
22
23        @sp.entrypoint
24        def default(self):
25            pass
26
27        @sp.entrypoint
28        def transfer(self, c):
29            contract = sp.contract(sp.int, c, entrypoint="call")
30            sp.transfer(1, sp.tez(0), contract.unwrap_some())
31            sp.transfer(2, sp.tez(0), contract.unwrap_some())
32            sp.transfer(3, sp.tez(0), contract.unwrap_some())
33
34        @sp.entrypoint
35        def transfer_operation(self, c):
36            contract = sp.contract(sp.int, c, entrypoint="call")
37            x = sp.transfer_operation(1, sp.tez(0), contract.unwrap_some())
38            y = sp.transfer_operation(2, sp.tez(0), contract.unwrap_some())
39            z = sp.transfer_operation(3, sp.tez(0), contract.unwrap_some())
40            sp.operations.push(x)
41            sp.operations.push(y)
42            sp.operations.push(z)
43
44        @sp.entrypoint
45        def transfer_operation_list(self, c):
46            contract = sp.contract(sp.int, c, entrypoint="call")
47            operations = []
48            x = sp.transfer_operation(1, sp.tez(0), contract.unwrap_some())
49            y = sp.transfer_operation(2, sp.tez(0), contract.unwrap_some())
50            z = sp.transfer_operation(3, sp.tez(0), contract.unwrap_some())
51            operations.push(x)
52            operations.push(y)
53            operations.push(z)
54            sp.operations = operations
55
56
57if "main" in __name__:
58
59    @sp.add_test()
60    def test():
61        sc = sp.test_scenario("Test operation order", main)
62        c1 = main.MyContract()
63        sc += c1
64        c2 = main.Called()
65        sc += c2
66
67        sc.h2("Transfers")
68        c1.transfer(c2.address)
69        sc.verify_equal(c2.data.calls, [3, 2, 1])
70        c2.reset()
71
72        sc.h2("Transfers operations")
73        c1.transfer_operation(c2.address)
74        sc.verify_equal(c2.data.calls, [3, 2, 1])
75        c2.reset()
76
77        sc.h2("Transfers operations")
78        c1.transfer_operation_list(c2.address)
79        sc.verify_equal(c2.data.calls, [3, 2, 1])
80        c2.reset()