cred402/scripts/collateral_run.ts
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

92 lines
3.6 KiB
TypeScript

/**
* collateral_run.ts — FTSO-priced multi-asset collateral end to end.
*
* An agent draws an FXRP position that trips a margin call against its reputation cap.
* Instead of deleveraging, it POSTS collateral in several assets (USDC, BTC, ETH) —
* each marked to its live FTSO feed and discounted by an LTV haircut — which expands
* its borrowing power and cures the position with no repay. This is Flare's multi-feed
* FTSO oracle turning agent credit into a real over-collateralized primitive.
*
* npm run collateral:run
*
* Sim by default (deterministic FTSO reference prices); set FLARE_RPC_URL for live FTSO.
*/
import { Ledger } from "../lib/ledger/index.js";
import { Cred402Economy } from "../agents/economy.js";
import { FlareCreditSatellite } from "../lib/flare/satellite.js";
import { PositionEngine } from "../lib/flare/positions.js";
import { banner, scene } from "./render.js";
async function main(): Promise<void> {
const ledger = new Ledger();
const econ = new Cred402Economy(ledger);
econ.bootstrap();
econ.createJob();
const { reports } = await econ.runEvidencePurchases();
await econ.runWatchdogAudit(reports);
econ.applyReputationEngine();
econ.scoreJob();
econ.underwriteSeller();
const agentId = econ.seller.agent_id;
const flare = new FlareCreditSatellite(ledger);
const engine = new PositionEngine(flare.vault, ledger, flare.priceClient, {}, flare.collateral);
banner("Cred402 collateral — FTSO-priced borrowing power");
// 1) Draw a position that exceeds the reputation cap → margin call.
await flare.draw(agentId, 8500n * 1_000_000n);
const p0 = await engine.assess(agentId);
scene({
scene: "Draw 8,500 FXRP — over the reputation cap",
lines: [
`debt $${p0.debt_usd} vs cap $${p0.cap_usd} (collateral $${p0.collateral_usd}) → borrowing power $${p0.borrowing_power_usd}`,
`health factor ${fmtHf(p0.health_factor)} · status ${p0.status.toUpperCase()}`,
],
});
// 2) Post collateral in three assets — FTSO-priced, LTV-discounted.
flare.collateral.deposit(agentId, "USDC", 1500);
flare.collateral.deposit(agentId, "BTC", 0.03);
flare.collateral.deposit(agentId, "ETH", 0.5);
const val = await flare.collateral.valueUsd(agentId);
scene({
scene: "Post collateral (USDC + BTC + ETH), valued by FTSO",
lines: [
...val.lines.map(
(l) => `${l.amount_whole} ${l.symbol} @ $${l.price_usd} (${l.price_source}) = $${l.value_usd} → $${l.borrowing_power_usd} power (LTV ${l.ltv_bps / 100}%)`,
),
`total collateral value $${val.total_value_usd} → borrowing power +$${val.borrowing_power_usd}`,
],
});
// 3) The position is cured by collateral — no repay.
const p1 = await engine.assess(agentId);
scene({
scene: "Position after collateral",
lines: [
`borrowing power now $${p1.borrowing_power_usd} (cap $${p1.cap_usd} + collateral $${p1.collateral_usd})`,
`health factor ${fmtHf(p0.health_factor)} → ${fmtHf(p1.health_factor)} · status ${p1.status.toUpperCase()} — cured with no repay`,
],
});
// 4) Stress test: what if BTC/ETH crash? (drop XRP too via price override for FXRP debt.)
const stress = await engine.assess(agentId, { priceOverride: p1.xrp_usd * 1.5 });
scene({
scene: `Stress — XRP/USD +50% to $${round2(p1.xrp_usd * 1.5)}`,
lines: [`debt would be $${stress.debt_usd} · HF ${fmtHf(stress.health_factor)} · status ${stress.status.toUpperCase()}`],
});
}
function fmtHf(hf: number): string {
return hf === Infinity ? "∞" : hf.toFixed(2);
}
function round2(n: number): number {
return Math.round(n * 100) / 100;
}
main().catch((err) => {
console.error(err);
process.exit(1);
});