Interop
LIGO can work together with other smart contract languages on Mavryk. However, data structures might have different representations in Michelson and not correctly match the standard LIGO types.
Michelson types and annotations
Michelson types consist of or
's and pair
's, combined with field
annotations. Field annotations add constraints on a Michelson type,
for example a pair of (pair (int %foo) (string %bar))
will only work
with the exact equivalence or the same type without the field
annotations.
To clarify:
works with
works with
works not with
works not with
In the case of annotated entrypoints --- the annotated or
tree
directly under parameter
in a contract --- you should use
annotations, as otherwise it's unclear which entrypoint you are
referring to.
Michelson layout of LIGO data structures
Right-comb tree by default
By default, the Michelson data representation of LIGO data structures is a location retaining right combed tree, like this:
You can use the decorator @layout comb
to make this choice explicitly:
The next section discusses an alternative layout, which used to be the default one until LIGO version 1.0.
Alternative alphabetically ordered left-balanced tree layout
Before version 1.0, LIGO used to translate its datatypes into an alphabetically ordered left balanced tree by default. So, for example:
will translate to:
This behaviour can be obtained using @layout("tree")
.
Different Michelson annotations
If the Michelson annotation should be different from the LIGO attribute, give it as an argument to the LIGO attribute. For example:
will result into:
The decorator @annot("<name>")
can also be used on record field
annotations:
If the decorators @layout("comb")
and @annot("<name>")
are not
adequate enough for your use-case, LIGO has more advanced advanced
interoperability features, which we will we discuss next.
Advanced interoperability with Michelson
To interoperate with existing Michelson code or to be compatible with
certain development tooling, LIGO has two special interoperation
types: michelson_or
and michelson_pair
. These types give the
flexibility to model the exact Michelson output, including field
annotations.
Take for example the following Michelson type that we want to interoperate with:
To reproduce this type we can use the following LIGO code:
If you do not want to inject a Michelson annotation, the you simply provide an empty string.
Alternatively, if annotations are not important you can also use plain tuples for pair's instead. Plain tuples don't have any annotations.
To use variables of type michelson_or
you have to use M_left
and
M_right
. M_left
picks the left or
case while M_right
picks
the right or
case. For michelson_pair
you need to use tuples.
Manual data structure conversion
If you want to get your hands dirty, it is also possible to do manual data structure conversion. The following code can be used as inspiration:
Entrypoints and annotations
It's possible for a contract to have multiple entrypoints, which is implicitly translated in
LIGO to a parameter
with a variant type as shown below. The following contract:
is translated internally to a contract similar to this one:
This contract can be called by another contract, like this one:
Notice how we directly use the %left
entrypoint without mentioning the
%right
entrypoint. This is done with the help of annotations. Without
annotations it wouldn't be clear what our int
would be referring to.
This currently only works for or
's or variant types in LIGO.
Amendment
With the upcoming 007 amendment to Mavryk this will change though, and also
pair
s can be ordered differently.