cred402/lib/services/timeseries.ts
caelum0x 2d43618725 feat: real integrations (Casper Testnet, x402/EIP-712, MCP, RealFi, cross-chain), wallet sign-in, branding
Replace the design-note docs (p1–p7) with the implemented "make it real" phase
docs (p8–p10) and wire production-grade, env-gated real integrations:

- p8 Casper Testnet: byte-exact deploys + WASM install via casper-js-sdk; live
  node reads (node.testnet.casper.network); CRED402_CHAIN=sim|testnet transport.
- p9 x402 + data + MCP: real EIP-712 typed-data digests (@casper-ecosystem/
  casper-eip-712); casper-x402 facilitator client (x402 V2); live Open-Meteo GHI;
  official @modelcontextprotocol/sdk server; casper-sidecar SSE event feed.
- p10 RealFi + cross-chain: real Stripe test-mode webhooks (HMAC-verified) and
  Plaid sandbox → privacy-preserving on-chain envelopes; Base Sepolia EVM deploy
  (Foundry); reachable via /api/realfi/* and /api/x402/facilitator routes.
- Sign-in with Casper Wallet: real extension provider + challenge/ed25519/session
  flow (/v1/auth/wallet/*), console connect + sign-in button.
- Branding: Cred402 logo mark, favicon, OG image; Vercel config for the console.

All env-gated with sim/free-API fallbacks; full suite green, all toolchains
(TS/Rust/Go/Solidity) build.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 00:37:36 +03:00

70 lines
2.1 KiB
TypeScript

import type { Ledger } from "../ledger/ledger.js";
/**
* Protocol time-series — cumulative metrics reconstructed from the event log.
*
* Walks the ordered event stream and tracks running pool liquidity, outstanding
* credit, and receipt count at each step, producing chartable series (CSPR). The
* console renders these as SVG sparklines; downsampled to a point cap so the
* payload stays small regardless of history length.
*/
export interface SeriesPoint {
seq: number;
liquidity: number; // CSPR
outstanding: number; // CSPR
receipts: number;
}
const MOTES = 1e9;
export function buildTimeseries(ledger: Ledger, maxPoints = 120): SeriesPoint[] {
let liquidity = 0n;
let outstanding = 0n;
let receipts = 0;
const points: SeriesPoint[] = [];
for (const e of ledger.bus.all()) {
const d = e.data as Record<string, unknown>;
switch (e.name) {
case "LiquidityDeposited":
liquidity = BigInt((d.total_liquidity as string) ?? liquidity.toString());
break;
case "ReceiptRecorded":
receipts++;
break;
case "CreditDrawn":
outstanding += BigInt((d.amount as string) ?? "0");
break;
case "CreditRepaid":
outstanding -= BigInt((d.principal as string) ?? "0");
if (outstanding < 0n) outstanding = 0n;
break;
case "CreditDefaulted":
outstanding -= BigInt((d.loss as string) ?? "0");
if (outstanding < 0n) outstanding = 0n;
break;
default:
continue; // only record a point on a metric-changing event
}
points.push({
seq: e.seq,
liquidity: Number(liquidity) / MOTES,
outstanding: Number(outstanding) / MOTES,
receipts,
});
}
return downsample(points, maxPoints);
}
function downsample(points: SeriesPoint[], max: number): SeriesPoint[] {
if (points.length <= max) return points;
const step = points.length / max;
const out: SeriesPoint[] = [];
for (let i = 0; i < max; i++) out.push(points[Math.floor(i * step)]!);
const last = points[points.length - 1]!;
if (out[out.length - 1] !== last) out.push(last);
return out;
}