@0xarchive/sdk 0.3.3 → 0.3.5
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 +127 -58
- package/dist/index.d.mts +400 -400
- package/dist/index.d.ts +400 -400
- package/dist/index.js +71 -50
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +71 -50
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ const client = new OxArchive({ apiKey: 'ox_your_api_key' });
|
|
|
21
21
|
|
|
22
22
|
// Get current order book
|
|
23
23
|
const orderbook = await client.orderbook.get('BTC');
|
|
24
|
-
console.log(`BTC mid price: ${orderbook.
|
|
24
|
+
console.log(`BTC mid price: ${orderbook.midPrice}`);
|
|
25
25
|
|
|
26
26
|
// Get historical order book snapshots
|
|
27
27
|
const history = await client.orderbook.history('ETH', {
|
|
@@ -35,13 +35,14 @@ const history = await client.orderbook.history('ETH', {
|
|
|
35
35
|
|
|
36
36
|
```typescript
|
|
37
37
|
const client = new OxArchive({
|
|
38
|
-
apiKey: 'ox_your_api_key',
|
|
39
|
-
baseUrl: 'https://api.0xarchive.io',
|
|
40
|
-
timeout: 30000,
|
|
38
|
+
apiKey: 'ox_your_api_key', // Required
|
|
39
|
+
baseUrl: 'https://api.0xarchive.io', // Optional
|
|
40
|
+
timeout: 30000, // Optional, request timeout in ms (default: 30000)
|
|
41
|
+
validate: false, // Optional, enable Zod schema validation
|
|
41
42
|
});
|
|
42
43
|
```
|
|
43
44
|
|
|
44
|
-
## API Reference
|
|
45
|
+
## REST API Reference
|
|
45
46
|
|
|
46
47
|
### Order Book
|
|
47
48
|
|
|
@@ -49,41 +50,55 @@ const client = new OxArchive({
|
|
|
49
50
|
// Get current order book
|
|
50
51
|
const orderbook = await client.orderbook.get('BTC');
|
|
51
52
|
|
|
52
|
-
// Get order book at specific timestamp
|
|
53
|
+
// Get order book at specific timestamp with custom depth
|
|
53
54
|
const historical = await client.orderbook.get('BTC', {
|
|
54
55
|
timestamp: 1704067200000,
|
|
55
56
|
depth: 20 // Number of levels per side
|
|
56
57
|
});
|
|
57
58
|
|
|
58
|
-
// Get historical snapshots
|
|
59
|
+
// Get historical snapshots (start is required)
|
|
59
60
|
const history = await client.orderbook.history('BTC', {
|
|
60
|
-
start:
|
|
61
|
-
end:
|
|
61
|
+
start: Date.now() - 86400000,
|
|
62
|
+
end: Date.now(),
|
|
62
63
|
limit: 1000
|
|
63
64
|
});
|
|
64
65
|
```
|
|
65
66
|
|
|
66
67
|
### Trades
|
|
67
68
|
|
|
69
|
+
The trades API uses cursor-based pagination for efficient retrieval of large datasets.
|
|
70
|
+
|
|
68
71
|
```typescript
|
|
69
72
|
// Get recent trades
|
|
70
73
|
const recent = await client.trades.recent('BTC', 100);
|
|
71
74
|
|
|
72
|
-
// Get trade history
|
|
73
|
-
|
|
74
|
-
start: Date.now() -
|
|
75
|
+
// Get trade history with cursor-based pagination
|
|
76
|
+
let result = await client.trades.list('BTC', {
|
|
77
|
+
start: Date.now() - 86400000,
|
|
75
78
|
end: Date.now(),
|
|
76
|
-
|
|
79
|
+
limit: 1000
|
|
77
80
|
});
|
|
81
|
+
|
|
82
|
+
// Paginate through all results
|
|
83
|
+
const allTrades = [...result.data];
|
|
84
|
+
while (result.nextCursor) {
|
|
85
|
+
result = await client.trades.list('BTC', {
|
|
86
|
+
start: Date.now() - 86400000,
|
|
87
|
+
end: Date.now(),
|
|
88
|
+
cursor: result.nextCursor,
|
|
89
|
+
limit: 1000
|
|
90
|
+
});
|
|
91
|
+
allTrades.push(...result.data);
|
|
92
|
+
}
|
|
78
93
|
```
|
|
79
94
|
|
|
80
95
|
### Instruments
|
|
81
96
|
|
|
82
97
|
```typescript
|
|
83
|
-
// List all instruments
|
|
98
|
+
// List all trading instruments
|
|
84
99
|
const instruments = await client.instruments.list();
|
|
85
100
|
|
|
86
|
-
// Get specific instrument
|
|
101
|
+
// Get specific instrument details
|
|
87
102
|
const btc = await client.instruments.get('BTC');
|
|
88
103
|
```
|
|
89
104
|
|
|
@@ -93,7 +108,7 @@ const btc = await client.instruments.get('BTC');
|
|
|
93
108
|
// Get current funding rate
|
|
94
109
|
const current = await client.funding.current('BTC');
|
|
95
110
|
|
|
96
|
-
// Get funding rate history
|
|
111
|
+
// Get funding rate history (start is required)
|
|
97
112
|
const history = await client.funding.history('ETH', {
|
|
98
113
|
start: Date.now() - 86400000 * 7,
|
|
99
114
|
end: Date.now()
|
|
@@ -106,72 +121,76 @@ const history = await client.funding.history('ETH', {
|
|
|
106
121
|
// Get current open interest
|
|
107
122
|
const current = await client.openInterest.current('BTC');
|
|
108
123
|
|
|
109
|
-
// Get open interest history
|
|
124
|
+
// Get open interest history (start is required)
|
|
110
125
|
const history = await client.openInterest.history('ETH', {
|
|
111
126
|
start: Date.now() - 86400000,
|
|
112
|
-
end: Date.now()
|
|
127
|
+
end: Date.now(),
|
|
128
|
+
limit: 100
|
|
113
129
|
});
|
|
114
130
|
```
|
|
115
131
|
|
|
116
|
-
## WebSocket
|
|
132
|
+
## WebSocket Client
|
|
117
133
|
|
|
118
|
-
|
|
134
|
+
The WebSocket client supports three modes: real-time streaming, historical replay, and bulk streaming.
|
|
119
135
|
|
|
120
136
|
```typescript
|
|
121
137
|
import { OxArchiveWs } from '@0xarchive/sdk';
|
|
122
138
|
|
|
123
139
|
const ws = new OxArchiveWs({ apiKey: 'ox_your_api_key' });
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Real-time Streaming
|
|
124
143
|
|
|
125
|
-
|
|
144
|
+
Subscribe to live market data from Hyperliquid.
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
126
147
|
ws.connect({
|
|
127
148
|
onOpen: () => console.log('Connected'),
|
|
128
149
|
onClose: (code, reason) => console.log(`Disconnected: ${code}`),
|
|
129
150
|
onError: (error) => console.error('Error:', error),
|
|
130
|
-
onMessage: (message) => console.log('Message:', message),
|
|
131
|
-
onStateChange: (state) => console.log('State:', state),
|
|
132
151
|
});
|
|
133
152
|
|
|
134
|
-
// Subscribe to
|
|
153
|
+
// Subscribe to channels
|
|
135
154
|
ws.subscribeOrderbook('BTC');
|
|
136
|
-
ws.
|
|
137
|
-
|
|
138
|
-
// Subscribe to trades
|
|
139
|
-
ws.subscribeTrades('BTC');
|
|
140
|
-
|
|
141
|
-
// Subscribe to all tickers
|
|
155
|
+
ws.subscribeTrades('ETH');
|
|
156
|
+
ws.subscribeTicker('SOL');
|
|
142
157
|
ws.subscribeAllTickers();
|
|
143
158
|
|
|
144
|
-
//
|
|
159
|
+
// Handle real-time data with typed callbacks
|
|
145
160
|
ws.onOrderbook((coin, data) => {
|
|
146
|
-
console.log(`${coin}
|
|
161
|
+
console.log(`${coin} mid price: ${data.midPrice}`);
|
|
147
162
|
});
|
|
148
163
|
|
|
149
164
|
ws.onTrades((coin, trades) => {
|
|
150
165
|
console.log(`${coin} new trades: ${trades.length}`);
|
|
151
166
|
});
|
|
152
167
|
|
|
153
|
-
// Unsubscribe
|
|
154
|
-
ws.unsubscribeOrderbook('
|
|
168
|
+
// Unsubscribe when done
|
|
169
|
+
ws.unsubscribeOrderbook('BTC');
|
|
155
170
|
|
|
156
|
-
// Disconnect
|
|
171
|
+
// Disconnect
|
|
157
172
|
ws.disconnect();
|
|
158
173
|
```
|
|
159
174
|
|
|
160
|
-
### Historical Replay
|
|
175
|
+
### Historical Replay
|
|
176
|
+
|
|
177
|
+
Replay historical data with original timing preserved. Perfect for backtesting.
|
|
161
178
|
|
|
162
|
-
Replay
|
|
179
|
+
> **Important:** Replay data is delivered via `onHistoricalData()`, NOT `onTrades()` or `onOrderbook()`.
|
|
180
|
+
> The real-time callbacks only receive live market data from subscriptions.
|
|
163
181
|
|
|
164
182
|
```typescript
|
|
165
183
|
const ws = new OxArchiveWs({ apiKey: 'ox_...' });
|
|
166
184
|
ws.connect();
|
|
167
185
|
|
|
168
|
-
// Handle replay data
|
|
186
|
+
// Handle replay data - this is where historical records arrive
|
|
169
187
|
ws.onHistoricalData((coin, timestamp, data) => {
|
|
170
|
-
console.log(`${new Date(timestamp)}: ${data.
|
|
188
|
+
console.log(`${new Date(timestamp).toISOString()}: ${data.midPrice}`);
|
|
171
189
|
});
|
|
172
190
|
|
|
191
|
+
// Replay lifecycle events
|
|
173
192
|
ws.onReplayStart((channel, coin, start, end, speed) => {
|
|
174
|
-
console.log(`Starting replay
|
|
193
|
+
console.log(`Starting replay: ${channel}/${coin} at ${speed}x`);
|
|
175
194
|
});
|
|
176
195
|
|
|
177
196
|
ws.onReplayComplete((channel, coin, recordsSent) => {
|
|
@@ -180,20 +199,21 @@ ws.onReplayComplete((channel, coin, recordsSent) => {
|
|
|
180
199
|
|
|
181
200
|
// Start replay at 10x speed
|
|
182
201
|
ws.replay('orderbook', 'BTC', {
|
|
183
|
-
start: Date.now() - 86400000,
|
|
184
|
-
|
|
202
|
+
start: Date.now() - 86400000, // 24 hours ago
|
|
203
|
+
end: Date.now(), // Optional, defaults to now
|
|
204
|
+
speed: 10 // Optional, defaults to 1x
|
|
185
205
|
});
|
|
186
206
|
|
|
187
207
|
// Control playback
|
|
188
208
|
ws.replayPause();
|
|
189
209
|
ws.replayResume();
|
|
190
|
-
ws.replaySeek(1704067200000);
|
|
210
|
+
ws.replaySeek(1704067200000); // Jump to timestamp
|
|
191
211
|
ws.replayStop();
|
|
192
212
|
```
|
|
193
213
|
|
|
194
|
-
### Bulk Streaming
|
|
214
|
+
### Bulk Streaming
|
|
195
215
|
|
|
196
|
-
Fast bulk download for data pipelines
|
|
216
|
+
Fast bulk download for data pipelines. Data arrives in batches without timing delays.
|
|
197
217
|
|
|
198
218
|
```typescript
|
|
199
219
|
const ws = new OxArchiveWs({ apiKey: 'ox_...' });
|
|
@@ -207,7 +227,7 @@ ws.onBatch((coin, records) => {
|
|
|
207
227
|
});
|
|
208
228
|
|
|
209
229
|
ws.onStreamProgress((snapshotsSent) => {
|
|
210
|
-
console.log(`
|
|
230
|
+
console.log(`Progress: ${snapshotsSent} snapshots`);
|
|
211
231
|
});
|
|
212
232
|
|
|
213
233
|
ws.onStreamComplete((channel, coin, recordsSent) => {
|
|
@@ -216,12 +236,12 @@ ws.onStreamComplete((channel, coin, recordsSent) => {
|
|
|
216
236
|
|
|
217
237
|
// Start bulk stream
|
|
218
238
|
ws.stream('orderbook', 'ETH', {
|
|
219
|
-
start: Date.now() - 3600000,
|
|
239
|
+
start: Date.now() - 3600000, // 1 hour ago
|
|
220
240
|
end: Date.now(),
|
|
221
|
-
batchSize: 1000
|
|
241
|
+
batchSize: 1000 // Optional, defaults to 1000
|
|
222
242
|
});
|
|
223
243
|
|
|
224
|
-
// Stop
|
|
244
|
+
// Stop if needed
|
|
225
245
|
ws.streamStop();
|
|
226
246
|
```
|
|
227
247
|
|
|
@@ -229,12 +249,12 @@ ws.streamStop();
|
|
|
229
249
|
|
|
230
250
|
```typescript
|
|
231
251
|
const ws = new OxArchiveWs({
|
|
232
|
-
apiKey: 'ox_your_api_key',
|
|
233
|
-
wsUrl: 'wss://api.0xarchive.io/ws',
|
|
234
|
-
autoReconnect: true,
|
|
235
|
-
reconnectDelay: 1000,
|
|
236
|
-
maxReconnectAttempts: 10,
|
|
237
|
-
pingInterval: 30000,
|
|
252
|
+
apiKey: 'ox_your_api_key', // Required
|
|
253
|
+
wsUrl: 'wss://api.0xarchive.io/ws', // Optional
|
|
254
|
+
autoReconnect: true, // Auto-reconnect on disconnect (default: true)
|
|
255
|
+
reconnectDelay: 1000, // Initial reconnect delay in ms (default: 1000)
|
|
256
|
+
maxReconnectAttempts: 10, // Max reconnect attempts (default: 10)
|
|
257
|
+
pingInterval: 30000, // Keep-alive ping interval in ms (default: 30000)
|
|
238
258
|
});
|
|
239
259
|
```
|
|
240
260
|
|
|
@@ -247,6 +267,38 @@ const ws = new OxArchiveWs({
|
|
|
247
267
|
| `ticker` | Price and 24h volume | Yes |
|
|
248
268
|
| `all_tickers` | All market tickers | No |
|
|
249
269
|
|
|
270
|
+
### WebSocket Connection States
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
ws.getState(); // 'disconnected' | 'connecting' | 'connected' | 'reconnecting'
|
|
274
|
+
ws.isConnected(); // boolean
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Timestamp Formats
|
|
278
|
+
|
|
279
|
+
The SDK accepts timestamps as Unix milliseconds or Date objects:
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
// Unix milliseconds (recommended)
|
|
283
|
+
client.orderbook.history('BTC', {
|
|
284
|
+
start: Date.now() - 86400000,
|
|
285
|
+
end: Date.now()
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// Date objects (converted automatically)
|
|
289
|
+
client.orderbook.history('BTC', {
|
|
290
|
+
start: new Date('2024-01-01'),
|
|
291
|
+
end: new Date('2024-01-02')
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// WebSocket replay/stream also accepts both
|
|
295
|
+
ws.replay('orderbook', 'BTC', {
|
|
296
|
+
start: Date.now() - 3600000,
|
|
297
|
+
end: Date.now(),
|
|
298
|
+
speed: 10
|
|
299
|
+
});
|
|
300
|
+
```
|
|
301
|
+
|
|
250
302
|
## Error Handling
|
|
251
303
|
|
|
252
304
|
```typescript
|
|
@@ -270,17 +322,34 @@ Full TypeScript support with exported types:
|
|
|
270
322
|
```typescript
|
|
271
323
|
import type {
|
|
272
324
|
OrderBook,
|
|
325
|
+
PriceLevel,
|
|
273
326
|
Trade,
|
|
274
327
|
Instrument,
|
|
275
328
|
FundingRate,
|
|
276
|
-
OpenInterest
|
|
329
|
+
OpenInterest,
|
|
330
|
+
CursorResponse,
|
|
331
|
+
WsOptions,
|
|
332
|
+
WsChannel,
|
|
333
|
+
WsConnectionState,
|
|
277
334
|
} from '@0xarchive/sdk';
|
|
278
335
|
```
|
|
279
336
|
|
|
337
|
+
## Runtime Validation
|
|
338
|
+
|
|
339
|
+
Enable Zod schema validation for API responses:
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
const client = new OxArchive({
|
|
343
|
+
apiKey: 'ox_your_api_key',
|
|
344
|
+
validate: true // Enable runtime validation
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
When enabled, responses are validated against Zod schemas and throw `OxArchiveError` with status 422 if validation fails.
|
|
349
|
+
|
|
280
350
|
## Requirements
|
|
281
351
|
|
|
282
|
-
- Node.js 18+
|
|
283
|
-
- Modern browsers with `fetch` support
|
|
352
|
+
- Node.js 18+ or modern browsers with `fetch` and `WebSocket` support
|
|
284
353
|
|
|
285
354
|
## License
|
|
286
355
|
|