cred402/lib/services/risk_alerts.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

112 lines
4.0 KiB
TypeScript

import type { Ledger } from "../ledger/ledger.js";
import { FraudService } from "./fraud_service.js";
import { buildPortfolioReport } from "./portfolio.js";
/**
* Risk alerts — the bureau's always-on monitoring sweep. It scans the live pool,
* credit lines and agent risk signals and emits actionable, severity-ranked alerts
* a risk officer (or an automated guard) should act on: concentration breaches,
* overdue lines, fraud exposure on open credit, frozen/defaulted lines, and
* liquidity stress. Read-only and deterministic, so it is safe to poll.
*/
export type AlertSeverity = "critical" | "warning" | "info";
export interface RiskAlert {
severity: AlertSeverity;
code: string;
subject: string; // the agent / pool the alert is about
message: string;
}
export interface RiskAlertReport {
generated_at: number;
counts: Record<AlertSeverity, number>;
alerts: RiskAlert[];
}
// Tunable thresholds (basis points / scores), kept here so the policy is explicit.
const HHI_CONCENTRATION = 2500; // > moderate
const UTILIZATION_STRESS_BPS = 9000; // 90%
const FRAUD_EXPOSURE_SCORE = 60; // open credit + fraud >= this is a red flag
const SEVERITY_RANK: Record<AlertSeverity, number> = { critical: 0, warning: 1, info: 2 };
export function buildRiskAlerts(ledger: Ledger): RiskAlertReport {
const now = ledger.clock.now();
const fraud = new FraudService(ledger);
const portfolio = buildPortfolioReport(ledger);
const lines = ledger.pool.list();
const alerts: RiskAlert[] = [];
// 1. Portfolio concentration.
if (portfolio.hhi >= HHI_CONCENTRATION && Number(portfolio.outstanding_motes) > 0) {
const top = portfolio.largest_borrower;
alerts.push({
severity: "warning",
code: "concentration_high",
subject: "pool",
message: `Credit book is concentrated (HHI ${portfolio.hhi})${top ? `; largest borrower ${top.key} at ${(top.share_bps / 100).toFixed(0)}%` : ""}.`,
});
}
// 2. Liquidity stress.
if (portfolio.utilization_bps >= UTILIZATION_STRESS_BPS) {
alerts.push({
severity: "warning",
code: "liquidity_stress",
subject: "pool",
message: `Pool utilization at ${(portfolio.utilization_bps / 100).toFixed(0)}% — limited free liquidity for new draws.`,
});
}
// 3. Per-line conditions.
for (const l of lines) {
if (l.status === "defaulted") {
alerts.push({ severity: "critical", code: "line_defaulted", subject: l.agent_id, message: `Credit line for ${l.agent_id} has defaulted.` });
} else if (l.status === "frozen") {
alerts.push({ severity: "warning", code: "line_frozen", subject: l.agent_id, message: `Credit line for ${l.agent_id} is frozen.` });
}
if (l.drawn > 0n && l.due_timestamp < now && l.status === "active") {
const daysOverdue = Math.floor((now - l.due_timestamp) / 86_400);
alerts.push({
severity: "critical",
code: "line_overdue",
subject: l.agent_id,
message: `${l.agent_id} is overdue by ${daysOverdue}d with an outstanding balance.`,
});
}
}
// 4. Fraud exposure on agents that currently hold credit.
for (const l of lines) {
if (l.drawn <= 0n) continue;
const score = fraud.analyze(l.agent_id).score;
if (score >= FRAUD_EXPOSURE_SCORE) {
alerts.push({
severity: "critical",
code: "fraud_exposure",
subject: l.agent_id,
message: `${l.agent_id} holds drawn credit but has a fraud score of ${score}.`,
});
}
}
// 5. Open disputes against agents with credit.
for (const l of lines) {
if (ledger.disputes.openCount(l.agent_id) > 0 && l.drawn > 0n) {
alerts.push({
severity: "warning",
code: "dispute_on_borrower",
subject: l.agent_id,
message: `${l.agent_id} has an open dispute while holding drawn credit.`,
});
}
}
alerts.sort((a, b) => SEVERITY_RANK[a.severity] - SEVERITY_RANK[b.severity]);
const counts: Record<AlertSeverity, number> = { critical: 0, warning: 0, info: 0 };
for (const a of alerts) counts[a.severity]++;
return { generated_at: now, counts, alerts };
}