templates.test_onchain_views

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    T: type = sp.record(
  7        now=sp.timestamp,
  8        level=sp.nat,
  9        amount=sp.mutez,
 10        balance=sp.mutez,
 11        chain_id=sp.chain_id,
 12        sender=sp.address,
 13        source=sp.address,
 14        total_voting_power=sp.nat,
 15    )
 16
 17    class Lib(sp.Contract):
 18        def __init__(self):
 19            self.data.x = 10
 20            self.data.z = sp.nat(42)
 21
 22        @sp.onchain_view()
 23        def view1(self):
 24            return sp.record(z=self.data.z, self_addr=sp.self_address())
 25
 26        @sp.onchain_view()
 27        def view2(self):
 28            return self.data.z
 29
 30        @sp.onchain_view()
 31        def view3(self, a):
 32            return self.data.x * a
 33
 34    class C(sp.Contract):
 35        def __init__(self, c):
 36            self.data.result = None
 37            self.data.result2 = None
 38            self.data.contract = c
 39            self.data.self_addr = None
 40            self.data.self_addr_2 = None
 41            self.data.self_addr_3 = None
 42
 43        # TODO: activate when view naming is possible
 44        # @sp.onchain_view(name="a_really_good_name")
 45        @sp.onchain_view
 46        def view(self):
 47            return sp.record(
 48                now=sp.now,
 49                level=sp.level,
 50                amount=sp.amount,
 51                balance=sp.balance,
 52                chain_id=sp.chain_id,
 53                sender=sp.sender,
 54                source=sp.source,
 55                total_voting_power=sp.total_voting_power,
 56            )
 57
 58        @sp.entrypoint
 59        def store(self):
 60            self.data.self_addr = sp.Some(sp.self_address())
 61            res = sp.view(
 62                "view1",
 63                self.data.contract,
 64                (),
 65                sp.record(z=sp.nat, self_addr=sp.address),
 66            ).unwrap_some(error="Invalid view")
 67
 68            # TODO: activate when view naming is possible
 69            # self.data.result2 = sp.view("a_really_good_name", sp.self_address(), (), T)
 70            self.data.result2 = sp.view("view", sp.self_address(), (), T)
 71            self.data.result = sp.Some(res.z)
 72            self.data.self_addr_2 = sp.Some(res.self_addr)
 73            self.data.self_addr_3 = sp.Some(sp.self_address())
 74            assert self.data.self_addr == self.data.self_addr_3
 75            assert self.data.self_addr_2 == sp.Some(self.data.contract)
 76
 77        @sp.entrypoint
 78        def check_view(self):
 79            """Check view, possibly missing but not failing"""
 80            _ = sp.view("abc", sp.self_address(), (), sp.unit)
 81
 82        @sp.entrypoint
 83        def failing_entrypoint(self):
 84            raise "This is a failure"
 85
 86
 87@sp.add_test()
 88def test():
 89    s = sp.test_scenario("Lib", main)
 90
 91    alice = sp.test_account("alice")
 92
 93    lib = main.Lib()
 94    s += lib
 95    s.verify(lib.view2() == 42)
 96    s.verify(lib.view3(2) == 20)
 97
 98    lib2 = main.Lib()
 99    s += lib2
100    s.verify(lib2.view2() == 42)
101    s.verify(lib2.view3(2) == 20)
102
103    c = main.C(lib.address)
104    c.set_initial_balance(sp.mutez(1234))
105    s += c
106
107    # This fails in Hangzhou
108    c.store(
109        (),
110        _now=sp.timestamp(1),
111        _level=101,
112        _amount=sp.mutez(100),
113        _chain_id=sp.chain_id_cst("0x9caecab9"),
114        _source=alice,
115        _sender=alice,
116        _voting_powers={sp.key_hash("KT1TezoooozzSmartPyzzSTATiCzzzwwBFA1"): 10},
117    )
118
119    s.show(c.data.result2.unwrap_some())
120
121    s.verify(
122        c.data.result2.unwrap_some()
123        == sp.record(
124            now=sp.timestamp(1),
125            level=101,
126            amount=sp.mutez(0),
127            balance=sp.mutez(1334),
128            chain_id=sp.chain_id_cst("0x9caecab9"),
129            total_voting_power=10,
130            sender=c.address,
131            source=alice.address,
132        )
133    )
134
135    c.store(
136        _now=sp.timestamp(2),
137        _level=102,
138        _amount=sp.mutez(150),
139        _chain_id=sp.chain_id_cst("0x9caecab9"),
140        _source=alice,
141        _sender=c.address,
142        _voting_powers={sp.key_hash("KT1TezoooozzSmartPyzzSTATiCzzzwwBFA1"): 10},
143    )
144
145    s.verify(
146        c.data.result2.unwrap_some()
147        == sp.record(
148            now=sp.timestamp(2),
149            level=102,
150            amount=sp.mutez(0),
151            balance=sp.mutez(1484),
152            chain_id=sp.chain_id_cst("0x9caecab9"),
153            total_voting_power=10,
154            sender=c.address,
155            source=alice.address,
156        )
157    )
158
159    c.failing_entrypoint(
160        _valid=False,
161        _now=sp.timestamp(3),
162        _level=103,
163        _amount=sp.mutez(100),
164        _chain_id=sp.chain_id_cst("0x9caecab9"),
165        _source=alice,
166        _sender=c.address,
167        _voting_powers={sp.key_hash("KT1TezoooozzSmartPyzzSTATiCzzzwwBFA1"): 10},
168    )
169
170    c.store(
171        _amount=sp.mutez(200),
172        _source=alice,
173        _sender=alice,
174    )
175
176    s.verify(
177        c.data.result2.unwrap_some()
178        == sp.record(
179            now=sp.timestamp(2),
180            level=102,
181            amount=sp.mutez(0),
182            balance=sp.mutez(1684),
183            chain_id=sp.chain_id_cst("0x9caecab9"),
184            total_voting_power=10,
185            sender=c.address,
186            source=alice.address,
187        )
188    )
189
190    c.store(
191        _source=alice,
192        _sender=alice,
193    )
194
195    s.verify(
196        c.data.result2.unwrap_some()
197        == sp.record(
198            now=sp.timestamp(2),
199            level=102,
200            amount=sp.mutez(0),
201            balance=sp.mutez(1684),
202            chain_id=sp.chain_id_cst("0x9caecab9"),
203            total_voting_power=10,
204            sender=c.address,
205            source=alice.address,
206        )
207    )
208
209    view_result = s.compute(c.view(), source=alice, sender=c.address)
210
211    s.verify(
212        view_result
213        == sp.record(
214            now=sp.timestamp(2),
215            level=102,
216            amount=sp.mutez(0),
217            balance=sp.mutez(1684),
218            chain_id=sp.chain_id_cst("0x9caecab9"),
219            total_voting_power=10,
220            sender=c.address,
221            source=alice.address,
222        )
223    )
224
225    s.verify(
226        s.compute(c.view(), source=alice, sender=c.address, now=sp.timestamp(42))
227        == sp.record(
228            now=sp.timestamp(42),
229            level=102,
230            amount=sp.mutez(0),
231            balance=sp.mutez(1684),
232            chain_id=sp.chain_id_cst("0x9caecab9"),
233            total_voting_power=10,
234            sender=c.address,
235            source=alice.address,
236        )
237    )
238
239    s.verify(s.compute(lib.view1()) == sp.record(z=42, self_addr=lib.address))