Michelson language

Michelson must be one of the most interesting programming languages for smart contracts at the moment. It is a stack-based, strictly typed language in which smart contracts are written to secure the Tezos blockchain. Michelson’s bytecode is comparable to Ethereum’s smart contract bytecode, but it is more readable, secure and reliable. All the high-level languages you can use to write smartcontracts for Tezos, such as SmartPy, Ligo or Lorentz, are eventually compiled into Michelson.

In this first article, we’ll dive into the Michelson language, understand what ‘stackable’ means, and write some very simple smart contracts. This article is mainly written for newcomers to programming and/or Tezos development, but intermediate level programmers who want to learn more about Michelson will also find useful information here. We’re going to use the Jupyter kernel, developed by Baking Bad, to write the Michelson code in Jupyter notepad. In each section you will find a link if you want to see how the code works.

Stack

To understand how Michelson works, one of the basic concepts that must be properly understood is the stack . Each Michelson contract is a list of instructions that follow one another. These instructions are in precise order and are executed in the order in which they are written.

Each instruction manages the stack in some way. Think of it as a pile of data. The instructions you write will affect the data in the stack. You can, for example, stack two pieces of data at the top of the pile, delete one at the top, put another piece of data at the top, pass a few tokens, etc. The stack works on a “last in, first out” basis. If you want to access a piece of data that is not at the top of the stack, you need to process the data above it first.

When coding in Michelson, you must remember three basic concepts:

  • New data is placed at the top of the stack.
  • Data on the stack only becomes available when it is at the top of the stack (or in the second position for some operations, as described below).
  • The order in which data is processed is from the top of the stack to the bottom.

Operation PUSH

PUSH value-type value

Structure of the Michelson smart contract

The smart contract in Michelson displays a simple structure consisting of three components:

  • Expected Parameter Type.
  • Storage type.
  • Michelson code.
parameter parameter-type ;
storage storage-type ;
code {
  ...
}

A pair containing a parameter and a storage (pair parameter storage) is always automatically placed on the stack when the code is executed. Remember – if there is no parameter, Unit is used instead.
The code should always return a pair containing list(operations) and (updated) storage. Execution will stop when this pair is the last one in the stack.

The complexity of the Michelson language is often overestimated. This is probably because there are no beginner-friendly guides, and the rare documentation available online is extremely technical and difficult for beginners to read. This is why I decided to go through the process of learning Michelson myself, using the complex documentation to create a series of guides that I hope will be more accessible.

Understanding Michelson is key to understanding and appreciating the uniqueness of the Tezos blockchain and what makes it more secure and useful.

Leave a Reply

Your email address will not be published. Required fields are marked *