@0xarchive/sdk 0.2.1 → 0.3.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.d.mts CHANGED
@@ -30,21 +30,23 @@ interface ApiResponse<T> {
30
30
  }
31
31
  /**
32
32
  * Pagination parameters for list endpoints
33
+ * @deprecated Use cursor-based pagination instead (CursorPaginationParams)
33
34
  */
34
35
  interface PaginationParams {
35
36
  /** Maximum number of results to return */
36
37
  limit?: number;
37
- /** Number of results to skip */
38
+ /** @deprecated Use cursor instead. Number of results to skip */
38
39
  offset?: number;
39
40
  }
40
41
  /**
41
42
  * Time range parameters for historical queries
43
+ * @deprecated Use CursorPaginationParams for better performance with large datasets
42
44
  */
43
45
  interface TimeRangeParams extends PaginationParams {
44
- /** Start timestamp (Unix ms or ISO string) */
45
- start?: number | string;
46
- /** End timestamp (Unix ms or ISO string) */
47
- end?: number | string;
46
+ /** Start timestamp (Unix ms or ISO string) - REQUIRED for history endpoints */
47
+ start: number | string;
48
+ /** End timestamp (Unix ms or ISO string) - REQUIRED for history endpoints */
49
+ end: number | string;
48
50
  }
49
51
  /**
50
52
  * A price level in the order book
@@ -129,10 +131,43 @@ interface Trade {
129
131
  /** User's wallet address */
130
132
  user_address?: string;
131
133
  }
134
+ /**
135
+ * @deprecated Use GetTradesCursorParams instead for better performance with large datasets
136
+ */
132
137
  interface GetTradesParams extends TimeRangeParams {
133
138
  /** Filter by side */
134
139
  side?: TradeSide;
135
140
  }
141
+ /**
142
+ * Cursor-based pagination parameters for trades (recommended)
143
+ * More efficient than offset-based pagination for large datasets.
144
+ * The cursor is a timestamp - use the `nextCursor` from the response to get the next page.
145
+ */
146
+ interface CursorPaginationParams {
147
+ /** Start timestamp (Unix ms or ISO string) - REQUIRED */
148
+ start: number | string;
149
+ /** End timestamp (Unix ms or ISO string) - REQUIRED */
150
+ end: number | string;
151
+ /** Cursor from previous response's nextCursor (timestamp). If not provided, starts from the beginning of the range. */
152
+ cursor?: number | string;
153
+ /** Maximum number of results to return (default: 100, max: 1000) */
154
+ limit?: number;
155
+ }
156
+ /**
157
+ * Parameters for getting trades with cursor-based pagination (recommended)
158
+ */
159
+ interface GetTradesCursorParams extends CursorPaginationParams {
160
+ /** Filter by side */
161
+ side?: TradeSide;
162
+ }
163
+ /**
164
+ * Response with cursor for pagination
165
+ */
166
+ interface CursorResponse<T> {
167
+ data: T;
168
+ /** Cursor for next page (use as cursor parameter) */
169
+ nextCursor?: string;
170
+ }
136
171
  /** Instrument type */
137
172
  type InstrumentType = 'perp' | 'spot';
138
173
  /**
@@ -467,10 +502,10 @@ declare class OrderBookResource {
467
502
  * Get historical order book snapshots
468
503
  *
469
504
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
470
- * @param params - Time range and pagination parameters
505
+ * @param params - Time range and pagination parameters (start is required)
471
506
  * @returns Array of order book snapshots
472
507
  */
473
- history(coin: string, params?: OrderBookHistoryParams): Promise<OrderBook[]>;
508
+ history(coin: string, params: OrderBookHistoryParams): Promise<OrderBook[]>;
474
509
  }
475
510
 
476
511
  /**
@@ -481,25 +516,60 @@ declare class OrderBookResource {
481
516
  * // Get recent trades
482
517
  * const trades = await client.trades.recent('BTC');
483
518
  *
484
- * // Get trade history with time range
485
- * const history = await client.trades.list('ETH', {
486
- * start: Date.now() - 3600000,
519
+ * // Get trade history with cursor-based pagination (recommended)
520
+ * let result = await client.trades.list('BTC', {
521
+ * start: Date.now() - 86400000,
487
522
  * end: Date.now(),
488
- * limit: 500
523
+ * limit: 1000
489
524
  * });
525
+ *
526
+ * // Get all pages
527
+ * const allTrades = [...result.data];
528
+ * while (result.nextCursor) {
529
+ * result = await client.trades.list('BTC', {
530
+ * start: Date.now() - 86400000,
531
+ * end: Date.now(),
532
+ * cursor: result.nextCursor,
533
+ * limit: 1000
534
+ * });
535
+ * allTrades.push(...result.data);
536
+ * }
490
537
  * ```
491
538
  */
492
539
  declare class TradesResource {
493
540
  private http;
494
541
  constructor(http: HttpClient);
495
542
  /**
496
- * Get trade history for a coin
543
+ * Get trade history for a coin using cursor-based pagination
544
+ *
545
+ * Uses cursor-based pagination by default, which is more efficient for large datasets.
546
+ * Use the `nextCursor` from the response as the `cursor` parameter to get the next page.
497
547
  *
498
548
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
499
- * @param params - Time range and pagination parameters
500
- * @returns Array of trades
549
+ * @param params - Time range and cursor pagination parameters (start and end are required)
550
+ * @returns Object with trades array and nextCursor for pagination
551
+ *
552
+ * @example
553
+ * ```typescript
554
+ * // First page
555
+ * let result = await client.trades.list('BTC', {
556
+ * start: Date.now() - 86400000,
557
+ * end: Date.now(),
558
+ * limit: 1000
559
+ * });
560
+ *
561
+ * // Subsequent pages
562
+ * while (result.nextCursor) {
563
+ * result = await client.trades.list('BTC', {
564
+ * start: Date.now() - 86400000,
565
+ * end: Date.now(),
566
+ * cursor: result.nextCursor,
567
+ * limit: 1000
568
+ * });
569
+ * }
570
+ * ```
501
571
  */
502
- list(coin: string, params?: GetTradesParams): Promise<Trade[]>;
572
+ list(coin: string, params: GetTradesCursorParams): Promise<CursorResponse<Trade[]>>;
503
573
  /**
504
574
  * Get most recent trades for a coin
505
575
  *
@@ -508,6 +578,26 @@ declare class TradesResource {
508
578
  * @returns Array of recent trades
509
579
  */
510
580
  recent(coin: string, limit?: number): Promise<Trade[]>;
581
+ /**
582
+ * Get trade history using cursor-based pagination (explicit endpoint)
583
+ *
584
+ * @deprecated Use `list()` instead - it now uses cursor-based pagination by default.
585
+ *
586
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
587
+ * @param params - Cursor pagination parameters (start and end are required)
588
+ * @returns Object with trades array and nextCursor for pagination
589
+ */
590
+ listWithCursor(coin: string, params: GetTradesCursorParams): Promise<CursorResponse<Trade[]>>;
591
+ /**
592
+ * Get trade history using offset-based pagination
593
+ *
594
+ * @deprecated Use `list()` with cursor-based pagination instead for better performance.
595
+ *
596
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
597
+ * @param params - Time range and offset pagination parameters
598
+ * @returns Array of trades (without cursor response wrapper)
599
+ */
600
+ listWithOffset(coin: string, params: GetTradesParams): Promise<Trade[]>;
511
601
  }
512
602
 
513
603
  /**
@@ -562,10 +652,10 @@ declare class FundingResource {
562
652
  * Get funding rate history for a coin
563
653
  *
564
654
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
565
- * @param params - Time range and pagination parameters
655
+ * @param params - Time range and pagination parameters (start is required)
566
656
  * @returns Array of funding rate records
567
657
  */
568
- history(coin: string, params?: TimeRangeParams): Promise<FundingRate[]>;
658
+ history(coin: string, params: TimeRangeParams): Promise<FundingRate[]>;
569
659
  /**
570
660
  * Get current funding rate for a coin
571
661
  *
@@ -598,10 +688,10 @@ declare class OpenInterestResource {
598
688
  * Get open interest history for a coin
599
689
  *
600
690
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
601
- * @param params - Time range and pagination parameters
691
+ * @param params - Time range and pagination parameters (start is required)
602
692
  * @returns Array of open interest records
603
693
  */
604
- history(coin: string, params?: TimeRangeParams): Promise<OpenInterest[]>;
694
+ history(coin: string, params: TimeRangeParams): Promise<OpenInterest[]>;
605
695
  /**
606
696
  * Get current open interest for a coin
607
697
  *
package/dist/index.d.ts CHANGED
@@ -30,21 +30,23 @@ interface ApiResponse<T> {
30
30
  }
31
31
  /**
32
32
  * Pagination parameters for list endpoints
33
+ * @deprecated Use cursor-based pagination instead (CursorPaginationParams)
33
34
  */
34
35
  interface PaginationParams {
35
36
  /** Maximum number of results to return */
36
37
  limit?: number;
37
- /** Number of results to skip */
38
+ /** @deprecated Use cursor instead. Number of results to skip */
38
39
  offset?: number;
39
40
  }
40
41
  /**
41
42
  * Time range parameters for historical queries
43
+ * @deprecated Use CursorPaginationParams for better performance with large datasets
42
44
  */
43
45
  interface TimeRangeParams extends PaginationParams {
44
- /** Start timestamp (Unix ms or ISO string) */
45
- start?: number | string;
46
- /** End timestamp (Unix ms or ISO string) */
47
- end?: number | string;
46
+ /** Start timestamp (Unix ms or ISO string) - REQUIRED for history endpoints */
47
+ start: number | string;
48
+ /** End timestamp (Unix ms or ISO string) - REQUIRED for history endpoints */
49
+ end: number | string;
48
50
  }
49
51
  /**
50
52
  * A price level in the order book
@@ -129,10 +131,43 @@ interface Trade {
129
131
  /** User's wallet address */
130
132
  user_address?: string;
131
133
  }
134
+ /**
135
+ * @deprecated Use GetTradesCursorParams instead for better performance with large datasets
136
+ */
132
137
  interface GetTradesParams extends TimeRangeParams {
133
138
  /** Filter by side */
134
139
  side?: TradeSide;
135
140
  }
141
+ /**
142
+ * Cursor-based pagination parameters for trades (recommended)
143
+ * More efficient than offset-based pagination for large datasets.
144
+ * The cursor is a timestamp - use the `nextCursor` from the response to get the next page.
145
+ */
146
+ interface CursorPaginationParams {
147
+ /** Start timestamp (Unix ms or ISO string) - REQUIRED */
148
+ start: number | string;
149
+ /** End timestamp (Unix ms or ISO string) - REQUIRED */
150
+ end: number | string;
151
+ /** Cursor from previous response's nextCursor (timestamp). If not provided, starts from the beginning of the range. */
152
+ cursor?: number | string;
153
+ /** Maximum number of results to return (default: 100, max: 1000) */
154
+ limit?: number;
155
+ }
156
+ /**
157
+ * Parameters for getting trades with cursor-based pagination (recommended)
158
+ */
159
+ interface GetTradesCursorParams extends CursorPaginationParams {
160
+ /** Filter by side */
161
+ side?: TradeSide;
162
+ }
163
+ /**
164
+ * Response with cursor for pagination
165
+ */
166
+ interface CursorResponse<T> {
167
+ data: T;
168
+ /** Cursor for next page (use as cursor parameter) */
169
+ nextCursor?: string;
170
+ }
136
171
  /** Instrument type */
137
172
  type InstrumentType = 'perp' | 'spot';
138
173
  /**
@@ -467,10 +502,10 @@ declare class OrderBookResource {
467
502
  * Get historical order book snapshots
468
503
  *
469
504
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
470
- * @param params - Time range and pagination parameters
505
+ * @param params - Time range and pagination parameters (start is required)
471
506
  * @returns Array of order book snapshots
472
507
  */
473
- history(coin: string, params?: OrderBookHistoryParams): Promise<OrderBook[]>;
508
+ history(coin: string, params: OrderBookHistoryParams): Promise<OrderBook[]>;
474
509
  }
475
510
 
476
511
  /**
@@ -481,25 +516,60 @@ declare class OrderBookResource {
481
516
  * // Get recent trades
482
517
  * const trades = await client.trades.recent('BTC');
483
518
  *
484
- * // Get trade history with time range
485
- * const history = await client.trades.list('ETH', {
486
- * start: Date.now() - 3600000,
519
+ * // Get trade history with cursor-based pagination (recommended)
520
+ * let result = await client.trades.list('BTC', {
521
+ * start: Date.now() - 86400000,
487
522
  * end: Date.now(),
488
- * limit: 500
523
+ * limit: 1000
489
524
  * });
525
+ *
526
+ * // Get all pages
527
+ * const allTrades = [...result.data];
528
+ * while (result.nextCursor) {
529
+ * result = await client.trades.list('BTC', {
530
+ * start: Date.now() - 86400000,
531
+ * end: Date.now(),
532
+ * cursor: result.nextCursor,
533
+ * limit: 1000
534
+ * });
535
+ * allTrades.push(...result.data);
536
+ * }
490
537
  * ```
491
538
  */
492
539
  declare class TradesResource {
493
540
  private http;
494
541
  constructor(http: HttpClient);
495
542
  /**
496
- * Get trade history for a coin
543
+ * Get trade history for a coin using cursor-based pagination
544
+ *
545
+ * Uses cursor-based pagination by default, which is more efficient for large datasets.
546
+ * Use the `nextCursor` from the response as the `cursor` parameter to get the next page.
497
547
  *
498
548
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
499
- * @param params - Time range and pagination parameters
500
- * @returns Array of trades
549
+ * @param params - Time range and cursor pagination parameters (start and end are required)
550
+ * @returns Object with trades array and nextCursor for pagination
551
+ *
552
+ * @example
553
+ * ```typescript
554
+ * // First page
555
+ * let result = await client.trades.list('BTC', {
556
+ * start: Date.now() - 86400000,
557
+ * end: Date.now(),
558
+ * limit: 1000
559
+ * });
560
+ *
561
+ * // Subsequent pages
562
+ * while (result.nextCursor) {
563
+ * result = await client.trades.list('BTC', {
564
+ * start: Date.now() - 86400000,
565
+ * end: Date.now(),
566
+ * cursor: result.nextCursor,
567
+ * limit: 1000
568
+ * });
569
+ * }
570
+ * ```
501
571
  */
502
- list(coin: string, params?: GetTradesParams): Promise<Trade[]>;
572
+ list(coin: string, params: GetTradesCursorParams): Promise<CursorResponse<Trade[]>>;
503
573
  /**
504
574
  * Get most recent trades for a coin
505
575
  *
@@ -508,6 +578,26 @@ declare class TradesResource {
508
578
  * @returns Array of recent trades
509
579
  */
510
580
  recent(coin: string, limit?: number): Promise<Trade[]>;
581
+ /**
582
+ * Get trade history using cursor-based pagination (explicit endpoint)
583
+ *
584
+ * @deprecated Use `list()` instead - it now uses cursor-based pagination by default.
585
+ *
586
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
587
+ * @param params - Cursor pagination parameters (start and end are required)
588
+ * @returns Object with trades array and nextCursor for pagination
589
+ */
590
+ listWithCursor(coin: string, params: GetTradesCursorParams): Promise<CursorResponse<Trade[]>>;
591
+ /**
592
+ * Get trade history using offset-based pagination
593
+ *
594
+ * @deprecated Use `list()` with cursor-based pagination instead for better performance.
595
+ *
596
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
597
+ * @param params - Time range and offset pagination parameters
598
+ * @returns Array of trades (without cursor response wrapper)
599
+ */
600
+ listWithOffset(coin: string, params: GetTradesParams): Promise<Trade[]>;
511
601
  }
512
602
 
513
603
  /**
@@ -562,10 +652,10 @@ declare class FundingResource {
562
652
  * Get funding rate history for a coin
563
653
  *
564
654
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
565
- * @param params - Time range and pagination parameters
655
+ * @param params - Time range and pagination parameters (start is required)
566
656
  * @returns Array of funding rate records
567
657
  */
568
- history(coin: string, params?: TimeRangeParams): Promise<FundingRate[]>;
658
+ history(coin: string, params: TimeRangeParams): Promise<FundingRate[]>;
569
659
  /**
570
660
  * Get current funding rate for a coin
571
661
  *
@@ -598,10 +688,10 @@ declare class OpenInterestResource {
598
688
  * Get open interest history for a coin
599
689
  *
600
690
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
601
- * @param params - Time range and pagination parameters
691
+ * @param params - Time range and pagination parameters (start is required)
602
692
  * @returns Array of open interest records
603
693
  */
604
- history(coin: string, params?: TimeRangeParams): Promise<OpenInterest[]>;
694
+ history(coin: string, params: TimeRangeParams): Promise<OpenInterest[]>;
605
695
  /**
606
696
  * Get current open interest for a coin
607
697
  *
package/dist/index.js CHANGED
@@ -122,7 +122,7 @@ var OrderBookResource = class {
122
122
  * Get historical order book snapshots
123
123
  *
124
124
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
125
- * @param params - Time range and pagination parameters
125
+ * @param params - Time range and pagination parameters (start is required)
126
126
  * @returns Array of order book snapshots
127
127
  */
128
128
  async history(coin, params) {
@@ -140,18 +140,44 @@ var TradesResource = class {
140
140
  this.http = http;
141
141
  }
142
142
  /**
143
- * Get trade history for a coin
143
+ * Get trade history for a coin using cursor-based pagination
144
+ *
145
+ * Uses cursor-based pagination by default, which is more efficient for large datasets.
146
+ * Use the `nextCursor` from the response as the `cursor` parameter to get the next page.
144
147
  *
145
148
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
146
- * @param params - Time range and pagination parameters
147
- * @returns Array of trades
149
+ * @param params - Time range and cursor pagination parameters (start and end are required)
150
+ * @returns Object with trades array and nextCursor for pagination
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * // First page
155
+ * let result = await client.trades.list('BTC', {
156
+ * start: Date.now() - 86400000,
157
+ * end: Date.now(),
158
+ * limit: 1000
159
+ * });
160
+ *
161
+ * // Subsequent pages
162
+ * while (result.nextCursor) {
163
+ * result = await client.trades.list('BTC', {
164
+ * start: Date.now() - 86400000,
165
+ * end: Date.now(),
166
+ * cursor: result.nextCursor,
167
+ * limit: 1000
168
+ * });
169
+ * }
170
+ * ```
148
171
  */
149
172
  async list(coin, params) {
150
173
  const response = await this.http.get(
151
174
  `/v1/trades/${coin.toUpperCase()}`,
152
175
  params
153
176
  );
154
- return response.data;
177
+ return {
178
+ data: response.data,
179
+ nextCursor: response.meta.next_cursor
180
+ };
155
181
  }
156
182
  /**
157
183
  * Get most recent trades for a coin
@@ -167,6 +193,34 @@ var TradesResource = class {
167
193
  );
168
194
  return response.data;
169
195
  }
196
+ /**
197
+ * Get trade history using cursor-based pagination (explicit endpoint)
198
+ *
199
+ * @deprecated Use `list()` instead - it now uses cursor-based pagination by default.
200
+ *
201
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
202
+ * @param params - Cursor pagination parameters (start and end are required)
203
+ * @returns Object with trades array and nextCursor for pagination
204
+ */
205
+ async listWithCursor(coin, params) {
206
+ return this.list(coin, params);
207
+ }
208
+ /**
209
+ * Get trade history using offset-based pagination
210
+ *
211
+ * @deprecated Use `list()` with cursor-based pagination instead for better performance.
212
+ *
213
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
214
+ * @param params - Time range and offset pagination parameters
215
+ * @returns Array of trades (without cursor response wrapper)
216
+ */
217
+ async listWithOffset(coin, params) {
218
+ const response = await this.http.get(
219
+ `/v1/trades/${coin.toUpperCase()}`,
220
+ params
221
+ );
222
+ return response.data;
223
+ }
170
224
  };
171
225
 
172
226
  // src/resources/instruments.ts
@@ -208,7 +262,7 @@ var FundingResource = class {
208
262
  * Get funding rate history for a coin
209
263
  *
210
264
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
211
- * @param params - Time range and pagination parameters
265
+ * @param params - Time range and pagination parameters (start is required)
212
266
  * @returns Array of funding rate records
213
267
  */
214
268
  async history(coin, params) {
@@ -241,7 +295,7 @@ var OpenInterestResource = class {
241
295
  * Get open interest history for a coin
242
296
  *
243
297
  * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
244
- * @param params - Time range and pagination parameters
298
+ * @param params - Time range and pagination parameters (start is required)
245
299
  * @returns Array of open interest records
246
300
  */
247
301
  async history(coin, params) {
@@ -317,6 +371,43 @@ var DEFAULT_WS_URL = "wss://api.0xarchive.io/ws";
317
371
  var DEFAULT_PING_INTERVAL = 3e4;
318
372
  var DEFAULT_RECONNECT_DELAY = 1e3;
319
373
  var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
374
+ function transformOrderbook(coin, raw) {
375
+ if ("bids" in raw && "asks" in raw) {
376
+ return raw;
377
+ }
378
+ const levels = raw.levels;
379
+ const time = raw.time;
380
+ const bids = [];
381
+ const asks = [];
382
+ if (levels && levels.length >= 2) {
383
+ for (const level of levels[0] || []) {
384
+ bids.push({ px: level.px, sz: level.sz, n: level.n });
385
+ }
386
+ for (const level of levels[1] || []) {
387
+ asks.push({ px: level.px, sz: level.sz, n: level.n });
388
+ }
389
+ }
390
+ let mid_price;
391
+ let spread;
392
+ let spread_bps;
393
+ if (bids.length > 0 && asks.length > 0) {
394
+ const bestBid = parseFloat(bids[0].px);
395
+ const bestAsk = parseFloat(asks[0].px);
396
+ const mid = (bestBid + bestAsk) / 2;
397
+ mid_price = mid.toString();
398
+ spread = (bestAsk - bestBid).toString();
399
+ spread_bps = ((bestAsk - bestBid) / mid * 1e4).toFixed(2);
400
+ }
401
+ return {
402
+ coin,
403
+ timestamp: time ? new Date(time).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
404
+ bids,
405
+ asks,
406
+ mid_price,
407
+ spread,
408
+ spread_bps
409
+ };
410
+ }
320
411
  var OxArchiveWs = class {
321
412
  ws = null;
322
413
  options;
@@ -730,8 +821,9 @@ var OxArchiveWs = class {
730
821
  }
731
822
  case "data": {
732
823
  if (message.channel === "orderbook") {
824
+ const orderbook = transformOrderbook(message.coin, message.data);
733
825
  for (const handler of this.orderbookHandlers) {
734
- handler(message.coin, message.data);
826
+ handler(message.coin, orderbook);
735
827
  }
736
828
  } else if (message.channel === "trades") {
737
829
  for (const handler of this.tradesHandlers) {