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

117 lines
4.1 KiB
TypeScript

/**
* KeeperHub — the execution & reliability layer for on-chain AI agents. Cred402's
* agents *decide* (who is creditworthy, how much to draw, when to repay); KeeperHub
* *executes* the resulting transaction on-chain with guarantees: preflight
* simulation, smart gas estimation with exponential backoff, MEV-protected private
* routing, an audit trail, and pay-per-execution over x402 / MPP.
*
* KeeperHub is a Streamable-HTTP MCP server (https://app.keeperhub.com/mcp,
* `Authorization: Bearer kh_<key>`). Its direct-execution tools —
* `execute_contract_call`, `execute_transfer`, `get_direct_execution_status` — take
* a `simulate: true` preflight flag and return an execution id you poll to a tx
* hash. This module models that surface faithfully with a deterministic sim path so
* the whole loop runs with no key and no network.
*
* Docs: https://docs.keeperhub.com/ai-tools/mcp-server
*/
/** What an agent wants executed on-chain. Chain ids are KeeperHub's string form. */
export interface ExecutionIntent {
kind: "credit_draw" | "credit_repay" | "contract_call" | "transfer";
/** KeeperHub numeric chain id as a string, e.g. "114" (Coston2), "8453" (Base). */
chain_id: string;
/** Target contract (write) or recipient (transfer). */
to: string;
/** Solidity function signature for a contract call, e.g. "executeDraw(bytes32,uint256)". */
function?: string;
/** ABI-typed args for the call. */
args?: unknown[];
/** Native value (wei, decimal string) for transfers. */
value?: string;
/** The agent this execution acts for — carried into the audit trail. */
agent_id?: string;
/** Human label for the audit trail + KeeperHub dashboard. */
label: string;
/** Extra context recorded on the audit trail (trigger, decision inputs, …). */
metadata?: Record<string, unknown>;
}
/** Result of KeeperHub's preflight simulation (no broadcast). */
export interface SimulationResult {
ok: boolean;
gas_estimate?: number;
revert_reason?: string;
source: "keeperhub" | "sim";
}
/** Smart gas pricing (EIP-1559) with the backoff attempt that produced it. */
export interface GasEstimate {
max_fee_per_gas: string; // wei
max_priority_fee_per_gas: string; // wei
gas_limit: number;
attempts: number; // how many congestion-backoff bumps were applied
strategy: "eip1559-exponential-backoff";
}
/** Which pay-per-execution rail settled (or would settle) this execution. */
export type PaymentProtocol = "x402" | "mpp" | "sponsored" | "none";
export interface PaymentReceipt {
protocol: PaymentProtocol;
/** USDC (Base) / USDC.e (Tempo) smallest-unit price paid for the execution. */
amount: string;
asset: string; // "USDC" | "USDC.e"
network: string; // "base" | "tempo" | ...
/** x402: EIP-3009 TransferWithAuthorization proof / MPP: facilitator proof id. */
payment_proof?: string;
detail?: string;
}
/** One immutable audit-trail entry: trigger → simulation → submit → outcome. */
export interface AuditRecord {
audit_id: string;
execution_id: string;
agent_id?: string;
chain_id: string;
intent_kind: ExecutionIntent["kind"];
label: string;
trigger: Record<string, unknown>;
simulation: SimulationResult;
gas: GasEstimate;
payment: PaymentReceipt;
private_routed: boolean;
sponsored: boolean;
tx_hash: string;
gas_used?: number;
status: "simulated" | "submitted" | "confirmed" | "failed";
detail?: string;
submitted_at: number;
confirmed_at?: number;
}
/** What the executor returns to the adapter after a real (or simulated) execution. */
export interface ExecutionResult {
ok: boolean;
execution_id: string;
tx_hash: string;
audit_id: string;
simulation: SimulationResult;
gas: GasEstimate;
payment: PaymentReceipt;
private_routed: boolean;
sponsored: boolean;
gas_used?: number;
source: "keeperhub" | "sim";
detail?: string;
}
/**
* The seam a satellite adapter uses to push its decided transaction through
* KeeperHub. Any executor (real KeeperHub or the deterministic sim) satisfies it.
*/
export interface OnchainExecutor {
execute(intent: ExecutionIntent): Promise<ExecutionResult>;
auditTrail(): AuditRecord[];
isLive(): boolean;
}