Contracts β
A contract consists of a state together with a single or multiple entrypoints.
It is a class definition that inherits from sp.Contract
. The constructor (__init__
) makes a call to self.init
and initializes fields that make up the contractβs state.
class Hello(sp.Contract):
def __init__(self):
self.init(x = 0)
@sp.entrypoint
def set_x(newX):
self.data.x = newX
@sp.entrypoint
def check_largerthan_x(p):
sp.verify(p > self.data.x)
self.init(..)
Alias for
self.init_storage(argΒ =Β None,Β **kwargs)
**
self.init_storage(argΒ =Β None,Β **kwargs)
**Set the storage of a contract with no initial storage.
arg
andkwargs
cannot be both present.
kwargs
is a dictionary of fields that are added to the storage (or removed when value isNone
).There are two different ways to call
self.init_storage
in a contract:- on a single value
self.init_storage(arg)
wherearg
is a SmartPy expression; - on a list of fields with values,
self.init_storage(**kwargs)
wherekwargs
is a dictionary.
- on a single value
**
self.update_initial_storage(argΒ =Β None,Β **kwargs)
**Update the storage of a contract.
arg
andkwargs
cannot be both present.kwargs
is a dictionary of fields that are added to the storage (or removed when value isNone
).self.update_initial_storage(e)
replaces the former storage whileself.update_initial_storage(**kwargs)
expects the former storage to be a record and adds, changes or removes (when value isNone
) former fields. This is very useful when we use complex inheritance patterns.
class Hello(sp.Contract):
def __init__(self):
""" Alias for init_storage. """
self.init(x = 0)
class Hello(sp.Contract):
def __init__(self):
""" Initialize the storage to a record containing
a single field x of value 0. """
self.init_storage(x = 0)
class Hello(sp.Contract):
def __init__(self):
""" It's allowed to call update_initial_storage even if no storage is defined. """
self.update_initial_storage(x = 0)
class Hello(sp.Contract):
def __init__(self):
self.init_storage(x = 0, y = 1)
class Hello(sp.Contract):
def __init__(self):
""" Define x then add y. """
self.init_storage(x = 0)
self.update_initial_storage(y = 1)
class Hello(sp.Contract):
def __init__(self):
""" Define x and t then remove x and add y. """
self.init_storage(x = 0, t = 12)
self.update_initial_storage(x = None, y = 1)
Setting storage type β
class Hello(sp.Contract):
def __init__(self):
self.init_type(sp.TRecord(x = sp.TNat))
self.init(x = 0)