Asset
A[k : asset_key<A>]
Returns an option of asset_value
value from key k
in collection A
. It is none
if key k
is not found in collection A
.
For example, consider the ledger
asset:
asset ledger {
holder : address;
amount : nat;
}
The following returns an option of the record value associated with caller
address:
const h : option<asset_value<ledger>> = ledger[caller];
Parameter
Returns
option<asset_value<A>>
k
.A[k : asset_key<A>].f
Returns the value of field f
of asset k
in collection A
, or fails if k
is not found in A
.
For example, consider the ledger asset:
asset ledger {
holder : address;
amount : nat;
}
The following retrieves the value of amount
field for caller
asset:
const a : nat = ledger[caller].amount;
It is equivalent to:
const a : option<nat> =
match ledger[caller] with
| some av -> av.amount
| none -> fail(("ASSET_NOT_FOUND", "ledger"))
end
When accessing several fields' values of the same asset, it is better gas-wise to retrieve first the asset_value
locally with []
operator, and then access a field value with the .
operator.
Parameters
f
:
literal
Returns
T
f
value of asset k
.Fails with
("ASSET_NOT_FOUND", "A")
k
is not found in collection A
A[k : asset_key<A>]?.f
Accesses field f
of asset with key k
in collection A
. It returns an option of field value, which is none
when asset k
is not found in collection A
.
For example, consider the ledger
asset:
asset ledger {
holder : address;
amount : nat;
}
The following returns an option of field amount
for caller
asset:
const a : option<nat> = ledger[caller]?.amount;
It is equivalent to:
const a : option<nat> =
match ledger[caller] with
| some av -> some(av.amount)
| none -> none
end
When accessing several fields' values of the same asset, it is better gas-wise to retrieve first the asset_value
locally with []
operator, and then access a field value with the .
operator.
Parameters
f
:
literal
Returns
A.contains(k : asset_key<A>)
A
contains asset with key k
.Parameter
Returns
A.count()
A
.Returns
Fails with
does not fail
A.nth(i : nat)
Returns an option of the key of i
th element (starting from 0) in collection A
, according to its order (natural key order for asset
collection).
Implementation is in O(n) complexity (Michelson lists are linked).
Parameter
Returns
option<asset_key<A>>
some
of ith elementnone
wheni
is out of bound
A.sort(f)
Sorts collection A
according to asset field(s) f
, in ascending or descending order.
It is possible to sort according to multiple criteria; consider for example the result
asset:
asset result {
id : string;
score : nat;
time : duration;
}
The following expression sorts result
assets in score
decreasing order, then in time
increasing order; so that the first asset of the resulting asset_view is the result with the highest score and minimal time:
var v := result.sort(desc(score), time)
Parameter
f
:
asset field
asc(f)
for ascending order;asc
is by default and may be omitteddesc(f)
for descending order
Returns
A.sum(f)
Sums values of asset field f
over collection A
.
For example, consider the asset declaration:
asset player {
id : address;
random : nat;
}
The following is the sum of the random
field over asset collection player
:
var r = player.sum(random);
Parameter
f
:
asset field
int
or nat
.Returns
A.select(p)
Selects assets from collection A
that verifies predicate p
.
A predicate is an expression typed bool
; its use the the
keyword to refer to the evaluated asset. For example, consider the following car
asset declaration:
asset car {
vin : string;
nbdoors : nat = 0;
}
The following expression returns cars with number of doors greater than 3:
var v : asset_view<car> = car.select(the.nbdoors > 3);
Parameter
p
:
predicate
Returns
A.head(i : nat)
A
according to its order (natural key order for asset
collection).Parameter
Returns
A.tail(i : nat)
A
according to its order (natural key order for asset
collection).Parameter
Returns
A.to_container()
Returns the basic container of collection of asset A
.
It is used to pass the collection of assets to an entry point.