@0xarchive/sdk 0.1.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/README.md ADDED
@@ -0,0 +1,301 @@
1
+ # @0xarchive/sdk
2
+
3
+ Official TypeScript/JavaScript SDK for [0xarchive](https://0xarchive.io) - Hyperliquid Historical Data API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @0xarchive/sdk
9
+ # or
10
+ yarn add @0xarchive/sdk
11
+ # or
12
+ pnpm add @0xarchive/sdk
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { OxArchive } from '@0xarchive/sdk';
19
+
20
+ const client = new OxArchive({ apiKey: 'ox_your_api_key' });
21
+
22
+ // Get current order book
23
+ const orderbook = await client.orderbook.get('BTC');
24
+ console.log(`BTC mid price: ${orderbook.mid_price}`);
25
+
26
+ // Get historical order book snapshots
27
+ const history = await client.orderbook.history('ETH', {
28
+ start: Date.now() - 86400000, // 24 hours ago
29
+ end: Date.now(),
30
+ limit: 100
31
+ });
32
+ ```
33
+
34
+ ## Configuration
35
+
36
+ ```typescript
37
+ const client = new OxArchive({
38
+ apiKey: 'ox_your_api_key', // Required
39
+ baseUrl: 'https://api.0xarchive.io', // Optional, defaults to production
40
+ timeout: 30000, // Optional, request timeout in ms
41
+ });
42
+ ```
43
+
44
+ ## API Reference
45
+
46
+ ### Order Book
47
+
48
+ ```typescript
49
+ // Get current order book
50
+ const orderbook = await client.orderbook.get('BTC');
51
+
52
+ // Get order book at specific timestamp
53
+ const historical = await client.orderbook.get('BTC', {
54
+ timestamp: 1704067200000,
55
+ depth: 20 // Number of levels per side
56
+ });
57
+
58
+ // Get historical snapshots
59
+ const history = await client.orderbook.history('BTC', {
60
+ start: '2024-01-01',
61
+ end: '2024-01-02',
62
+ limit: 1000
63
+ });
64
+ ```
65
+
66
+ ### Trades
67
+
68
+ ```typescript
69
+ // Get recent trades
70
+ const recent = await client.trades.recent('BTC', 100);
71
+
72
+ // Get trade history
73
+ const trades = await client.trades.list('ETH', {
74
+ start: Date.now() - 3600000,
75
+ end: Date.now(),
76
+ side: 'buy' // Optional: filter by side
77
+ });
78
+ ```
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
+ ### Instruments
94
+
95
+ ```typescript
96
+ // List all instruments
97
+ const instruments = await client.instruments.list();
98
+
99
+ // Get specific instrument
100
+ const btc = await client.instruments.get('BTC');
101
+ ```
102
+
103
+ ### Funding Rates
104
+
105
+ ```typescript
106
+ // Get current funding rate
107
+ const current = await client.funding.current('BTC');
108
+
109
+ // Get funding rate history
110
+ const history = await client.funding.history('ETH', {
111
+ start: Date.now() - 86400000 * 7,
112
+ end: Date.now()
113
+ });
114
+ ```
115
+
116
+ ### Open Interest
117
+
118
+ ```typescript
119
+ // Get current open interest
120
+ const current = await client.openInterest.current('BTC');
121
+
122
+ // Get open interest history
123
+ const history = await client.openInterest.history('ETH', {
124
+ start: Date.now() - 86400000,
125
+ end: Date.now()
126
+ });
127
+ ```
128
+
129
+ ## WebSocket Streaming
130
+
131
+ For real-time data, use the WebSocket client:
132
+
133
+ ```typescript
134
+ import { OxArchiveWs } from '@0xarchive/sdk';
135
+
136
+ const ws = new OxArchiveWs({ apiKey: 'ox_your_api_key' });
137
+
138
+ // Connect and set up handlers
139
+ ws.connect({
140
+ onOpen: () => console.log('Connected'),
141
+ onClose: (code, reason) => console.log(`Disconnected: ${code}`),
142
+ onError: (error) => console.error('Error:', error),
143
+ onMessage: (message) => console.log('Message:', message),
144
+ onStateChange: (state) => console.log('State:', state),
145
+ });
146
+
147
+ // Subscribe to order book updates
148
+ ws.subscribeOrderbook('BTC');
149
+ ws.subscribeOrderbook('ETH');
150
+
151
+ // Subscribe to trades
152
+ ws.subscribeTrades('BTC');
153
+
154
+ // Subscribe to all tickers
155
+ ws.subscribeAllTickers();
156
+
157
+ // Typed handlers for specific data
158
+ ws.onOrderbook((coin, data) => {
159
+ console.log(`${coin} orderbook: ${data.mid_price}`);
160
+ });
161
+
162
+ ws.onTrades((coin, trades) => {
163
+ console.log(`${coin} new trades: ${trades.length}`);
164
+ });
165
+
166
+ // Unsubscribe
167
+ ws.unsubscribeOrderbook('ETH');
168
+
169
+ // Disconnect when done
170
+ ws.disconnect();
171
+ ```
172
+
173
+ ### Historical Replay (like Tardis.dev)
174
+
175
+ Replay historical data with timing preserved:
176
+
177
+ ```typescript
178
+ const ws = new OxArchiveWs({ apiKey: 'ox_...' });
179
+ ws.connect();
180
+
181
+ // Handle replay data
182
+ ws.onHistoricalData((coin, timestamp, data) => {
183
+ console.log(`${new Date(timestamp)}: ${data.mid_price}`);
184
+ });
185
+
186
+ ws.onReplayStart((channel, coin, totalRecords, speed) => {
187
+ console.log(`Starting replay of ${totalRecords} records at ${speed}x`);
188
+ });
189
+
190
+ ws.onReplayComplete((channel, coin, recordsSent) => {
191
+ console.log(`Replay complete: ${recordsSent} records`);
192
+ });
193
+
194
+ // Start replay at 10x speed
195
+ ws.replay('orderbook', 'BTC', {
196
+ start: Date.now() - 86400000, // 24 hours ago
197
+ speed: 10
198
+ });
199
+
200
+ // Control playback
201
+ ws.replayPause();
202
+ ws.replayResume();
203
+ ws.replaySeek(1704067200000); // Jump to timestamp
204
+ ws.replayStop();
205
+ ```
206
+
207
+ ### Bulk Streaming (like Databento)
208
+
209
+ Fast bulk download for data pipelines:
210
+
211
+ ```typescript
212
+ const ws = new OxArchiveWs({ apiKey: 'ox_...' });
213
+ ws.connect();
214
+
215
+ const allData: OrderBook[] = [];
216
+
217
+ // Handle batched data
218
+ ws.onBatch((coin, records) => {
219
+ allData.push(...records.map(r => r.data));
220
+ });
221
+
222
+ ws.onStreamProgress((sent, total, pct) => {
223
+ console.log(`Progress: ${pct.toFixed(1)}%`);
224
+ });
225
+
226
+ ws.onStreamComplete((channel, coin, recordsSent) => {
227
+ console.log(`Downloaded ${recordsSent} records`);
228
+ });
229
+
230
+ // Start bulk stream
231
+ ws.stream('orderbook', 'ETH', {
232
+ start: Date.now() - 3600000, // 1 hour ago
233
+ end: Date.now(),
234
+ batchSize: 1000
235
+ });
236
+
237
+ // Stop stream if needed
238
+ ws.streamStop();
239
+ ```
240
+
241
+ ### WebSocket Configuration
242
+
243
+ ```typescript
244
+ const ws = new OxArchiveWs({
245
+ apiKey: 'ox_your_api_key',
246
+ wsUrl: 'wss://ws.0xarchive.io', // Optional
247
+ autoReconnect: true, // Auto-reconnect on disconnect
248
+ reconnectDelay: 1000, // Initial reconnect delay (ms)
249
+ maxReconnectAttempts: 10, // Max reconnect attempts
250
+ pingInterval: 30000, // Keep-alive ping interval (ms)
251
+ });
252
+ ```
253
+
254
+ ### Available Channels
255
+
256
+ | Channel | Description | Requires Coin |
257
+ |---------|-------------|---------------|
258
+ | `orderbook` | L2 order book updates | Yes |
259
+ | `trades` | Trade/fill updates | Yes |
260
+ | `ticker` | Price and 24h volume | Yes |
261
+ | `all_tickers` | All market tickers | No |
262
+
263
+ ## Error Handling
264
+
265
+ ```typescript
266
+ import { OxArchive, OxArchiveError } from '@0xarchive/sdk';
267
+
268
+ try {
269
+ const orderbook = await client.orderbook.get('INVALID');
270
+ } catch (error) {
271
+ if (error instanceof OxArchiveError) {
272
+ console.error(`API Error: ${error.message}`);
273
+ console.error(`Status Code: ${error.code}`);
274
+ console.error(`Request ID: ${error.requestId}`);
275
+ }
276
+ }
277
+ ```
278
+
279
+ ## TypeScript Support
280
+
281
+ Full TypeScript support with exported types:
282
+
283
+ ```typescript
284
+ import type {
285
+ OrderBook,
286
+ Trade,
287
+ Candle,
288
+ Instrument,
289
+ FundingRate,
290
+ OpenInterest
291
+ } from '@0xarchive/sdk';
292
+ ```
293
+
294
+ ## Requirements
295
+
296
+ - Node.js 18+
297
+ - Modern browsers with `fetch` support
298
+
299
+ ## License
300
+
301
+ MIT