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.
Tezos.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
Tezos.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
to the Michelson code
In a contract, we can make reference to a global constant by using the
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
contract sub-command in the
Registering global constants with
Global constants can be registered on the chain by using
tezos-client as follows:
which will register the global constant with source
Compiling global constants
In general, we can compile a constant doing the following steps:
compile expressionsub-command to compile the expression we are interested in capturing in a constant.
Register the output of
Capture the constant hash given by
tezos-clientand use it in the code with
Compile the contract that uses the hash constant by passing the argument
In case that
tezos-client is not available, LIGO provides a custom
sub-command to compile constants, which works similar to the
expression sub-command, but has the following differences:
The output is given as a escaped JSON string that can be given directly to the
compile contract(or put in a JSON list in a file passed to
The hash of the constant is also given in output, so that it can be used without need to call
For LIGO users, we recommend to use
compile constant as it
simplifies the usage flow (see example below).
Given the following contract
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
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
We can compile the code using the
compile contract sub-command,
passing the file with constants in the flag
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
The string returned by
Test.register_constant can be used via
Tezos.constant, as in the examples above.
A simple usage case is the following, in which we obtain a
michelson_program by using