templates.lambdas

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    class Created(sp.Contract):
  7        def __init__(self, a, b):
  8            sp.cast(a, sp.int)
  9            sp.cast(b, sp.nat)
 10            self.data.a = a
 11            self.data.b = b
 12
 13        @sp.entrypoint
 14        def myEntryPoint(self, params):
 15            self.data.a += params.x
 16            self.data.b += params.y
 17
 18    class MyContract(sp.Contract):
 19        def __init__(self):
 20            self.data.value = 0
 21            self.data.ggg = sp.Some(42)
 22            self.data.fff = None
 23            self.data.abcd = 0
 24            self.data.f = lambda x: x + 1
 25
 26        @sp.entrypoint
 27        def f(self):
 28            toto = lambda x: sp.fst(x) + sp.snd(x)
 29            titi = toto.apply(5)
 30            self.data.value = titi(8)
 31
 32        @sp.entrypoint
 33        def flambda(self):
 34            self.data.value = self.flam(self.flam(15)) + self.square_root(12345)
 35
 36        @sp.private()
 37        def flam(self, params):
 38            return 322 * params
 39
 40        @sp.private()
 41        def square_root(self, x):
 42            assert x >= 0
 43            y = x
 44            while y * y > x:
 45                y = (x / y + y) / 2
 46            assert (y * y <= x) and (x < (y + 1) * (y + 1))
 47            return y
 48
 49        @sp.private()
 50        def comp(self, params):
 51            return params.f(params.x)
 52
 53        @sp.entrypoint
 54        def comp_test(self):
 55            self.data.abcd = self.comp(sp.record(f=lambda x: x + 3, x=2))
 56
 57        @sp.private()
 58        def abs(self, x):
 59            if x > 0:
 60                return x
 61            else:
 62                return -x
 63
 64        @sp.entrypoint
 65        def abs_test(self, x):
 66            self.data.abcd = self.abs(x)
 67
 68        @sp.entrypoint
 69        def h(self):
 70            def sqrt(x):
 71                assert x >= 0
 72                y = x
 73                while y * y > x:
 74                    y = (x / y + y) / 2
 75                assert y * y <= x and x < (y + 1) * (y + 1)
 76                return y
 77
 78            self.data.fff = sp.Some(sqrt)
 79
 80        @sp.entrypoint
 81        def hh(self, params):
 82            self.data.value = self.data.fff.unwrap_some()(params)
 83
 84        @sp.entrypoint
 85        def i(self):
 86            def ch1(x):
 87                assert x >= 0
 88
 89            def ch2(x):
 90                assert x >= 0
 91                return x - 2
 92
 93            def ch3(x):
 94                assert x >= 0
 95                return True
 96
 97            def ch4(x):
 98                def check3bis(x):
 99                    assert x >= 0
100                    return False
101
102                assert x >= 0
103                return 3 * x
104
105            self.data.value = ch4(12)
106            assert self.not_pure() == self.data.value
107
108        @sp.entrypoint
109        def operation_creation(self):
110            @sp.effects(with_operations=True)
111            def test(x):
112                _ = sp.create_contract(Created, None, sp.tez(0), sp.record(a=x, b=15))
113                _ = sp.create_contract(
114                    Created, None, sp.tez(0), sp.record(a=2 * x, b=15)
115                )
116
117            f = test
118            f(12345001)
119            f(12345002)
120
121        @sp.entrypoint
122        def operation_creation_result(self):
123            @sp.effects(with_operations=True)
124            def test(x):
125                _ = sp.create_contract(Created, None, sp.tez(0), sp.record(a=x, b=15))
126                return 4
127
128            f = test
129            x = f(12345001)
130            y = f(12345002)
131            sum = x + y
132
133        @sp.entrypoint
134        def managed_operation_creation(self):
135            @sp.effects()
136            def test(x):
137                ops = []
138                r = sp.create_contract_operation(
139                    Created, None, sp.tez(0), sp.record(a=x, b=15)
140                )
141                ops.push(r.operation)
142                r = sp.create_contract_operation(
143                    Created, None, sp.tez(0), sp.record(a=2 * x, b=15)
144                )
145                ops.push(r.operation)
146                return ops
147
148            f = test
149            ops = f(12345001)
150            for op in reversed(ops):
151                sp.operations.push(op)
152            ops = f(12345002)
153            for op in reversed(ops):
154                sp.operations.push(op)
155
156        # @sp.private()
157        # def oh_no(self, params):
158        #     with sp.set_result_type(sp.int):
159        #     if params > 0:
160        #         sp.failwith("too big")
161        #     else:
162        #         sp.failwith("too small")
163
164        @sp.private(with_storage="read-write")
165        def not_pure(self, x):
166            return self.data.value
167
168        # @sp.entrypoint
169        # def fact(self, n):
170        #     fact = sp.compute(sp.build_lambda((lambda n, fact: sp.eif(n<=1,1,n*fact(n-1))), recursive=True))
171        #     self.data.abcd = fact(n)
172
173    c1: type = sp.pair[sp.nat, sp.unit]
174    c2: type = sp.pair[sp.address, c1]
175    c3: type = sp.pair[sp.bool, c2]
176    c4: type = sp.pair[sp.int, c3]
177    t_record: type = sp.record(a=sp.int, b=sp.bool, c=sp.address, d=sp.nat, e=sp.unit)
178
179    def format_record(param):
180        sp.cast(param, c4)
181        (a, p2) = param
182        (b, p3) = p2
183        (c, p4) = p3
184        (d, e) = p4
185        return sp.cast(sp.record(a=a, b=b, c=c, d=d, e=e), t_record)
186
187    class C2(sp.Contract):
188        def __init__(self):
189            self.data.value = sp.cast(None, sp.option[t_record])
190            l1 = format_record.apply(1)
191            l2 = l1.apply(True)
192            l3 = l2.apply(sp.address("tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU"))
193            l4 = l3.apply(2)
194            self.data.l1 = l1
195            self.data.l2 = l2
196            self.data.l3 = l3
197            self.data.l4 = l4
198
199        @sp.entrypoint
200        def exec_l1(self):
201            self.data.value = sp.Some(
202                self.data.l1(
203                    (
204                        True,
205                        (sp.address("tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU"), (2, ())),
206                    )
207                )
208            )
209
210        @sp.entrypoint
211        def exec_l2(self):
212            self.data.value = sp.Some(
213                self.data.l2(
214                    (sp.address("tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU"), (2, ()))
215                )
216            )
217
218        @sp.entrypoint
219        def exec_l3(self):
220            self.data.value = sp.Some(self.data.l3((2, ())))
221
222        @sp.entrypoint
223        def exec_l4(self):
224            self.data.value = sp.Some(self.data.l4())
225
226
227@sp.add_test()
228def test():
229    scenario = sp.test_scenario("Lambdas", main)
230    scenario.h1("Lambdas")
231    c1 = main.MyContract()
232    scenario += c1
233    c1.f()
234    c1.flambda()
235    c1.i()
236    c1.comp_test()
237    c1.abs_test(5)
238    scenario.verify(c1.data.abcd == 5)
239    c1.abs_test(-42)
240    scenario.verify(c1.data.abcd == 42)
241    # c1.fact(3)
242    # scenario.verify(c1.data.abcd == 6)
243    # c1.fact(5)
244    # scenario.verify(c1.data.abcd == 120)
245
246    c2 = main.C2()
247    scenario += c2
248    c2.exec_l1()
249    c2.exec_l2()
250    c2.exec_l3()
251    c2.exec_l4()