-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebsocket_streaming.ts
More file actions
133 lines (113 loc) · 4.04 KB
/
websocket_streaming.ts
File metadata and controls
133 lines (113 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env npx ts-node
// @ts-nocheck
/**
* WebSocket Streaming Example - Real-time market data via WebSocket.
*
* This example demonstrates:
* - Connecting to Hyperliquid's WebSocket API
* - Subscribing to trades, orders, and book updates
* - Automatic reconnection handling
* - Graceful shutdown
*
* Requirements:
* npm install hyperliquid-sdk ws
*
* Usage:
* export ENDPOINT="https://your-endpoint.example.com/TOKEN"
* npx ts-node websocket_streaming.ts
*
* The SDK automatically handles URL parsing - you can pass any valid endpoint URL.
*/
import { HyperliquidSDK, StreamConnectionState } from '@quicknode/hyperliquid-sdk';
// Get endpoint from args or environment
const ENDPOINT = process.argv[2] || process.env.ENDPOINT;
if (!ENDPOINT) {
console.log("Hyperliquid WebSocket Streaming Example");
console.log("=".repeat(50));
console.log();
console.log("Usage:");
console.log(" export ENDPOINT='https://your-endpoint.example.com/TOKEN'");
console.log(" npx ts-node websocket_streaming.ts");
console.log();
console.log("Or:");
console.log(" npx ts-node websocket_streaming.ts 'https://your-endpoint.example.com/TOKEN'");
process.exit(1);
}
function onTrade(data: Record<string, unknown>) {
const block = data.block as Record<string, unknown> | undefined;
const events = block?.events as [string, Record<string, unknown>][] | undefined;
if (events && Array.isArray(events)) {
for (const [, trade] of events) {
const coin = trade.coin || "?";
const px = parseFloat(String(trade.px || 0));
const sz = trade.sz || "?";
const side = trade.side === "B" ? "BUY" : "SELL";
console.log(`[TRADE] ${coin}: ${side} ${sz} @ $${px.toLocaleString()}`);
}
}
}
function onBookUpdate(data: Record<string, unknown>) {
const block = data.block as Record<string, unknown> | undefined;
const events = block?.events as [string, Record<string, unknown>][] | undefined;
if (events && Array.isArray(events)) {
for (const [, update] of events) {
const coin = update.coin || "?";
const levels = update.levels as { px: string; sz: string; n: number }[][] | undefined;
if (levels && levels.length >= 2) {
const bids = levels[0] || [];
const asks = levels[1] || [];
if (bids.length && asks.length) {
const bestBid = bids[0];
const bestAsk = asks[0];
const bidPx = parseFloat(bestBid.px || "0");
const askPx = parseFloat(bestAsk.px || "0");
const spread = askPx - bidPx;
console.log(`[BOOK] ${coin}: Bid $${bidPx.toLocaleString()} | Ask $${askPx.toLocaleString()} | Spread $${spread.toLocaleString()}`);
}
}
}
}
}
function onStateChange(state: StreamConnectionState) {
console.log(`[STATE] ${state}`);
}
function onReconnect(attempt: number) {
console.log(`[RECONNECT] Attempt ${attempt}`);
}
function onError(error: Error) {
console.log(`[ERROR] ${error.message}`);
}
function onClose() {
console.log("[CLOSED] Stream stopped");
}
async function main() {
console.log("Hyperliquid WebSocket Streaming Example");
console.log("=".repeat(50));
console.log(`Endpoint: ${ENDPOINT.slice(0, 60)}${ENDPOINT.length > 60 ? '...' : ''}`);
console.log();
// Create SDK client
const sdk = new HyperliquidSDK(ENDPOINT);
// Configure stream callbacks
sdk.stream.onError = onError;
sdk.stream.onClose = onClose;
sdk.stream.onStateChange = onStateChange;
sdk.stream.onReconnect = onReconnect;
// Subscribe to BTC and ETH trades
sdk.stream.trades(["BTC", "ETH"], onTrade);
console.log("Subscribed to: BTC, ETH trades");
// Subscribe to BTC book updates
sdk.stream.bookUpdates(["BTC"], onBookUpdate);
console.log("Subscribed to: BTC book updates");
// Handle Ctrl+C gracefully
process.on('SIGINT', () => {
console.log("\nShutting down gracefully...");
sdk.stream.stop();
process.exit(0);
});
console.log();
console.log("Streaming... Press Ctrl+C to stop");
console.log("-".repeat(50));
// Start the stream
await sdk.stream.start();
}
main().catch(console.error);