code wiki / (root) / nx_ledger_lib.nx

nx_ledger_lib.nx

buildroot/runtime/nx_ledger_lib.nx

12137 B273 linesdepth 7pulls 12 transitivereach 10 importersview sourcekind librarytopic ledger
docsdependenciesstructsconstsfunctions

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

nx_matter_lib.nx nx_ledger_lib.nx nx_coa_lib.nx nx_ledger_gate.nx nx_ledger_scale_gate.nx nx_ledger_trust_equiv_gate.nx nx_recon_lib.nx

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

30const LED_IDXCAP: i64 = 1048576 // transfer-index read buffer (declared envelope)
31const LED_RECCAP: i64 = 1024 // one canonical transfer record
32const LED_IDCAP: i64 = 128 // max id length
33const LED_COLON: i64 = 58

functions

36func led_atoi(s: *u8) -> i64
called by 1: led_sums_idx
47func led_dollars(cents: i64, out: *u8) -> i64
calls 1: mt_catn
71func led_acct_idx(acct: *u8, out: *u8) -> i64
calls 1: mt_catcopy
81func led_xfer(prefix: *u8, id: *u8, dr: *u8, cr: *u8, cents: i64, ttype: *u8) -> i64
108func led_resolve(prefix: *u8, id: *u8, res: *u8) -> i64
called by 1: main calls 2: canon_encodereg_put
119func led_resolution(prefix: *u8, id: *u8) -> i64
134func led_sums_idx(prefix: *u8, acct: *u8, idxkey: *u8, out: *i64) -> i64
203func led_sums(prefix: *u8, acct: *u8, out: *i64) -> i64
210func led_sums_fast(prefix: *u8, acct: *u8, out: *i64) -> i64
called by 1: main calls 1: led_sums
219func led_balance(prefix: *u8, acct: *u8) -> i64
226func led_available(prefix: *u8, acct: *u8) -> i64
called by 2: mainled_can_fund calls 1: led_sums
234func led_drift(prefix: *u8) -> i64
called by 1: led_sound calls 1: led_sums
239func led_sound(prefix: *u8) -> i64
calls 1: led_drift
245func led_can_fund(prefix: *u8, acct: *u8, cents: i64) -> i64
called by 2: led_chainmain calls 1: led_available
255func led_chain(prefix: *u8, ids: *i64, drs: *i64, crs: *i64, amts: *i64, n: i64, funder: *u8) -> i64
called by 1: main calls 3: mt_streqled_can_fundled_xfer