File size: 10,915 Bytes
6b57d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e45dc9f
6b57d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import express, {
  type Express,
  type NextFunction,
  type Request,
  type Response,
} from "express";
import {
  paymentMiddleware,
  x402ResourceServer,
} from "@okxweb3/x402-express";
import { ExactEvmScheme } from "@okxweb3/x402-evm/exact/server";
import { OKXFacilitatorClient } from "@okxweb3/x402-core";
import { buildCatalog } from "./catalog.js";
import { CHAIN_ID, PRICE_USD } from "./config.js";
import {
  buildPreIpo,
  buildPreview,
  buildRunners,
  buildSignal,
  buildUniverse,
} from "./intel/builders.js";
import {
  validatePreIpoInput,
  validateRunnersInput,
  validateSignalInput,
} from "./intel/input.js";
import type { UniverseProvider } from "./intel/types.js";
import { isNyseOpen } from "./core/rules/marketHours.js";
import { log } from "./log.js";
import type { PaymentScanState } from "./payments.js";

export interface FakeFacilitator {
  getSupported(): Promise<unknown>;
  verify(): Promise<unknown>;
  settle(): Promise<unknown>;
}

export function challengeOnlyFacilitator(network: string): FakeFacilitator {
  return {
    async getSupported() {
      return {
        kinds: [
          {
            x402Version: 2,
            scheme: "exact",
            network: network as `${string}:${string}`,
          },
        ],
        extensions: [],
        signers: {},
      };
    },
    async verify() {
      throw new Error("payments not configured");
    },
    async settle() {
      throw new Error("payments not configured");
    },
  };
}

export interface AppDeps {
  env: {
    NETWORK: string;
    PAY_TO: string;
    PUBLIC_API_BASE_URL: string;
    WEB_ORIGIN: string;
  };
  universe: UniverseProvider;
  facilitator?: { apiKey: string; secretKey: string; passphrase: string } | FakeFacilitator;
  syncFacilitatorOnStart?: boolean;
  payments?: () => PaymentScanState;
}

const startedAt = Date.now();

export function createApp(deps: AppDeps): Express {
  const app = express();
  app.set("trust proxy", true);
  app.use(express.json());
  app.use((req: Request, res: Response, next: NextFunction) => {
    res.setHeader("access-control-allow-origin", deps.env.WEB_ORIGIN);
    res.setHeader("access-control-allow-methods", "GET, POST, OPTIONS");
    res.setHeader(
      "access-control-allow-headers",
      "content-type, payment-signature, x-payment",
    );
    res.setHeader(
      "access-control-expose-headers",
      "PAYMENT-REQUIRED, PAYMENT-RESPONSE",
    );
    if (req.method === "OPTIONS") {
      res.sendStatus(204);
      return;
    }
    next();
  });
  app.use((req: Request, _res: Response, next: NextFunction) => {
    log("info", "request", { method: req.method, path: req.path });
    next();
  });

  app.get("/health", (_req, res) => {
    const market = isNyseOpen(new Date());
    const universe = deps.universe.status?.() ?? {
      ready: deps.universe.snapshot() !== null,
      perps: 0,
      spots: 0,
      preIpo: 0,
      candlesRefreshedAt: null,
      tickersRefreshedAt: null,
    };
    res.json({
      status: "ok",
      service: "UltraX",
      network: deps.env.NETWORK,
      payTo: deps.env.PAY_TO,
      paymentsConfigured: Boolean(
        deps.facilitator && "apiKey" in deps.facilitator,
      ),
      universe,
      usMarketOpen: market.open,
      uptime: Math.floor((Date.now() - startedAt) / 1000),
    });
  });

  app.get("/catalog", (_req, res) => {
    res.json(
      buildCatalog(
        deps.env.PUBLIC_API_BASE_URL,
        deps.env.NETWORK,
        deps.env.PAY_TO,
      ),
    );
  });

  app.get("/universe", (_req, res) => {
    const snapshot = deps.universe.snapshot();
    if (!snapshot) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    res.json(buildUniverse(snapshot));
  });

  app.get("/preview", async (_req, res) => {
    const snapshot = deps.universe.snapshot();
    if (!snapshot) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    res.json(await buildPreview(snapshot));
  });

  app.get("/payments/recent", (req, res) => {
    const requestedLimit = Number(req.query.limit ?? 20);
    const limit = Math.min(
      Number.isFinite(requestedLimit) ? Math.max(1, requestedLimit) : 20,
      50,
    );
    const snap = deps.payments?.() ?? {
      warming: false,
      scannedFrom: null,
      lastScanned: null,
      payments: [],
    };
    const recent = snap.payments.filter(
      (payment) => payment.timestamp >= Date.now() - 86_400_000,
    );
    res.json({
      payTo: deps.env.PAY_TO,
      count24h: recent.length,
      totalUsd24h:
        Math.round(
          recent.reduce((sum, payment) => sum + payment.amountUsd, 0) * 100,
        ) / 100,
      warming: snap.warming,
      scannedFrom: snap.scannedFrom,
      lastScanned: snap.lastScanned,
      payments: snap.payments.slice(0, limit),
    });
  });

  const respondValidation = (
    res: Response,
    validation:
      | { ok: true }
      | { ok: false; error: string }
      | { ok: false; missing: string[]; required: unknown },
  ) => {
    if (validation.ok) return false;
    if ("missing" in validation) {
      res.status(400).json({
        status: "input_required",
        missing: validation.missing,
        required: validation.required,
      });
      return true;
    }
    const hint = validation.error.includes("symbol")
      ? { hint: "GET /universe" }
      : {};
    res.status(400).json({ error: validation.error, ...hint });
    return true;
  };

  app.post("/runners", (req, res, next) => {
    const validation = validateRunnersInput(req.body);
    if (respondValidation(res, validation)) return;
    if (!deps.universe.snapshot()) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    next();
  });
  app.post("/signal", (req, res, next) => {
    const snapshot = deps.universe.snapshot();
    const validation = validateSignalInput(req.body, snapshot ?? undefined);
    if (respondValidation(res, validation)) return;
    if (!snapshot) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    next();
  });
  app.post("/preipo", (req, res, next) => {
    const snapshot = deps.universe.snapshot();
    const validation = validatePreIpoInput(req.body, snapshot ?? undefined);
    if (respondValidation(res, validation)) return;
    if (!snapshot) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    next();
  });

  const usingRealFacilitator = Boolean(
    deps.facilitator && "apiKey" in deps.facilitator,
  );
  const facilitator = usingRealFacilitator
    ? new OKXFacilitatorClient({
        ...(deps.facilitator as {
          apiKey: string;
          secretKey: string;
          passphrase: string;
        }),
        syncSettle: true,
      })
    : ((deps.facilitator as FakeFacilitator | undefined) ??
      challengeOnlyFacilitator(deps.env.NETWORK));
  const paymentServer = new x402ResourceServer(
    facilitator as unknown as ConstructorParameters<typeof x402ResourceServer>[0],
  ).register(
    deps.env.NETWORK as `${string}:${string}`,
    new ExactEvmScheme(),
  );
  paymentServer.onAfterSettle(async (ctx) => {
    const result = ctx.result as {
      transaction?: string;
      payer?: string;
      amount?: string;
    };
    log("info", "payment settled", {
      txHash: result.transaction,
      payer: result.payer,
      amount: result.amount,
    });
  });
  if (!usingRealFacilitator) {
    log("warn", "payments cannot be verified; 402 challenges still issued", {
      job: "x402",
    });
  }
  app.use(
    paymentMiddleware(
      {
        "POST /runners": {
          accepts: [
            {
              scheme: "exact",
              network: deps.env.NETWORK as `${string}:${string}`,
              payTo: deps.env.PAY_TO as `0x${string}`,
              price: `$${PRICE_USD.runners}`,
            },
          ],
          description: "OKX stock market runners",
          mimeType: "application/json",
        },
        "POST /signal": {
          accepts: [
            {
              scheme: "exact",
              network: deps.env.NETWORK as `${string}:${string}`,
              payTo: deps.env.PAY_TO as `0x${string}`,
              price: `$${PRICE_USD.signal}`,
            },
          ],
          description: "OKX stock predictive intelligence signal",
          mimeType: "application/json",
        },
        "POST /preipo": {
          accepts: [
            {
              scheme: "exact",
              network: deps.env.NETWORK as `${string}:${string}`,
              payTo: deps.env.PAY_TO as `0x${string}`,
              price: `$${PRICE_USD.preipo}`,
            },
          ],
          description: "OKX stock pre-IPO intelligence",
          mimeType: "application/json",
        },
      },
      paymentServer,
      undefined,
      undefined,
      deps.syncFacilitatorOnStart ?? true,
    ),
  );

  app.post("/runners", (req: Request, res: Response) => {
    const validation = validateRunnersInput(req.body);
    const snapshot = deps.universe.snapshot();
    if (!validation.ok || !snapshot) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    res.json(buildRunners(snapshot, validation.value));
  });
  app.post("/signal", async (req: Request, res: Response) => {
    const snapshot = deps.universe.snapshot();
    if (!snapshot) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    const validation = validateSignalInput(req.body, snapshot);
    if (!validation.ok) {
      respondValidation(res, validation);
      return;
    }
    try {
      res.json(
        await buildSignal(
          snapshot,
          validation.value.symbol,
          validation.value.period,
          (instId) => deps.universe.positioning(instId),
          deps.universe.orderBook
            ? (instId) => deps.universe.orderBook!(instId)
            : undefined,
        ),
      );
    } catch (error) {
      log("error", "signal failed", { err: String(error) });
      res.status(503).json({ error: "signal unavailable" });
    }
  });
  app.post("/preipo", async (req: Request, res: Response) => {
    const snapshot = deps.universe.snapshot();
    if (!snapshot) {
      res.status(503).json({ error: "universe warming up" });
      return;
    }
    const validation = validatePreIpoInput(req.body, snapshot);
    if (!validation.ok) {
      respondValidation(res, validation);
      return;
    }
    try {
      res.json(
        await buildPreIpo(
          snapshot,
          validation.value.symbol,
          deps.universe.orderBook
            ? (instId) => deps.universe.orderBook!(instId)
            : undefined,
        ),
      );
    } catch (error) {
      log("error", "pre-IPO request failed", { err: String(error) });
      res.status(503).json({ error: "pre-IPO data unavailable" });
    }
  });

  return app;
}