Skip to main content

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];

Type of collection A


Parameter


k :

Asset key

Returns


option<asset_value<A>>

Returns an option of the record value of asset k.

Fails with

does not fail


Related

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
info

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


Key of asset to access


f :

literal

field name to access

Returns


T

Returns field f value of asset k.

Fails with

("ASSET_NOT_FOUND", "A")

Fails if 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
info

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


Key of asset to access


f :

literal

field name to access

Returns


Returns an option of field f value of asset k.

Fails with

does not fail


Related

A.contains(k : asset_key<A>)

Tests whether collection A contains asset with key k.

Parameter


Key value to test

Returns


  • true if A contains k
  • false otherwise

Fails with

does not fail


Related

A.count()

Retuns the number of elements in collection A.

Returns


Number of elements

Fails with

does not fail

A.nth(i : nat)

Returns an option of the key of ith element (starting from 0) in collection A, according to its order (natural key order for asset collection).

Parameter


i :

Position of element (between 0 and A.count() - 1)

Returns


option<asset_key<A>>

Option of key of ith element of the collection:
  • some of ith element
  • none when i is out of bound

Fails with

does not fail


Related

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

Asset field name of comparable type; it accepts wrapping decorator to specify sort order:
  • asc(f) for ascending order; asc is by default and may be omitted
  • desc(f) for descending order

Returns


A asset_view of asset A sorted by values of field f.

Fails with

does not fail


Related

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

Asset field name of type int or nat.

Returns


The sum of field f values in collection A.

Fails with

does not fail


Related

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

Predicate to select assets.

Returns


A asset_view of asset A that verifies predicate p.

Fails with

does not fail


Related

A.head(i : nat)

Returns the first i elements of collection A according to its order (natural key order for asset collection).

Parameter


i :

Number of elements to consider

Returns


A asset_view of the first i elements of A (or all elements if A.count() <= i).

Fails with

does not fail


Related

A.tail(i : nat)

Returns the last i elements of collection A according to its order (natural key order for asset collection).

Parameter


i :

Number of elements to consider

Returns


A asset_view of the last i elements of A (or all elements if A.count() <= i).

Fails with

does not fail


Related