Pascal-Chapter Main Function

Chapter 13 : Main function

God damn it rookie! What are you still doing at the space port? Take off already and go shoot some alien!!

Smart contracts are small programs that are stored and executed on the blockchain. They allow people to cooperate and exchange tokens without requiring them to trust one another.

A LIGO contract is made of a series of constant and function declarations. Only functions having a special type can be called when the contract is activated: we call them main functions. A main function takes two parameters, the contract parameter and the on-chain storage, and returns a pair made of a list of operations and a (new) storage.

When the contract is originated, the initial value of the storage is provided. When a main function is later called, only the parameter is provided, but the type of a main function contains both.

The type of the contract parameter and the storage are up to the contract designer, but the type for list operations is not. The return type of a main function is as follows, assuming that the type storage has been defined elsewhere. (Note that you can use any type with any name for the storage.)

type storage is// Any name, any type
type return is list (operation) * storage

The contract storage can only be modified by activating a main function: given the state of the storage on-chain, a main function specifies how to create another state for it, depending on the contract’s parameter.

Here is an example where the storage is a single natural number that is updated by the parameter.

type parameter is nat
type storage is nat
type return is list (operation) * storage

function save (const action : parameter; const store : storage) : return is
((nil : list (operation)), store)


In LIGO, the design pattern is to have one main function called main, that dispatches the control flow according to its parameter. Those functions called for those actions are called entrypoints.

As an analogy, in the C programming language, the main function is the unique main function and any function called from it would be an entrypoint.

The parameter of the contract is then a variant type, and, depending on the constructors of that type, different functions in the contract are called. In other terms, the unique main function dispatches the control flow depending on a pattern matching on the contract parameter.

In the following example, the storage contains a counter of type nat and a name of type string. Depending on the parameter of the contract, either the counter or the name is updated.

type parameter is
Action_A of string
| Action_B of string

type storage is record [
stored_string_A : string;
stored_string_B : string

type return is list (operation) * storage

function entry_A (const input_string : string; const store : storage) : return is
((nil : list (operation)), store with record [stored_string_A = input_string])

function entry_B (const input_string : string; const store : storage) : return is
((nil : list (operation)), store with record [stored_string_B = input_string])

function main (const action : parameter; const store : storage): return is
case action of
Action_A (input_string) -> entry_A (input_string, store)
| Action_B (input_string) -> entry_B (input_string, store)

ℹ️ Now that you created a main function, you can now transpile your code into Michaelson and deploy it on Tezos. Try it out on the LIGOlang IDE

Your mission

1- The editor contains an example of main function with two functions. In the parameter variant, replace Action_A and Action_B with our actions Set_ship_code and Go_to

2- In the storage record, replace stored_string_A and stored_string_B with the strings we want to store in the contract: ship_code and destination

3- Modify the name of our entrypoints entry_A and entry_B to set_ship_code and go_to

4- Modify the main function to reflect the new names above


Type your solution above and validate your answer