x402-perpcheck / tree / src/lite.js ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// PerpCheck client used by the stdio MCP server and as a small library. Free tier: the hosted preview (verdict, issue and
// warning codes, confidence; 10/min per IP). Messages, fix values, liquidation, impact and funding: paid API over x402 (paid.js).
// Input validation is the same as the hosted API (engine.js parseOrder, no network).
import { withDeadline } from './deadline.js';
import { parseOrder, DISCLAIMER } from './engine.js';
export { DISCLAIMER };

export const API_BASE = 'https://api.youraitools.co/perpcheck';
export const FALLBACK_API_BASE = 'https://perpcheck.agentgate.workers.dev';
export const PAID_URL = `${API_BASE}/v1/full`;
export const PRICES = { lite: '$0.003', full: '$0.01' };
export const FULL_DETAILS = `Issue messages, fix values and coin rules: paid API ${API_BASE}/v1/lite (${PRICES.lite} USDC over x402, or a prepaid credits key); plus liquidation estimate, taker impact, funding carry and market facts: ${API_BASE}/v1/full (${PRICES.full}). Free full checks (30/day per IP): remote MCP ${API_BASE}/mcp.`;

/** Validate an order locally (no network). Returns { order, query } or { error }. */
export function toOrder(args = {}) {
  try {
    const o = parseOrder(args);
    const query = Object.fromEntries(Object.entries({ coin: o.coin, side: o.side > 0 ? 'long' : 'short', size: o.rawSize, notional: o.rawSize === undefined ? o.notional : undefined, price: o.rawPrice, leverage: o.leverage, margin: o.margin, reduceOnly: o.reduceOnly ? 'true' : undefined }).filter(([, v]) => v !== undefined).map(([k, v]) => [k, String(v)]));
    return { order: o, query };
  } catch (e) { return { error: e.message }; }
}

/** Free hosted preview. Returns the preview JSON or { error, message }. */
export async function preview(query, { fetchImpl = globalThis.fetch, apiBase = API_BASE, ms = 30000 } = {}) {
  try {
    const res = await withDeadline(fetchImpl(`${apiBase.replace(/\/+$/, '')}/v1/preview?${new URLSearchParams(query)}`, { headers: { accept: 'application/json', 'user-agent': 'perpcheck-stdio' } }), ms, 'PerpCheck preview');
    const j = await res.json().catch(() => null);
    if (!res.ok) return { error: j?.error || `http_${res.status}`, message: j?.message || `PerpCheck preview answered HTTP ${res.status}` };
    return j;
  } catch (e) { return { error: 'network', message: String(e?.message || e).slice(0, 200) }; }
}

agentgate-source: code mirror on Cloudflare Pages. All repos