templates.test_pack

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    tv: type = sp.variant(a=sp.int, b=sp.int)
  7
  8    t1: type = sp.lambda_[sp.int, tv]
  9    t2: type = sp.lambda_[sp.int, sp.variant(a=sp.int)]
 10    int_to_string: type = sp.lambda_[sp.int, sp.string]
 11
 12    def list_equal(lists):
 13        (l1, l2) = lists
 14        return sp.len(l1) == sp.len(l2) and sum(l1) == sum(l2)
 15
 16    class Pack(sp.Contract):
 17        def __init__(self):
 18            self.data.unit = sp.bytes("0x05030b")
 19            self.data.true = sp.bytes("0x05030a")
 20            self.data.false = sp.bytes("0x050303")
 21            self.data.mutez_0 = sp.bytes("0x050000")
 22            self.data.mutez_1 = sp.bytes("0x050001")
 23            self.data.mutez_2 = sp.bytes("0x050002")
 24            self.data.mutez_3 = sp.bytes("0x050003")
 25            self.data.mutez_10 = sp.bytes("0x05000a")
 26            self.data.mutez_10_000 = sp.bytes("0x0500909c01")
 27            self.data.mutez_1_000_000 = sp.bytes("0x050080897a")
 28            self.data.mutez_1_000_000_000_000 = sp.bytes("0x050080c0a8ca9a3a")
 29            self.data.nat_0 = sp.bytes("0x050000")
 30            self.data.nat_1 = sp.bytes("0x050001")
 31            self.data.nat_2 = sp.bytes("0x050002")
 32            self.data.nat_3 = sp.bytes("0x050003")
 33            self.data.nat_10 = sp.bytes("0x05000a")
 34            self.data.nat_10_000 = sp.bytes("0x0500909c01")
 35            self.data.nat_1_000_000 = sp.bytes("0x050080897a")
 36            self.data.nat_1_000_000_000_000 = sp.bytes("0x050080c0a8ca9a3a")
 37            self.data.int_0 = sp.bytes("0x050000")
 38            self.data.int_1 = sp.bytes("0x050001")
 39            self.data.int_2 = sp.bytes("0x050002")
 40            self.data.int_3 = sp.bytes("0x050003")
 41            self.data.int_10 = sp.bytes("0x05000a")
 42            self.data.int_10_000 = sp.bytes("0x0500909c01")
 43            self.data.int_1_000_000 = sp.bytes("0x050080897a")
 44            self.data.int_1_000_000_000_000 = sp.bytes("0x050080c0a8ca9a3a")
 45            self.data.int_minus_0 = sp.bytes("0x050000")
 46            self.data.int_minus_1 = sp.bytes("0x050041")
 47            self.data.int_minus_2 = sp.bytes("0x050042")
 48            self.data.int_minus_3 = sp.bytes("0x050043")
 49            self.data.int_minus_10 = sp.bytes("0x05004a")
 50            self.data.int_minus_10_000 = sp.bytes("0x0500d09c01")
 51            self.data.int_minus_1_000_000 = sp.bytes("0x0500c0897a")
 52            self.data.int_minus_1_000_000_000_000 = sp.bytes("0x0500c0c0a8ca9a3a")
 53            self.data.string_ = sp.bytes("0x050100000000")
 54            self.data.string_a = sp.bytes("0x05010000000161")
 55            self.data.string_ab = sp.bytes("0x0501000000026162")
 56            self.data.string_abc = sp.bytes("0x050100000003616263")
 57            self.data.bytes_0x = sp.bytes("0x050a00000000")
 58            self.data.bytes_0x00 = sp.bytes("0x050a0000000100")
 59            self.data.bytes_0xab00 = sp.bytes("0x050a00000002ab00")
 60            self.data.pair_int_1_int_2 = sp.bytes("0x05070700010002")
 61            self.data.pair_string_a_true = sp.bytes("0x050707010000000161030a")
 62            self.data.timestamp_123 = sp.bytes("0x0500bb01")
 63            self.data.signature_sig = sp.bytes(
 64                "0x050a0000004027bcc456b12981bacfbf9755c589c16772da5200656386f34c46ee2a70a9c07be36268ce666894a5e8a0828027b6fa1a7a084a5ffa24208cc1dfec07d6f66e0f"
 65            )
 66            self.data.signature_spsig = sp.bytes(
 67                "0x050a00000040865b0ceaa2decd64fea1b7e0e97d6ae8035125d821ebe99d7d5c8a559f36fdd43388a570e475011f6d2bbef184ece9bafbf0ac12157f7257988e9575ae2cbca9"
 68            )
 69            self.data.signature_p2sig = sp.bytes(
 70                "0x050a00000040e7b25662eb9bb1e92ef7cb20c2fcaca120eccbe2d6af7511c2e510eb586ca8612d9dfe26bc7882a33b29b8c9706a04124f98dd281f1622d8da50db7a5cd4ab43"
 71            )
 72            self.data.signature = sp.bytes(
 73                "0x050a000000404d6738931b59605ca0449b7b76a01d210ace9220051821ecf43382a7024ed99063c5bedb85c81b919d33a213c6d8fb47f2c9b4deaf6f68f56dee038ea739740f"
 74            )
 75            self.data.address_tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA = sp.bytes(
 76                "0x050a000000160000db8fc132cf9624febe1675fbfd0c0fcdd7655f23"
 77            )
 78            self.data.address_tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93 = sp.bytes(
 79                "0x050a0000001600014b7c404fd4fbcf931cde0a8971caf76f53c8e5c0"
 80            )
 81            self.data.address_tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K = sp.bytes(
 82                "0x050a0000001600026e08178148d348cb59efffbdb403ca546b13304e"
 83            )
 84            self.data.address_tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Cc = sp.bytes(
 85                "0x050a000000160003e0b0f0c1fa4d9d46c8281d3e98900f942283b15c"
 86            )
 87            self.data.key_hash_tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA = sp.bytes(
 88                "0x050a0000001500db8fc132cf9624febe1675fbfd0c0fcdd7655f23"
 89            )
 90            self.data.key_hash_tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93 = sp.bytes(
 91                "0x050a00000015014b7c404fd4fbcf931cde0a8971caf76f53c8e5c0"
 92            )
 93            self.data.key_hash_tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K = sp.bytes(
 94                "0x050a00000015026e08178148d348cb59efffbdb403ca546b13304e"
 95            )
 96            self.data.key_hash_tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Cc = sp.bytes(
 97                "0x050a0000001503e0b0f0c1fa4d9d46c8281d3e98900f942283b15c"
 98            )
 99            self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy = sp.bytes(
100                "0x050a00000016019142eb01374f37f03023eefc82f2ede96e8ae51500"
101            )
102            self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy_ = sp.bytes(
103                "0x050a00000016019142eb01374f37f03023eefc82f2ede96e8ae51500"
104            )
105            self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy_foo = sp.bytes(
106                "0x050a00000019019142eb01374f37f03023eefc82f2ede96e8ae51500666f6f"
107            )
108            self.data.list_int_0_to_6 = sp.bytes(
109                "0x05020000000e0000000100020003000400050006"
110            )
111            self.data.list_int_range_0_1 = sp.bytes("0x0502000000020000")
112            self.data.list_int_range_0_2 = sp.bytes("0x05020000000400000001")
113            self.data.list_int_range_0_3 = sp.bytes("0x050200000006000000010002")
114            self.data.list_int_range_0_4 = sp.bytes("0x0502000000080000000100020003")
115            self.data.list_int_range_0_5 = sp.bytes(
116                "0x05020000000a00000001000200030004"
117            )
118            self.data.list_int_range_0_6 = sp.bytes(
119                "0x05020000000c000000010002000300040005"
120            )
121            self.data.list_int_range_0_7 = sp.bytes(
122                "0x05020000000e0000000100020003000400050006"
123            )
124            self.data.set_int_1_to_4 = sp.bytes("0x0502000000080001000200030004")
125
126            self.data.lambda_x_plus_2 = sp.bytes("0x0502000000080743035b00020312")
127            self.data.lambda_f1 = sp.bytes(
128                "0x05020000001b03210316072c020000000903200743035b00bb0102000000020317"
129            )
130            self.data.lambda_f2 = sp.bytes(
131                "0x05020000002703210316072c020000000903200743035b00bb01020000000e032105290004034c052900030312"
132            )
133            self.data.lambda_f3 = sp.bytes(
134                "0x05020000004c0743035b000007430368010000000141052100030325072c0200000022034c0570000203190325072c020000000c03200743036801000000014202000000000200000008034c0320034c0320"
135            )
136            self.data.lambda_michelson = sp.bytes(
137                "0x05020000000c03210743035b002a0312033a"
138            )
139
140            self.data.list_string = sp.bytes(
141                "05020000002201000000057468697320010000000369732001000000026120010000000474657374"
142            )
143            self.data.map_string_int = sp.bytes(
144                "05020000000f07040100000006616e73776572002a"
145            )
146            self.data.record_string_int = sp.bytes("05070701000000047465737400b914")
147            self.data.variant_a_string_b_nat = sp.bytes("050505010000000474657374")
148            self.data.option_string = sp.bytes("050509010000000568656c6c6f")
149            self.data.none_string = sp.bytes("050306")
150
151            # Activate when sapling is implemented
152            # self.data.sapling_transaction = sp.bytes("0x00")
153            # self.data.sapling_state = sp.bytes("0x00")
154
155            self.data.chain_id = sp.bytes("050a00000004af1864d9")  # NetXnHfVqm9iesp
156            self.data.level_2548242 = sp.bytes("05009288b702")  # 2548242
157
158        @sp.private()
159        def f1(self, x):
160            if x.a:
161                return 123
162            else:
163                return x.b
164
165        @sp.private()
166        def f2(self, x):
167            sp.cast(x.b, sp.int)
168            if x.a:
169                return 123
170            else:
171                return x.b + x.c
172
173        @sp.private()
174        def f3(self, x):
175            y = 0
176            r = "A"
177            if x == 0:
178                if x == y:
179                    r = "B"
180            return r
181
182        @sp.entrypoint
183        def pack(self, signature_sig, signature_spsig, signature_p2sig, s):
184            sp.cast(signature_sig, sp.signature)
185            sp.cast(signature_spsig, sp.signature)
186            sp.cast(signature_p2sig, sp.signature)
187            sp.cast(s, sp.signature)
188            assert self.data.unit == sp.pack(())
189            assert self.data.true == sp.pack(True)
190            assert self.data.false == sp.pack(False)
191            assert self.data.mutez_0 == sp.pack(sp.mutez(0))
192            assert self.data.mutez_1 == sp.pack(sp.mutez(1))
193            assert self.data.mutez_2 == sp.pack(sp.mutez(2))
194            assert self.data.mutez_3 == sp.pack(sp.mutez(3))
195            assert self.data.mutez_10 == sp.pack(sp.mutez(10))
196            assert self.data.mutez_10_000 == sp.pack(sp.mutez(10000))
197            assert self.data.mutez_1_000_000 == sp.pack(sp.mutez(1000000))
198            assert self.data.mutez_1_000_000_000_000 == sp.pack(sp.mutez(1000000000000))
199            assert self.data.nat_0 == sp.pack(sp.nat(0))
200            assert self.data.nat_1 == sp.pack(sp.nat(1))
201            assert self.data.nat_2 == sp.pack(sp.nat(2))
202            assert self.data.nat_3 == sp.pack(sp.nat(3))
203            assert self.data.nat_10 == sp.pack(sp.nat(10))
204            assert self.data.nat_10_000 == sp.pack(sp.nat(10000))
205            assert self.data.nat_1_000_000 == sp.pack(sp.nat(1000000))
206            assert self.data.nat_1_000_000_000_000 == sp.pack(sp.nat(1000000000000))
207            assert self.data.int_0 == sp.pack(sp.int(0))
208            assert self.data.int_1 == sp.pack(sp.int(1))
209            assert self.data.int_2 == sp.pack(sp.int(2))
210            assert self.data.int_3 == sp.pack(sp.int(3))
211            assert self.data.int_10 == sp.pack(sp.int(10))
212            assert self.data.int_10_000 == sp.pack(sp.int(10000))
213            assert self.data.int_1_000_000 == sp.pack(sp.int(1000000))
214            assert self.data.int_1_000_000_000_000 == sp.pack(sp.int(1000000000000))
215            assert self.data.int_minus_0 == sp.pack(sp.int(-0))
216            assert self.data.int_minus_1 == sp.pack(sp.int(-1))
217            assert self.data.int_minus_2 == sp.pack(sp.int(-2))
218            assert self.data.int_minus_3 == sp.pack(sp.int(-3))
219            assert self.data.int_minus_10 == sp.pack(sp.int(-10))
220            assert self.data.int_minus_10_000 == sp.pack(sp.int(-10000))
221            assert self.data.int_minus_1_000_000 == sp.pack(sp.int(-1000000))
222            assert self.data.int_minus_1_000_000_000_000 == sp.pack(
223                sp.int(-1000000000000)
224            )
225            assert self.data.string_ == sp.pack("")
226            assert self.data.string_a == sp.pack("a")
227            assert self.data.string_ab == sp.pack("ab")
228            assert self.data.string_abc == sp.pack("abc")
229            assert self.data.bytes_0x == sp.pack(sp.bytes("0x"))
230            assert self.data.bytes_0x00 == sp.pack(sp.bytes("0x00"))
231            assert self.data.bytes_0xab00 == sp.pack(sp.bytes("0xab00"))
232            assert self.data.pair_int_1_int_2 == sp.pack((1, 2))
233            assert self.data.pair_string_a_true == sp.pack(("a", True))
234            assert self.data.timestamp_123 == sp.pack(sp.timestamp(123))
235            # assert self.data.signature_sig == sp.pack(sp.signature("sigTBpkXw6tC72L2nJ2r2Jm5iB6uidTWqoMNd4oEawUbGBf5mHVfKawFYL8X8MJECpL73oBnfujyUZNLK2LQWD1FaCkYMP4j"))
236            # assert self.data.signature_spsig == sp.pack(sp.signature("spsig1PPUFZucuAQybs5wsqsNQ68QNgFaBnVKMFaoZZfi1BtNnuCAWnmL9wVy5HfHkR6AeodjVGxpBVVSYcJKyMURn6K1yknYLm"))
237            # assert self.data.signature_p2sig == sp.pack(sp.signature("p2sigsceCzcDw2AeYDzUonj4JT341WC9Px4wdhHBxbZcG1FhfqFVuG7f2fGCzrEHSAZgrsrQWpxduDPk9qZRgrpzwJnSHC3gZJ"))
238            # assert self.data.signature == sp.pack(s)
239
240            assert self.data.signature_sig == sp.pack(signature_sig)
241            # assert self.data.signature_spsig == sp.pack(signature_spsig)
242            # assert self.data.signature_p2sig == sp.pack(signature_p2sig)
243            # self.data.signature = sp.pack(s)
244            assert self.data.address_tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA == sp.pack(
245                sp.address("tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA")
246            )
247            assert self.data.address_tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93 == sp.pack(
248                sp.address("tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93")
249            )
250            assert self.data.address_tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K == sp.pack(
251                sp.address("tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K")
252            )
253            assert self.data.key_hash_tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA == sp.pack(
254                sp.key_hash("tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA")
255            )
256            assert self.data.key_hash_tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93 == sp.pack(
257                sp.key_hash("tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93")
258            )
259            assert self.data.key_hash_tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K == sp.pack(
260                sp.key_hash("tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K")
261            )
262            # TODO activate when tz4 are implemented
263            # assert self.data.key_hash_tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Cc == sp.pack(sp.key_hash("tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Cc"))
264            assert self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy == sp.pack(
265                sp.address("KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy")
266            )
267            assert self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy_ == sp.pack(
268                sp.address("KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy%")
269            )
270            assert (
271                self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy_foo
272                == sp.pack(sp.address("KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy%foo"))
273            )
274            assert self.data.list_int_0_to_6 == sp.pack([0, 1, 2, 3, 4, 5, 6])
275            assert self.data.list_int_range_0_1 == sp.pack(range(0, 1))
276            assert self.data.list_int_range_0_2 == sp.pack(range(0, 2))
277            assert self.data.list_int_range_0_3 == sp.pack(range(0, 3))
278            assert self.data.list_int_range_0_4 == sp.pack(range(0, 4))
279            assert self.data.list_int_range_0_5 == sp.pack(range(0, 5))
280            assert self.data.list_int_range_0_6 == sp.pack(range(0, 6))
281            assert self.data.list_int_range_0_7 == sp.pack(range(0, 7))
282            assert self.data.set_int_1_to_4 == sp.pack({1, 2, 3, 4})
283
284            assert self.data.lambda_x_plus_2 == sp.pack(lambda x: x + 2)
285            assert self.data.lambda_f1 == sp.pack(self.f1)
286            assert self.data.lambda_f2 == sp.pack(self.f2)
287            assert self.data.lambda_f3 == sp.pack(self.f3)
288            assert self.data.lambda_michelson == sp.pack(
289                lambda x: sp.michelson(
290                    "DUP; PUSH int 42; ADD; MUL", [sp.int], [sp.int], x
291                )
292            )
293
294            assert self.data.list_string == sp.pack(["this ", "is ", "a ", "test"])
295            assert self.data.map_string_int == sp.pack({"answer": 42})
296            assert self.data.record_string_int == sp.pack(sp.record(a="test", b=1337))
297            assert self.data.variant_a_string_b_nat == sp.pack(
298                sp.cast(sp.variant.a("test"), sp.variant(a=sp.string, b=sp.nat))
299            )
300            assert self.data.option_string == sp.pack(
301                sp.cast(sp.Some("hello"), sp.option[sp.string])
302            )
303            assert self.data.none_string == sp.pack(sp.cast(None, sp.option[sp.string]))
304
305            # assert self.data.chain_id == sp.pack(sp.chain_id)
306            # TODO when chain_id creation is implemented
307            # assert self.data.chain_id == sp.chain_id("NetXnHfVqm9iesp")
308            assert self.data.level_2548242 == sp.pack(sp.level)
309            assert self.data.level_2548242 == sp.pack(sp.nat(2548242))
310
311            # TODO: activate when sapling is implemented
312            # assert self.data.sapling_transaction = sp.pack(...)
313            # assert self.data.sapling_state = sp.pack(...)
314
315        @sp.entrypoint
316        def unpack(self, signature_sig, signature_spsig, signature_p2sig, s):
317            sp.cast(signature_sig, sp.signature)
318            sp.cast(signature_spsig, sp.signature)
319            sp.cast(signature_p2sig, sp.signature)
320            sp.cast(s, sp.signature)
321            assert sp.unpack(self.data.unit, sp.unit).unwrap_some() == ()
322            assert sp.unpack(self.data.true, sp.bool).unwrap_some() == True
323            assert sp.unpack(self.data.false, sp.bool).unwrap_some() == False
324            assert sp.unpack(self.data.mutez_0, sp.mutez).unwrap_some() == sp.mutez(0)
325            assert sp.unpack(self.data.mutez_1, sp.mutez).unwrap_some() == sp.mutez(1)
326            assert sp.unpack(self.data.mutez_2, sp.mutez).unwrap_some() == sp.mutez(2)
327            assert sp.unpack(self.data.mutez_3, sp.mutez).unwrap_some() == sp.mutez(3)
328            assert sp.unpack(self.data.mutez_10, sp.mutez).unwrap_some() == sp.mutez(10)
329            assert sp.unpack(
330                self.data.mutez_10_000, sp.mutez
331            ).unwrap_some() == sp.mutez(10000)
332            assert sp.unpack(
333                self.data.mutez_1_000_000, sp.mutez
334            ).unwrap_some() == sp.mutez(1000000)
335            assert sp.unpack(
336                self.data.mutez_1_000_000_000_000, sp.mutez
337            ).unwrap_some() == sp.mutez(1000000000000)
338            assert sp.unpack(self.data.nat_0, sp.nat).unwrap_some() == sp.nat(0)
339            assert sp.unpack(self.data.nat_1, sp.nat).unwrap_some() == sp.nat(1)
340            assert sp.unpack(self.data.nat_2, sp.nat).unwrap_some() == sp.nat(2)
341            assert sp.unpack(self.data.nat_3, sp.nat).unwrap_some() == sp.nat(3)
342            assert sp.unpack(self.data.nat_10, sp.nat).unwrap_some() == sp.nat(10)
343            assert sp.unpack(self.data.nat_10_000, sp.nat).unwrap_some() == sp.nat(
344                10000
345            )
346            assert sp.unpack(self.data.nat_1_000_000, sp.nat).unwrap_some() == sp.nat(
347                1000000
348            )
349            assert sp.unpack(
350                self.data.nat_1_000_000_000_000, sp.nat
351            ).unwrap_some() == sp.nat(1000000000000)
352            assert sp.unpack(self.data.int_0, sp.int).unwrap_some() == sp.int(0)
353            assert sp.unpack(self.data.int_1, sp.int).unwrap_some() == sp.int(1)
354            assert sp.unpack(self.data.int_2, sp.int).unwrap_some() == sp.int(2)
355            assert sp.unpack(self.data.int_3, sp.int).unwrap_some() == sp.int(3)
356            assert sp.unpack(self.data.int_10, sp.int).unwrap_some() == sp.int(10)
357            assert sp.unpack(self.data.int_10_000, sp.int).unwrap_some() == sp.int(
358                10000
359            )
360            assert sp.unpack(self.data.int_1_000_000, sp.int).unwrap_some() == sp.int(
361                1000000
362            )
363            assert sp.unpack(
364                self.data.int_1_000_000_000_000, sp.int
365            ).unwrap_some() == sp.int(1000000000000)
366            assert sp.unpack(self.data.int_minus_0, sp.int).unwrap_some() == sp.int(-0)
367            assert sp.unpack(self.data.int_minus_1, sp.int).unwrap_some() == sp.int(-1)
368            assert sp.unpack(self.data.int_minus_2, sp.int).unwrap_some() == sp.int(-2)
369            assert sp.unpack(self.data.int_minus_3, sp.int).unwrap_some() == sp.int(-3)
370            assert sp.unpack(self.data.int_minus_10, sp.int).unwrap_some() == sp.int(
371                -10
372            )
373            assert sp.unpack(
374                self.data.int_minus_10_000, sp.int
375            ).unwrap_some() == sp.int(-10000)
376            assert sp.unpack(
377                self.data.int_minus_1_000_000, sp.int
378            ).unwrap_some() == sp.int(-1000000)
379            assert sp.unpack(
380                self.data.int_minus_1_000_000_000_000, sp.int
381            ).unwrap_some() == sp.int(-1000000000000)
382            assert sp.unpack(self.data.string_, sp.string).unwrap_some() == ""
383            assert sp.unpack(self.data.string_a, sp.string).unwrap_some() == "a"
384            assert sp.unpack(self.data.string_ab, sp.string).unwrap_some() == "ab"
385            assert sp.unpack(self.data.string_abc, sp.string).unwrap_some() == "abc"
386            assert sp.unpack(self.data.bytes_0x, sp.bytes).unwrap_some() == sp.bytes(
387                "0x"
388            )
389            assert sp.unpack(self.data.bytes_0x00, sp.bytes).unwrap_some() == sp.bytes(
390                "0x00"
391            )
392            assert sp.unpack(
393                self.data.bytes_0xab00, sp.bytes
394            ).unwrap_some() == sp.bytes("0xab00")
395            assert sp.unpack(
396                self.data.pair_int_1_int_2, sp.pair[sp.int, sp.int]
397            ).unwrap_some() == (1, 2)
398            assert sp.unpack(
399                self.data.pair_string_a_true, sp.pair[sp.string, sp.bool]
400            ).unwrap_some() == ("a", True)
401            assert sp.unpack(
402                self.data.timestamp_123, sp.timestamp
403            ).unwrap_some() == sp.timestamp(123)
404            assert (
405                sp.unpack(self.data.signature_sig, sp.signature).unwrap_some()
406                == signature_sig
407            )
408            # assert sp.unpack(self.data.signature_spsig, sp.signature).unwrap_some() == signature_spsig
409            # assert sp.unpack(self.data.signature_p2sig, sp.signature).unwrap_some() == signature_p2sig
410            # assert sp.unpack(self.data.signature, sp.signature).unwrap_some() == s
411            assert sp.unpack(
412                self.data.address_tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA, sp.address
413            ).unwrap_some() == sp.address("tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA")
414            assert sp.unpack(
415                self.data.address_tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93, sp.address
416            ).unwrap_some() == sp.address("tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93")
417            assert sp.unpack(
418                self.data.address_tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K, sp.address
419            ).unwrap_some() == sp.address("tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K")
420            # TODO activate when tz4 are implemented
421            # assert sp.unpack(self.data.address_tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Cc, sp.address).unwrap_some() == sp.address("tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Cc")
422            assert sp.unpack(
423                self.data.key_hash_tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA, sp.key_hash
424            ).unwrap_some() == sp.key_hash("tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA")
425            assert sp.unpack(
426                self.data.key_hash_tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93, sp.key_hash
427            ).unwrap_some() == sp.key_hash("tz2FCNBrERXtaTtNX6iimR1UJ5JSDxvdHM93")
428            assert sp.unpack(
429                self.data.key_hash_tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K, sp.key_hash
430            ).unwrap_some() == sp.key_hash("tz3WMqdzXqRWXwyvj5Hp2H7QEepaUuS7vd9K")
431            # TODO activate when tz4 are implemented
432            # assert sp.unpack(self.data.key_hash_tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Cc, sp.key_hash).unwrap_some() == sp.key_hash("tz4VVJutTUzLVFY64H1HTtNzFB4Uzfisk6Ccc")
433            assert sp.unpack(
434                self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy, sp.address
435            ).unwrap_some() == sp.address("KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy")
436            assert sp.unpack(
437                self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy_, sp.address
438            ).unwrap_some() == sp.address("KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy")
439            assert sp.unpack(
440                self.data.address_KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy_foo, sp.address
441            ).unwrap_some() == sp.address("KT1Mpqi89gRyUuoXUPAWjHkqkk1F48eUKUVy%foo")
442
443            assert list_equal(
444                (
445                    sp.unpack(self.data.list_int_0_to_6, sp.list[sp.int]).unwrap_some(),
446                    [0, 1, 2, 3, 4, 5, 6],
447                )
448            )
449            assert list_equal(
450                (
451                    sp.unpack(
452                        self.data.list_int_range_0_1, sp.list[sp.int]
453                    ).unwrap_some(),
454                    range(0, 1),
455                )
456            )
457            assert list_equal(
458                (
459                    sp.unpack(
460                        self.data.list_int_range_0_2, sp.list[sp.int]
461                    ).unwrap_some(),
462                    range(0, 2),
463                )
464            )
465            assert list_equal(
466                (
467                    sp.unpack(
468                        self.data.list_int_range_0_3, sp.list[sp.int]
469                    ).unwrap_some(),
470                    range(0, 3),
471                )
472            )
473            assert list_equal(
474                (
475                    sp.unpack(
476                        self.data.list_int_range_0_4, sp.list[sp.int]
477                    ).unwrap_some(),
478                    range(0, 4),
479                )
480            )
481            assert list_equal(
482                (
483                    sp.unpack(
484                        self.data.list_int_range_0_5, sp.list[sp.int]
485                    ).unwrap_some(),
486                    range(0, 5),
487                )
488            )
489            assert list_equal(
490                (
491                    sp.unpack(
492                        self.data.list_int_range_0_6, sp.list[sp.int]
493                    ).unwrap_some(),
494                    range(0, 6),
495                )
496            )
497            assert list_equal(
498                (
499                    sp.unpack(
500                        self.data.list_int_range_0_7, sp.list[sp.int]
501                    ).unwrap_some(),
502                    range(0, 7),
503                )
504            )
505            assert list_equal(
506                (
507                    sp.unpack(self.data.set_int_1_to_4, sp.set[sp.int])
508                    .unwrap_some()
509                    .elements(),
510                    [1, 2, 3, 4],
511                )
512            )
513
514            assert sp.pack(
515                sp.unpack(
516                    self.data.lambda_x_plus_2, sp.lambda_[sp.int, sp.int]
517                ).unwrap_some()
518            ) == sp.pack(lambda x: x + 2)
519            assert sp.pack(
520                sp.unpack(
521                    self.data.lambda_f1,
522                    sp.lambda_[sp.record(a=sp.bool, b=sp.int), sp.int],
523                ).unwrap_some()
524            ) == sp.pack(self.f1)
525            assert sp.pack(
526                sp.unpack(
527                    self.data.lambda_f2,
528                    sp.lambda_[sp.record(a=sp.bool, b=sp.int, c=sp.int), sp.int],
529                ).unwrap_some()
530            ) == sp.pack(self.f2)
531            unpacked_f3 = sp.unpack(
532                self.data.lambda_f3, sp.lambda_[sp.int, sp.string]
533            ).unwrap_some()
534            assert unpacked_f3(0) == self.f3(0)
535            assert unpacked_f3(1) == self.f3(1)
536            assert unpacked_f3(2) == self.f3(2)
537            unpacked_michelson = sp.unpack(
538                self.data.lambda_michelson, sp.lambda_[sp.int, sp.int]
539            ).unwrap_some()
540            assert sp.pack(unpacked_michelson) == sp.pack(
541                lambda x: sp.michelson(
542                    "DUP; PUSH int 42; ADD; MUL", [sp.int], [sp.int], x
543                )
544            )
545            assert unpacked_michelson(2) == 88
546
547            assert (
548                sp.concat(
549                    sp.unpack(self.data.list_string, sp.list[sp.string]).unwrap_some()
550                )
551                == "this is a test"
552            )
553            m = sp.unpack(
554                self.data.map_string_int, sp.map[sp.string, sp.int]
555            ).unwrap_some()
556            assert m["answer"] == 42 and sp.len(m) == 1
557            assert sp.unpack(
558                self.data.record_string_int, sp.record(a=sp.string, b=sp.int)
559            ).unwrap_some() == sp.record(a="test", b=1337)
560            assert sp.unpack(
561                self.data.variant_a_string_b_nat, sp.variant(a=sp.string, b=sp.nat)
562            ).unwrap_some() == sp.cast(
563                sp.variant.a("test"), sp.variant(a=sp.string, b=sp.nat)
564            )
565            assert sp.unpack(
566                self.data.option_string, sp.option[sp.string]
567            ).unwrap_some() == sp.Some("hello")
568            assert sp.unpack(
569                self.data.none_string, sp.option[sp.string]
570            ).unwrap_some() == sp.cast(None, sp.option[sp.string])
571
572            # TODO when chain_id creation is implemented
573            # assert sp.unpack(self.data.chain_id, sp.chain_id).unwrap_some() == sp.chain_id("NetXnHfVqm9iesp")
574            assert sp.unpack(self.data.level_2548242, sp.nat).unwrap_some() == 2548242
575
576            # TODO: activate when sapling is implemented
577            # assert sp.unpack(self.data.sapling_transaction, ...).unwrap_some() == ...
578            # assert sp.unpack(self.data.sapling_state, ...).unwrap_some() == ...
579
580        @sp.entrypoint
581        def pack_lambdas(self):
582            f1 = self.f3
583            sp.cast(f1, sp.lambda_[sp.int, sp.string])
584            p1 = sp.pack(f1)
585            f2 = sp.unpack(p1, sp.lambda_[sp.int, sp.string]).unwrap_some()
586            p2 = sp.pack(f2)
587            assert p1 == p2
588            assert f1(0) == f2(0)
589            assert f1(1) == f2(1)
590            assert f1(2) == f2(2)
591
592        @sp.entrypoint
593        def pack_lambdas2(self):
594            def f(x):
595                return x > 0
596
597            f1 = sp.cast(f, sp.lambda_[int, bool])
598            p1 = sp.pack(f1)
599            f2 = sp.unpack(p1, sp.lambda_[int, bool]).unwrap_some()
600            assert f2(1)
601
602        @sp.entrypoint
603        def pack_lambda_record(self):
604            f = lambda x: sp.record(a=1, b=x + 2)
605
606            f2 = sp.unpack(
607                sp.pack(f), sp.lambda_[int, sp.record(a=int, b=int)]
608            ).unwrap_some()
609            assert f2(100).a == 1
610
611            g = lambda x: sp.record(a=x + 1)
612
613            g2 = sp.unpack(sp.pack(g), sp.lambda_[int, sp.record(a=int)]).unwrap_some()
614            assert g2(100).a == 101
615
616        @sp.entrypoint
617        def pack_lambda_variant(self):
618            f = lambda x: sp.cast(sp.variant.a(x + 1), tv)
619            f2 = sp.unpack(sp.pack(f), t1).unwrap_some()
620            assert f2(100).unwrap.a() == 101
621
622            f_ = lambda x: sp.variant.a(x + 1)
623            f2_ = sp.unpack(sp.pack(f_), t2).unwrap_some()
624            assert f2_(100).unwrap.a() == 101
625
626        @sp.entrypoint
627        def pack_unpack_pack_lambda(self):
628            assert sp.pack(self.f3) == sp.pack(
629                sp.unpack(sp.pack(self.f3), int_to_string).unwrap_some()
630            )
631            assert sp.pack(
632                sp.unpack(sp.pack(self.f3), int_to_string).unwrap_some()
633            ) == sp.pack(
634                sp.unpack(
635                    sp.pack(sp.unpack(sp.pack(self.f3), int_to_string).unwrap_some()),
636                    int_to_string,
637                ).unwrap_some()
638            )
639
640
641if "main" in __name__:
642
643    @sp.add_test()
644    def test():
645        sc = sp.test_scenario("Pack", main)
646        c1 = main.Pack()
647        sc += c1
648        signature = sp.make_signature(
649            sp.test_account("keystore").secret_key,
650            sp.pack("value"),
651            message_format="Raw",
652        )
653        c1.pack(
654            signature_sig=sp.signature(
655                "sigTBpkXw6tC72L2nJ2r2Jm5iB6uidTWqoMNd4oEawUbGBf5mHVfKawFYL8X8MJECpL73oBnfujyUZNLK2LQWD1FaCkYMP4j"
656            ),
657            signature_spsig=sp.signature(
658                "spsig1PPUFZucuAQybs5wsqsNQ68QNgFaBnVKMFaoZZfi1BtNnuCAWnmL9wVy5HfHkR6AeodjVGxpBVVSYcJKyMURn6K1yknYLm"
659            ),
660            signature_p2sig=sp.signature(
661                "p2sigsceCzcDw2AeYDzUonj4JT341WC9Px4wdhHBxbZcG1FhfqFVuG7f2fGCzrEHSAZgrsrQWpxduDPk9qZRgrpzwJnSHC3gZJ"
662            ),
663            s=signature,
664            _level=2548242,
665            _chain_id=sp.chain_id_cst("0xaf1864d9feee0e1f"),
666        )
667        c1.unpack(
668            signature_sig=sp.signature(
669                "sigTBpkXw6tC72L2nJ2r2Jm5iB6uidTWqoMNd4oEawUbGBf5mHVfKawFYL8X8MJECpL73oBnfujyUZNLK2LQWD1FaCkYMP4j"
670            ),
671            signature_spsig=sp.signature(
672                "spsig1PPUFZucuAQybs5wsqsNQ68QNgFaBnVKMFaoZZfi1BtNnuCAWnmL9wVy5HfHkR6AeodjVGxpBVVSYcJKyMURn6K1yknYLm"
673            ),
674            signature_p2sig=sp.signature(
675                "p2sigsceCzcDw2AeYDzUonj4JT341WC9Px4wdhHBxbZcG1FhfqFVuG7f2fGCzrEHSAZgrsrQWpxduDPk9qZRgrpzwJnSHC3gZJ"
676            ),
677            s=signature,
678            _level=2548242,
679            _chain_id=sp.chain_id_cst("0xaf1864d9feee0e1f"),
680        )
681        c1.pack_lambdas()
682        c1.pack_lambdas2()
683        c1.pack_lambda_record()
684        c1.pack_lambda_variant()
685        c1.pack_unpack_pack_lambda()