# Assets, Amounts, Balances, and Volumes

Source: https://docs.formance.com/v4.0/modules/ledger/modeling/assets-amounts-and-balances

Stack: v4.0

Ledger records amounts as unsigned integers. The asset identifier carries the unit and, when needed, its decimal precision. This keeps monetary arithmetic deterministic and avoids floating-point rounding inside the ledger.

To run these behaviors against a live ledger, follow [Amounts, balances, and volumes](https://docs.formance.com/v4.0/modules/ledger/hands-on#amounts-balances-and-volumes) in the hands-on walkthrough. It sets up a disposable ledger once and reproduces every result shown below.

For the complete route contracts, see [Create Transactions with Postings](https://docs.formance.com/v4.0/modules/ledger/transactions/create-with-postings) and [Revert Transactions](https://docs.formance.com/v4.0/modules/ledger/transactions/revert).

## Define one asset identifier

An asset uses an uppercase base followed by an optional decimal precision:

```text
EUR/2
USD/2
TOKEN/18
POINTS
```

The base must match `[A-Z][A-Z0-9]{0,16}`. The optional precision is an integer from 1 through 255 without a leading zero. `USD`, `USD/2`, and `USD/4` are distinct stored assets.

Choose one representation for each business asset and use it in every writer. Inconsistent precision creates separate balances that only look related to a human reader.

## Convert display values to integers

The precision states how many decimal places your application applies when it displays an amount:

| Display value | Asset      |         Ledger amount |
| ------------- | ---------- | --------------------: |
| EUR 12.34     | `EUR/2`    |                `1234` |
| USD 0.01      | `USD/2`    |                   `1` |
| 1 token       | `TOKEN/18` | `1000000000000000000` |
| 25 points     | `POINTS`   |                  `25` |

Use the same rule in both directions:

```text
ledger amount = display amount * 10^precision
display amount = ledger amount / 10^precision
```

Perform this conversion with decimal or integer arithmetic. Do not pass an IEEE-754 floating-point result into a transaction request.

Posting amounts and cumulative volumes can use the full unsigned 256-bit range. JavaScript `Number` cannot represent all of these integers. Use `BigInt`, a decimal library, or a serializer that preserves the original integer text.

The HTTP transaction request represents a posting amount as a JSON integer:

```json
{
  "postings": [
    {
      "source": "world",
      "destination": "customers:alice:wallets:main",
      "asset": "EUR/2",
      "amount": 1234
    }
  ]
}
```

Ledger rejects malformed asset identifiers and any operation that makes a cumulative input or output volume exceed `2^256 - 1`.

## Read input, output, and balance

For each `(account, asset, color)` bucket, Ledger maintains two cumulative volumes:

- `input` is the total received by the account;
- `output` is the total sent by the account;
- `balance` is `input - output`.

Consider three movements in `EUR/2`:

1. `world` sends `10000` to Alice;
2. Alice sends `2500` to a merchant;
3. the merchant sends `500` to platform fees.

| Account                        | Input | Output | Balance |
| ------------------------------ | ----: | -----: | ------: |
| `customers:alice:wallets:main` | 10000 |   2500 |    7500 |
| `merchants:acme:earnings`      |  2500 |    500 |    2000 |
| `platform:fees`                |   500 |      0 |     500 |

Reading an account returns those volumes as decimal strings:

```json
{
  "data": {
    "address": "customers:alice:wallets:main",
    "volumes": [
      {
        "asset": "EUR/2",
        "color": "",
        "volumes": {
          "input": "10000",
          "output": "2500",
          "balance": "7500"
        }
      }
    ]
  }
}
```

Parse these response values as arbitrary-precision integers. A balance can be negative when a transaction explicitly permits an overdraft, even though `input` and `output` remain unsigned.

## Volumes preserve movement history

Input and output answer different questions from balance. Two accounts can have the same balance but very different activity.

A reversal records new postings in the opposite direction. It does not subtract from the original cumulative volumes. After a transfer and its reversal, the balance can return to its previous value while both input and output have increased.

For `NORMAL` account types, these volumes remain available after the balance returns to zero. An `EPHEMERAL` type can remove a zero-balance volume row, and a `TRANSIENT` type does not store a volume row after the transaction. Do not use either mode when reports require lifetime input and output from direct account reads.

Use:

- `balance` for the current net position;
- `input` and `output` for cumulative flow and turnover;
- transactions and logs for the exact history that produced those values.

## Aggregate volumes across accounts

Use the `GET /v3/{ledger}/volumes` endpoint when you need totals for a set of accounts. It returns decimal-string `input`, `output`, and `balance` values, and supports:

| Parameter              | Purpose                                                                                                                                 |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `filter`               | Select the accounts to total. `address ^= "customers:"` matches an address prefix. This is the only account selector the route accepts. |
| `groupByPrefixes`      | Return separate totals for a comma-separated set of prefixes.                                                                           |
| `useMaxPrecision=true` | Rescale assets with the same base to the highest precision found in the result.                                                         |
| `collapseColors=true`  | Sum color buckets for reporting. Stored balances remain separate.                                                                       |

Ledger 3.0 removed the `prefix` parameter this route used to accept. An unrecognized parameter is ignored rather than rejected, so sending `prefix=` still returns `200` — with a total across every account in the ledger, `world` included, that looks exactly like a scoped one. The response carries no addresses, so nothing in it reveals the difference.

`useMaxPrecision` is a reporting aid, not a correction for inconsistent writers. Standardize the asset at write time and use precision merging only when you deliberately need a combined view.

## Verify an amount integration

Run the following checks before production traffic:

1. convert a display amount to an integer and back without loss;
2. send the smallest supported unit, such as `1` for `EUR/2`;
3. read the destination and confirm its `input` and `balance` increased by exactly that integer;
4. move part of the balance out and confirm only `output` increased;
5. revert the movement and confirm the balance recovers while cumulative volumes continue to record activity;
6. test values above your language's native safe-integer range.

## Common failure modes

| Symptom                                                                  | Cause                                                                | Correction                                                      |
| ------------------------------------------------------------------------ | -------------------------------------------------------------------- | --------------------------------------------------------------- |
| EUR 12.34 is recorded as `12` or `1233`                                  | Floating-point conversion or wrong scale                             | Convert with decimal arithmetic to `1234 EUR/2`.                |
| One currency appears in several balance rows                             | Writers use `USD`, `USD/2`, and `USD/4` inconsistently               | Publish one official asset identifier.                          |
| A client rounds large amounts                                            | It parses JSON values through a fixed-width or floating-point number | Preserve arbitrary-precision integers end to end.               |
| Balance returns to zero but activity totals do not                       | A reversal added compensating postings                               | Use balance for net position and volumes for cumulative flow.   |
| Prefix aggregation includes `customers:10` when requesting `customers:1` | Prefix matching is byte-based                                        | Include the intended segment delimiter, such as `customers:1:`. |

Use [colors](https://docs.formance.com/v4.0/modules/ledger/modeling/color-of-money) only when two balances with the same asset must remain isolated. Do not encode provenance by creating ad hoc asset names.
