@0xarchive/sdk 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -12,7 +12,7 @@ var OxArchiveError = class extends Error {
12
12
 
13
13
  // src/http.ts
14
14
  function snakeToCamel(str) {
15
- return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
15
+ return str.replace(/_([a-z0-9])/g, (_, char) => char.toUpperCase());
16
16
  }
17
17
  function transformKeys(obj) {
18
18
  if (obj === null || obj === void 0) {
@@ -217,7 +217,26 @@ var CandleSchema = z.object({
217
217
  quoteVolume: z.number().optional(),
218
218
  tradeCount: z.number().optional()
219
219
  });
220
- var WsChannelSchema = z.enum(["orderbook", "trades", "candles", "liquidations", "ticker", "all_tickers"]);
220
+ var WsChannelSchema = z.enum([
221
+ "orderbook",
222
+ "trades",
223
+ "candles",
224
+ "liquidations",
225
+ "ticker",
226
+ "all_tickers",
227
+ "open_interest",
228
+ "funding",
229
+ "lighter_orderbook",
230
+ "lighter_trades",
231
+ "lighter_candles",
232
+ "lighter_open_interest",
233
+ "lighter_funding",
234
+ "hip3_orderbook",
235
+ "hip3_trades",
236
+ "hip3_candles",
237
+ "hip3_open_interest",
238
+ "hip3_funding"
239
+ ]);
221
240
  var WsConnectionStateSchema = z.enum(["connecting", "connected", "disconnected", "reconnecting"]);
222
241
  var WsSubscribedSchema = z.object({
223
242
  type: z.literal("subscribed"),
@@ -252,17 +271,17 @@ var WsReplayStartedSchema = z.object({
252
271
  });
253
272
  var WsReplayPausedSchema = z.object({
254
273
  type: z.literal("replay_paused"),
255
- currentTimestamp: z.number()
274
+ current_timestamp: z.number()
256
275
  });
257
276
  var WsReplayResumedSchema = z.object({
258
277
  type: z.literal("replay_resumed"),
259
- currentTimestamp: z.number()
278
+ current_timestamp: z.number()
260
279
  });
261
280
  var WsReplayCompletedSchema = z.object({
262
281
  type: z.literal("replay_completed"),
263
282
  channel: WsChannelSchema,
264
283
  coin: z.string(),
265
- snapshotsSent: z.number()
284
+ snapshots_sent: z.number()
266
285
  });
267
286
  var WsReplayStoppedSchema = z.object({
268
287
  type: z.literal("replay_stopped")
@@ -274,6 +293,13 @@ var WsHistoricalDataSchema = z.object({
274
293
  timestamp: z.number(),
275
294
  data: z.unknown()
276
295
  });
296
+ var WsReplaySnapshotSchema = z.object({
297
+ type: z.literal("replay_snapshot"),
298
+ channel: WsChannelSchema,
299
+ coin: z.string(),
300
+ timestamp: z.number(),
301
+ data: z.unknown()
302
+ });
277
303
  var WsStreamStartedSchema = z.object({
278
304
  type: z.literal("stream_started"),
279
305
  channel: WsChannelSchema,
@@ -283,7 +309,7 @@ var WsStreamStartedSchema = z.object({
283
309
  });
284
310
  var WsStreamProgressSchema = z.object({
285
311
  type: z.literal("stream_progress"),
286
- snapshotsSent: z.number()
312
+ snapshots_sent: z.number()
287
313
  });
288
314
  var TimestampedRecordSchema = z.object({
289
315
  timestamp: z.number(),
@@ -299,11 +325,11 @@ var WsStreamCompletedSchema = z.object({
299
325
  type: z.literal("stream_completed"),
300
326
  channel: WsChannelSchema,
301
327
  coin: z.string(),
302
- snapshotsSent: z.number()
328
+ snapshots_sent: z.number()
303
329
  });
304
330
  var WsStreamStoppedSchema = z.object({
305
331
  type: z.literal("stream_stopped"),
306
- snapshotsSent: z.number()
332
+ snapshots_sent: z.number()
307
333
  });
308
334
  var WsServerMessageSchema = z.discriminatedUnion("type", [
309
335
  WsSubscribedSchema,
@@ -316,6 +342,7 @@ var WsServerMessageSchema = z.discriminatedUnion("type", [
316
342
  WsReplayResumedSchema,
317
343
  WsReplayCompletedSchema,
318
344
  WsReplayStoppedSchema,
345
+ WsReplaySnapshotSchema,
319
346
  WsHistoricalDataSchema,
320
347
  WsStreamStartedSchema,
321
348
  WsStreamProgressSchema,
@@ -334,6 +361,70 @@ var OpenInterestResponseSchema = ApiResponseSchema(OpenInterestSchema);
334
361
  var OpenInterestArrayResponseSchema = ApiResponseSchema(z.array(OpenInterestSchema));
335
362
  var CandleArrayResponseSchema = ApiResponseSchema(z.array(CandleSchema));
336
363
  var LiquidationArrayResponseSchema = ApiResponseSchema(z.array(LiquidationSchema));
364
+ var LiquidationVolumeSchema = z.object({
365
+ coin: z.string(),
366
+ timestamp: z.string(),
367
+ totalUsd: z.number(),
368
+ longUsd: z.number(),
369
+ shortUsd: z.number(),
370
+ count: z.number(),
371
+ longCount: z.number(),
372
+ shortCount: z.number()
373
+ });
374
+ var LiquidationVolumeArrayResponseSchema = z.object({
375
+ success: z.boolean().optional(),
376
+ data: z.array(LiquidationVolumeSchema),
377
+ meta: ApiMetaSchema.optional()
378
+ });
379
+ var DataTypeFreshnessInfoSchema = z.object({
380
+ lastUpdated: z.string().nullable().optional(),
381
+ lagMs: z.number().nullable().optional()
382
+ });
383
+ var CoinFreshnessSchema = z.object({
384
+ coin: z.string(),
385
+ exchange: z.string(),
386
+ measuredAt: z.string(),
387
+ orderbook: DataTypeFreshnessInfoSchema,
388
+ trades: DataTypeFreshnessInfoSchema,
389
+ funding: DataTypeFreshnessInfoSchema,
390
+ openInterest: DataTypeFreshnessInfoSchema,
391
+ liquidations: DataTypeFreshnessInfoSchema.optional()
392
+ });
393
+ var CoinFreshnessResponseSchema = z.object({
394
+ success: z.boolean().optional(),
395
+ data: CoinFreshnessSchema,
396
+ meta: ApiMetaSchema.optional()
397
+ });
398
+ var CoinSummarySchema = z.object({
399
+ coin: z.string(),
400
+ timestamp: z.string(),
401
+ markPrice: z.string().nullable().optional(),
402
+ oraclePrice: z.string().nullable().optional(),
403
+ midPrice: z.string().nullable().optional(),
404
+ fundingRate: z.string().nullable().optional(),
405
+ premium: z.string().nullable().optional(),
406
+ openInterest: z.string().nullable().optional(),
407
+ volume24h: z.string().nullable().optional(),
408
+ liquidationVolume24h: z.number().nullable().optional(),
409
+ longLiquidationVolume24h: z.number().nullable().optional(),
410
+ shortLiquidationVolume24h: z.number().nullable().optional()
411
+ });
412
+ var CoinSummaryResponseSchema = z.object({
413
+ success: z.boolean().optional(),
414
+ data: CoinSummarySchema,
415
+ meta: ApiMetaSchema.optional()
416
+ });
417
+ var PriceSnapshotSchema = z.object({
418
+ timestamp: z.string(),
419
+ markPrice: z.string().nullable().optional(),
420
+ oraclePrice: z.string().nullable().optional(),
421
+ midPrice: z.string().nullable().optional()
422
+ });
423
+ var PriceSnapshotArrayResponseSchema = z.object({
424
+ success: z.boolean().optional(),
425
+ data: z.array(PriceSnapshotSchema),
426
+ meta: ApiMetaSchema.optional()
427
+ });
337
428
 
338
429
  // src/orderbook-reconstructor.ts
339
430
  var OrderBookReconstructor = class {
@@ -1077,6 +1168,27 @@ var LiquidationsResource = class {
1077
1168
  nextCursor: response.meta.nextCursor
1078
1169
  };
1079
1170
  }
1171
+ /**
1172
+ * Get aggregated liquidation volume in time-bucketed intervals
1173
+ *
1174
+ * Returns pre-aggregated data with total/long/short USD volumes per bucket,
1175
+ * reducing data transfer by 100-1000x compared to individual liquidation records.
1176
+ *
1177
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
1178
+ * @param params - Time range, cursor, and interval parameters
1179
+ * @returns CursorResponse with liquidation volume buckets
1180
+ */
1181
+ async volume(coin, params) {
1182
+ const response = await this.http.get(
1183
+ `${this.basePath}/liquidations/${coin.toUpperCase()}/volume`,
1184
+ params,
1185
+ this.http.validationEnabled ? LiquidationVolumeArrayResponseSchema : void 0
1186
+ );
1187
+ return {
1188
+ data: response.data,
1189
+ nextCursor: response.meta.nextCursor
1190
+ };
1191
+ }
1080
1192
  };
1081
1193
 
1082
1194
  // src/resources/data-quality.ts
@@ -1181,7 +1293,7 @@ var DataQualityResource = class {
1181
1293
  */
1182
1294
  async symbolCoverage(exchange, symbol, options) {
1183
1295
  return this.http.get(
1184
- `${this.basePath}/coverage/${exchange.toLowerCase()}/${symbol.toUpperCase()}`,
1296
+ `${this.basePath}/coverage/${exchange.toLowerCase()}/${symbol}`,
1185
1297
  options
1186
1298
  );
1187
1299
  }
@@ -1308,7 +1420,9 @@ var HyperliquidClient = class {
1308
1420
  * HIP-3 builder-deployed perpetuals (February 2026+)
1309
1421
  */
1310
1422
  hip3;
1423
+ http;
1311
1424
  constructor(http) {
1425
+ this.http = http;
1312
1426
  const basePath = "/v1/hyperliquid";
1313
1427
  this.orderbook = new OrderBookResource(http, basePath);
1314
1428
  this.trades = new TradesResource(http, basePath);
@@ -1319,6 +1433,52 @@ var HyperliquidClient = class {
1319
1433
  this.liquidations = new LiquidationsResource(http, basePath);
1320
1434
  this.hip3 = new Hip3Client(http);
1321
1435
  }
1436
+ /**
1437
+ * Get per-coin data freshness across all data types
1438
+ *
1439
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
1440
+ * @returns Per-coin freshness with last_updated and lag_ms for each data type
1441
+ */
1442
+ async freshness(coin) {
1443
+ const response = await this.http.get(
1444
+ `/v1/hyperliquid/freshness/${coin.toUpperCase()}`,
1445
+ void 0,
1446
+ this.http.validationEnabled ? CoinFreshnessResponseSchema : void 0
1447
+ );
1448
+ return response.data;
1449
+ }
1450
+ /**
1451
+ * Get combined market summary (price, funding, OI, volume, liquidations) in one call
1452
+ *
1453
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
1454
+ * @returns Combined market summary
1455
+ */
1456
+ async summary(coin) {
1457
+ const response = await this.http.get(
1458
+ `/v1/hyperliquid/summary/${coin.toUpperCase()}`,
1459
+ void 0,
1460
+ this.http.validationEnabled ? CoinSummaryResponseSchema : void 0
1461
+ );
1462
+ return response.data;
1463
+ }
1464
+ /**
1465
+ * Get mark/oracle/mid price history (projected from OI data)
1466
+ *
1467
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
1468
+ * @param params - Time range, cursor, and interval parameters
1469
+ * @returns CursorResponse with price snapshots
1470
+ */
1471
+ async priceHistory(coin, params) {
1472
+ const response = await this.http.get(
1473
+ `/v1/hyperliquid/prices/${coin.toUpperCase()}`,
1474
+ params,
1475
+ this.http.validationEnabled ? PriceSnapshotArrayResponseSchema : void 0
1476
+ );
1477
+ return {
1478
+ data: response.data,
1479
+ nextCursor: response.meta.nextCursor
1480
+ };
1481
+ }
1322
1482
  };
1323
1483
  var Hip3Client = class {
1324
1484
  /**
@@ -1546,6 +1706,7 @@ var OxArchiveWs = class {
1546
1706
  batchHandlers = [];
1547
1707
  replayStartHandlers = [];
1548
1708
  replayCompleteHandlers = [];
1709
+ replaySnapshotHandlers = [];
1549
1710
  streamStartHandlers = [];
1550
1711
  streamProgressHandlers = [];
1551
1712
  streamCompleteHandlers = [];
@@ -1726,6 +1887,41 @@ var OxArchiveWs = class {
1726
1887
  interval: options.interval
1727
1888
  });
1728
1889
  }
1890
+ /**
1891
+ * Start a multi-channel historical replay with timing preserved.
1892
+ * Data from all channels is interleaved chronologically. Before the timeline
1893
+ * begins, `replay_snapshot` messages provide initial state for each channel.
1894
+ *
1895
+ * @param channels - Array of data channels to replay simultaneously
1896
+ * @param coin - Trading pair (e.g., 'BTC', 'ETH')
1897
+ * @param options - Replay options
1898
+ *
1899
+ * @example
1900
+ * ```typescript
1901
+ * ws.onReplaySnapshot((channel, coin, timestamp, data) => {
1902
+ * console.log(`Initial ${channel} state at ${new Date(timestamp).toISOString()}`);
1903
+ * });
1904
+ * ws.onHistoricalData((coin, timestamp, data) => {
1905
+ * // Interleaved data from all channels
1906
+ * });
1907
+ * ws.multiReplay(['orderbook', 'trades', 'funding'], 'BTC', {
1908
+ * start: Date.now() - 86400000,
1909
+ * speed: 10
1910
+ * });
1911
+ * ```
1912
+ */
1913
+ multiReplay(channels, coin, options) {
1914
+ this.send({
1915
+ op: "replay",
1916
+ channels,
1917
+ coin,
1918
+ start: options.start,
1919
+ end: options.end,
1920
+ speed: options.speed ?? 1,
1921
+ granularity: options.granularity,
1922
+ interval: options.interval
1923
+ });
1924
+ }
1729
1925
  /**
1730
1926
  * Pause the current replay
1731
1927
  */
@@ -1782,6 +1978,43 @@ var OxArchiveWs = class {
1782
1978
  interval: options.interval
1783
1979
  });
1784
1980
  }
1981
+ /**
1982
+ * Start a multi-channel bulk stream for fast data download.
1983
+ * Data from all channels arrives in batches without timing delays.
1984
+ * Before batches begin, `replay_snapshot` messages provide initial state
1985
+ * for each channel.
1986
+ *
1987
+ * @param channels - Array of data channels to stream simultaneously
1988
+ * @param coin - Trading pair (e.g., 'BTC', 'ETH')
1989
+ * @param options - Stream options
1990
+ *
1991
+ * @example
1992
+ * ```typescript
1993
+ * ws.onReplaySnapshot((channel, coin, timestamp, data) => {
1994
+ * console.log(`Initial ${channel} state`);
1995
+ * });
1996
+ * ws.onBatch((coin, records) => {
1997
+ * // Batches contain data from all requested channels
1998
+ * });
1999
+ * ws.multiStream(['orderbook', 'trades', 'open_interest'], 'BTC', {
2000
+ * start: Date.now() - 3600000,
2001
+ * end: Date.now(),
2002
+ * batchSize: 1000
2003
+ * });
2004
+ * ```
2005
+ */
2006
+ multiStream(channels, coin, options) {
2007
+ this.send({
2008
+ op: "stream",
2009
+ channels,
2010
+ coin,
2011
+ start: options.start,
2012
+ end: options.end,
2013
+ batch_size: options.batchSize ?? 1e3,
2014
+ granularity: options.granularity,
2015
+ interval: options.interval
2016
+ });
2017
+ }
1785
2018
  /**
1786
2019
  * Stop the current bulk stream
1787
2020
  */
@@ -1823,6 +2056,29 @@ var OxArchiveWs = class {
1823
2056
  onReplayComplete(handler) {
1824
2057
  this.replayCompleteHandlers.push(handler);
1825
2058
  }
2059
+ /**
2060
+ * Handle replay snapshot events (multi-channel mode).
2061
+ * Called with the initial state for each channel before the replay/stream
2062
+ * timeline begins. Use this to initialize local state (e.g., set the current
2063
+ * orderbook or latest funding rate) before `historical_data` messages start
2064
+ * arriving.
2065
+ *
2066
+ * @param handler - Callback receiving channel, coin, timestamp (ms), and data payload
2067
+ *
2068
+ * @example
2069
+ * ```typescript
2070
+ * ws.onReplaySnapshot((channel, coin, timestamp, data) => {
2071
+ * if (channel === 'orderbook') {
2072
+ * currentOrderbook = data;
2073
+ * } else if (channel === 'funding') {
2074
+ * currentFundingRate = data;
2075
+ * }
2076
+ * });
2077
+ * ```
2078
+ */
2079
+ onReplaySnapshot(handler) {
2080
+ this.replaySnapshotHandlers.push(handler);
2081
+ }
1826
2082
  /**
1827
2083
  * Handle stream started event
1828
2084
  */
@@ -1981,6 +2237,13 @@ var OxArchiveWs = class {
1981
2237
  }
1982
2238
  break;
1983
2239
  }
2240
+ case "replay_snapshot": {
2241
+ const msg = message;
2242
+ for (const handler of this.replaySnapshotHandlers) {
2243
+ handler(msg.channel, msg.coin, msg.timestamp, msg.data);
2244
+ }
2245
+ break;
2246
+ }
1984
2247
  case "stream_started": {
1985
2248
  const msg = message;
1986
2249
  for (const handler of this.streamStartHandlers) {
@@ -2032,6 +2295,11 @@ export {
2032
2295
  CandleArrayResponseSchema,
2033
2296
  CandleIntervalSchema,
2034
2297
  CandleSchema,
2298
+ CoinFreshnessResponseSchema,
2299
+ CoinFreshnessSchema,
2300
+ CoinSummaryResponseSchema,
2301
+ CoinSummarySchema,
2302
+ DataTypeFreshnessInfoSchema,
2035
2303
  FundingRateArrayResponseSchema,
2036
2304
  FundingRateResponseSchema,
2037
2305
  FundingRateSchema,
@@ -2045,6 +2313,8 @@ export {
2045
2313
  LiquidationArrayResponseSchema,
2046
2314
  LiquidationSchema,
2047
2315
  LiquidationSideSchema,
2316
+ LiquidationVolumeArrayResponseSchema,
2317
+ LiquidationVolumeSchema,
2048
2318
  OpenInterestArrayResponseSchema,
2049
2319
  OpenInterestResponseSchema,
2050
2320
  OpenInterestSchema,
@@ -2056,6 +2326,8 @@ export {
2056
2326
  OxArchiveError,
2057
2327
  OxArchiveWs,
2058
2328
  PriceLevelSchema,
2329
+ PriceSnapshotArrayResponseSchema,
2330
+ PriceSnapshotSchema,
2059
2331
  TimestampedRecordSchema,
2060
2332
  TradeArrayResponseSchema,
2061
2333
  TradeDirectionSchema,
@@ -2071,6 +2343,7 @@ export {
2071
2343
  WsReplayCompletedSchema,
2072
2344
  WsReplayPausedSchema,
2073
2345
  WsReplayResumedSchema,
2346
+ WsReplaySnapshotSchema,
2074
2347
  WsReplayStartedSchema,
2075
2348
  WsReplayStoppedSchema,
2076
2349
  WsServerMessageSchema,