An Introduction to Michelson: The Scripting Language of Tezos (Part 2)
By: Claude Barde
Tags: Crypto, Smart Contracts, Programming, Blockchain, Tezos

Handling Parameters in a Michelson Smart Contract

In the last lesson, we introduced a few instructions that manipulated data either already in the stack or that we pushed ourselves. But what about handling users’ input? This is actually pretty easy!

We’re going to work with the contract present in the Jupyter notebook introduced above. You’re welcome to follow along in the notebook and run the instructions one by one to get a better understanding of how the stack looks like after each instruction. Here’s the full smart contract:

If you remember, a pair containing the parameter and the storage is automatically pushed at the top of the stack when the contract is called. We can then get the left part of the pair (the parameter) and use it. Here’s how it works.

First, let’s initialize our stack:

Initial state

In this step, we get a fresh stack with a pair containing a parameter of type string ( world ) and the storage of type string , too ( Hello ). Our final goal is to concatenate these two strings and return them.

In the next step, we duplicate the top element of the stack because we need the parameter and the storage in two separate elements of the stack to be able to put them together in one single string.

Duplicating the top element of the stack

Michelson doesn’t have variables and is immutable by nature. New elements of the stack can be created with different methods — for example, by pushing them (with the PUSH instruction) or by duplicating them (with the DUP instruction). Remember at every step of the execution, you’re dealing with the top element of the stack. It’s the only one you can work with.

Let’s start with the first element of the stack, and let’s get the parameter out of the pair. We use the CAR instruction, whose purpose is to break an element of type pair and to return the left side of the pair (in this case, the parameter passed to the smart contract). After running CAR , we’re left with the string world on top of the stack.

Getting the left part of a pair

Now you may be wondering: How can I work with the second element of the stack if Michelson only deals with the first one? It’s very easy! Bring the second element to the top with the SWAP instruction, and keep the other one for later.

Swapping two elements

When using the SWAP instruction, you tell Michelson: “I don’t need the first element for now, but I’d like to work on the second one.” Michelson will bring down the first element to the second position and the second element up to the first position.

Now that the second pair we duplicated at the beginning is at the top, we can use a reverse CAR to get the right part of the pair with the CDR instruction.

Getting the right part of a pair

The CDR instruction works like the CAR instruction, but you’ll keep the right side of the pair you’re manipulating (a pair always contains only two elements, so CDR and CAR are the only possible operations). After that, you can see our stack is made of two elements: in the first position, the string Hello and in the second position, the string world .

Now let’s put them together!

Concatenating two elements

In computer programming, the operation of putting two strings together is called concatenation. It’s only logical that the instruction to put two strings together in Michelson is called CONCAT . The operation takes the two strings currently at the top of the stack and returns a single string. For the operation to be successful, two conditions must be true:

You do have two elements on top of the stack (it’ll generate an error if there’s only one element left). If more elements are present, it’ll only deal with the two elements at the top.

The two elements on top of the stack are strings (Michelson is strictly typed — it knows what kind of values it has in its stack, so don’t even try to fool it)

Now if you remember what we talked about in Part 1, you know what’s going to happen. We must create a pair with a list and the new storage to end the execution of the smart contract (it’s like saying goodbye in Michelson).

First, let’s push an empty list on the top of the stack:

Creating an empty list

Once the empty list is there, we can create a pair with the empty list and the string:

Pairing the list with the storage

Once again, be very wary of the order of your elements and their type: Michelson will take the first element to put it on the left side of the pair and the second element to put it on the right side of the pair. Returning a pair of type (Pair string list (operation)) will generate an error.

Now our stack is clean and contains a pair with a list of operations and a string, the execution of the smart contract comes to an end.

You can see the full execution and the transformations of the stack in the video below: