Repository Public
1// Derivatives module (wave 2: read-only and paper) — public surface for the rest of the API service.2// No live trading here: no signing, no signer call, no request to a venue's trading endpoint. Controlled live trading is a3// separate part with its own venue objects: exec.ts, live.ts, live-venue.ts, live-routes.ts.4import { DerivData } from './data.ts';5import { PaperService } from './paper.ts';6import { createDefaultRegistry, type VenueRegistry } from './venues.ts';7// --- router (simulation) ---8import type { ChainCostsReader } from './chain-costs.ts';9import { RouteService, type LivePort } from './route.ts';10// --- /router ---1112export interface Derivatives { registry: VenueRegistry; data: DerivData; paper: PaperService; /** venue comparison in simulation (route.ts); absent on hand-built instances */ router?: RouteService }1314/** the parts wired together; tests pass a registry of fixture-backed adapters and a clock (and, for the router, a chain reader and a live port) */15export function createDerivatives(o: { registry?: VenueRegistry; now?: () => number; chain?: ChainCostsReader; live?: LivePort | null; gmx?: import('./route.ts').GmxPort | null } = {}): Derivatives {16 const now = o.now ?? Date.now, registry = o.registry ?? createDefaultRegistry({ now });17 const data = new DerivData(registry, { now }), paper = new PaperService(data, now);18 return { registry, data, paper, router: new RouteService({ data, paper, ...(o.chain ? { chain: o.chain } : {}), live: o.live ?? null, gmx: o.gmx ?? null }) };19}2021let shared: Derivatives | null = null;22/**23 * the process-wide instance on the real adapters (routes and worker share its caches and its rate-limit budget).24 * The router asks the live service only for its account list and its `preview`; it is imported on first use so that this25 * module does not load the signer client.26 */27export function derivatives(): Derivatives {28 const live: LivePort = {29 list: async () => (await import('./live.ts')).live().list(),30 preview: async (id, body) => (await import('./live.ts')).live().preview(id, body),31 };32 // v8: the router asks the GMX engine whether the wallet can create GMX orders now (imported on first use, like the live service)33 const gmx = { tradability: async () => (await import('./gmx-live.ts')).gmx().tradability() };34 return (shared ??= createDerivatives({ live, gmx }));35}3637export { DerivData, DATA_BOUNDS } from './data.ts';38export { PaperService, defaultPolicy } from './paper.ts';39export { RouteService, type LivePort, type RouteAnswer } from './route.ts';40export { createChainCostsReader, type ChainCostsReader } from './chain-costs.ts';41export { createDefaultRegistry, createRegistry, VenueRegistry, DYDX_RESTRICTION } from './venues.ts';42