# 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 assets caller-supplied ids, reasoning that an asset is named like an account rather than minted. In practice a caller names an asset by its code and has no opinion about its id, so registering one took two steps and an invented number: ```rust ledger.create_asset(Asset::new(AssetId::new(2), "EUR", "€", 2)).await?; let eur = ledger.asset(&AssetId::new(2)).await?; ``` The `2` is the caller keeping a registry of its own alongside the ledger's, which is the thing the registry exists to stop. Every test, example and seed in the tree carried such a table, and nothing but convention kept two of them from disagreeing. ## Decision Drivers * A caller names money by its code, not by a number. * An id must stay stable, because postings, the amount columns and the content address all store it. * Precision is the dangerous field: an asset returned at the wrong scale mis-states every amount read through it. * Setup code runs repeatedly (tests, seeds, a service starting against an existing database), so registration wants to 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 id space. **Cons:** * Bad, because the caller still holds a table mapping codes to numbers, which is what went wrong. #### Derive the id from the code An `AssetId` is a hash of the code, so one code is one id in every database. **Pros:** * Good, because two ledgers registering `EUR` agree without coordination, which helps if they are ever compared. **Cons:** * Bad, because a 32-bit hash collides, and a collision silently merges two assets into one balance. * Bad, because a code could then never be re-spelled. #### Name by code, mint the id (chosen) **Pros:** * Good, because the caller states what it knows (code, symbol, precision) and nothing it does not. * Good, because it matches how accounts and books already get their ids, in Rust through `AutoId`, never from the database. * Good, because one call both registers and returns the handle, so setup code is idempotent. **Cons:** * Bad, because the same code has different ids in two databases, so an id is not portable between them. * Bad, because it needs a unique index on the code, and a migration to add it. ## Decision Outcome Chosen option: **name by code, mint the id**. 1. **`Ledger::asset(code, symbol, decimals)` is get-or-create** and returns `Arc`. The by-id lookup keeps its behaviour as `asset_by_id`. `create_asset` stays for a caller that wants to choose the id. 2. **A code registered with other metadata is `LedgerError::AssetRedefined`.** Returning the stored row would hand back a precision the caller did not ask for and mis-state every amount formatted through it; rewriting the row is not possible, since a registry row is immutable (ADR-0028). So neither side wins and the disagreement is reported, carrying both decimal counts. 3. **The id comes from `AutoId`**, folded into the `u32` an `AssetId` carries, retried past the small chance that a truncated snowflake lands on an id already taken. A code's uniqueness is the index's job, so a concurrent registration of the same code is caught by re-reading it rather than by locking. 4. **`code` is unique** (migration `015_asset_code_unique`). Without it a get-or-create could register a second `EUR` under a different id and split every balance held in it. 5. **`AssetStore` gains `get_asset_by_code`**, returning an `Option` rather than `NotFound`, because the caller asking by code is deciding whether to register. ### Positive Consequences * Registering an asset is one line and says only what the caller knows: `ledger.asset("EUR", "€", 2)`. * The id-to-code tables in the tests, the examples and the dashboard are gone; the dashboard reads the registry for the list it renders. * Setup code is idempotent, so a seed can run against a populated database without an ignore-already-exists dance. ### Negative Consequences * An `AssetId` is no longer portable between databases, which it was when a caller chose it. Nothing in the ledger compares ids across databases, but a tool that did would have to map by code. * A code is now part of an asset's identity in practice, so re-spelling one means a new asset. * Migration 015 fails on an existing database that already holds two assets sharing a code. That is the condition the index exists to prevent, and it has to be resolved by hand. ## Links * Supersedes the caller-supplied-id clause of [ADR-0028](0028-amount-carries-its-asset.md); the rest of it, and all of [ADR-0029](0029-stored-amounts-and-a-closed-money-surface.md), stand. * Ids minted in Rust, never by the database: the `AutoId` rule in `CLAUDE.md`, as used by accounts and books. * Immutable registry rows: [ADR-0028](0028-amount-carries-its-asset.md), point 1.