cred402/lib/ledger/contracts/service_category_registry.ts
caelum0x 0813724602 feat(p1): universal x402 receipts — credit for any service, not just RWA
Roadmap p1 shipped. Cred402 now underwrites ANY x402 service category, not just
RWA:
- lib/core/service_categories.ts: canonical category taxonomy + per-family
  credit-risk weights (rwa/data/compute/inference/storage/api/defi/compliance…),
  with legacy-type aliasing.
- ServiceCategoryRegistry (lib/ledger/contracts) — on-chain-style, governance-tunable
  weights, seeded defaults, wired into the Ledger.
- ServiceType generalized to an open category string (was a 6-value RWA union).
- Risk policy v1/v2 scale the credit line by the category risk weight; rationale
  records it. RWA evidence is now an optional boost, not a gate.
- test/universal_credit.test.ts (5 cases) + npm run demo:x402: a non-RWA
  inference.llm agent builds a credit line from x402 receipts alone.
- docs/risk_model.md + docs/roadmap (p1 ✅).

176 tests green; main demo unchanged (legacy RWA types keep the rwa weight).

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

95 lines
3.2 KiB
TypeScript

import { deployHash } from "../../core/hash.js";
import { SEED_SERVICE_CATEGORIES, categoryRiskBps, categoryFamily } from "../../core/service_categories.js";
import type { EventBus } from "../events.js";
import type { Clock } from "../clock.js";
const CONTRACT = "ServiceCategoryRegistry";
export interface ServiceCategoryRecord {
category: string; // "<family>.<name>", e.g. "inference.llm"
family: string; // "inference"
risk_bps: number; // credit-risk weight (10000 = 1.0x)
enabled: boolean;
created_at: number;
}
/**
* ServiceCategoryRegistry (roadmap p1) — the on-chain catalog of x402 service
* categories Cred402 will underwrite, with a governance-tunable credit-risk weight
* per category. This is what makes Cred402 a credit layer for the WHOLE x402
* economy: any registered category (data, compute, inference, storage, api, defi,
* compliance, rwa, …) is a first-class credit input. Mirrors the canonical
* defaults in `lib/core/service_categories.ts`; governance can add categories or
* tune weights without redeploying the pool or registries.
*/
export class ServiceCategoryRegistry {
private readonly categories = new Map<string, ServiceCategoryRecord>();
constructor(
private readonly bus: EventBus,
private readonly clock: Clock,
) {
// Seed the default taxonomy.
for (const c of SEED_SERVICE_CATEGORIES) this.seed(c);
}
private seed(category: string): void {
this.categories.set(category, {
category,
family: categoryFamily(category),
risk_bps: categoryRiskBps(category),
enabled: true,
created_at: this.clock.now(),
});
}
/** Governance: register (or re-enable) a category with an optional risk weight. */
register_category(category: string, risk_bps?: number): ServiceCategoryRecord {
const rec: ServiceCategoryRecord = {
category,
family: categoryFamily(category),
risk_bps: risk_bps ?? categoryRiskBps(category),
enabled: true,
created_at: this.clock.now(),
};
this.categories.set(category, rec);
this.bus.emit("ServiceCategoryRegistered", CONTRACT, deployHash(), {
category,
family: rec.family,
risk_bps: rec.risk_bps,
});
return { ...rec };
}
/** Governance: tune a category's credit-risk weight (bps). */
set_risk_weight(category: string, risk_bps: number): void {
const rec = this.categories.get(category);
if (!rec) return;
rec.risk_bps = risk_bps;
this.bus.emit("ServiceCategoryRiskUpdated", CONTRACT, deployHash(), { category, risk_bps });
}
set_enabled(category: string, enabled: boolean): void {
const rec = this.categories.get(category);
if (rec) rec.enabled = enabled;
}
/** Risk weight (bps) for a service type — registered value or canonical default. */
risk_bps(serviceType: string): number {
return this.categories.get(serviceType)?.risk_bps ?? categoryRiskBps(serviceType);
}
is_registered(serviceType: string): boolean {
return this.categories.has(serviceType);
}
get(category: string): ServiceCategoryRecord | undefined {
const r = this.categories.get(category);
return r ? { ...r } : undefined;
}
list(): ServiceCategoryRecord[] {
return [...this.categories.values()].map((r) => ({ ...r }));
}
}