The following question has been asked on Telegram:
How can we use lazify entrypoint with new syntax?
The following question has been asked on Telegram:
How can we use lazify entrypoint with new syntax?
You can use lambdas in the storage.
To do this, you can create a field named storage
in self.data
. It will contain everything you usually store in self.data
. Then you can have option of lambdas in self.data
that correspond to the entrypoints you want to lazify. These lambdas parameter is in the form sp.lambda_(sp.pair[t_storage, t_param], t_storage, with_operations=True)
where t_storage
is the type of the storage
field and t_param
the type of the entrypoint parameter.
For example:
import smartpy as sp
@sp.module
def main():
t_storage: type = sp.record(x=sp.int, y=sp.int)
t_lazy_ep: type = sp.record(a=sp.int, b=sp.int)
class MyContract(sp.Contract):
def __init__(self):
self.data.lazy_ep_ = sp.cast(
None,
sp.option[
sp.lambda_(
sp.pair[t_storage, t_lazy_ep],
t_storage,
with_operations=True,
)
],
)
storage = sp.record(x=0, y=1)
self.data.storage = sp.cast(storage, t_storage)
@sp.entrypoint
def lazy_ep(self, param):
sp.cast(param, sp.record(a=sp.int, b=sp.int))
p = (self.data.storage, param)
storage = self.data.lazy_ep_.unwrap_some()(p)
self.data.storage = storage
@sp.entrypoint
def set_lazy_ep(self, l):
self.data.lazy_ep_ = l
@sp.effects(with_operations=True)
def lazy_ep(p):
(storage, param) = p
sp.cast(storage, t_storage)
sp.cast(param, t_lazy_ep)
return sp.record(x=storage.x + param.a, y=storage.y + param.b)
if "templates" not in __name__:
@sp.add_test(name="MyContract")
def test():
sc = sp.test_scenario(main)
c1 = main.MyContract()
sc += c1
c1.set_lazy_ep(sp.some(main.lazy_ep))