Skip to content
On this page

Unit

The sp.unit type in SmartPy represents the absence of a meaningful result or value. It corresponds to the unit type in Michelson.

The unit type is commonly used in contracts and in variants.

Using unit in contract parameters

On Tezos every contract has a parameter type. For example, you can call a contract of type sp.contract[sp.int] with an argument of type sp.int. This example calls a contract with this type:

smartpy
sp.transfer(42, sp.mutez(0), contract)

But what if a contract doesn't require any arguments? In this case it can specify a parameter of type sp.unit, giving the contract the type sp.contract[sp.unit].

To call such a contract from SmartPy, pass the unit value, represented in SmartPy as (). For example, this code calls a contract that has the type sp.contract[sp.unit]:

sp.transfer((), sp.mutez(0), contract)

For more information about contracts and calling them, see Contracts and sp.transfer.

Using unit in variants

SmartPy variants can be used to represent alternative cases with extra information. For example this variant represents a shape that can be either a circle or a rectangle:

smartpy
shape : type = sp.variant(
   Circle=sp.int,
   Rectangle=sp.pair[sp.int, sp.int]
)

In the previous example, a circle comes with only one number (its radius) and a rectangle carries two numbers (its length and width).

But what about cases that don't require any extra info? sp.unit to the rescue! Here is a type that models events coming from a physical sensor:

smartpy
sensor_event : type = sp.variant(
    TemperatureChange=sp.int,
    HumidityChange=sp.int,
    MotionDetected=sp.unit
)

Temperature and humidity changes each come with a number, whereas for detected motion there is no extra information, hence sp.unit.