nx_ledger_lib.nx
buildroot/runtime/nx_ledger_lib.nx
about
nx_ledger_lib.nx -- F981: THE SOVEREIGN DOUBLE-ENTRY LEDGER (the first byte of Nishi finance).
Two entities (accounts, transfers) and ONE invariant: every transfer DEBITS one account and
CREDITS another by the same amount, so sum(debits) == sum(credits) ALWAYS. Money cannot be
created or destroyed -- not by promise, BY CONSTRUCTION, and the gate proves it.
Money = integer MINOR UNITS (cents). No float anywhere -- a float ledger drifts cent-by-cent and
an auditor cannot reproduce it. Append-only on the immutable CID plane (rule 13, history sacred):
nothing is ever mutated or deleted; a reversal is a NEW opposing transfer, a two-phase resolution
is a NEW record. That is exactly what regulators and bar audits want.
TWO-PHASE (pending -> post|void): a pending transfer reserves funds without moving them. It counts
toward an account's RESERVED total and reduces AVAILABLE, but never touches the posted balance
until a resolution record posts it. Resolutions are keyed by the transfer id = O(1) lookup, not a
second scan.
LINKED CHAINS: led_chain validates EVERY leg BEFORE committing ANY (validate-then-commit, rule 12)
so a refused chain leaves ZERO partial state -- no half-moved money.
BALANCE CONVENTION: led_balance = credits - debits (custodial/liability sense: a deposit CREDITS
the holder, matching nx_trust). An ASSET account reads the negation; led_debits/led_credits are
exposed raw so a caller can apply either convention explicitly.
SCALE ENVELOPE (declared, never silent): led_sums scans the whole transfer index each call --
O(transfers) per query, index buffer LED_IDXCAP. A per-account index is the next rung.
DRY: reuses nx_matter_lib helpers (mt_catcopy/mt_catn/mt_field/mt_streq, canon_encode, reg_*).
license_tier: ORIGINAL No hw writes (Rule 26). LIB (no main).
dependencies 1 imports · 5 importers
imports: nx_matter_lib.nx
imported by: nx_coa_lib.nxnx_ledger_gate.nxnx_ledger_scale_gate.nxnx_ledger_trust_equiv_gate.nxnx_recon_lib.nx
structs
| none |
consts
| 30 | const LED_IDXCAP: i64 = 1048576 // transfer-index read buffer (declared envelope) |
| 31 | const LED_RECCAP: i64 = 1024 // one canonical transfer record |
| 32 | const LED_IDCAP: i64 = 128 // max id length |
| 33 | const LED_COLON: i64 = 58 |
functions
| 36 | func led_atoi(s: *u8) -> i64 called by 1: led_sums_idx |
| 47 | func led_dollars(cents: i64, out: *u8) -> i64 calls 1: mt_catn |
| 71 | func led_acct_idx(acct: *u8, out: *u8) -> i64 calls 1: mt_catcopy |
| 81 | func led_xfer(prefix: *u8, id: *u8, dr: *u8, cr: *u8, cents: i64, ttype: *u8) -> i64 |
| 108 | func led_resolve(prefix: *u8, id: *u8, res: *u8) -> i64 |
| 119 | func led_resolution(prefix: *u8, id: *u8) -> i64 |
| 134 | func led_sums_idx(prefix: *u8, acct: *u8, idxkey: *u8, out: *i64) -> i64 |
| 203 | func led_sums(prefix: *u8, acct: *u8, out: *i64) -> i64 |
| 210 | func led_sums_fast(prefix: *u8, acct: *u8, out: *i64) -> i64 |
| 219 | func led_balance(prefix: *u8, acct: *u8) -> i64 |
| 226 | func led_available(prefix: *u8, acct: *u8) -> i64 |
| 234 | func led_drift(prefix: *u8) -> i64 |
| 239 | func led_sound(prefix: *u8) -> i64 calls 1: led_drift |
| 245 | func led_can_fund(prefix: *u8, acct: *u8, cents: i64) -> i64 |
| 255 | func led_chain(prefix: *u8, ids: *i64, drs: *i64, crs: *i64, amts: *i64, n: i64, funder: *u8) -> i64 |