/** * Thin buyer-side fetch wrapper for agent apps. * * Composes official `@x402/fetch` (`wrapFetchWithPayment`) + `@x402/evm` * (`ExactEvmScheme`) so we do not reinvent PAYMENT-REQUIRED / PAYMENT-SIGNATURE. * * Hard locks: * - Headers: PAYMENT-REQUIRED / PAYMENT-SIGNATURE / PAYMENT-RESPONSE * - Max automatic payment retries = 1 (official wrapFetchWithPayment; we do not * register recovery hooks that would authorize a second paid retry) * - Circuit breaker (rate / minute spend / fingerprint) then budgets enforced * BEFORE signing via selector + onBeforePaymentCreation */ import { type Network, type SelectPaymentRequirements } from "@x402/fetch"; import { type ClientEvmSigner } from "@x402/evm"; import { type PrivateKeyAccount } from "viem/accounts"; import { createBudgetTracker, type BudgetLimits, type BudgetTracker } from "./budget.js"; import { type CircuitBreaker } from "./circuit-breaker.js"; /** Hardcoded: never loop payment retries (official client retries once). */ export declare const MAX_AUTOMATIC_PAYMENT_RETRIES: 1; /** Local / signing account usable with ExactEvmScheme (needs signTypedData). */ export type X402BuyerAccount = PrivateKeyAccount | ClientEvmSigner; export type CreateX402FetchOptions = { /** Hex private key for EIP-3009 signing (viem privateKeyToAccount). */ privateKey?: `0x${string}`; /** Pre-built signing account (alternative to privateKey). */ account?: X402BuyerAccount; /** Max USDC for a single accept (default 0.05). */ maxSingleSpendUsdc?: number; /** Max cumulative USDC across calls on this fetch instance (default 1.00). */ maxTotalSpendUsdc?: number; /** * Max paid 402s in a rolling 60s window (default 10). * Circuit breaker dimension 1 — inspected before budget / sign. */ maxPaidRequestsPerMinute?: number; /** * Max USDC paid in a rolling 60s window (default 0.05). * Circuit breaker dimension 2 — inspected before budget / sign. */ maxSpendUsdcPerMinute?: number; /** * Halt when the same request fingerprint appears ≥2 prior times in the * rolling window (default true). Circuit breaker dimension 3. */ enableFingerprintBreaker?: boolean; /** * CAIP-2 networks to register (default `["eip155:*"]`). * Pass specific ids (e.g. `eip155:8453`) to restrict. */ networks?: Network[]; /** Underlying fetch (default globalThis.fetch). Injectable for tests. */ fetch?: typeof globalThis.fetch; }; export type X402Fetch = (input: RequestInfo | URL, init?: RequestInit) => Promise; /** * Prefer the cheapest accept that fits circuit + budgets; throw a clear Error if none do. * Used as paymentRequirementsSelector so checks run before EIP-3009 sign. */ export declare function createBudgetSelector(tracker: BudgetTracker, circuit?: CircuitBreaker): SelectPaymentRequirements; /** * Create a fetch that auto-handles HTTP 402 once: * parse PAYMENT-REQUIRED → circuit breaker → budget check → EIP-3009 sign → * retry with PAYMENT-SIGNATURE. * * @example * ```ts * import { createX402Fetch } from "x402-micro-tollgate/client"; * const fetch402 = createX402Fetch({ privateKey: process.env.BUYER_KEY as `0x${string}` }); * const res = await fetch402("https://example.com/v1/quote"); * ``` */ export declare function createX402Fetch(options: CreateX402FetchOptions): X402Fetch; /** Test/helper: expose remaining budget on a tracker (not on the fetch itself). */ export { createBudgetTracker, type BudgetTracker, type BudgetLimits };