@0xarchive/sdk 0.1.1 → 0.2.1

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/README.md CHANGED
@@ -77,19 +77,6 @@ const trades = await client.trades.list('ETH', {
77
77
  });
78
78
  ```
79
79
 
80
- ### Candles (OHLCV)
81
-
82
- ```typescript
83
- // Get hourly candles
84
- const candles = await client.candles.list('BTC', {
85
- interval: '1h',
86
- start: Date.now() - 86400000,
87
- end: Date.now()
88
- });
89
-
90
- // Available intervals: '1m', '5m', '15m', '1h', '4h', '1d'
91
- ```
92
-
93
80
  ### Instruments
94
81
 
95
82
  ```typescript
@@ -183,8 +170,8 @@ ws.onHistoricalData((coin, timestamp, data) => {
183
170
  console.log(`${new Date(timestamp)}: ${data.mid_price}`);
184
171
  });
185
172
 
186
- ws.onReplayStart((channel, coin, totalRecords, speed) => {
187
- console.log(`Starting replay of ${totalRecords} records at ${speed}x`);
173
+ ws.onReplayStart((channel, coin, start, end, speed) => {
174
+ console.log(`Starting replay from ${start} to ${end} at ${speed}x`);
188
175
  });
189
176
 
190
177
  ws.onReplayComplete((channel, coin, recordsSent) => {
@@ -219,8 +206,8 @@ ws.onBatch((coin, records) => {
219
206
  allData.push(...records.map(r => r.data));
220
207
  });
221
208
 
222
- ws.onStreamProgress((sent, total, pct) => {
223
- console.log(`Progress: ${pct.toFixed(1)}%`);
209
+ ws.onStreamProgress((snapshotsSent) => {
210
+ console.log(`Sent: ${snapshotsSent} snapshots`);
224
211
  });
225
212
 
226
213
  ws.onStreamComplete((channel, coin, recordsSent) => {
@@ -284,7 +271,6 @@ Full TypeScript support with exported types:
284
271
  import type {
285
272
  OrderBook,
286
273
  Trade,
287
- Candle,
288
274
  Instrument,
289
275
  FundingRate,
290
276
  OpenInterest
package/dist/index.d.mts CHANGED
@@ -9,14 +9,24 @@ interface ClientOptions {
9
9
  /** Request timeout in milliseconds (defaults to 30000) */
10
10
  timeout?: number;
11
11
  }
12
+ /**
13
+ * Response metadata
14
+ */
15
+ interface ApiMeta {
16
+ /** Number of records returned */
17
+ count: number;
18
+ /** Cursor for next page (if available) */
19
+ next_cursor?: string;
20
+ /** Unique request ID for debugging */
21
+ request_id: string;
22
+ }
12
23
  /**
13
24
  * Standard API response wrapper
14
25
  */
15
26
  interface ApiResponse<T> {
16
27
  success: boolean;
17
28
  data: T;
18
- count: number;
19
- request_id: string;
29
+ meta: ApiMeta;
20
30
  }
21
31
  /**
22
32
  * Pagination parameters for list endpoints
@@ -36,19 +46,35 @@ interface TimeRangeParams extends PaginationParams {
36
46
  /** End timestamp (Unix ms or ISO string) */
37
47
  end?: number | string;
38
48
  }
39
- /** A price level in the order book [price, size] */
40
- type PriceLevel = [string, string];
49
+ /**
50
+ * A price level in the order book
51
+ */
52
+ interface PriceLevel {
53
+ /** Price at this level */
54
+ px: string;
55
+ /** Total size at this price level */
56
+ sz: string;
57
+ /** Number of orders at this level */
58
+ n: number;
59
+ }
41
60
  /**
42
61
  * Order book snapshot
43
62
  */
44
63
  interface OrderBook {
64
+ /** Trading pair symbol (e.g., BTC, ETH) */
45
65
  coin: string;
46
- timestamp: number;
66
+ /** Snapshot timestamp (UTC) */
67
+ timestamp: string;
68
+ /** Bid price levels (best bid first) */
47
69
  bids: PriceLevel[];
70
+ /** Ask price levels (best ask first) */
48
71
  asks: PriceLevel[];
49
- mid_price: string;
50
- spread: string;
51
- spread_bps: string;
72
+ /** Mid price (best bid + best ask) / 2 */
73
+ mid_price?: string;
74
+ /** Spread in absolute terms (best ask - best bid) */
75
+ spread?: string;
76
+ /** Spread in basis points */
77
+ spread_bps?: string;
52
78
  }
53
79
  interface GetOrderBookParams {
54
80
  /** Timestamp to get order book at (Unix ms or ISO string) */
@@ -60,72 +86,112 @@ interface OrderBookHistoryParams extends TimeRangeParams {
60
86
  /** Number of price levels to return per side */
61
87
  depth?: number;
62
88
  }
89
+ /** Trade side: 'A' (ask/sell) or 'B' (bid/buy) */
90
+ type TradeSide = 'A' | 'B';
91
+ /** Position direction */
92
+ type TradeDirection = 'Open Long' | 'Open Short' | 'Close Long' | 'Close Short';
93
+ /** Data source */
94
+ type DataSource = 's3' | 'ws' | 'api';
63
95
  /**
64
- * Trade/fill record
96
+ * Trade/fill record with full execution details
65
97
  */
66
98
  interface Trade {
67
- id: string;
99
+ /** Trading pair symbol */
68
100
  coin: string;
69
- side: 'buy' | 'sell';
101
+ /** Trade side: 'A' (ask/sell) or 'B' (bid/buy) */
102
+ side: TradeSide;
103
+ /** Execution price */
70
104
  price: string;
105
+ /** Trade size */
71
106
  size: string;
72
- value: string;
73
- timestamp: number;
74
- trade_type: string;
107
+ /** Execution timestamp (UTC) */
108
+ timestamp: string;
109
+ /** Blockchain transaction hash */
110
+ tx_hash?: string;
111
+ /** Unique trade ID */
112
+ trade_id?: number;
113
+ /** Associated order ID */
114
+ order_id?: number;
115
+ /** True if taker (crossed the spread), false if maker */
116
+ crossed?: boolean;
117
+ /** Trading fee amount */
118
+ fee?: string;
119
+ /** Fee denomination (e.g., USDC) */
120
+ fee_token?: string;
121
+ /** Realized PnL if closing a position */
122
+ closed_pnl?: string;
123
+ /** Position direction */
124
+ direction?: TradeDirection;
125
+ /** Position size before this trade */
126
+ start_position?: string;
127
+ /** Data source */
128
+ source?: DataSource;
129
+ /** User's wallet address */
130
+ user_address?: string;
75
131
  }
76
132
  interface GetTradesParams extends TimeRangeParams {
77
133
  /** Filter by side */
78
- side?: 'buy' | 'sell';
79
- }
80
- /**
81
- * OHLCV candle
82
- */
83
- interface Candle {
84
- coin: string;
85
- interval: string;
86
- timestamp: number;
87
- open: string;
88
- high: string;
89
- low: string;
90
- close: string;
91
- volume: string;
92
- trades: number;
93
- }
94
- type CandleInterval = '1m' | '5m' | '15m' | '1h' | '4h' | '1d';
95
- interface GetCandlesParams extends TimeRangeParams {
96
- /** Candle interval */
97
- interval?: CandleInterval;
134
+ side?: TradeSide;
98
135
  }
136
+ /** Instrument type */
137
+ type InstrumentType = 'perp' | 'spot';
99
138
  /**
100
139
  * Trading instrument metadata
101
140
  */
102
141
  interface Instrument {
103
- coin: string;
142
+ /** Instrument symbol (e.g., BTC) */
104
143
  name: string;
105
- sz_decimals: number;
106
- max_leverage: number;
107
- only_isolated: boolean;
108
- is_active: boolean;
144
+ /** Size decimal precision */
145
+ szDecimals: number;
146
+ /** Maximum leverage allowed */
147
+ maxLeverage?: number;
148
+ /** If true, only isolated margin mode is allowed */
149
+ onlyIsolated?: boolean;
150
+ /** Type of instrument */
151
+ instrumentType?: InstrumentType;
152
+ /** Whether the instrument is currently tradeable */
153
+ isActive: boolean;
109
154
  }
110
155
  /**
111
156
  * Funding rate record
112
157
  */
113
158
  interface FundingRate {
159
+ /** Trading pair symbol */
114
160
  coin: string;
161
+ /** Funding timestamp (UTC) */
162
+ timestamp: string;
163
+ /** Funding rate as decimal (e.g., 0.0001 = 0.01%) */
115
164
  funding_rate: string;
116
- premium: string;
117
- timestamp: number;
165
+ /** Premium component of funding rate */
166
+ premium?: string;
118
167
  }
119
168
  /**
120
- * Open interest record
169
+ * Open interest snapshot with market context
121
170
  */
122
171
  interface OpenInterest {
172
+ /** Trading pair symbol */
123
173
  coin: string;
174
+ /** Snapshot timestamp (UTC) */
175
+ timestamp: string;
176
+ /** Total open interest in contracts */
124
177
  open_interest: string;
125
- timestamp: number;
126
- }
127
- /** WebSocket channel types */
128
- type WsChannel = 'orderbook' | 'trades' | 'ticker' | 'all_tickers' | 'candles' | 'funding' | 'openinterest';
178
+ /** Mark price used for liquidations */
179
+ mark_price?: string;
180
+ /** Oracle price from external feed */
181
+ oracle_price?: string;
182
+ /** 24-hour notional volume */
183
+ day_ntl_volume?: string;
184
+ /** Price 24 hours ago */
185
+ prev_day_price?: string;
186
+ /** Current mid price */
187
+ mid_price?: string;
188
+ /** Impact bid price for liquidations */
189
+ impact_bid_price?: string;
190
+ /** Impact ask price for liquidations */
191
+ impact_ask_price?: string;
192
+ }
193
+ /** WebSocket channel types. Note: ticker/all_tickers are real-time only. */
194
+ type WsChannel = 'orderbook' | 'trades' | 'ticker' | 'all_tickers';
129
195
  /** Subscribe message from client */
130
196
  interface WsSubscribe {
131
197
  op: 'subscribe';
@@ -207,7 +273,7 @@ interface WsError {
207
273
  type: 'error';
208
274
  message: string;
209
275
  }
210
- /** Data message from server */
276
+ /** Data message from server (real-time) */
211
277
  interface WsData<T = unknown> {
212
278
  type: 'data';
213
279
  channel: WsChannel;
@@ -219,10 +285,12 @@ interface WsReplayStarted {
219
285
  type: 'replay_started';
220
286
  channel: WsChannel;
221
287
  coin: string;
288
+ /** Start timestamp in milliseconds */
222
289
  start: number;
290
+ /** End timestamp in milliseconds */
223
291
  end: number;
292
+ /** Playback speed multiplier */
224
293
  speed: number;
225
- total_records: number;
226
294
  }
227
295
  /** Replay paused response */
228
296
  interface WsReplayPaused {
@@ -239,7 +307,7 @@ interface WsReplayCompleted {
239
307
  type: 'replay_completed';
240
308
  channel: WsChannel;
241
309
  coin: string;
242
- records_sent: number;
310
+ snapshots_sent: number;
243
311
  }
244
312
  /** Replay stopped response */
245
313
  interface WsReplayStopped {
@@ -258,39 +326,39 @@ interface WsStreamStarted {
258
326
  type: 'stream_started';
259
327
  channel: WsChannel;
260
328
  coin: string;
329
+ /** Start timestamp in milliseconds */
261
330
  start: number;
331
+ /** End timestamp in milliseconds */
262
332
  end: number;
263
- batch_size: number;
264
- total_records: number;
265
333
  }
266
- /** Stream progress response */
334
+ /** Stream progress response (sent periodically during streaming) */
267
335
  interface WsStreamProgress {
268
336
  type: 'stream_progress';
269
- records_sent: number;
270
- total_records: number;
271
- progress_pct: number;
337
+ snapshots_sent: number;
338
+ }
339
+ /** A record with timestamp for batched data */
340
+ interface TimestampedRecord<T = unknown> {
341
+ timestamp: number;
342
+ data: T;
272
343
  }
273
- /** Stream batch (bulk data) */
344
+ /** Batch of historical data (bulk streaming) */
274
345
  interface WsHistoricalBatch<T = unknown> {
275
346
  type: 'historical_batch';
276
347
  channel: WsChannel;
277
348
  coin: string;
278
- batch_index: number;
279
- records: Array<{
280
- timestamp: number;
281
- data: T;
282
- }>;
349
+ data: TimestampedRecord<T>[];
283
350
  }
284
351
  /** Stream completed response */
285
352
  interface WsStreamCompleted {
286
353
  type: 'stream_completed';
287
354
  channel: WsChannel;
288
355
  coin: string;
289
- records_sent: number;
356
+ snapshots_sent: number;
290
357
  }
291
358
  /** Stream stopped response */
292
359
  interface WsStreamStopped {
293
360
  type: 'stream_stopped';
361
+ snapshots_sent: number;
294
362
  }
295
363
  /** Server message union type */
296
364
  type WsServerMessage = WsSubscribed | WsUnsubscribed | WsPong | WsError | WsData | WsReplayStarted | WsReplayPaused | WsReplayResumed | WsReplayCompleted | WsReplayStopped | WsHistoricalData | WsStreamStarted | WsStreamProgress | WsHistoricalBatch | WsStreamCompleted | WsStreamStopped;
@@ -340,6 +408,8 @@ declare class OxArchiveError extends Error {
340
408
  requestId?: string;
341
409
  constructor(message: string, code: number, requestId?: string);
342
410
  }
411
+ /** Timestamp can be Unix ms (number), ISO string, or Date object */
412
+ type Timestamp = number | string | Date;
343
413
 
344
414
  interface HttpClientOptions {
345
415
  baseUrl: string;
@@ -440,38 +510,6 @@ declare class TradesResource {
440
510
  recent(coin: string, limit?: number): Promise<Trade[]>;
441
511
  }
442
512
 
443
- /**
444
- * Candles (OHLCV) API resource
445
- *
446
- * @example
447
- * ```typescript
448
- * // Get hourly candles
449
- * const candles = await client.candles.list('BTC', {
450
- * interval: '1h',
451
- * start: Date.now() - 86400000,
452
- * end: Date.now()
453
- * });
454
- *
455
- * // Get daily candles
456
- * const daily = await client.candles.list('ETH', {
457
- * interval: '1d',
458
- * limit: 30
459
- * });
460
- * ```
461
- */
462
- declare class CandlesResource {
463
- private http;
464
- constructor(http: HttpClient);
465
- /**
466
- * Get OHLCV candles for a coin
467
- *
468
- * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
469
- * @param params - Interval, time range, and pagination parameters
470
- * @returns Array of candles
471
- */
472
- list(coin: string, params?: GetCandlesParams): Promise<Candle[]>;
473
- }
474
-
475
513
  /**
476
514
  * Instruments API resource
477
515
  *
@@ -607,10 +645,6 @@ declare class OxArchive {
607
645
  * Trade/fill history
608
646
  */
609
647
  readonly trades: TradesResource;
610
- /**
611
- * OHLCV candles
612
- */
613
- readonly candles: CandlesResource;
614
648
  /**
615
649
  * Trading instruments metadata
616
650
  */
@@ -693,6 +727,15 @@ declare class OxArchiveWs {
693
727
  private reconnectAttempts;
694
728
  private pingTimer;
695
729
  private reconnectTimer;
730
+ private historicalDataHandlers;
731
+ private batchHandlers;
732
+ private replayStartHandlers;
733
+ private replayCompleteHandlers;
734
+ private streamStartHandlers;
735
+ private streamProgressHandlers;
736
+ private streamCompleteHandlers;
737
+ private orderbookHandlers;
738
+ private tradesHandlers;
696
739
  constructor(options: WsOptions);
697
740
  /**
698
741
  * Connect to the WebSocket server
@@ -818,23 +861,23 @@ declare class OxArchiveWs {
818
861
  /**
819
862
  * Handle replay started event
820
863
  */
821
- onReplayStart(handler: (channel: WsChannel, coin: string, totalRecords: number, speed: number) => void): void;
864
+ onReplayStart(handler: (channel: WsChannel, coin: string, start: number, end: number, speed: number) => void): void;
822
865
  /**
823
866
  * Handle replay completed event
824
867
  */
825
- onReplayComplete(handler: (channel: WsChannel, coin: string, recordsSent: number) => void): void;
868
+ onReplayComplete(handler: (channel: WsChannel, coin: string, snapshotsSent: number) => void): void;
826
869
  /**
827
870
  * Handle stream started event
828
871
  */
829
- onStreamStart(handler: (channel: WsChannel, coin: string, totalRecords: number) => void): void;
872
+ onStreamStart(handler: (channel: WsChannel, coin: string, start: number, end: number) => void): void;
830
873
  /**
831
874
  * Handle stream progress event
832
875
  */
833
- onStreamProgress(handler: (recordsSent: number, totalRecords: number, progressPct: number) => void): void;
876
+ onStreamProgress(handler: (snapshotsSent: number) => void): void;
834
877
  /**
835
878
  * Handle stream completed event
836
879
  */
837
- onStreamComplete(handler: (channel: WsChannel, coin: string, recordsSent: number) => void): void;
880
+ onStreamComplete(handler: (channel: WsChannel, coin: string, snapshotsSent: number) => void): void;
838
881
  /**
839
882
  * Get current connection state
840
883
  */
@@ -863,6 +906,7 @@ declare class OxArchiveWs {
863
906
  private resubscribe;
864
907
  private scheduleReconnect;
865
908
  private clearReconnectTimer;
909
+ private handleMessage;
866
910
  }
867
911
 
868
- export { type ApiError, type ApiResponse, type Candle, type CandleInterval, type ClientOptions, type FundingRate, type GetCandlesParams, type GetOrderBookParams, type GetTradesParams, type Instrument, type OpenInterest, type OrderBook, type OrderBookHistoryParams, OxArchive, OxArchiveError, OxArchiveWs, type PaginationParams, type PriceLevel, type TimeRangeParams, type Trade, type WsChannel, type WsClientMessage, type WsConnectionState, type WsData, type WsError, type WsEventHandlers, type WsHistoricalBatch, type WsHistoricalData, type WsOptions, type WsPing, type WsPong, type WsReplay, type WsReplayCompleted, type WsReplayPause, type WsReplayPaused, type WsReplayResume, type WsReplayResumed, type WsReplaySeek, type WsReplayStarted, type WsReplayStop, type WsReplayStopped, type WsServerMessage, type WsStream, type WsStreamCompleted, type WsStreamProgress, type WsStreamStarted, type WsStreamStop, type WsStreamStopped, type WsSubscribe, type WsSubscribed, type WsUnsubscribe, type WsUnsubscribed, OxArchive as default };
912
+ export { type ApiError, type ApiMeta, type ApiResponse, type ClientOptions, type DataSource, type FundingRate, type GetOrderBookParams, type GetTradesParams, type Instrument, type InstrumentType, type OpenInterest, type OrderBook, type OrderBookHistoryParams, OxArchive, OxArchiveError, OxArchiveWs, type PaginationParams, type PriceLevel, type TimeRangeParams, type Timestamp, type TimestampedRecord, type Trade, type TradeDirection, type TradeSide, type WsChannel, type WsClientMessage, type WsConnectionState, type WsData, type WsError, type WsEventHandlers, type WsHistoricalBatch, type WsHistoricalData, type WsOptions, type WsPing, type WsPong, type WsReplay, type WsReplayCompleted, type WsReplayPause, type WsReplayPaused, type WsReplayResume, type WsReplayResumed, type WsReplaySeek, type WsReplayStarted, type WsReplayStop, type WsReplayStopped, type WsServerMessage, type WsStream, type WsStreamCompleted, type WsStreamProgress, type WsStreamStarted, type WsStreamStop, type WsStreamStopped, type WsSubscribe, type WsSubscribed, type WsUnsubscribe, type WsUnsubscribed, OxArchive as default };