Types
AssetName
A type-alias for ’AssetName`, which are free-form byte-arrays between 0 and 32 bytes.
Alias
AssetName = ByteArrayAssets
A multi-asset output Assets. Contains tokens indexed by PolicyId and AssetName.
Lovelace
Lovelace is a type-alias for Int.
Alias
Lovelace = IntPolicyId
A type-alias for a PolicyId. A PolicyId is always 28-byte long
Alias
PolicyId = Hash<Blake2b_224, Script>Tokens
Token quantities indexed by asset name for a single policy.
Alias
Tokens = Dict<AssetName, Int>Constants
ada_asset_name
ada_asset_name: ByteArray = ""Ada, the native currency, isn’t associated with any AssetName (it’s not
possible to mint Ada!).
By convention, it is an empty ByteArray.
ada_policy_id
ada_policy_id: ByteArray = ""Ada, the native currency, isn’t associated with any PolicyId (it’s not
possible to mint Ada!).
By convention, it is an empty ByteArray.
zero
zero: AssetsConstruct an empty Assets with nothing in it.
Functions
Constructing
from_asset
from_asset(policy_id: PolicyId, asset_name: AssetName, quantity: Int) -> AssetsConstruct Assets from an asset identifier (i.e. PolicyId + AssetName)
and a given quantity.
from_value
from_value(value: Value) -> AssetsConstruct an Assets map from a builtin Value
from_ascending_pairs
from_ascending_pairs(xs: Pairs<PolicyId, Dict<AssetName, Int>>) -> Assetsfrom_asset_list
from_asset_list(xs: Pairs<PolicyId, Pairs<AssetName, Int>>) -> AssetsPromote an arbitrary list of assets into Assets. This function fails
(i.e. halts the program execution) if:
- there’s any duplicate amongst
PolicyId; - there’s any duplicate amongst
AssetName; - the
AssetNamearen’t sorted in ascending lexicographic order; or - any asset quantity is null.
This function is meant to turn arbitrary user-defined Data into safe Assets,
while checking for internal invariants.
from_lovelace
from_lovelace(quantity: Int) -> AssetsConstruct Assets from a lovelace quantity.
Friendly reminder: 1 Ada = 1.000.000 Lovelace
Inspecting
contains
contains(self: Dict<AssetName, Int>, subset: Dict<AssetName, Int>) -> BoolCheck whether some tokens list is contained within another. This is a
specialised version of dict.{contains}.
has_any_nft
has_any_nft(self: Assets, policy: PolicyId) -> BoolCheck whether local Assets carry a quantity of exactly 1 of at least one asset
of the given policy, as expected of an NFT. Other assets are tolerated.
let my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
|> assets.add("bar", "token#2", 14)
assets.has_any_nft(my_assets, "foo") == True
assets.has_any_nft(my_assets, "bar") == False
assets.has_any_nft(my_assets, "baz") == Falsehas_any_nft_strict
has_any_nft_strict(self: Assets, policy: PolicyId) -> BoolCheck whether Assets carry a quantity of exactly 1 of a single asset of
the given policy, as expected of an NFT. Other assets (other than
Ada) aren’t tolerated. Said differently, the check succeeds if and only if
the assets contain no assets other than the expected NFT or Ada.
let my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
|> assets.add("bar", "token#2", 14)
assets.has_any_nft_strict(my_assets, "foo") == False
assets.has_any_nft_strict(my_assets, "bar") == False
assets.has_any_nft_strict(my_assets, "baz") == Falselet my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
assets.has_any_nft_strict(my_assets, "foo") == True
assets.has_any_nft_strict(my_assets, "bar") == Falselet my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
|> assets.add("foo", "token#2", 1)
assets.has_any_nft_strict(my_assets, "foo") == False
assets.has_any_nft_strict(my_assets, "bar") == Falsehas_nft
has_nft(self: Assets, policy: PolicyId, asset_name: AssetName) -> BoolCheck whether Assets carry a quantity of exactly 1 of the given asset,
as expected of an NFT. Other assets are tolerated.
let my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
|> assets.add("bar", "token#2", 14)
assets.has_nft(my_assets, "foo", "token#1") == True
assets.has_nft(my_assets, "foo", "token#2") == False
assets.has_nft(my_assets, "bar", "token#2") == False
assets.has_nft(my_assets, "baz", "token#3") == Falselet my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
|> assets.add("foo", "token#2", 1)
assets.has_nft(my_assets, "foo", "token#1") == True
assets.has_nft(my_assets, "foo", "token#2") == True
assets.has_nft(my_assets, "bar", "token#2") == Falsehas_nft_strict
has_nft_strict(self: Assets, policy: PolicyId, asset_name: AssetName) -> BoolCheck whether a Assets carry a quantity of exactly 1 of the given asset,
as expected of an NFT. Other assets (other than
Ada) aren’t tolerated. Said differently, the check succeeds if and only if
assets contain no assets other than the expected NFT or Ada.
let my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
|> assets.add("bar", "token#2", 14)
assets.has_nft_strict(my_assets, "foo", "asset#1") == False
assets.has_nft_strict(my_assets, "bar", "asset#2") == False
assets.has_nft_strict(my_assets, "baz", "asset#3") == Falselet my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
assets.has_nft_strict(my_assets, "foo", "token#1") == True
assets.has_nft_strict(my_assets, "foo", "token#2") == False
assets.has_nft_strict(my_assets, "bar", "token#2") == Falselet my_assets = assets.from_lovelace(42)
|> assets.add("foo", "token#1", 1)
|> assets.add("foo", "token#2", 1)
assets.has_nft_strict(my_assets, "foo", "asset#1") == False
assets.has_nft_strict(my_assets, "foo", "asset#2") == Falseis_zero
is_zero(self: Assets) -> BoolCheck if Assets have no assets and holds no Ada/Lovelace.
match
match(
left: Assets,
right: Data,
assert_lovelace: fn(Lovelace, Lovelace) -> Bool,
) -> BoolEfficiently compare two asset maps together, allowing a custom behaviour for Ada/Lovelace.
The second parameter is provided as Data, allowing to conveniently compare serialized
datums or similar structurually equivalent types (such as Pairs<PolicyId, Pairs<AssetName, Lovelace>>).
The third argument is a callback function to assert the left and right lovelace
quantities. Its first argument refers to the quantity of the first argument of
match, and the second argument of the callback to the quantity of the second
argument of match. In the absence of lovelace in any assets, it defaults to 0.
const my_assets: Assets =
assets.from_lovelace(30)
|> assets.add("foo", "bar", 1)
|> assets.add("foo", "baz", 42)
const datum: Data =
assets.from_lovelace(20)
|> assets.add("foo", "bar", 1)
|> assets.add("foo", "baz", 42)
True == assets.match(my_assets, datum, >=)
False == assets.match(my_assets, datum, ==)
True == assets.match(my_assets, datum, fn(my_assets_lovelace, datum_lovelace) {
2 * datum_lovelace >= my_assets_lovelace
})
False == assets.match(assets.zero, datum, fn(_, _) { True })match_assets
match_assets(left: Assets, right: Data) -> BoolA version of match which simply ignores lovelace in either operands.
const my_assets: Assets =
assets.from_lovelace(30)
|> assets.add("foo", "bar", 1)
|> assets.add("foo", "baz", 42)
const datum: Data =
assets.from_lovelace(20)
|> assets.add("foo", "bar", 1)
|> assets.add("foo", "baz", 42)
True == assets.match(my_assets, datum)
False == assets.match(assets.zero, datum)lovelace_of
lovelace_of(self: Assets) -> IntA specialized version of quantity_of for the Ada currency.
policies
policies(self: Assets) -> List<PolicyId>A list of all token policies in that Assets with non-zero tokens.
quantity_of
quantity_of(self: Assets, policy_id: PolicyId, asset_name: AssetName) -> IntExtract the quantity of a given asset.
tokens
tokens(self: Assets, policy_id: PolicyId) -> Dict<AssetName, Int>Get all tokens associated with a given policy.
Inspecting Forcibly
expect_lovelace_of
expect_lovelace_of(self: Assets) -> IntMore efficient version of lovelace_of, but fails when
there’s no lovelace quantity in the Assets.
expect_match
expect_match(
left: Assets,
right: Data,
assert_lovelace: fn(Lovelace, Lovelace) -> Bool,
) -> BoolA slightly more efficient version of match which fails if the
provided Data is not akin to a Assets.
expect_match_assets
expect_match_assets(left: Assets, right: Data) -> BoolA slightly more efficient version of match_assets which
fails if the provided Data is not akin to a Assets.
expect_quantity_of
expect_quantity_of(
self: Assets,
policy_id: PolicyId,
asset_name: AssetName,
) -> IntMore efficient version of quantity_of, but fails if the
target policy and asset aren’t present in the assets.
Modifying
negate
negate(self: Assets) -> AssetsNegates quantities of all tokens (including Ada) in that Assets.
v1
|> assets.negate
|> assets.merge(v1)
|> assets.is_zero
// Truerestricted_to
restricted_to(self: Assets, mask: List<PolicyId>) -> AssetsGet a subset of the assets restricted to the given policies.
without_lovelace
without_lovelace(self: Assets) -> AssetsGet Assets excluding Ada.
Modifying Forcibly
expect_tail
expect_tail(self: Assets) -> AssetsRemoves the first policy id from an Assets map, but fails if the Assets
are empty and contains no assets whatsoever.
This is useful to strip Ada forcibly out of assets that are known to contain Ada (e.g. inputs or outputs).
Combining
add
add(
self: Assets,
policy_id: PolicyId,
asset_name: AssetName,
quantity: Int,
) -> AssetsAdd a (positive or negative) quantity of a single token to a assets.
This is more efficient than merge for a single asset.
difference
difference(left: Assets, right: Assets) -> Assetsmerge
merge(left: Assets, right: Assets) -> AssetsCombine two Assets together.
Transforming
flatten
flatten(self: Assets) -> List<(PolicyId, AssetName, Int)>Flatten Assets into a list of 3-tuple (PolicyId, AssetName, Quantity).
Handy to manipulate assets as uniform lists.
flatten_with
flatten_with(self: Assets, with: FlattenStrategy<result>) -> List<result>Flatten Assets into a list of results, possibly discarding some along the
way. In particular, we have:
assets.flatten(my_assets) == assets.flatten_with(my_assets, strategy.triple())reduce
reduce(
self: Assets,
start: result,
with: fn(PolicyId, AssetName, Int, result) -> result,
) -> resultReduce assets into a single result
assets.zero
|> assets.add("a", "1", 10)
|> assets.add("b", "2", 20)
|> assets.reduce(v, 0, fn(_, _, quantity, acc) { acc + quantity })
// 30to_dict
to_dict(self: Assets) -> Dict<PolicyId, Tokens>Convert assets into a dictionary of tokens.
to_pairs
to_pairs(self: Assets) -> Pairs<PolicyId, Tokens>Convert assets into pairs of tokens.
to_value
to_value(self: Assets) -> ValueConvert assets into a builtin Value