caelum0x 24d073ee83 feat: KeeperHub execution layer + Flare interoperable/confidential credit
Two hackathon integrations spanning the whole credit stack, sim-first
(green with no keys) with real integrations behind env flags.

KeeperHub — the on-chain execution & reliability layer (lib/keeperhub/):
- OnchainExecutor: pay-per-execution (x402/MPP) -> simulate -> smart gas
  with exponential backoff -> MEV-protected private routing -> audit trail;
  real MCP client to app.keeperhub.com; deterministic sim fallback.
- Autonomous Credit Keeper: FTSO position health -> check->execute deleverage.
- Credit Automations: declarative price/health/schedule rules as workflows.
- Autonomous Scheduler: KeeperHub-cron loop running keeper + automations.

Flare — interoperable + confidential primitives (packages/.../flare, lib/flare):
- FXRP credit satellite: CAN-gated, FTSO-priced, FDC-attested; Coston2
  Solidity vault (contracts/flare/).
- Confidential Compute credit scoring (TEE-attested, private inputs).
- FTSO-priced multi-asset collateral; FAssets mint->collateralize lifecycle.

x402 Credit-Service Marketplace: sells cred402's intelligence pay-per-call
over x402 (402->pay->200); paid receipts feed the revenue->reputation loop.

Wiring: 78 MCP tools, /v1 + /api routes, console Flare + x402 tabs, 11 demo
CLIs, 8 feature docs + docs/SUBMISSIONS.md, recorded demo video (media/).
346 tests pass; 7 adversarial verification workflows caught + fixed ~37
correctness/security issues.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-11 23:34:23 +03:00

88 lines
3.4 KiB
TypeScript

/**
* KeeperHubClient — a thin client for KeeperHub's Streamable-HTTP MCP server. The
* REAL path (active when `KEEPERHUB_API_KEY` is set) speaks MCP over HTTP to
* `https://app.keeperhub.com/mcp` with `Authorization: Bearer kh_<key>` and invokes
* KeeperHub's direct-execution tools:
*
* • `execute_contract_call` — state-changing call, with `simulate: true` preflight
* • `execute_transfer` — native / ERC-20 transfer
* • `get_direct_execution_status`— poll an execution id to a tx hash + status
*
* The MCP SDK is imported dynamically so the sim path (no key) needs neither the
* transport nor the network. Docs: https://docs.keeperhub.com/ai-tools/mcp-server
*/
const DEFAULT_MCP_URL = "https://app.keeperhub.com/mcp";
export interface KeeperHubToolResult {
content?: Array<{ type: string; text?: string }>;
isError?: boolean;
structuredContent?: unknown;
}
export class KeeperHubClient {
private readonly url: string;
private readonly apiKey?: string;
private client?: unknown; // lazily-connected MCP Client
private connecting?: Promise<unknown>;
constructor(opts: { url?: string; apiKey?: string } = {}) {
this.url = opts.url ?? process.env.KEEPERHUB_API_URL ?? DEFAULT_MCP_URL;
this.apiKey = opts.apiKey ?? process.env.KEEPERHUB_API_KEY;
}
/** True when a real KeeperHub credential is configured. */
isLive(): boolean {
return Boolean(this.apiKey);
}
private async connect(): Promise<unknown> {
if (this.client) return this.client;
if (this.connecting) return this.connecting;
this.connecting = (async () => {
const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
const transport = new StreamableHTTPClientTransport(new URL(this.url), {
requestInit: {
headers: this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {},
},
});
const client = new Client({ name: "cred402", version: "0.1.0" }, { capabilities: {} });
await client.connect(transport);
this.client = client;
return client;
})().catch((err) => {
// Never cache a rejected connect — a transient failure must not poison every
// future call. Clear the memo so the next callTool retries the connection.
this.connecting = undefined;
throw err;
});
return this.connecting;
}
/** Invoke a KeeperHub MCP tool and return its parsed JSON payload. */
async callTool(name: string, args: Record<string, unknown>): Promise<unknown> {
if (!this.isLive()) throw new Error("keeperhub: no KEEPERHUB_API_KEY configured");
const client = (await this.connect()) as {
callTool(params: { name: string; arguments: Record<string, unknown> }): Promise<KeeperHubToolResult>;
};
const res = await client.callTool({ name, arguments: args });
if (res.isError) throw new Error(`keeperhub: tool ${name} returned an error`);
if (res.structuredContent !== undefined) return res.structuredContent;
const text = res.content?.find((c) => c.type === "text")?.text;
if (!text) return {};
try {
return JSON.parse(text);
} catch {
return { text };
}
}
async close(): Promise<void> {
const c = this.client as { close?: () => Promise<void> } | undefined;
if (c?.close) await c.close();
this.client = undefined;
this.connecting = undefined;
}
}