Global constants
Since the update to Hangzhou protocol, global constants can be registered on chain. These global constants are Micheline values stored on chain, and they can be referred to from a contract we are deploying. Using global constants, we will be able to originate contracts that (after expansion) surpass size limit for contracts.
API primer
The primitive Mavryk.constant
allows you to use a predefined constant already registered on chain.
It accepts a hash in the form of a string and will require a type annotation.
Using global constants
Global constants are introduced using Mavryk.constant
. This function
expects a constant hash, i.e. a hash that corresponds to a
particular constant that was registered on network.
For instance, the constant hash
expruCKsgmUZjC7k8NRcwbcGbFSuLHv5rUyApNd972MwArLuxEZQm2
corresponds
to the Michelson code
In a contract, we can make reference to a global constant by using the
Mavryk.constant
operation:
Note that the constant's type needs to be annotated.
When we compile a contract, we need to tell LIGO (and Michelson
type-checker) which are the constants that we are assuming to be
already registered in the context. They are passed to the compile
contract
sub-command in the --constants
argument:
Registering global constants with mavryk-client
Global constants can be registered on the chain by using mavryk-client
as follows:
which will register the global constant with source bootstrap1
.
Compiling global constants
In general, we can compile a constant doing the following steps:
Use
compile expression
sub-command to compile the expression we are interested in capturing in a constant.Register the output of
compile expression
usingmavryk-client
.Capture the constant hash given by
mavryk-client
and use it in the code withMavryk.constant
.Compile the contract that uses the hash constant by passing the argument
--constants
.
In case that mavryk-client
is not available, LIGO provides a custom
sub-command to compile constants, which works similar to the compile
expression
sub-command, but has the following differences:
The output is given as a escaped JSON string that can be given directly to the
--constants
argument ofcompile contract
(or put in a JSON list in a file passed to--file-constants
).The hash of the constant is also given in output, so that it can be used without need to call
mavryk-client
.
For LIGO users, we recommend to use compile constant
as it
simplifies the usage flow (see example below).
Usage example
Given the following contract global_const
:
We want to turn the function helper
into a global constant. The first
step is to ask LIGO to compile the constant:
As we can see, the constant hash is:
We can now remove the helper
function from the code, and replace the
references to helper
by
The new version of global_call
looks as follows:
Save the constant's Micheline value in a file consts.json
(that will be
used when calling compile contract
). This file must be a JSON list
consisting of the string returned by compile constant
:
We can compile the code using the compile contract
sub-command,
passing the file with constants in the flag --file-constants
:
Remember that to deploy this contract, first the global constant needs to be registered in the network where we want to deploy it.
Global constants in the testing framework
In the testing framework, global constants can be registered to the
context by using Test.register_constant
. This primitive takes a
michelson_program
representing the constant to be registered, and
returns an string that is the constant hash corresponding to the
constant registered.
The string returned by Test.register_constant
can be used via
Mavryk.constant
, as in the examples above.
A simple usage case is the following, in which we obtain a
michelson_program
by using Test.eval
: