Getting started with Ligo (Part 2)

Create smart contracts for the Tezos blockchain using Ligo

Claude Barde
Coinmonks
Published in
6 min readFeb 24, 2020

--

(This is part 2 of a series of tutorials about Ligolang, a smart contract language for Tezos. You can find part 1 here.)

In part 1 of this series, we created a simple contract based on the contract provided when you open the online editor. You can access the contract we created easily with this link.

A big part of writing smart contracts is testing them to verify they behave as planned. The online IDE provides a few functions that can help us achieve it, this will be the subject of this tutorial. We will first have a look at the different functions before pressing some buttons :)

The interface

Whether you’ve been typing the code of Part 1 from scratch ou clicked on the link above, you should be presented with an interface that looks more or less like the following one (as of February 2020):

Screenshot of Ligo online IDE

The IDE can be divided roughly into 4 areas:

  • The navbar area: this will not be very useful for now, but you can find a link for quick access to the documentation if needed while you are writing your code (Docs).
  • The syntaxes area: Ligo supports three different syntaxes, Cameligo (inspired by OCaml, we will have a better look at it in a future tutorial), Pascal (we will most probably not speak about it here) and ReasonML (this is the one we are using here, which is, in my opinion, a great gateway for programmers coming from JavaScript/Solidity). Note that when loading the page, “Cameligo” is selected by default, although we are using Reasonligo. Just switch it manually to Reasonligo in this case.
  • The editor area: this is where you will write your code! There is a simple, but helpful highlighted syntax, automatic indentation, but no support for syntax errors as you type (Ligo team, if you read this!)
  • The “actions” area: this is how I call the zone of the IDE where you can compile, deploy, dry run and evaluate functions and values of your code. We will use this area in this tutorial to test our smart contract.

Now let’s go through the different options given in the dropdown menu in the “Configure” tab.

Compiling the smart contract

Now that our smart contract is ready, we must first make sure that it compiles properly. The compiling process turns our Ligo code into Michelson, the language that’s actually used under the hood to run smart contracts on the Tezos blockchain.
Most of the time, you are not going to change the name of the entry point and use main, so you don’t have to change it in the “Entrypoint” field. You just have to press “Run” and wait a few seconds for the compiler to spit out Michelson code. If there is any problem with your code, you will receive an error message at this time. Once your code is compiled and you see the Michelson code, you can choose to copy it or download it in a text file using the two icons at the top of the “Output” column.

It will be no surprise to you that this code compiles successfully 🙂

Deploying the smart contract

We are not going to use this function here as our goal is only to test the smart contract locally, just know that it will deploy the contract to the Babylon network.

Dry-running the smart contract

This is where we are going to test our smart contract! A dry run allows you to test the different pseudo-entry points of the contracts as if the contract was deployed and you were sending a transaction. The right panel on your screen should look like the following after selecting “Dry run”:

Dry Run

As you can see, there are 3 different fields: the “Entrypoint” field must contain the real entry point of the contract, i.e the main function in our case; the “Parameters” field receives the name of the pseudo-entry point you want to test and its arguments (if any); the “Storage” field takes the current value of your storage.

Although the default parameter in the field is correct, the storage is not, as we replaced the default integer with a record. You can fix it now by typing: {count: 0, name: ""} (i.e the default storage you would probably give to your contract on deployment).

You can now click on “Run”. After waiting for a few seconds, you should see the following result in the “Output” column: (list[] , record[count->1, name->""]). You probably guessed what it means, this is the return value from the main function, a list of operations and the new storage. You can see that the count value has been incremented to 1 and the name value has remained unchanged. It is also important to notice that the IDE doesn’t keep the new storage in memory, it will always dry run using the storage value that you provide in the “Storage” field.

Now let’s save a name in the storage! I am sure you know how to do it now 😏
We are going to remove Increment 1 from the “Parameters” field and add SaveName "John" instead. Do not forget to put the value between double-quotes as Ligo compiler expects a string here. You can keep the default storage with the same value and dry run the contract. If everything goes well, you should see (list[] , record[count->0, name->"John"]).

This is a very basic way of testing your smart contract in the online editor to verify that the values you get are the ones that you expected. This is a very simple smart contract, so the testing is simple, but we will use it later in future tutorials to test more complex smart contracts.

Evaluating a function

This option can be useful if you only want to test one of the functions you assign to a pseudo-entry point and not the whole smart contract. After selecting it, you should see the following interface:

Evaluating a function

We kept the add function in our code, so we don’t have to change the entry point, however, the parameters are now different, as we expect a record for the storage. Remove the default values in the “Parameters” field and type ({count:0,name:""}, 6). This will pass a storage record with a default value and the value 6. Click on “Run” and after a few seconds, you will see in the “Output” column: record[count -> 6 , name -> ""]. And indeed, we were expecting a record with a field count equal to 6!

You can also test the “sub” and “saveName” functions to verify that the storage is correctly updated.

Conclusion

You have now a better understanding of the way the interface of the online editor for Ligo works. The interface is very useful to write contracts, compile them to Michelson, test them and deploy them.

In the next part of this tutorial, we will start diving deeper in Ligo and write a real-world contract that handles different kinds of data and tez!

Stay tuned!

This article is also available on my IPFS-based blog at this address.

Read the Part 3 here

Get Best Software Deals Directly In Your Inbox

--

--

Claude Barde
Coinmonks

Self-taught developer interested in web3 and functional programming