0030-assets-named-by-code.md 5.5 KB

An asset is named by its code, and the ledger mints its id

  • Status: accepted
  • Authors: Cesar Rodas
  • Date: 2026-12-02
  • Targeted modules: kuatia-types (asset, book), kuatia-storage (AssetStore), kuatia-storage-sql (migration 015), kuatia (ledger::asset), kuatia-dashboard
  • Associated tickets/PRs: N/A

Context and Problem Statement

ADR-0028 gave an asset an id that the caller supplied. Its reason was that something names an asset the way it names an account, and does not mint it.

In practice a caller names an asset by its code and holds no opinion about its id. So a registration took two steps and a number the caller invented:

ledger.create_asset(Asset::new(AssetId::new(2), "EUR", "€", 2)).await?;
let eur = ledger.asset(&AssetId::new(2)).await?;

That 2 is the caller's own registry, beside the ledger's. The ledger's registry exists to stop exactly that. Every test, every example and every seed in the tree carried such a table, and only convention kept two of them in agreement.

Decision Drivers

  • A caller names money by its code, not by a number.
  • An id must stay stable. The postings, the amount columns and the content address all store it.
  • Precision is the dangerous field. An asset that comes back at the wrong scale mis-states every amount a caller reads through it.
  • Setup code runs again and again. A test runs it, a seed runs it, and a service runs it against a database that exists. So a registration must be idempotent.

Considered Options

Keep caller-supplied ids, add a by-code lookup

Pros:

  • Good, because it is the smallest change, and the caller keeps its own id space.

Cons:

  • Bad, because the caller still holds a table from a code to a number, and that table is what went wrong.

Derive the id from the code

An AssetId is a hash of the code, so one code gives one id in every database.

Pros:

  • Good, because two ledgers that register EUR agree with no coordination. That helps if anything ever compares them.

Cons:

  • Bad, because a 32-bit hash collides, and a collision merges two assets into one balance without a word.
  • Bad, because nothing could then ever re-spell a code.

Name by code, mint the id (chosen)

Pros:

  • Good, because the caller states what it knows, which is the code, the symbol and the precision, and states nothing else.
  • Good, because it matches how an account and a book already get an id, in Rust through AutoId and never from the database.
  • Good, because one call registers the asset and returns the handle. So setup code is idempotent.

Cons:

  • Bad, because one code has two different ids in two databases. So an id does not move between them.
  • Bad, because it needs a unique index on the code, and a migration that adds that index.

Decision Outcome

Chosen option: name by code, mint the id.

  1. Ledger::asset(code, symbol, decimals) is a get-or-create and returns Arc<Asset>. The lookup by id keeps its behaviour, as asset_by_id. create_asset stays, for a caller that wants to choose the id.

  2. A code that arrives with other metadata gives LedgerError::AssetRedefined. A return of the stored row would hand back a precision the caller did not ask for. It would then mis-state every amount formatted through it. A rewrite of the row is impossible, because a registry row is immutable (ADR-0028). So neither side wins. The ledger reports the disagreement, and the report carries both decimal counts.

  3. The id comes from AutoId, folded into the u32 that an AssetId carries. A truncated snowflake has a small chance to land on an id that is taken, and a retry covers that. The index owns the uniqueness of a code. So a re-read catches a second concurrent registration of one code, and no lock does.

  4. code is unique, through migration 015_asset_code_unique. Without the index, a get-or-create could register a second EUR under another id, and split every balance held in it.

  5. AssetStore gains get_asset_by_code. It returns an Option rather than a NotFound, because a caller that asks by code has yet to decide whether to register.

Positive Consequences

  • A registration of an asset is one line, and it says only what the caller knows: ledger.asset("EUR", "€", 2).
  • The tables from an id to a code are gone from the tests, the examples and the dashboard. The dashboard reads the registry for the list it renders.
  • Setup code is idempotent. So a seed can run against a database that already holds rows, and it needs no step that ignores a row already there.

Negative Consequences

  • An AssetId no longer moves between two databases, and it did when a caller chose it. Nothing in the ledger compares an id across two databases. A tool that does must map by code.
  • A code is now part of the identity of an asset in practice. So a code written another way gives a new asset.
  • Migration 015 fails on a database that already holds two assets under one code. That is the condition the index exists to prevent, and someone must resolve it by hand.

Links

  • Supersedes the clause of ADR-0028 where the caller supplies the id. The rest of that record stands, and so does all of ADR-0029.
  • Rust mints every id, and the database mints none. That is the AutoId rule in CLAUDE.md, and an account and a book both follow it.
  • A registry row is immutable: ADR-0028, point 1.