Action

Read, compute and write data.

Contract data is read and written with actions. They are the entry points of the contract.

Arguments

An action may take input arguments. For example the snippet below declare an action named complete which takes two arguments value and amount , respectively of type string and int:

action complete (value : string) (amount : int) = {
... (* action body *)
}

Sections

An Action is made of sections listed below:

name

description

optional

called by

specifies which role(s) may call the action

yes

accept transfer

specifies whether the action accepts incoming currency transfer (refused by default).

yes

require

lists the required condition to pass in order to execute the action.

yes

specification

specifies formal properties the action effect has (see next section)

yes

effect

codes the effect of the action:

  • changes in data storage

  • transfers of currencies

no

For example, the complete action example may be enhanced as follows:

constant owner role = @tz1KksC8RvjUWAbXYJuNrUbontHGor25Cztk
variable threshold tez = 100tz
action complete (value : string) (amount : int) = {
called by owner
accept transfer
require {
r : transferred > threshold
}
effect {
... (* effect code *)
}
}

Effect

Local variable

A local variable is declared as exampled below:

let p = amount + 10tz in
...

Data assignment

A variable (global or local) is assigned a new value as exampled below:

p := amount;

After this instruction, the value of threshold is the value of amount.

Currency transfer

The instruction to transfer currency is exampled below:

transfer 10tz to owner

The recipient of the transfer may be omitted when it is specified in the tez value declaration. Consider the following declaration:

constant price from buyer to seller = 50tz

The transfer of price to seller is simply executed with:

transfer price;

The transfer to buyer is simply executed with:

transfer back price;

Conditional

The basic conditional expression is exampled below :

if transferred > threshold then (
transfer price
) else (
fail "not enough"
)

The require expression fails if the condition is not met:

require (transferred > threshold);

The failif expression fails if the condition is met:

failif (transferred <= threshold)

Asset collection

Consider the following car asset identified by its vin id:

asset car identified by vin = {
vin : string;
model : string;
year : int
}

The following table gives the basic instructions to get, add, remove, update an asset.

operation

expression

get an asset

car.get vid

add an asset

car.add { "1GNEK13ZX3R298984"; "Bugatti Chiron"; "2018" }

remove an asset

car.remove vid

update an asset

car.update "1GNEK13ZX3R298984" { year = 2019 }

retrieve asset nb. i

car.nth i (an asset collection is sorted)

Advanced operations over a collection are listed in the table below:

operation

expression

count

car.count

maximum of a field

car.max { year }

minimum of a field

car.min { year }

sum of a field

car.sum { year }

select a subset collection

car.select { year >= 2019 }

sort a collection

car.sort { year }

Iteration over a collection is as follows:

for (c in car) (
...
)