Files
zeekayandHanzo Dev ec66306aa3 the ledger's units are types now, and the product mounts in two places
Every money bug this session was int64 meeting int64. A debt in cents was
subtracted from a balance in an asset's own units and a +$340 vault
position rendered as -$5,600; a repayment sized in the collateral's
decimals reached a contract counting the synthetic's, off by 1e10. The
comments carried the unit — CollateralUsd int64 // USD cents — and
comments do not typecheck.

Cents and Minor are distinct types. The only crossing is usd(), which
needs a price, which is why it should be the only one. The compiler found
every place they met, including the control in the Earn guard, which
deliberately reproduces the old sum: it no longer compiles, so it now
casts by hand and says why. money[T] reads a whole amount off a record
and rounds, once, instead of at the thirty call sites that each had to
remember — one of which had not.

An existing database could not boot. Base refuses to update any collection
whose name equals its own id, because checkUniqueName looks up that id and
does not exclude the collection being saved. Every collection here is
NewBaseCollection(name, name), so each was creatable once and never
updatable, and a schema addition failed a running bank at startup. The
upgrade path saves without that validation; nothing there changes a name.
Reproduced on a database written by the older build and now booting clean
on this one.

The dash also ships as a library. lux.finance renders it whole; the Lux
Cloud console renders the same screens beside a validator's nodes and
keys. bankd validates a bearer at IAM's userinfo without checking
audience, so the console's Lux ID session already authorizes the bank —
no second sign-in, no exchange. The grid compiler runs at library build
time, so a host needs no plugin of its own.

Colour belonged to the brand tokens but the layout runtime painted body
from its own theme, unlayered, so everything that inherited landed on the
wrong brand — white text on the light brand's white page, across the
whole app.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-25 13:32:29 -07:00

38 lines
1.4 KiB
Go

package bank
import (
"math"
"github.com/hanzoai/base/core"
)
// The ledger counts two different things, and any sum that mixes them is wrong.
//
// Cents — US dollars in hundredths. Every *Usd field, and all fiat.
// Minor — one asset's smallest unit at the ledger's resolution, which is not
// the token's resolution on chain; see evmChain.scale for that gap.
//
// Both are whole counts of a smallest unit, so both were int64 and the compiler
// was content to add them. It did: a vault position worth +$340 rendered as
// -$5,600, because a debt counted in the collateral's own units was subtracted
// from a total counted in cents. Naming them ends that at compile time. The
// only way across is usd, and usd needs a price.
type (
Cents int64
Minor int64
)
// money reads a whole-unit amount off a record.
//
// Base keeps every number as a float, so a read has to round rather than
// truncate: a cent that arrives as 0.9999999999 is one cent, and int64 of it is
// nothing. That rounding was spelled out at thirty call sites, which is thirty
// chances to leave it out — and one of them had.
func money[T ~int64](rec *core.Record, field string) T {
return T(math.Round(rec.GetFloat(field)))
}
// round turns a computed float into whole units the same way, for the places
// that price or scale an amount rather than read one.
func round[T ~int64](v float64) T { return T(math.Round(v)) }