0009-monetary-representation-integer-minor-units.md 8.2 KB

Monetary amounts as integer minor units, scale outside the value

  • Status: accepted (refined by ADR-0011)
  • Authors: Cesar Rodas
  • Date: 2026-06-29
  • Targeted modules: kuatia-types (Cent, Amount, AssetId), kuatia-core
  • Associated tickets/PRs: N/A

Context and Problem Statement

Every posting carries an amount of one asset (ADR-0001). The core invariant is per-asset conservation, sum(consumed) == sum(created), and every commit checks it. That sum must be exact. A ledger that rounds is not a ledger.

So the monetary type must do four things. It must be exact under addition, subtraction and negation. It must refuse a silent overflow. It must hash deterministically, for the content address. And it must still represent assets at different decimal precisions: USD has 2, a token has 8, and JPY has 0.

So what type represents a stored monetary amount? And where does the decimal scale of an asset live?

Decision Drivers

  • Exactness: addition, subtraction and negation must each be exact. The conservation check and the floor check tolerate no error from rounding.
  • No silent overflow: an amount that overflows must come back as an error and must not wrap. A wrap would forge value or destroy it.
  • Deterministic bytes: the content-addressed EnvelopeId hashes the amounts. So the representation must serialize identically everywhere, with no locale and no ambiguity in a float bit pattern.
  • Multi-asset precision: two assets have two different decimal places, and the stored value must stay one uniform type.
  • No arithmetic in the database: every sum happens in Rust, with a checked operation. The store never computes on an amount (CLAUDE.md, ADR-0003).

Considered Options

Option 1: Floating point (f64)

Store an amount as a binary floating-point number.

Pros:

  • Good, because the language has it already, and it holds a fractional value with no scale.

Cons:

  • Bad, because f64 cannot hold most decimal fractions exactly, as 0.1 + 0.2 shows. So a conservation sum drifts, and the Σ consumed == Σ created check becomes approximate. That rules it out for a ledger.
  • Bad, because a float bit pattern and a rounding mode together make the hash fragile, and make the result differ between platforms.

Option 2: A decimal / big-integer library (rust_decimal, i128, rationals)

Use a wider numeric type, or one that knows about decimals, and let it carry its own scale.

Pros:

  • Good, because it offers a larger range through i128, or decimal arithmetic that knows the scale. It can also hold the precision inside the value.

Cons:

  • Bad, because it pulls a large dependency, or a wider column, into the type that appears most often. That complicates both the storage layout and the serialization, for a need the domain does not have yet.
  • Bad, because a value that carries its own scale invites a quiet mix of two scales. Something must still pin the scale down, for a deterministic hash.
  • Bad, because i64 minor units already cover about ±9.2×10¹⁸ of the smallest unit. That is ample for a realistic balance, so the extra range is weight that almost nothing uses.

Option 3: Cent, an i64 newtype of minor units, scale held outside

Cent(i64) is a newtype with a private field. It holds an amount in the asset's smallest unit, such as a cent or a satoshi.

It offers checked arithmetic alone, which is checked_add, checked_sub, checked_neg and checked_sum, and each returns an OverflowError on overflow. It serializes as big-endian bytes through ToBytes, for the hash. It is Ord and Hash.

Neither the value nor the asset stores the decimal scale. AssetId(u32) is an opaque identifier. Amount { decimals: u8 } parses and formats for presentation alone, between a string and a Cent, and nothing ever persists it.

Pros:

  • Good, because integer minor units are exact under addition, subtraction and negation. So the conservation check and the overdraft floor both run on exact integers.
  • Good, because the private field keeps a monetary amount and a plain i64 apart, so nothing can mistake one for the other. The only arithmetic on offer is checked, so an overflow is a Result and never a wrap.
  • Good, because big-endian bytes give one deterministic representation with no locale. The content address then holds across every backend and every platform.
  • Good, because the scale stays out of the stored value. The persisted ledger is then pure integers, with no precision field per row for a migration to move. A presentation concern then never touches the conservation arithmetic.
  • Good, because i64 minor units are compact, at a fixed 8 bytes. Rust indexes them cheaply and sums them cheaply.

Cons:

  • Bad, because the scale is a convention, and the application must apply it consistently. A Cent means nothing unless a reader knows the decimals of its asset. Nothing in the type stops a format of a satoshi amount at 2 decimals.
  • Bad, because i64 caps one amount, and one sum, at about ±9.2×10¹⁸ minor units. An asset with very high precision and a very large supply can in principle pass that. The result is an OverflowError and not a wrap, and the cap is still hard.
  • Bad, because a fractional or proportional operation is not closed over Cent, and interest, a fee and an FX rate are all such operations. Each one needs an explicit definition, with an agreed rounding policy, on the day something adds it.

Decision Outcome

Chosen option: Option 3, Cent, an i64 newtype of minor units, with the scale held outside the value. It is the only option that makes the conservation sum exact and deterministic. It also keeps the stored ledger pure integers, and keeps an overflow an explicit error.

The scale lives in Amount, which is presentation, rather than on Cent or AssetId, which are storage. So precision stays at the edge, on the boundary of the application. It never reaches the arithmetic of the invariant, and it never reaches the database schema.

i64 wins over i128 and over a decimal type. Its range is more than adequate. Its fixed width keeps the most common type small and simple to serialize. A later move to a wider type is a contained change to the newtype, if a real asset ever needs one.

Positive Consequences

  • Every piece of monetary arithmetic is checked and exact. The conservation check and the floor check in validate_and_plan both work on integers, and those integers can neither round quietly nor wrap.
  • The big-endian ToBytes of a Cent feeds the content-addressed EnvelopeId deterministically. One transfer then hashes identically on every backend.
  • The persisted amount is a plain BIGINT, an i64, with no metadata for its precision. That agrees with the rule against arithmetic in the database, and with an identity that Rust owns (ADR-0003).
  • Amount keeps two things apart. One is the input and output a person reads, with the decimals of each asset. The other is the stored value, which has no scale.

Negative Consequences

  • The scale of an asset is a convention at the application level. The type system does not bind a Cent to the decimal places of its asset. So a caller must format and parse through the right Amount for that asset.
  • i64 is a hard ceiling on the magnitude of one amount and of one sum. An overflow gives a Result and never a wrap. A later asset with high precision and a high supply can force a move to a wider newtype.
  • A multiplicative or fractional operation needs an explicit rounding policy on the day something adds it. A fee, interest and FX are all such operations. None of them is part of Cent today, and that is deliberate.

Links

  • Makes the conservation invariant of ADR-0001 exact. It also feeds the content-addressed id that gives idempotency, which ADR-0005 covers, and which a later record on content-addressed transfer ids will cover.
  • The floor checks that depend on an exact integer: ADR-0004.
  • Background: crates/kuatia-types/src/lib.rs (Cent, Amount, AssetId), glossary.md.