kuatia-types (asset, book), kuatia-storage
(AssetStore), kuatia-storage-sql (migration 015), kuatia
(ledger::asset), kuatia-dashboardADR-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.
Pros:
Cons:
An AssetId is a hash of the code, so one code gives one id in every database.
Pros:
EUR agree with no coordination. That
helps if anything ever compares them.Cons:
Pros:
AutoId and never from the database.Cons:
Chosen option: name by code, mint the id.
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.
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.
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.
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.
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.
ledger.asset("EUR", "€", 2).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.AutoId rule in
CLAUDE.md, and an account and a book both follow it.