Pascal-Chapter Deploycontract

Chapter 23 : Deploy contract

Time to deploy into the battlefield Rookie!

Smart contract

A smart contract is some code written in Michelson language (a low-level stack-based turing-complete language).

It defines all entry points (invocable functions) of the smart contract. It defines the prototype of each entry point (e.g. specifies the parameters types of the entry point). It defines the storage of the smart contract.

Storage

The storage is an allocated memory space associated to a smart contract. The storage is a persistent data store for the smart contract.

The description of the storage is done by strongly-typing the data structure.

Entry points

Entry points of a smart contract describe how to mutate a storage.

Executing an entry point takes some parameters and a state of a storage and returns a new state of storage and some operations

The execution of an entry point produces a new state of the storage of the smart contract. If the entry point execution did not throw an exception and did not fail then the new state of storage is applied.

Operations are transactions (smart contract invocation) that will be sent to some other contracts. They will trigger an entry point of the targeted contract or a tez transfer (no invocation of entry point). If the execution of an entry point produces operations (an ordered list of transactions) then they are sent and executed following the order of the list of operations.

Deploy

A smart contract must be deployed on the blockchain in order to be invoked. When deploying a smart contract on the blockchain, one must specify the initial state of the storage.

Deployment of a smart contract in Tezos is called “origination”.

Here is the syntax of the tezos command line to deploy a smart contract :

tezos-client originate contract <contract_name> for <user> transferring <amount_tez> from <from_user> \
running <tz_file> \
–init ‘<initial_storage>’ –burn-cap <gaz_fee>

<contract_name> name given to the contract

<tz_file> path of the Michelson smart contract code (TZ file).

<amount_tez> is the quantity of tez being transferred to the newly deployed contract. If a contract balance reaches 0 then it is deactivated.

<from_user> account from which the tez are taken from (and transferred to the new contract).

<initial_storage> is a Michelson expression. The –init parameter is used to specify initial state of the storage.

<gaz_fee> it specifies the the maximal fee the user is willing to pay for this operation (using the –burn-cap parameter).

Invoke

Once the smart contract has been deployed on the blockchain (contract-origination operation baked into a block), it is possible to invoke an entry point of the smart contract using the command line.

Here is the syntax of the tezos command line to invoke a smart contract :

tezos-client transfer <amount_tez> from <user> to <contract_name> –arg ‘<entrypoint_invocation>’ –dry-run

<amount_tez> is the quantity of tez being transferred to the contract.

<contract_name> name given to the contract

<entrypoint_invocation> is a Michelson expression specifying the entry point and its corresponding parameters. For example, in Ligo the invocation call ‘Increment(5)’ would be translated into following Michelson expression ‘Left (Right 5)’.

⚠️ Notice that the –arg parameter specifies an entry point call.

⚠️ Notice that the –dry-run parameter simulate the invocation of the entry point.

Ligo compiler

In order to produce a smart contract, a tool called transpiler (aka LIGO compiler) is used to transform LIGO code into Michelson code. Michelson smart contract are stored in a file with .tz extension.

This LIGO compiler is also used to transform “LIGO expression” into “Michelson expression” as needed to deploy or invoke a smart contract.

Compile

Here is how to transform LIGO code into Michelson code using the LIGO compiler in command line.

ligo compile-contract code.ligo mainFunc > code.tz

argument is the name of the “main function” in the .ligo file. (see Chapter “Main Function”).

⚠️ Notice that the output of the command is the Michelson code. We just redirect the command output into a .tz file.

Initial storage

Here is how to transform LIGO expression into Michelson expression using the LIGO compiler in command line.

ligo compile-storage [options] code.ligo mainFunc ‘<ligo_expression>’

<ligo_expression> is a LIGO expression

Invocation parameter

Here is how to transform LIGO expression into Michelson expression using the LIGO compiler in command line.

ligo compile-parameter [options] code.ligo mainFunc ‘<ligo_expression>’

<ligo_expression> is a LIGO expression

Simulating

Here is how to simulate the execution of an entry point using the LIGO compiler in command line.

ligo dry-run [options] code.ligo mainFunc ‘<entrypoint(p)>’ ‘<storage_state>’

<storage*state> state of the storage when simulating the execution of the entry point

<entrypoint(p)> entry point of the smart contract that is invoked (parameter _p* of this entry point is specified between parentheses).

Ligo Expression in command line

Let’s see some example how to transpile a storage LIGO expression into a Michelson one.

Let’s consider this smart contract which associates coordinates to a planet name.

// starmap.ligo
type coordinates is ( int * int * int)
type storage is map (string, coordinates)
type return is (list(operation) * storage)

type parameter is AddPlanet of (string * coordinates) | DoNothing

function addPlanet (const input : (string * coordinates); const store : storage) : return is
block {
const modified : storage = case Map.find_opt(input.0, store) of
Some (p) -> (failwith(“planet already exist”) : storage)
| None -> Map.add (input.0, input.1, store)
end;
} with ((nil : list(operation)), modified)

function main (const action : parameter; const store : storage) : return is
block { skip } with case action of
AddPlanet (input) -> addPlanet (input,store)
| DoNothing -> ((nil : list(operation)),store)
end

AWAITING VALIDATION

Type your solution above and validate your answer