---
title: "cardano/value"
---

> Documentation Index
> Fetch the complete documentation index at: https://aiken-lang.com/llms.txt
> Use this file to discover all available pages before exploring further.

# cardano/value

A module for working with native builtin `Value`.

Native values keep policy ids and asset names in canonical order, omit
zero quantities and empty token maps, and bound quantities to signed
128-bit integers. They enable specialized builtins for common operations
without first converting to `Data`.

## Constants

### `zero` {#zero}

```aiken
zero: Value = {}
```

A zero `Value` with no assets whatsoever.

## Functions

### Constructing

### `from_assets` {#from_assets}

```aiken
from_assets(assets: Assets) -> Value
```

Convert an existing [`Assets`](/stdlib/cardano/assets/#Assets) to a built-in `Value`.

```aiken
use cardano/assets
use aiken/primitive/bytearray

const max_quantity = math.pow2(127) - 1
const min_quantity = -math.pow2(127)

value.from_assets(assets.from_lovelace(42)) == { lovelace: 42 }
value.from_assets(assets.from_lovelace(max_quantity) == { lovelace: max_quantity }
value.from_assets(assets.from_lovelace(max_quantity + 1)) → 💥
value.from_assets(assets.from_lovelace(min_quantity)) == { lovelace: min_quantity }
value.from_assets(assets.from_lovelace(min_quantity - 1)) → 💥
value.from_assets(assets.from_asset("", bytearray.replicate(33, 0))) → 💥
```

:::caution
This function fails if any policy id or asset name is longer than 32
bytes, or if any quantity falls outside of the interval $[-2^{127},2^{127}-1]$.
:::

### `from_data` {#from_data}

```aiken
from_data(assets: Data) -> Option<Value>
```

Convert arbitrary `Data` into a `Value`. Returns `None` when the shape isn't
a map of byte-array policy ids to maps of byte-array asset names and integer
quantities.

```aiken
const max_quantity = math.pow2(127) - 1
const my_policy = "0000000000000000000000000000"

value.from_data([Pair("", [Pair("", 42)])]) == Some({ lovelace: 42 })
value.from_data([Pair(my_policy, [Pair("foo", 1)])]) == Some({ my_policy: { "foo": 1 } })
value.from_data(42) == None
value.from_data([Pair("", [Pair("", max_quantity + 1)])]) → 💥
value.from_data([Pair(my_policy, [Pair("foo", 1), Pair("bar", 1)])]) → 💥
value.from_data([Pair(my_policy, [Pair("foo", 1), Pair("foo", 1)])]) → 💥
```

:::caution
A correctly shaped value still fails if it has unsorted or duplicate
keys, empty token maps, zero quantities, keys longer than 32 bytes, or
quantities outside of the interval $[-2^{127},2^{127}-1]$.
:::

### Constructing Forcibly

### `expect_from_data` {#expect_from_data}

```aiken
expect_from_data(assets: Data) -> Value
```

Like [`from_data`](#from_data), but without checking the shape first.

```aiken
value.expect_from_data([Pair("", [Pair("", 42)])]) == { lovelace: 42 }
value.expect_from_data(42) → 💥
// All other failing cases from `from_data` still apply.
```

:::caution
This function fails for malformed or non-canonical data. In particular,
keys must be strictly sorted and no token map or quantity may be empty or
zero. Keys are limited to 32 bytes and quantities to signed 128-bit
integers.
:::

### Inspecting

### `contains` {#contains}

```aiken
contains(self: Value, other: Value) -> Bool
```

Check whether every asset in `other` is present in `self` with at least the
same quantity.

```aiken
let policy_a = #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
let policy_b = #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"

value.contains({ policy_a: { "token": 42 } }, { policy_a: { "token": 14 } }) == True

value.contains({ policy_a: { "token": 42 } }, { policy_a: { "token": 43 } }) == False

value.contains(
  { policy_a: { "token": 42 } },
  { policy_a: { "token": 14 }, policy_b: { "token": 1 } },
) == False

value.contains({ policy_a: { "token": -1 } }, {}) → 💥
```

:::caution
This function fails if either value contains a negative quantity.
:::

### `is_zero` {#is_zero}

```aiken
is_zero(self: Value) -> Bool
```

Check whether a `Value` contains no assets.

```aiken
value.is_zero({}) == True
value.is_zero({ lovelace: 42 }) == False
value.is_zero({ #"00000000000000000000000000000000000000000000000000000000": { "foo": 1 } }) == False
```

### `lovelace_of` {#lovelace_of}

```aiken
lovelace_of(self: Value) -> Int
```

Get the lovelace quantity, or zero when the `Value` contains no lovelace.

```aiken
value.lovelace_of({ lovelace: 42 }) == 42
value.lovelace_of({ #"00000000000000000000000000000000000000000000000000000000": { "foo": 1 } }) == 0
value.lovelace_of({}) == 0
```

### `policies` {#policies}

```aiken
policies(self: Value) -> List<PolicyId>
```

List all policy ids in ascending lexicographic order.

```aiken
value.policies({
  #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 1 },
  #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { #"abcd": 2 },
}) == [
  #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
]

value.policies({ lovelace: 42 }) == [ada_policy_id]
```

:::important
This function is expensive and converts the entire `Value` to `Data`. It
is more expensive than direct inspection functions and fails for values
with more than ~40,000 assets.
:::

### `quantity_of` {#quantity_of}

```aiken
quantity_of(self: Value, policy_id: PolicyId, asset_name: AssetName) -> Int
```

Get the quantity of an asset, or zero when the asset isn't present.

```aiken
value.quantity_of(value.insert({}, "a", "token", 42), "a", "token") == 42
value.quantity_of(value.insert({}, "a", "token", 42), "b", "token") == 0
value.quantity_of({}, "a", "token") == 0
```

### Modifying

### `delete` {#delete}

```aiken
delete(self: Value, policy_id: PolicyId, asset_name: AssetName) -> Value
```

Remove an asset. If the asset isn't present, the `Value` is unchanged.

```aiken
let my_value = value.insert({}, "a", "token", 42)
value.delete(my_value, "a", "token") == {}
value.delete(my_value, "b", "token") == my_value
value.delete(my_value, "a", "not_my_token") == my_value
value.delete({}, "a", "token") == {}
```

### `insert` {#insert}

```aiken
insert(
  self: Value,
  policy_id: PolicyId,
  asset_name: AssetName,
  quantity: Int,
) -> Value
```

Insert an asset quantity, replacing any existing quantity for the same
policy id and asset name. A zero quantity removes the asset instead.

```aiken
let my_value = value.insert({}, "a", "token", 42)
value.quantity_of(my_value, "a", "token") == 42
value.quantity_of(my_value, "b", "token") == 0
value.quantity_of(my_value, "a", "not_my_token") == 0
(my_value |> value.insert("a", "token", 14) |> value.quantity_of("a", "token")) == 14
```

:::important
Unlike [`assets.add`](/stdlib/cardano/assets/#add), this function replaces an
existing quantity instead of adding to it. Use [`merge`](#merge) to add
quantities from two values.
:::

:::caution
This function fails when inserting a key longer than 32 bytes or a
quantity outside of the interval $[-2^{127},2^{127}-1]$.
:::

### `negate` {#negate}

```aiken
negate(self: Value) -> Value
```

Negate every asset quantities.

```aiken
const min_quantity = -math.pow2(127)

value.negate({ lovelace: 42 }) == { lovelace: -42 }
value.negate({ lovelace: -42 }) == { lovelace: 42 }
value.negate({ lovelace: min_quantity }) → 💥
```

:::caution
This function fails when negating the minimum signed 128-bit quantity.
:::

### `restricted_to` {#restricted_to}

```aiken
restricted_to(self: Value, mask: List<PolicyId>) -> Value
```

Keep only assets whose policy id occurs in `mask`.

```aiken
let my_value = {
  #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 1 },
  #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { "y": 1 },
}

value.restricted_to(my_value, [#"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]) == {
  #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { "y": 1 },
}

value.restricted_to(my_value, []) == {}

value.restricted_to(my_value, [#"cccccccccccccccccccccccccccccccccccccccccccccccccccccccc"]) == {}
```

:::important
This function is expensive, for it traverses and rebuilds the entire
`Value`. It fails for values with more than ~40,000 assets.
:::

### `scale` {#scale}

```aiken
scale(self: Value, factor: Int) -> Value
```

Multiply every asset quantity by `factor`. A zero factor produces an empty
`Value`.

```aiken
value.scale({ lovelace: 14 }, 2) == { lovelace: 28 }
value.scale({ lovelace: 14 }, 0) == {}
value.scale(any_value, -1) == value.negate(any_value)
```

:::caution
This function fails if a resulting quantity falls outside the signed
128-bit range.
:::

### `without_lovelace` {#without_lovelace}

```aiken
without_lovelace(self: Value) -> Value
```

Remove lovelace while preserving all native assets.

```aiken
value.without_lovelace({ lovlace: 14 }) == {}
value.without_lovelace({}) == {}
value.lovelace_of(value.without_lovelace(any_value)) == 0
```

### Combining

### `difference` {#difference}

```aiken
difference(left: Value, right: Value) -> Value
```

Subtract all quantities in `right` from their counterparts in `left`.
Assets present only in `right` are included with negative quantities.

```aiken
value.difference(
  {
    #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 42, "y": 14 },
    #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { "x": 42 },
    #"cccccccccccccccccccccccccccccccccccccccccccccccccccccccc": { "x": 1 },
  },
  {
    #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 14 },
    #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { "x": 42 },
  },
) == {
  #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 28 } },
  #"cccccccccccccccccccccccccccccccccccccccccccccccccccccccc": { "x": 1 } },
}
```

:::caution
This function fails if negation or subtraction produces a quantity
outside of the interval $[-2^{127},2^{127}-1]$.
:::

### `merge` {#merge}

```aiken
merge(left: Value, right: Value) -> Value
```

Add corresponding quantities from two values, removing assets whose sum is
zero.

```aiken
value.merge(
  {
    #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 42, "y": 14 },
    #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { "x": 42 },
    #"cccccccccccccccccccccccccccccccccccccccccccccccccccccccc": { "x": 1 },
  },
  {
    #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 14 },
    #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { "x": 42 },
  },
) == {
  #"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": { "x": 56, "y": 14 },
  #"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": { "x": 84 },
  #"cccccccccccccccccccccccccccccccccccccccccccccccccccccccc": { "x": 1 },
}
```

:::caution
This function fails if a sum falls outside of the interval $[-2^{127},2^{127}-1]$.
:::

### Transforming

### `reduce` {#reduce}

```aiken
reduce(
  self: Value,
  start: result,
  with: fn(PolicyId, AssetName, Int, result) -> result,
) -> result
```

Reduce all assets from right to left and in ascending policy-id and
asset-name order.

```aiken
let assets = {} |> value.insert("a", "x", 14) |> value.insert("b", "y", 28)
value.reduce(assets, 0, fn(_, _, quantity, total) { quantity + total }) == 42
```

:::important
This function converts the entire `Value` to `Data` before traversing it,
and fails for values with more than ~40,000 assets.
:::

### `to_data` {#to_data}

```aiken
to_data(self: Value) -> Data
```

Convert a `Value` to its map-shaped `Data` representation.

```aiken
value.to_data({ lovelace: 42 }) == [Pair(#"", [Pair(#"", 42)])]
```

:::caution
This function fails for values with more than ~40,000 assets.
:::

### `to_pairs` {#to_pairs}

```aiken
to_pairs(self: Value) -> Pairs<PolicyId, Pairs<AssetName, Int>>
```

Convert a `Value` to policy-token pairs in ascending lexicographic order.

```aiken
use aiken/collection/dict

value.to_pairs(value.insert({}, "a", "token", 42)) == [Pair("a", Pair("token", 42))]
```

:::important
This function converts the entire `Value` to `Data`. It is relatively
expensive and fails for values with more than ~40,000 assets.
:::

Source: https://aiken-lang.com/stdlib/cardano/value/index.mdx
