templates.test_ticket

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    class C1(sp.Contract):
 7        def __init__(self):
 8            self.data.x = None
 9            self.data.y = None
10
11        @sp.entrypoint
12        def auto_call(self):
13            sp.transfer(sp.ticket(1, 43), sp.tez(0), sp.self_entrypoint("run"))
14
15        @sp.entrypoint
16        def run(self, params):
17            sp.cast(params, sp.ticket[sp.int])
18            (ticket_data, copy) = sp.read_ticket(params)
19            self.data.y = sp.Some(sp.ticket("abc", 42))
20            (t1, t2) = sp.split_ticket(
21                copy,
22                ticket_data.amount / 3,
23                sp.as_nat(ticket_data.amount - ticket_data.amount / 3),
24            )
25            self.data.x = sp.Some(sp.join_tickets(t2, t1))
26
27        @sp.entrypoint
28        def run2(self, params):
29            sp.cast(params, sp.record(t=sp.ticket[int], x=int))
30            record(x, t).match = params
31            assert x == 42
32            (ticket_data, copy) = sp.read_ticket(t)
33            self.data.y = sp.Some(sp.ticket("abc", 42))
34            (t1, t2) = sp.split_ticket(
35                copy,
36                ticket_data.amount / 3,
37                sp.as_nat(ticket_data.amount - ticket_data.amount / 3),
38            )
39            self.data.x = sp.Some(sp.join_tickets(t2, t1))
40
41    class C2(sp.Contract):
42        def __init__(self, t):
43            sp.cast(t, sp.ticket[sp.nat])
44            self.data.t = t
45
46        @sp.entrypoint
47        def run(self):
48            with sp.modify_record(self.data) as data:
49                (ticket_data, copy) = sp.read_ticket(data.t)
50                assert ticket_data.contents == 42
51                (t1, t2) = sp.split_ticket(
52                    copy, ticket_data.amount / 2, ticket_data.amount / 2
53                )
54                data.t = sp.join_tickets(t2, t1)
55
56    class C3(sp.Contract):
57        def __init__(self):
58            self.data.m = sp.cast({}, sp.map[int, sp.ticket[int]])
59
60        @sp.entrypoint
61        def ep1(self):
62            with sp.modify_record(self.data) as d:
63                (t, m) = sp.get_and_update(42, None, d.m)
64                d.m = m
65
66    class C4(sp.Contract):
67        def __init__(self):
68            self.data.m = sp.cast({}, sp.map[int, sp.ticket[int]])
69            self.data.x = 0
70
71        @sp.entrypoint
72        def ep1(self):
73            with sp.modify_record(self.data) as data:
74                (t, m) = sp.get_and_update(42, None, data.m)
75                data.m = m
76                data.x = 0
77
78        @sp.entrypoint
79        def ep2(self, cb):
80            with sp.modify_record(self.data) as data:
81                t1 = sp.ticket("a", 1)
82                t2 = sp.ticket("b", 2)
83                sp.transfer((t1, t2), sp.mutez(0), cb)
84
85
86@sp.add_test()
87def test():
88    s = sp.test_scenario("Ticket", main)
89    c = main.C1()
90    s += c
91    c.auto_call()
92    t = sp.test_ticket(c.address, 5, 6)
93    c.run(t)
94
95    s += main.C2(t)
96    s += main.C3()
97    s += main.C4()