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

58 lines
1.9 KiB
TypeScript

import type { Ledger } from "../ledger/ledger.js";
/**
* Reputation movers — "who's rising and who's falling". A point-in-time leaderboard
* hides momentum; a counterparty wants to see trajectory. This reconstructs each
* agent's net reputation change from the canonical event log (ReputationUpdated)
* and surfaces the biggest gainers and losers, so trend — not just level — is
* visible.
*/
export interface Mover {
agent_id: string;
change: number; // net reputation delta over the observed window
current: number;
events: number; // number of reputation updates observed
}
export interface ReputationMovers {
generated_at: number;
gainers: Mover[];
losers: Mover[];
}
export function buildReputationMovers(ledger: Ledger, limit = 5): ReputationMovers {
// Walk the log once, tracking first/last observed reputation per agent.
const first = new Map<string, number>();
const last = new Map<string, number>();
const counts = new Map<string, number>();
for (const e of ledger.bus.all()) {
if (e.name !== "ReputationUpdated") continue;
const id = e.data.agent_id;
const value = Number(e.data.current ?? e.data.new_score);
if (typeof id !== "string" || !Number.isFinite(value)) continue;
if (!first.has(id)) first.set(id, Number(e.data.previous ?? value));
last.set(id, value);
counts.set(id, (counts.get(id) ?? 0) + 1);
}
const movers: Mover[] = [...last.keys()].map((id) => ({
agent_id: id,
change: (last.get(id) ?? 0) - (first.get(id) ?? 0),
current: ledger.agents.get(id)?.reputation_score ?? last.get(id) ?? 0,
events: counts.get(id) ?? 0,
}));
const cap = Math.max(1, Math.min(limit, 50));
const gainers = movers
.filter((m) => m.change > 0)
.sort((a, b) => b.change - a.change)
.slice(0, cap);
const losers = movers
.filter((m) => m.change < 0)
.sort((a, b) => a.change - b.change)
.slice(0, cap);
return { generated_at: ledger.clock.now(), gainers, losers };
}