cred402/lib/keeperhub/payments.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

87 lines
3.3 KiB
TypeScript

import { keccak256 } from "../x402/evm.js";
import type { ExecutionIntent, PaymentProtocol, PaymentReceipt } from "./types.js";
/**
* PaymentRouter — pay-per-execution over KeeperHub's two rails, indexed on
* x402scan.com when settled on Base:
*
* • x402 — Base USDC, EIP-3009 `TransferWithAuthorization` (the default rail)
* • MPP — Tempo USDC.e, an authorization the MPP facilitator settles
*
* KeeperHub's agentic wallet pays via x402 by default and uses MPP when a workflow
* is MPP-only; a paid `call_workflow` returns an HTTP 402 challenge the wallet
* settles server-side, then the call retries transparently. On mainnet Ethereum
* KeeperHub can sponsor gas, so an execution can settle at zero cost to the agent.
*
* This models that selection deterministically (real settlement stays server-side
* inside KeeperHub's wallet). Docs: https://docs.keeperhub.com/ai-tools/agentic-wallet
*/
/** An HTTP 402 pay-per-execution challenge as returned by a paid workflow call. */
export interface PaymentChallenge {
/** Protocols the workflow will accept, in server preference order. */
accepts: PaymentProtocol[];
/** Price in the asset's smallest unit (USDC/USDC.e are 6 dp). */
amount: string;
asset: string;
network: string;
pay_to?: string;
}
export interface PaymentRouterOptions {
/** Per-execution price in USDC/USDC.e smallest units (6 dp). Default 10000 = $0.01. */
pricePerExecution?: string;
/** Force MPP (workflow is MPP-only). Default false → x402 preferred. */
mppOnly?: boolean;
/** KeeperHub gas sponsorship (mainnet Ethereum). Default false. */
sponsorship?: boolean;
}
const ETHEREUM_MAINNET = "1";
export class PaymentRouter {
private readonly price: string;
private readonly mppOnly: boolean;
private readonly sponsorship: boolean;
constructor(opts: PaymentRouterOptions = {}) {
this.price = opts.pricePerExecution ?? "10000"; // $0.01 USDC
this.mppOnly = opts.mppOnly ?? (process.env.KEEPERHUB_MPP_ONLY === "1");
this.sponsorship = opts.sponsorship ?? (process.env.KEEPERHUB_GAS_SPONSORSHIP === "1");
}
/** Decide which rail settles this execution and produce its receipt. */
settle(intent: ExecutionIntent): PaymentReceipt {
// Gas sponsorship (mainnet Ethereum) → the agent pays nothing.
if (this.sponsorship && intent.chain_id === ETHEREUM_MAINNET) {
return {
protocol: "sponsored",
amount: "0",
asset: "ETH",
network: "ethereum",
payment_proof: keccak256(`kh-sponsor:${intent.label}:${intent.chain_id}`),
detail: "gas sponsored by KeeperHub on mainnet Ethereum",
};
}
const protocol: PaymentProtocol = this.mppOnly ? "mpp" : "x402";
if (protocol === "mpp") {
return {
protocol: "mpp",
amount: this.price,
asset: "USDC.e",
network: "tempo",
payment_proof: keccak256(`mpp:${intent.label}:${intent.chain_id}:${this.price}`),
detail: "settled via MPP facilitator on Tempo",
};
}
return {
protocol: "x402",
amount: this.price,
asset: "USDC",
network: "base",
payment_proof: keccak256(`x402:${intent.label}:${intent.chain_id}:${this.price}`),
detail: "settled via x402 (EIP-3009 TransferWithAuthorization) on Base; indexed on x402scan.com",
};
}
}