kuatia-types (Cent, Amount, AssetId), kuatia-coreEvery 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?
EnvelopeId hashes the
amounts. So the representation must serialize identically everywhere, with no
locale and no ambiguity in a float bit pattern.f64)Store an amount as a binary floating-point number.
Pros:
Cons:
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.rust_decimal, i128, rationals)Use a wider numeric type, or one that knows about decimals, and let it carry its own scale.
Pros:
i128, or decimal arithmetic
that knows the scale. It can also hold the precision inside the value.Cons:
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.Cent, an i64 newtype of minor units, scale held outsideCent(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:
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.i64 minor units are compact, at a fixed 8 bytes. Rust indexes
them cheaply and sums them cheaply.Cons:
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.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.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.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.
validate_and_plan both work on integers, and
those integers can neither round quietly nor wrap.ToBytes of a Cent feeds the content-addressed EnvelopeId
deterministically. One transfer then hashes identically on every backend.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.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.Cent today, and that is deliberate.crates/kuatia-types/src/lib.rs (Cent, Amount, AssetId),
glossary.md.