Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat: add economy ledger module for durable, tamper-evident financial record keeping
nirholas7505b10b7b5e
diff --git a/api/_lib/economy-ledger.js b/api/_lib/economy-ledger.jsnew file mode 100644index 000000000..b139f7998--- /dev/null+++ b/api/_lib/economy-ledger.js@@ -0,0 +1,349 @@+// @ts-check+// api/_lib/economy-ledger.js+//+// The financial book of record for the economy master funding wallet+// (api/_lib/economy-master.js). Every sweep appends an append-only, hash-chained+// batch of rows to `economy_master_ledger`: one `transfer` row per SOL movement+// (with the signature, a running balance, and the USD value at the instant of the+// transfer), one `failed`/`blocked` row per rejected attempt, and one `sweep`+// heartbeat summary — so there is a durable, ordered, tamper-evident record of+// what left the wallet, to whom, when, why, and what it was worth.+//+// Tamper-evidence: each row's `entry_hash` = sha256(canonical fields | prev_hash),+// so the head commits the entire history (a Merkle-equivalent). Editing or+// deleting any historical row breaks the chain from that point; verifyChain()+// and the economy-reconcile cron detect the break and the gap.+//+// This module never throws into the money path: a DB stall drops the ledger batch+// (logged + surfaced), it never blocks or reverts a transfer that already landed.++import { createHash } from 'node:crypto';+import { sql } from './db.js';+import { withDbRetry } from './db-retry.js';+import { solPriceUsd } from './sol-price.js';++let _schemaReady = false;++/** Lazily create the ledger table (mirror of the migration) so a fresh env works. */+export async function ensureSchema() {+ if (_schemaReady) return;+ await withDbRetry(() => sql`+ CREATE TABLE IF NOT EXISTS economy_master_ledger (+ id bigserial PRIMARY KEY,+ seq bigint NOT NULL,+ ts timestamptz NOT NULL,+ run_id uuid,+ master_pubkey text NOT NULL,+ event text NOT NULL,+ target_name text,+ target_pubkey text,+ lamports bigint,+ sol numeric(20,9),+ sol_usd numeric(20,6),+ usd_value numeric(20,6),+ tx_signature text,+ reason text,+ master_sol_before numeric(20,9),+ master_sol_after numeric(20,9),+ reserve_sol numeric(20,9),+ run_cap_sol numeric(20,9),+ per_topup_max_sol numeric(20,9),+ network text NOT NULL DEFAULT 'mainnet',+ detail jsonb,+ prev_hash text,+ entry_hash text NOT NULL+ )+ `);+ await withDbRetry(() => sql`+ CREATE UNIQUE INDEX IF NOT EXISTS economy_master_ledger_seq_idx+ ON economy_master_ledger (master_pubkey, seq)+ `);+ await withDbRetry(() => sql`+ CREATE INDEX IF NOT EXISTS economy_master_ledger_sig_idx+ ON economy_master_ledger (tx_signature)+ `);+ _schemaReady = true;+}++function sha256hex(s) {+ return createHash('sha256').update(s).digest('hex');+}++/**+ * Canonical hash of a ledger row given the prior row's hash. The field set is+ * fixed and order-stable — it commits the position (seq, ts), the movement+ * (event, target, lamports, signature), the resulting balance, and the prior+ * hash. verifyChain() recomputes exactly this, so any of these fields changing+ * after the fact is detectable.+ * @param {string} prevHash+ * @param {object} r+ * @returns {string}+ */+export function hashEntry(prevHash, r) {+ const payload = [+ r.seq,+ r.ts,+ r.master_pubkey,+ r.event,+ r.target_pubkey || '',+ r.lamports == null ? '' : String(r.lamports),+ r.tx_signature || '',+ r.reason || '',+ r.master_sol_after == null ? '' : String(r.master_sol_after),+ prevHash || '',+ ].join('|');+ return sha256hex(payload);+}++function round9(n) {+ return Math.round(Number(n) * 1e9) / 1e9;+}+function round6(n) {+ return Math.round(Number(n) * 1e6) / 1e6;+}++/** Current chain head (highest seq) for a master, or null before genesis. */+export async function getHead(masterPubkey) {+ const rows = await withDbRetry(() => sql`+ SELECT seq, entry_hash FROM economy_master_ledger+ WHERE master_pubkey = ${masterPubkey}+ ORDER BY seq DESC LIMIT 1+ `);+ return rows[0] ? { seq: Number(rows[0].seq), entryHash: rows[0].entry_hash } : null;+}++/**+ * Append one sweep's worth of events to the ledger as a single hash-chained+ * batch. Pure-ish orchestration around DB writes; returns what it wrote.+ *+ * @param {object} args+ * @param {string} args.runId+ * @param {string} args.masterPubkey+ * @param {'mainnet'|'devnet'} [args.network]+ * @param {object} args.result the object returned by sweepTopUps()+ * @param {{reserveSol?:number, runCapSol?:number, perTopupMaxSol?:number}} [args.caps]+ * @param {number} [args.now] epoch ms (injectable for tests)+ * @returns {Promise<{written:number, seqFrom:number|null, seqTo:number|null, headHash:string|null, skippedWrite?:string}>}+ */+export async function recordSweep({ runId, masterPubkey, network = 'mainnet', result, caps = {}, now = Date.now() }) {+ await ensureSchema();+ const solUsd = round6((await solPriceUsd(now)) || 0);+ const rows = buildSweepRows({ masterPubkey, network, result, caps, solUsd, now });+ if (!rows.length) return { written: 0, seqFrom: null, seqTo: null, headHash: null };++ // Chain onto the current head. One retry if a concurrent writer took our seq.+ for (let attempt = 0; attempt < 2; attempt++) {+ const head = await getHead(masterPubkey);+ let seq = head ? head.seq : 0;+ let prevHash = head ? head.entryHash : '';+ const chained = rows.map((r, i) => {+ const row = { ...r, seq: seq + i + 1, prev_hash: prevHash, run_id: runId };+ row.entry_hash = hashEntry(prevHash, row);+ prevHash = row.entry_hash;+ return row;+ });+ try {+ for (const row of chained) await insertRow(row);+ return {+ written: chained.length,+ seqFrom: chained[0].seq,+ seqTo: chained[chained.length - 1].seq,+ headHash: prevHash,+ };+ } catch (err) {+ const conflict = /duplicate key|unique/i.test(err?.message || '');+ if (conflict && attempt === 0) continue; // re-read head and rebuild the chain+ console.error('[economy-ledger] recordSweep write failed', { runId, error: err?.message });+ return { written: 0, seqFrom: null, seqTo: null, headHash: null, skippedWrite: err?.message || 'write_failed' };+ }+ }+ return { written: 0, seqFrom: null, seqTo: null, headHash: null, skippedWrite: 'seq_conflict' };+}++/**+ * Turn a sweepTopUps() result into ordered ledger rows with a running balance.+ * Pure — no DB, no clock beyond the injected `now` — so it is unit-tested.+ * @returns {Array<object>}+ */+export function buildSweepRows({ masterPubkey, network = 'mainnet', result, caps = {}, solUsd = 0, now = Date.now() }) {+ const rows = [];+ const before = round9(result?.masterSol ?? 0);+ let running = before;+ const baseTs = now;+ let i = 0;+ const ts = () => new Date(baseTs + i++).toISOString();++ const common = {+ master_pubkey: masterPubkey,+ network,+ reserve_sol: caps.reserveSol ?? result?.reserveSol ?? null,+ run_cap_sol: caps.runCapSol ?? null,+ per_topup_max_sol: caps.perTopupMaxSol ?? null,+ master_sol_before: before,+ };++ for (const f of result?.funded || []) {+ const solAfter = round9(running - f.sol);+ rows.push({+ ...common,+ ts: ts(),+ event: 'transfer',+ target_name: f.name,+ target_pubkey: f.pubkey,+ lamports: Math.round(f.sol * 1e9),+ sol: round9(f.sol),+ sol_usd: solUsd || null,+ usd_value: solUsd ? round6(f.sol * solUsd) : null,+ tx_signature: f.signature,+ reason: null,+ master_sol_after: solAfter,+ detail: null,+ });+ running = solAfter;+ }+ for (const f of result?.failed || []) {+ rows.push({+ ...common,+ ts: ts(),+ event: 'failed',+ target_name: f.name,+ target_pubkey: f.pubkey,+ lamports: f.sol != null ? Math.round(f.sol * 1e9) : null,+ sol: f.sol != null ? round9(f.sol) : null,+ sol_usd: solUsd || null,+ usd_value: null,+ tx_signature: null,+ reason: f.reason || 'send_failed',+ master_sol_after: running,+ detail: null,+ });+ }+ for (const r of result?.rejected || []) {+ rows.push({+ ...common,+ ts: ts(),+ event: 'blocked',+ target_name: r.name,+ target_pubkey: r.pubkey,+ lamports: null,+ sol: null,+ sol_usd: solUsd || null,+ usd_value: null,+ tx_signature: null,+ reason: r.reason || 'not_in_registry',+ master_sol_after: running,+ detail: null,+ });+ }+ // Heartbeat summary — always written, even on a no-op sweep, so a continuous+ // monitoring trail exists (regulatory "we watch this every 30 min" evidence).+ rows.push({+ ...common,+ ts: ts(),+ event: 'sweep',+ target_name: null,+ target_pubkey: null,+ lamports: null,+ sol: round9(result?.spentSol ?? 0),+ sol_usd: solUsd || null,+ usd_value: solUsd ? round6((result?.spentSol ?? 0) * solUsd) : null,+ tx_signature: null,+ reason: result?.configured === false ? 'unconfigured' : null,+ master_sol_after: running,+ detail: {+ configured: result?.configured ?? false,+ funded: (result?.funded || []).length,+ failed: (result?.failed || []).length,+ blocked: (result?.rejected || []).length,+ skipped: result?.skipped || [],+ spent_sol: round9(result?.spentSol ?? 0),+ spendable_sol: result?.spendableSol ?? null,+ },+ });+ return rows;+}++async function insertRow(r) {+ await withDbRetry(() => sql`+ INSERT INTO economy_master_ledger+ (seq, ts, run_id, master_pubkey, event, target_name, target_pubkey,+ lamports, sol, sol_usd, usd_value, tx_signature, reason,+ master_sol_before, master_sol_after, reserve_sol, run_cap_sol,+ per_topup_max_sol, network, detail, prev_hash, entry_hash)+ VALUES+ (${r.seq}, ${r.ts}, ${r.run_id}, ${r.master_pubkey}, ${r.event},+ ${r.target_name}, ${r.target_pubkey}, ${r.lamports}, ${r.sol},+ ${r.sol_usd}, ${r.usd_value}, ${r.tx_signature}, ${r.reason},+ ${r.master_sol_before}, ${r.master_sol_after}, ${r.reserve_sol},+ ${r.run_cap_sol}, ${r.per_topup_max_sol}, ${r.network},+ ${r.detail ? JSON.stringify(r.detail) : null}, ${r.prev_hash}, ${r.entry_hash})+ `);+}++/**+ * Recompute the hash chain for a master and report any integrity failure. This is+ * the tamper detector — a broken link means a historical row was edited/deleted,+ * a seq gap means a row is missing. Reads the whole chain (bounded by `limit`).+ *+ * @param {string} masterPubkey+ * @param {{limit?:number}} [opts]+ * @returns {Promise<{ok:boolean, count:number, brokenAtSeq:number|null, gapAtSeq:number|null, headSeq:number|null, headHash:string|null}>}+ */+export async function verifyChain(masterPubkey, { limit = 100_000 } = {}) {+ const rows = await withDbRetry(() => sql`+ SELECT seq, ts, master_pubkey, event, target_pubkey, lamports, tx_signature,+ reason, master_sol_after, prev_hash, entry_hash+ FROM economy_master_ledger+ WHERE master_pubkey = ${masterPubkey}+ ORDER BY seq ASC LIMIT ${limit}+ `);+ let prevHash = '';+ let prevSeq = 0;+ for (const row of rows) {+ const seq = Number(row.seq);+ if (seq !== prevSeq + 1) {+ return { ok: false, count: rows.length, brokenAtSeq: null, gapAtSeq: seq, headSeq: null, headHash: null };+ }+ // Normalize ts to the ISO form we hashed (Postgres returns a Date).+ const tsIso = row.ts instanceof Date ? row.ts.toISOString() : String(row.ts);+ const recomputed = hashEntry(row.prev_hash || '', {+ seq,+ ts: tsIso,+ master_pubkey: row.master_pubkey,+ event: row.event,+ target_pubkey: row.target_pubkey,+ lamports: row.lamports,+ tx_signature: row.tx_signature,+ reason: row.reason,+ master_sol_after: row.master_sol_after == null ? null : String(row.master_sol_after),+ });+ if ((row.prev_hash || '') !== (prevHash || '') || recomputed !== row.entry_hash) {+ return { ok: false, count: rows.length, brokenAtSeq: seq, gapAtSeq: null, headSeq: null, headHash: null };+ }+ prevHash = row.entry_hash;+ prevSeq = seq;+ }+ return {+ ok: true,+ count: rows.length,+ brokenAtSeq: null,+ gapAtSeq: null,+ headSeq: prevSeq || null,+ headHash: prevHash || null,+ };+}++/**+ * Read ledger rows for accounting/reconciliation over a window.+ * @param {{masterPubkey?:string, from?:string, to?:string, event?:string, limit?:number}} [opts]+ */+export async function readLedger({ masterPubkey = null, from = null, to = null, event = null, limit = 10_000 } = {}) {+ return withDbRetry(() => sql`+ SELECT * FROM economy_master_ledger+ WHERE (${masterPubkey}::text IS NULL OR master_pubkey = ${masterPubkey})+ AND (${from}::timestamptz IS NULL OR ts >= ${from})+ AND (${to}::timestamptz IS NULL OR ts <= ${to})+ AND (${event}::text IS NULL OR event = ${event})+ ORDER BY seq ASC LIMIT ${limit}+ `);+}diff --git a/api/_lib/migrations/20260702100000_treasury_ledger.sql b/api/_lib/migrations/20260702100000_treasury_ledger.sqlnew file mode 100644index 000000000..09d3c6baa--- /dev/null+++ b/api/_lib/migrations/20260702100000_treasury_ledger.sql@@ -0,0 +1,65 @@+-- Treasury Ledger — the durable, append-only accounting + audit record for every+-- SOL movement the economy master wallet (api/_lib/economy-master.js) makes.+--+-- WHY: the treasury-topup cron auto-refills engine signers from the ONE master+-- (Www…WwW). That moves real money on a schedule. Telegram alerts + an ephemeral+-- HTTP response are not an accounting record: you cannot reconcile a month, prove+-- to a regulator/auditor where funds went, or detect a breach after the fact.+-- This table is that record — one row per transfer/skip/reject, plus a `summary`+-- row per sweep carrying the before/after master balance, the caps in force, the+-- reconciliation result, and a tamper-evident hash chain over the summary rows.+--+-- Ground truth is still the chain: every `transfer` row carries the on-chain+-- `tx_signature`, independently verifiable on Solscan forever. This table is the+-- queryable index over that truth plus the off-chain context (which run, which+-- caps, which reconciliation) the chain does not hold.+--+-- APPEND-ONLY: our code only ever INSERTs. Rows are never updated. The+-- prev_hash/row_hash chain over `summary` rows makes any retroactive edit or+-- deletion detectable (a broken link). Consumers: api/cron/treasury-topup.js+-- (writer), api/ops/treasury-ledger.js (owner/accounting reader + CSV export).++create table if not exists treasury_ledger (+ id bigserial primary key,+ ts timestamptz not null default now(),+ -- Groups every row emitted by one sweep run.+ sweep_id uuid not null,+ -- Monotonic sweep counter; orders the hash chain deterministically.+ seq bigint not null default 0,+ -- 'transfer' | 'skip' | 'reject' | 'summary'+ kind text not null,+ master text not null,+ network text not null default 'mainnet',+ -- Line rows (transfer/skip/reject): which engine and how much.+ engine_name text,+ engine_pubkey text,+ amount_lamports bigint,+ amount_sol numeric(20,9),+ -- funded | failed | rejected | skipped | ok | breach+ status text not null,+ reason text,+ tx_signature text,+ -- Summary rows only: the run's balances, caps, and reconciliation.+ master_sol_before numeric(20,9),+ master_sol_after numeric(20,9),+ spent_sol numeric(20,9),+ reserve_sol numeric(20,9),+ per_topup_max_sol numeric(20,9),+ run_cap_sol numeric(20,9),+ expected_after_sol numeric(20,9),+ -- Breach signal: SOL that left the master beyond what this cron sent (fees+ -- excluded). Non-zero means the master key moved funds out of band.+ unexplained_sol numeric(20,9),+ breach boolean not null default false,+ -- Provenance: the deployed commit that produced this row.+ git_sha text,+ -- Tamper-evident chain over summary rows (sha256(prev_hash || canonical row)).+ prev_hash text,+ row_hash text,+ meta jsonb not null default '{}'::jsonb+);++create index if not exists treasury_ledger_ts on treasury_ledger (ts desc);+create index if not exists treasury_ledger_sweep on treasury_ledger (sweep_id);+create index if not exists treasury_ledger_engine on treasury_ledger (engine_name, ts desc);+create index if not exists treasury_ledger_breach on treasury_ledger (breach) where breach = true;diff --git a/api/cron/treasury-topup.js b/api/cron/treasury-topup.jsindex b56794d0f..11f3a85b1 100644--- a/api/cron/treasury-topup.js+++ b/api/cron/treasury-topup.js@@ -26,12 +26,14 @@// ECONOMY_MASTER_SECRET_BASE58 — the funding-root key (unset ⇒ inert)// ECONOMY_MASTER_RESERVE_SOL / _PER_TOPUP_MAX_SOL / _RUN_CAP_SOL — guard caps+import { randomUUID } from 'node:crypto';import { error, json, method, wrapCron } from '../_lib/http.js';import { env } from '../_lib/env.js';import { constantTimeEquals } from '../_lib/crypto.js';import { sendOpsAlert } from '../_lib/alerts.js';import { SOLANA_SIGNERS, resolveSignerPubkey } from '../_lib/solana-signers.js';-import { sweepTopUps } from '../_lib/economy-master.js';+import { sweepTopUps, RESERVE_SOL, RUN_CAP_SOL, PER_TOPUP_MAX_SOL } from '../_lib/economy-master.js';+import { recordSweep } from '../_lib/economy-ledger.js';const LAMPORTS_PER_SOL = 1_000_000_000;// How high to lift an engine when it falls below its floor, unless the spec