How to Use the Michelson Programming Language to Write Smart Contracts on TezosS

June 14th 2020

Author profile picture

@claudebardeClaude Barde

Tezos good contract developer within the making 🌮

github social icon

This is (Part 1): An Introduction to Michelson: the Scripting Language of Tezos

Michelson will have to be one of essentially the most thrilling programming languages for good contracts at the present time. It’s a stack-based, strictly typed language wherein good contracts are written to make sure the protection of the Tezos blockchain. Michelson is analogous to the bytecode of the Ethereum good contracts but it surely’s extra readable, more secure, and extra tough. All the high-level languages you’ll use to put in writing good contracts for Tezos — like SmartPy, Ligo, or Lorentz — in the end bring together right down to Michelson.

In this primary article, we can dip our ft into Michelson language, perceive what “stack-based” approach and write some quite simple good contracts. This article is principally written for rookies in programming and/or Tezos building, however intermediate programmers who wish to know extra about Michelson may even in finding helpful data right here. We are going to make use of the Jupyter kernel advanced through Baking Bad to put in writing Michelson code within the Jupyter pocket book. You will discover a hyperlink in every segment if you wish to see the code at paintings.

Let’s write some code!

The stack

To know the way Michelson works, one of the principle ideas to know correctly is the stack. Every Michelson contract is an inventory of directions that practice every different. These directions are in an exact order and are done within the order they’re written in.

Every instruction will manipulate the stack by some means. Imagine it as a pile of information. The directions you write will purpose an impact at the knowledge provide within the pile. You can, as an example, upload in combination two items of information on best of the pile, take away the one at the best, put every other piece of information on best, switch some tokens, and so on. The stack works on a remaining in, first out foundation: if you wish to get admission to a work of information that isn’t on the best of the stack, you will have to first take care of those above it.

There are three primary ideas you will have to take into accout when coding in Michelson:

  • New knowledge is going on best of the stack.
  • The knowledge within the stack handiest change into obtainable when they’re on the best of the stack (or in the second one place for some operations, as described beneath).
  • The order wherein the information are processed is going from the highest of the stack to the ground.

Let’s have a look at an instance.

The PUSH Operation

If you need so as to add a work of information on best of the stack, you’ll name the PUSH operation. This is the way it works:

Note that there might already be knowledge within the stack, wherein case the brand new price will likely be placed on best of them. This is the way you push new knowledge in Michelson:

PUSH value-type price
For instance, if you wish to push an integer, you’ll write 

PUSH int 2

, for a string, you’ll write 

PUSH string "Tezos"


The Michelson Smart Contract Structure

A sensible contract in Michelson shows a easy construction made from three elements:

  • The form of the anticipated parameter.
  • The form of the garage.
  • The Michelson code.

This interprets to the next code:

parameter parameter-type ;
garage storage-type ;
code { ...

In addition to this construction, there are two regulations you will have to have in mind when writing a sensible contract in Michelson:

  1. A couple containing the parameter and the garage 
    (pair parameter garage) 

    is all the time routinely driven to the stack when the code is done. Remember — if there’s no parameter, 


     is used as an alternative.

  2. The code will have to all the time go back a couple containing an inventory of operations and the (up to date) garage 
    (pair listing(operation) garage)

    . The execution will prevent when this type of pair is the very last thing ultimate within the stack.

A Simple Michelson Smart Contract

Now that we learn about PUSH and the construction of a sensible contract in Michelson, let’s write one!

For this contract, we’re going to write a “Hello global” contract and save a string into the garage:

Here’s what occurs when this code is done:

  1. parameter unit

     signifies that the handed parameter is of form 


     (principally, no parameter).

  2. garage string

     signifies that the contract has garage of form 



  3. DROP

     is an operation code that eliminates no matter is on the best of the stack. Remember, we mentioned previous {that a} pair with the parameter and the garage is routinely incorporated on best of the stack in the beginning, we don’t seem to be going to make use of it, we will be able to simply drop it.

  4. PUSH

     brings a price on best of the stack, right here the string “Hello global”.

  5. NIL

     is an opcode that provides an empty listing of the desired form (


     right here) on best of the stack.

  6. PAIR

     takes the two components on best of the stack, creates a brand new pair containing those two components, and pushes again the pair at the stack.

Note: Every instruction ends with a semi-colon (it’s not obligatory for the remaining instruction although).

Adding Integers and Saving the Result

Let’s introduce a brand new operation: 


. You most certainly guessed what it does — upload two numerical values in combination.

Here’s a easy contract that demonstrates the way it works:

Let’s undergo every operation to know what is occurring throughout the stack:

parameter unit

: Once once more, we don’t seem to be the usage of any parameter, so we’re passing a unit.

garage int

: This time, we’re saving a price of form integer into the garage.


: We don’t want the preliminary pair, so we will be able to eliminate it to make room for the values we can in fact want.

PUSH int 2 ; PUSH int 3 ;

: Note that the order is an important. 

int 2

 goes to be on the backside of the stack if you push 

int 3

. In the case of an addition, the order doesn’t subject an excessive amount of, however in case you subtract two numbers, as an example, it’s very important to push them in the proper order.


 works at the similar concept as 


. You take the primary two components on best of the stack and get a unmarried price out of them that you simply ward off to the stack. 


 will upload two numbers in combination. Note that the numbers must be either one of the similar numerical form (you can’t, as an example, upload an integer and a nat in combination).


: As within the earlier contract, we push an empty listing of operations.


: Creates the pair containing the listing of operations and the brand new garage we want to prevent the execution of the contract.


The complexity of the Michelson language is frequently overrated. That’s most certainly because of the truth that there aren’t any beginner-friendly tutorials in the market and the uncommon documentation to be had on-line is terribly technical and hard to learn for neophytes. This’s why I made up our minds to head during the technique of finding out Michelson myself, the usage of the tricky documentation to create a sequence of tutorials that I’m hoping are extra obtainable.

Understanding Michelson is essential to figuring out and appreciating the distinctiveness of the Tezos blockchain and what makes it extra safe and extra helpful.

In the following phase, we can proceed diving into Michelson. We’ll write some easy good contracts and discover the superb Jupyter notebooks created through the Baking Bad crew that can permit us to put in writing Michelson code and perceive precisely what’s happening.

Stay tuned!


The Noonification banner

Subscribe to get your day-to-day round-up of best tech tales!