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 = ... // Any name, any typetype return = operation list * 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 = nat type storage = nat type return = operation list * storage let save (action, store: parameter * storage) : return = (([] : operation list), store)
Entrypoints
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 =
Action_A of nat
| Action_B of string
type storage = {
counter : nat;
name : string
}
type return = operation list * storage
let entry_A (n, store : nat * storage) : return =
([] : operation list), {store with counter = n}
let entry_B (s, store : string * storage) : return =
([] : operation list), {store with name = s}
let main (action, store: parameter * storage) : return =
match action with
Action_A n -> entry_A (n, store)
| Action_B s -> entry_B (s, store)
ℹ️ Now that you created a main function, you can now transpile your code into Michelson 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