1import smartpy as sp
2
3
4@sp.module
5def main():
6 shape: type = sp.variant(Circle=sp.int, Rectangle=sp.pair[sp.int, sp.int])
7
8 class Aux(sp.Contract):
9 @sp.entrypoint
10 def default(self, x):
11 assert x == sp.int(100)
12
13 class C(sp.Contract):
14 def __init__(self):
15 self.data.s = 0
16 self.data.value = 3
17
18 @sp.entrypoint
19 def ep1(self, x, y):
20 r = x * x + y * y
21 assert r == 25
22
23 @sp.entrypoint
24 def ep2(self, x):
25 if x > 42:
26 raise "too big"
27
28 @sp.entrypoint
29 def ep3(self, x):
30 pass
31 assert x <= 42
32 self.data.s = x
33
34 @sp.entrypoint
35 def myEntryPoint(self, params):
36 assert self.data.s <= 123
37 self.data.s += params
38
39 @sp.entrypoint
40 def multiply(self, x, y):
41 self.data.value = x * y
42
43 @sp.entrypoint
44 def add(self, x, y):
45 self.data.value = x + y
46
47 @sp.entrypoint
48 def square(self, x):
49 self.data.value = x * x
50
51 @sp.entrypoint
52 def test_layout(self, x):
53 sp.cast(x, sp.record(a=int, b=sp.string, c=bool).layout((("a", "c"), "b")))
54
55 @sp.entrypoint
56 def squareRoot(self, x):
57 assert x >= 0
58 y = 42
59 while y * y > x:
60 y = (x / y + y) / 2
61 assert y * y <= x and x < (y + 1) * (y + 1)
62 self.data.value = y
63
64 @sp.entrypoint
65 def strings_and_bytes(self):
66 assert "abc" == "abc"
67 assert sp.slice(3, 5, "0123456789") == sp.Some("34567")
68 assert sp.slice(3, 5, "01234") == None
69 assert sp.bytes("0xaBcd") == sp.bytes("abcd")
70
71 assert sp.slice(3, 5, "0123456789") == sp.Some("34567")
72 assert sp.slice(3, 5, "01234") == None
73
74 assert sp.slice(3, 5, sp.bytes("0x00010203040506070809")) == sp.Some(
75 sp.bytes("0x0304050607")
76 )
77 assert sp.slice(3, 5, sp.bytes("0x0001020304")) == None
78
79 assert "ab" + "c" == "abc"
80 assert sp.concat(["ab", "cd", "ef"]) == "abcdef"
81 assert sp.concat(
82 [sp.bytes("ab"), sp.bytes("cd"), sp.bytes("ef")]
83 ) == sp.bytes("abcdef")
84
85 @sp.entrypoint
86 def tuples_and_records(self):
87 assert sp.fst((1, "abc")) == 1
88 (a, b, c) = (42, "abc", True)
89 assert a == 42
90 assert b == "abc"
91 assert c == True
92 x = sp.record(a=42, b="abc")
93 sp.cast(x, sp.record(a=sp.int, b=sp.string))
94
95 @sp.entrypoint
96 def options_and_variants(self):
97 assert not sp.cast(None, sp.option[sp.int]).is_some()
98 assert sp.Some(42).is_some()
99 assert sp.cast(None, sp.option[sp.int]).is_none()
100 assert not sp.Some(42).is_none()
101
102 c = sp.variant.Circle(2)
103 assert c.is_variant.Circle()
104 assert c.unwrap.Circle() == 2
105
106 @sp.entrypoint
107 def lists_sets_and_maps(self):
108 l = [1, 2, 3]
109 s = {1, 2, 3}
110 m = {"a": 65, "b": 66}
111 assert s.contains(1)
112 assert len(l) == 3
113 assert len(s) == 3
114 assert len(m) == 2
115 assert len(l) == 3
116 assert len(s) == 3
117 assert len(m) == 2
118 assert sum([1, 2, 3]) == 6
119 assert sum(sp.cons(1, [2, 3])) == 6
120 # assert m.items() ==
121 assert sum(m.values()) == 131
122 assert sp.concat(m.keys()) == "ab"
123 assert sp.pack(range(7)) == sp.pack([0, 1, 2, 3, 4, 5, 6])
124 assert sp.pack(range(3, 7)) == sp.pack([3, 4, 5, 6])
125 assert sp.pack(range(3, 7, 2)) == sp.pack([3, 5])
126 assert m.get("xyz", default=100) == 100
127 m = {"a": 65, "b": 66}
128 del m["a"]
129 assert not m.contains("a")
130 assert not s.contains(4)
131 s.add(4)
132 assert s.contains(4)
133
134 @sp.entrypoint
135 def lambdas(self):
136 @sp.effects(with_storage="read-write")
137 def f(x):
138 self.data.s = 42
139 return x
140
141 assert f(42) == 42
142
143 @sp.entrypoint
144 def abc(self, x):
145 assert x == 42
146
147 @sp.entrypoint
148 def operations(self, address):
149 sp.set_delegate(None)
150 sp.emit("Hello")
151 sp.emit("World", tag="mytag")
152 sp.emit(sp.record(a="ABC", b="XYZ"), tag="mytag2")
153 sp.emit(sp.record(a="ABC", b="XYZ"), tag="mytag2", with_type=False)
154 sp.transfer(42, sp.mutez(0), sp.self_entrypoint("abc"))
155 c = sp.contract(sp.int, address).unwrap_some()
156 sp.transfer(100, sp.mutez(0), c)
157
158 @sp.entrypoint
159 def arithmetic(self):
160 assert sp.ediv(14, 3) == sp.Some((4, 2))
161 assert sp.ediv(-14, 3) == sp.Some((-5, 1))
162 assert sp.ediv(14, -3) == sp.Some((-4, 2)) # -4 * -3 + 2 == 14
163 assert sp.ediv(-14, -3) == sp.Some((5, 1)) # 5 * -3 + 1 == -14
164 assert 14 / 3 == 4
165 assert -14 / 3 == -5
166 assert 14 / -3 == -4 # -4 * -3 + 2 == 14
167 assert -14 / -3 == 5 # 5 * -3 + 1 == -14
168 assert -4 * -3 + 2 == 14
169 assert 5 * -3 + 1 == -14
170 assert sp.nat(2) - sp.nat(3) == -1
171 assert -sp.nat(2) == sp.int(-2)
172 assert sp.is_nat(2) == sp.Some(2)
173 assert sp.is_nat(-2) == None
174 assert sp.as_nat(2) == 2
175 assert sp.to_int(2) == 2
176 assert sp.to_int(sp.nat(2)) == sp.to_int(2)
177 assert sp.to_int(2) == 2
178 assert sp.mutez(2) == sp.mutez(2)
179 assert sp.mutez(2) + sp.mutez(2) == sp.mutez(4)
180 assert sp.mutez(10) - sp.mutez(8) == sp.mutez(2)
181 assert sp.split_tokens(sp.mutez(100), 1, 20) == sp.mutez(5)
182 assert sp.split_tokens(sp.mutez(100), 2, 20) == sp.mutez(10)
183 assert sp.split_tokens(sp.mutez(101), 1, 20) == sp.mutez(5)
184 assert sp.split_tokens(sp.mutez(101), 2, 20) == sp.mutez(10)
185 assert 42 & 1 == 0
186 assert 42 | 1 == 43
187 assert not 42 == 43
188 assert True or 1 / 0 == 42
189 assert not (False and (1 / 0 == 42))
190 assert sp.split_tokens(sp.mutez(100), 20, 30) == sp.mutez(66)
191 assert 8 / 3 == 2
192 assert -8 / 3 == -3
193 assert 8 / -3 == -2 # -2 * -3 + 2 == 8
194 assert -8 / -3 == 3 # 3 * -3 + 1 == 8
195 a1 = sp.int(8)
196 b1 = sp.int(3)
197 assert sp.cast(
198 sp.ediv(a1, b1), sp.option[sp.pair[sp.int, sp.nat]]
199 ) == sp.Some((2, 2))
200 a2 = sp.int(8)
201 b2 = sp.nat(3)
202 assert sp.cast(
203 sp.ediv(a2, b2), sp.option[sp.pair[sp.int, sp.nat]]
204 ) == sp.Some((2, 2))
205 a3 = sp.nat(8)
206 b3 = sp.int(3)
207 assert sp.cast(
208 sp.ediv(a3, b3), sp.option[sp.pair[sp.int, sp.nat]]
209 ) == sp.Some((2, 2))
210 a4 = sp.nat(8)
211 b4 = sp.nat(3)
212 assert sp.cast(
213 sp.ediv(a4, b4), sp.option[sp.pair[sp.nat, sp.nat]]
214 ) == sp.Some((2, 2))
215 assert sp.cast(sp.ediv(a4, b4), sp.option[sp.pair[sp.nat, sp.nat]]) != None
216
217
218@sp.add_test()
219def test():
220 s = sp.test_scenario("Parsed", main)
221 c = main.C()
222 s += c
223
224 c.ep1(x=3, y=4)
225 c.ep1(x=3, y=5, _valid=False)
226
227 c.ep2(10)
228 c.ep2(100, _valid=False)
229
230 c.ep3(10)
231 s.verify(c.data.s == 10)
232 c.ep3(100, _valid=False)
233
234 c.arithmetic()
235 c.strings_and_bytes()
236 c.tuples_and_records()
237 c.options_and_variants()
238 c.lists_sets_and_maps()
239 c.lambdas()
240
241 aux = main.Aux()
242 s += aux
243 c.operations(aux.address)