Interop
LIGO can work together with other smart contract languages on Tezos. However data structures might have different representations in Michelson and not correctly match the standard LIGO types.
#
Michelson types and annotationsMichelson types consist of or
's and pair
's, combined with field annotations.
Field annotations add contraints 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
info
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.
#
Default LIGO outputBy default LIGO translates its datatypes into a alphabetically left balanced tree. So, for example:
will translate to:
#
Right combed tree outputIf you want to change the data representation in Michelson to a location retaining right combed tree, like this:
you can use the layout:comb
attribute:
The layout:comb
attribute can also be used on record types:
#
Different Michelson annotationsIf the Michelson annotation should be different from the LIGO representation,
the annot:<string>
attribute can be used. For example:
will result into:
The annot:<string>
attribute can also be used on record field annotations:
If the layout:comb
and annot:<string>
attributes are not adequate enough
for your use case, LIGO has more advanced advanced interop features which we
will we discuss next.
#
Advanced interop with MichelsonTo interop with existing Michelson code or for compatibility with certain
development tooling, LIGO has two special interop 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 interop with:
To reproduce this type we can use the following LIGO code:
If you don't want to have an annotation, you need to provide an empty string.
info
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.
#
Helper functionsConverting between different LIGO types and data structures can happen in two ways. The first way is to use the provided layout conversion functions, and the second way is to handle the layout conversion manually.
info
In all cases it will increase the size of the smart contract and the conversion will happen when running the smart contract.
#
Converting left combed Michelson data structuresHere's an example of a left combed Michelson data structure using pairs:
Which could respond with the following record type:
If we want to convert from the Michelson type to our record type and vice versa, we can use the following code:
In the case of a left combed Michelson or
data structure, that you want to
translate to a variant, you can use the michelson_or_left_comb
type.
For example:
And then use these types in Layout.convert_from_left_comb
or
Layout.convert_to_left_comb
, similar to the pair
s example above, like this:
#
Converting right combed Michelson data structuresIn the case of right combed data structures, like:
you can almost use the same code as that for the left combed data structures,
but with michelson_or_right_comb
, michelson_pair_right_comb
,
Layout.convert_from_right_comb
, and Layout.convert_to_left_comb
respectively.
#
Manual data structure conversionIf you want to get your hands dirty, it's also possible to do manual data structure conversion.
The following code can be used as inspiration:
#
Entrypoints and annotationsIt's possible for a contract to have multiple entrypoints, which translates in
LIGO to a parameter
with a variant type as shown here:
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.
#
AmendmentWith the upcoming 007 amendment to Tezos this will change though, and also
pair
's can be ordered differently.