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

refuse transfer

specifies whether the action refuses incoming currency transfer (accepted 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
require {
r : transferred > threshold
}
effect {
... (* effect code *)
}
}

In the expression r : ... line 8 above, r is a label for the require expression (see Label section below).

Label

In Archetype, some expressions are named. The following syntax is used to name an expression:

l : ... expression ... /* l is a label */

The following is the list of expressions that require a label:

Type

Example

Require

require {

enough_transfer : transferred > threshold

}

Fail if

failif {

not_enough_transfer : transferred <= threshold

}

Asset invariant

asset mile {

quantity : int

} with {

quant_strictly_pos : quantity > 0

}

(see Contract specification)

Security predicate

security {

s1 : only_by_role(anyaction, admin)

}

(see Contract specification)

Effect

Local variable

A local variable is declared as exampled below:

var p = amount + 10tz;
...

Data assignment

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

p := amount;

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

Currency transfer

The instruction to transfer currency is exampled below:

transfer 10tz to owner

Conditional

The basic conditional expression is exampled below :

if transferred > threshold then
transfer price to owner
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 ({ vin = "1GNEK13ZX3R298984"; model = "Bugatti Chiron"; year = 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 ()

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 do
...
done