Skip to main content
Version: 1.4.0

Testing Michelson code

Testing Michelson code

There are multiple frameworks for testing Michelson contracts, we will not get into details, but here is a list of tutorials showing how to test contracts in Michelson:

Another alternative is to use Mavryk's binary mavryk-client directly. There's a new mockup mode which is does not need a Mavryk node to be running (albeit this is less similar to mainnet than running a Mavryk sandbox node).

Testing with mavryk-client's mockup

We show the main steps that need to be done to use the mockup mode to test our LIGO contracts. As a first step, we need to compile our LIGO contract to Michelson code. Suppose we write the following simple contract:

// This is mockup_testme.jsligo
type storage = string;
type result = [list<operation>, storage];
@entry
const append = (s : string, store: storage): result =>
[list([]), store + s]

To obtain Michelson code from it, we run the LIGO compiler like so:

ligo compile contract gitlab-pages/docs/advanced/src/michelson_testing/mockup_testme.jsligo
# Outputs:
# { parameter string ;
# storage string ;
# code { UNPAIR ; SWAP ; CONCAT ; NIL operation ; PAIR } }

Instead of outputting the resulted compiled code in the screen, we can tell LIGO to write it in a file called mockup_testme.mv:

ligo compile contract gitlab-pages/docs/advanced/src/michelson_testing/mockup_testme.jsligo --output-file mockup_testme.mv

Now it is time to test this Michelson code we obtained: we want to execute it using the mockup mode.

Before anything, make sure you have installed mavryk-client, a simple way to do so is by using opam (opam install mavryk-client).

We can list all the protocols available using mavryk-client list mockup protocols. In this example, we will use Edo for testing, so the command we use for creating a mockup instance on the directory /tmp/mockup/ is:

mavryk-client \
--protocol PtEdoTezd3RHSC31mpxxo1npxFjoWWcFgQtxapi51Z8TLu6v6Uq \
--base-dir /tmp/mockup \
--mode mockup \
create mockup

This command returns a list of Mavryk addresses that we can use with the client in subsequent commands. As recommended in the Mavryk documentation, we can add a shell alias to avoid mistakes:

alias mockup-client='mavryk-client --mode mockup --base-dir /tmp/mockup'

We can list the addresses returned above by running:

mockup-client list known addresses
# Outputs:
# bootstrap5: mv1GvL4GYjch8gDBcnAejHBfNqNWVHoenSVS (unencrypted sk known)
# bootstrap4: mv1SHJm7osyS2G9ge4kjbmUMS3xr36VQWRKe (unencrypted sk known)
# bootstrap3: mv1MLj377UstLn5gzHBr6FQtjM812NbB3RXe (unencrypted sk known)
# bootstrap2: mv1Bbr38otexaqYQBJHHqV4uCYncf2y1HR9k (unencrypted sk known)
# bootstrap1: mv18Cw7psUrAAPBpXYd9CtCpHg9EgjHP9KTe (unencrypted sk known)

We are now ready to originate (or "deploy") the contract on our mockup Mavryk:

mockup-client originate contract mockup_testme \
transferring 0 from bootstrap1 \
running "`cat mockup_testme.mv`" \
--init \"foo\" --burn-cap 0.1

The --init argument ("foo") is the initial storage for our deployed contract. In case we had a more complex storage, we could have used LIGO's compile-storage sub-command to compile a LIGO expression to a Michelson storage.

Now it is time to test! The property we want to check is that if we execute Append ("bar") on our contract with storage "foo", then the contract updates its storage to "foobar".

As a first sanity check, we can confirm that the storage is currently "foo":

mockup-client get contract storage for mockup_testme
# Outputs:
# "foo"

Then, we execute a call to our contract with parameter Append ("bar"). To do so, we first compile the parameter as follows:

So our parameter is simply the string (notice that the constructor Append was removed). We execute a call to the contract with this compiled parameter as follows:

mockup-client transfer 0 from bootstrap2 \
to mockup_testme \
--arg \"bar\" --burn-cap 0.01

We have chosen bootstrap2 as the origin of this call (for no particular reason, any address could do).

We can finally check that that our property holds: the storage is now "foobar":

mockup-client get contract storage for mockup_testme
# Outputs:
# "foobar"

Good! Our contract passed the test successfully!