Entries, Functions
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:
1
action complete (value : string, amount : int) {
2
3
... (* action body *)
4
5
}
Copied!

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:
1
constant owner : role = @tz1KksC8RvjUWAbXYJuNrUbontHGor25Cztk
2
3
variable threshold : tez = 100tz
4
5
action complete (value : string, amount : int) {
6
called by owner
7
8
require {
9
r : transferred > threshold
10
}
11
12
effect {
13
... (* effect code *)
14
}
15
}
Copied!
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:
1
l : ... expression ... /* l is a label */
Copied!
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
}
Security predicate
security {
s1 : only_by_role(anyaction, admin)
}

Effect

Local variable

A local variable is declared as exampled below:
1
var p = amount + 10tz;
2
...
Copied!

Data assignment

A variable (global or local) is assigned a new value as exampled below:
1
p := amount;
Copied!
After this instruction, the value of p is the value of amount.

Currency transfer

The instruction to transfer currency is exampled below:
1
transfer 10tz to owner
Copied!

Conditional

The basic conditional expression is exampled below :
1
if transferred > threshold then
2
transfer price to owner
3
else
4
fail ("not enough")
Copied!
The require expression fails if the condition is not met:
1
require (transferred > threshold)
Copied!
The failif expression fails if the condition is met:
1
failif (transferred <= threshold)
Copied!

Asset collection

Consider the following car asset identified by its vin id:
1
asset car identified by vin {
2
vin : string;
3
model : string;
4
year : int;
5
}
Copied!
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:
1
for c in car do
2
...
3
done
Copied!