@0xarchive/sdk 0.6.3 → 0.6.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 +19 -4
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +70 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -153,14 +153,24 @@ const tickData = await client.lighter.orderbook.historyTick('BTC', {
|
|
|
153
153
|
console.log(`Checkpoint: ${tickData.checkpoint.bids.length} bids`);
|
|
154
154
|
console.log(`Deltas: ${tickData.deltas.length} updates`);
|
|
155
155
|
|
|
156
|
-
// Option 3:
|
|
156
|
+
// Option 3: Auto-paginating iterator (recommended for large time ranges)
|
|
157
|
+
// Automatically handles pagination, fetching up to 1,000 deltas per request
|
|
158
|
+
for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', {
|
|
159
|
+
start: Date.now() - 86400000, // 24 hours of data
|
|
160
|
+
end: Date.now()
|
|
161
|
+
})) {
|
|
162
|
+
console.log(snapshot.timestamp, 'Mid:', snapshot.midPrice);
|
|
163
|
+
if (someCondition(snapshot)) break; // Early exit supported
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Option 4: Manual iteration (single page, for custom logic)
|
|
157
167
|
const reconstructor = client.lighter.orderbook.createReconstructor();
|
|
158
168
|
for (const snapshot of reconstructor.iterate(tickData.checkpoint, tickData.deltas)) {
|
|
159
169
|
// Process each snapshot without loading all into memory
|
|
160
170
|
if (someCondition(snapshot)) break; // Early exit if needed
|
|
161
171
|
}
|
|
162
172
|
|
|
163
|
-
// Option
|
|
173
|
+
// Option 5: Get only final state (most efficient)
|
|
164
174
|
const final = reconstructor.reconstructFinal(tickData.checkpoint, tickData.deltas);
|
|
165
175
|
|
|
166
176
|
// Check for sequence gaps
|
|
@@ -173,10 +183,13 @@ if (gaps.length > 0) {
|
|
|
173
183
|
**Methods:**
|
|
174
184
|
| Method | Description |
|
|
175
185
|
|--------|-------------|
|
|
176
|
-
| `historyTick(coin, params)` | Get raw checkpoint + deltas
|
|
177
|
-
| `historyReconstructed(coin, params, options)` | Get fully reconstructed snapshots |
|
|
186
|
+
| `historyTick(coin, params)` | Get raw checkpoint + deltas (single page, max 1,000 deltas) |
|
|
187
|
+
| `historyReconstructed(coin, params, options)` | Get fully reconstructed snapshots (single page) |
|
|
188
|
+
| `iterateTickHistory(coin, params, depth?)` | Auto-paginating async iterator for large time ranges |
|
|
178
189
|
| `createReconstructor()` | Create a reconstructor instance for manual control |
|
|
179
190
|
|
|
191
|
+
**Note:** The API returns a maximum of 1,000 deltas per request. For time ranges with more deltas, use `iterateTickHistory()` which handles pagination automatically.
|
|
192
|
+
|
|
180
193
|
**ReconstructOptions:**
|
|
181
194
|
| Option | Default | Description |
|
|
182
195
|
|--------|---------|-------------|
|
|
@@ -211,6 +224,8 @@ while (result.nextCursor) {
|
|
|
211
224
|
const recent = await client.lighter.trades.recent('BTC', 100);
|
|
212
225
|
```
|
|
213
226
|
|
|
227
|
+
**Note:** The `recent()` method is only available for Lighter.xyz (`client.lighter.trades.recent()`). Hyperliquid does not have a recent trades endpoint - use `list()` with a time range instead.
|
|
228
|
+
|
|
214
229
|
### Instruments
|
|
215
230
|
|
|
216
231
|
```typescript
|
package/dist/index.d.mts
CHANGED
|
@@ -1203,6 +1203,37 @@ declare class OrderBookResource {
|
|
|
1203
1203
|
* ```
|
|
1204
1204
|
*/
|
|
1205
1205
|
createReconstructor(): OrderBookReconstructor;
|
|
1206
|
+
/**
|
|
1207
|
+
* Iterate over tick-level orderbook history with automatic pagination (Enterprise tier only).
|
|
1208
|
+
*
|
|
1209
|
+
* This async generator automatically handles pagination, fetching up to 1,000 deltas
|
|
1210
|
+
* per API request and yielding reconstructed orderbook snapshots one at a time.
|
|
1211
|
+
* Memory-efficient for processing large time ranges.
|
|
1212
|
+
*
|
|
1213
|
+
* @param coin - The coin symbol (e.g., 'BTC', 'ETH')
|
|
1214
|
+
* @param params - Time range parameters
|
|
1215
|
+
* @param depth - Maximum price levels to include in output snapshots
|
|
1216
|
+
* @yields Reconstructed orderbook snapshots
|
|
1217
|
+
*
|
|
1218
|
+
* @example
|
|
1219
|
+
* ```typescript
|
|
1220
|
+
* // Process 24 hours of tick data with automatic pagination
|
|
1221
|
+
* for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', {
|
|
1222
|
+
* start: Date.now() - 86400000,
|
|
1223
|
+
* end: Date.now()
|
|
1224
|
+
* })) {
|
|
1225
|
+
* console.log(snapshot.timestamp, 'Mid:', snapshot.midPrice);
|
|
1226
|
+
* if (someCondition) break; // Early exit supported
|
|
1227
|
+
* }
|
|
1228
|
+
*
|
|
1229
|
+
* // Collect all snapshots (caution: may use significant memory for large ranges)
|
|
1230
|
+
* const allSnapshots: ReconstructedOrderBook[] = [];
|
|
1231
|
+
* for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', { start, end })) {
|
|
1232
|
+
* allSnapshots.push(snapshot);
|
|
1233
|
+
* }
|
|
1234
|
+
* ```
|
|
1235
|
+
*/
|
|
1236
|
+
iterateTickHistory(coin: string, params: TickHistoryParams, depth?: number): AsyncGenerator<ReconstructedOrderBook, void, undefined>;
|
|
1206
1237
|
}
|
|
1207
1238
|
|
|
1208
1239
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1203,6 +1203,37 @@ declare class OrderBookResource {
|
|
|
1203
1203
|
* ```
|
|
1204
1204
|
*/
|
|
1205
1205
|
createReconstructor(): OrderBookReconstructor;
|
|
1206
|
+
/**
|
|
1207
|
+
* Iterate over tick-level orderbook history with automatic pagination (Enterprise tier only).
|
|
1208
|
+
*
|
|
1209
|
+
* This async generator automatically handles pagination, fetching up to 1,000 deltas
|
|
1210
|
+
* per API request and yielding reconstructed orderbook snapshots one at a time.
|
|
1211
|
+
* Memory-efficient for processing large time ranges.
|
|
1212
|
+
*
|
|
1213
|
+
* @param coin - The coin symbol (e.g., 'BTC', 'ETH')
|
|
1214
|
+
* @param params - Time range parameters
|
|
1215
|
+
* @param depth - Maximum price levels to include in output snapshots
|
|
1216
|
+
* @yields Reconstructed orderbook snapshots
|
|
1217
|
+
*
|
|
1218
|
+
* @example
|
|
1219
|
+
* ```typescript
|
|
1220
|
+
* // Process 24 hours of tick data with automatic pagination
|
|
1221
|
+
* for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', {
|
|
1222
|
+
* start: Date.now() - 86400000,
|
|
1223
|
+
* end: Date.now()
|
|
1224
|
+
* })) {
|
|
1225
|
+
* console.log(snapshot.timestamp, 'Mid:', snapshot.midPrice);
|
|
1226
|
+
* if (someCondition) break; // Early exit supported
|
|
1227
|
+
* }
|
|
1228
|
+
*
|
|
1229
|
+
* // Collect all snapshots (caution: may use significant memory for large ranges)
|
|
1230
|
+
* const allSnapshots: ReconstructedOrderBook[] = [];
|
|
1231
|
+
* for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', { start, end })) {
|
|
1232
|
+
* allSnapshots.push(snapshot);
|
|
1233
|
+
* }
|
|
1234
|
+
* ```
|
|
1235
|
+
*/
|
|
1236
|
+
iterateTickHistory(coin: string, params: TickHistoryParams, depth?: number): AsyncGenerator<ReconstructedOrderBook, void, undefined>;
|
|
1206
1237
|
}
|
|
1207
1238
|
|
|
1208
1239
|
/**
|
package/dist/index.js
CHANGED
|
@@ -690,13 +690,14 @@ var OrderBookResource = class {
|
|
|
690
690
|
granularity: "tick"
|
|
691
691
|
}
|
|
692
692
|
);
|
|
693
|
-
|
|
693
|
+
const tickData = response.data;
|
|
694
|
+
if (!tickData?.checkpoint || !tickData?.deltas) {
|
|
694
695
|
const errorMsg = response.error || response.message || "Tick-level orderbook data requires Enterprise tier. Upgrade your subscription or use a different granularity.";
|
|
695
696
|
throw new Error(errorMsg);
|
|
696
697
|
}
|
|
697
698
|
return {
|
|
698
|
-
checkpoint:
|
|
699
|
-
deltas:
|
|
699
|
+
checkpoint: tickData.checkpoint,
|
|
700
|
+
deltas: tickData.deltas
|
|
700
701
|
};
|
|
701
702
|
}
|
|
702
703
|
/**
|
|
@@ -766,6 +767,72 @@ var OrderBookResource = class {
|
|
|
766
767
|
createReconstructor() {
|
|
767
768
|
return new OrderBookReconstructor();
|
|
768
769
|
}
|
|
770
|
+
/**
|
|
771
|
+
* Iterate over tick-level orderbook history with automatic pagination (Enterprise tier only).
|
|
772
|
+
*
|
|
773
|
+
* This async generator automatically handles pagination, fetching up to 1,000 deltas
|
|
774
|
+
* per API request and yielding reconstructed orderbook snapshots one at a time.
|
|
775
|
+
* Memory-efficient for processing large time ranges.
|
|
776
|
+
*
|
|
777
|
+
* @param coin - The coin symbol (e.g., 'BTC', 'ETH')
|
|
778
|
+
* @param params - Time range parameters
|
|
779
|
+
* @param depth - Maximum price levels to include in output snapshots
|
|
780
|
+
* @yields Reconstructed orderbook snapshots
|
|
781
|
+
*
|
|
782
|
+
* @example
|
|
783
|
+
* ```typescript
|
|
784
|
+
* // Process 24 hours of tick data with automatic pagination
|
|
785
|
+
* for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', {
|
|
786
|
+
* start: Date.now() - 86400000,
|
|
787
|
+
* end: Date.now()
|
|
788
|
+
* })) {
|
|
789
|
+
* console.log(snapshot.timestamp, 'Mid:', snapshot.midPrice);
|
|
790
|
+
* if (someCondition) break; // Early exit supported
|
|
791
|
+
* }
|
|
792
|
+
*
|
|
793
|
+
* // Collect all snapshots (caution: may use significant memory for large ranges)
|
|
794
|
+
* const allSnapshots: ReconstructedOrderBook[] = [];
|
|
795
|
+
* for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', { start, end })) {
|
|
796
|
+
* allSnapshots.push(snapshot);
|
|
797
|
+
* }
|
|
798
|
+
* ```
|
|
799
|
+
*/
|
|
800
|
+
async *iterateTickHistory(coin, params, depth) {
|
|
801
|
+
const startTs = typeof params.start === "string" ? new Date(params.start).getTime() : params.start;
|
|
802
|
+
const endTs = typeof params.end === "string" ? new Date(params.end).getTime() : params.end;
|
|
803
|
+
let cursor = startTs;
|
|
804
|
+
const reconstructor = new OrderBookReconstructor();
|
|
805
|
+
const MAX_DELTAS_PER_PAGE = 1e3;
|
|
806
|
+
let isFirstPage = true;
|
|
807
|
+
while (cursor < endTs) {
|
|
808
|
+
const tickData = await this.historyTick(coin, {
|
|
809
|
+
start: cursor,
|
|
810
|
+
end: endTs,
|
|
811
|
+
depth: params.depth
|
|
812
|
+
});
|
|
813
|
+
if (tickData.deltas.length === 0) {
|
|
814
|
+
if (isFirstPage) {
|
|
815
|
+
reconstructor.initialize(tickData.checkpoint);
|
|
816
|
+
yield reconstructor.getSnapshot(depth);
|
|
817
|
+
}
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
let skipFirst = !isFirstPage;
|
|
821
|
+
for (const snapshot of reconstructor.iterate(tickData.checkpoint, tickData.deltas, { depth })) {
|
|
822
|
+
if (skipFirst) {
|
|
823
|
+
skipFirst = false;
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
yield snapshot;
|
|
827
|
+
}
|
|
828
|
+
isFirstPage = false;
|
|
829
|
+
const lastDelta = tickData.deltas[tickData.deltas.length - 1];
|
|
830
|
+
cursor = lastDelta.timestamp + 1;
|
|
831
|
+
if (tickData.deltas.length < MAX_DELTAS_PER_PAGE) {
|
|
832
|
+
break;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
769
836
|
};
|
|
770
837
|
|
|
771
838
|
// src/resources/trades.ts
|