@rabby-wallet/hyperliquid-sdk 1.0.0-beta.3 → 1.0.0-beta.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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AllMids, L2Book, Candle, WsFill, WsOrder, WebData2 } from '../types';
|
|
1
|
+
import type { AllMids, L2Book, Candle, WsFill, WsOrder, WebData2, WsActiveAssetCtx } from '../types';
|
|
2
2
|
export interface WebSocketClientConfig {
|
|
3
3
|
masterAddress: string;
|
|
4
4
|
isTestnet?: boolean;
|
|
@@ -18,7 +18,6 @@ export declare class WebSocketClient {
|
|
|
18
18
|
private readonly url;
|
|
19
19
|
private readonly config;
|
|
20
20
|
private subscriptions;
|
|
21
|
-
private subscriptionId;
|
|
22
21
|
private reconnectAttempts;
|
|
23
22
|
private isConnecting;
|
|
24
23
|
private pendingSubscriptions;
|
|
@@ -43,6 +42,10 @@ export declare class WebSocketClient {
|
|
|
43
42
|
* Subscribe to L2 order book for a specific coin
|
|
44
43
|
*/
|
|
45
44
|
subscribeToL2Book(coin: string, callback: SubscriptionCallback<L2Book>): Subscription;
|
|
45
|
+
/**
|
|
46
|
+
* Subscribe to active asset ctx
|
|
47
|
+
*/
|
|
48
|
+
subscribeToActiveAssetCtx(coin: string, callback: SubscriptionCallback<WsActiveAssetCtx>): Subscription;
|
|
46
49
|
/**
|
|
47
50
|
* Subscribe to trades for a specific coin
|
|
48
51
|
*/
|
|
@@ -18,7 +18,6 @@ class WebSocketClient {
|
|
|
18
18
|
constructor(config) {
|
|
19
19
|
this.ws = null;
|
|
20
20
|
this.subscriptions = new Map();
|
|
21
|
-
this.subscriptionId = 0;
|
|
22
21
|
this.reconnectAttempts = 0;
|
|
23
22
|
this.isConnecting = false;
|
|
24
23
|
this.pendingSubscriptions = [];
|
|
@@ -57,13 +56,13 @@ class WebSocketClient {
|
|
|
57
56
|
this.ws.onopen = () => {
|
|
58
57
|
this.isConnecting = false;
|
|
59
58
|
this.reconnectAttempts = 0;
|
|
60
|
-
console.log('WebSocket connected');
|
|
59
|
+
console.log('hyperliquid sdk WebSocket connected');
|
|
61
60
|
// Resubscribe to pending subscriptions
|
|
62
|
-
this.pendingSubscriptions.forEach(({
|
|
61
|
+
this.pendingSubscriptions.forEach(({ message, callback }) => {
|
|
63
62
|
var _a;
|
|
64
|
-
this.subscriptions.set(id, callback);
|
|
65
63
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
66
|
-
this.
|
|
64
|
+
this.subscriptions.set(message.subscription.type, callback);
|
|
65
|
+
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'subscribe' })));
|
|
67
66
|
}
|
|
68
67
|
});
|
|
69
68
|
this.pendingSubscriptions = [];
|
|
@@ -72,12 +71,10 @@ class WebSocketClient {
|
|
|
72
71
|
this.ws.onmessage = (event) => {
|
|
73
72
|
try {
|
|
74
73
|
const data = JSON.parse(event.data);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
callback(data.data);
|
|
80
|
-
}
|
|
74
|
+
const type = data.channel;
|
|
75
|
+
const callback = this.subscriptions.get(type);
|
|
76
|
+
if (callback) {
|
|
77
|
+
callback(data.data);
|
|
81
78
|
}
|
|
82
79
|
}
|
|
83
80
|
catch (error) {
|
|
@@ -117,15 +114,13 @@ class WebSocketClient {
|
|
|
117
114
|
*/
|
|
118
115
|
subscribe(message, callback) {
|
|
119
116
|
var _a;
|
|
120
|
-
const id = ++this.subscriptionId;
|
|
121
|
-
const subscriptionMessage = Object.assign(Object.assign({}, message), { id });
|
|
122
117
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
123
|
-
this.subscriptions.set(
|
|
124
|
-
this.ws.send(JSON.stringify(Object.assign(Object.assign({},
|
|
118
|
+
this.subscriptions.set(message.subscription.type, callback);
|
|
119
|
+
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'subscribe' })));
|
|
125
120
|
}
|
|
126
121
|
else {
|
|
127
122
|
// Store pending subscription for when connection is established
|
|
128
|
-
this.pendingSubscriptions.push({
|
|
123
|
+
this.pendingSubscriptions.push({ message, callback });
|
|
129
124
|
// Auto-connect if not connected
|
|
130
125
|
if (!this.isConnecting && (!this.ws || this.ws.readyState === WebSocket.CLOSED)) {
|
|
131
126
|
this.connect().catch(console.error);
|
|
@@ -134,12 +129,12 @@ class WebSocketClient {
|
|
|
134
129
|
return {
|
|
135
130
|
unsubscribe: () => {
|
|
136
131
|
var _a;
|
|
137
|
-
this.subscriptions.delete(
|
|
132
|
+
this.subscriptions.delete(message.subscription.type);
|
|
138
133
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
139
|
-
this.ws.send(JSON.stringify(Object.assign(Object.assign({},
|
|
134
|
+
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'unsubscribe' })));
|
|
140
135
|
}
|
|
141
136
|
// Remove from pending subscriptions
|
|
142
|
-
this.pendingSubscriptions = this.pendingSubscriptions.filter(sub => sub.
|
|
137
|
+
this.pendingSubscriptions = this.pendingSubscriptions.filter(sub => sub.message.subscription.type !== message.subscription.type);
|
|
143
138
|
},
|
|
144
139
|
};
|
|
145
140
|
}
|
|
@@ -159,6 +154,14 @@ class WebSocketClient {
|
|
|
159
154
|
subscription: { type: 'l2Book', coin },
|
|
160
155
|
}, callback);
|
|
161
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Subscribe to active asset ctx
|
|
159
|
+
*/
|
|
160
|
+
subscribeToActiveAssetCtx(coin, callback) {
|
|
161
|
+
return this.subscribe({
|
|
162
|
+
subscription: { type: 'activeAssetCtx', coin },
|
|
163
|
+
}, callback);
|
|
164
|
+
}
|
|
162
165
|
/**
|
|
163
166
|
* Subscribe to trades for a specific coin
|
|
164
167
|
*/
|
package/dist/types/index.d.ts
CHANGED
|
@@ -237,6 +237,18 @@ export interface L2Book {
|
|
|
237
237
|
levels: [WsLevel[], WsLevel[]];
|
|
238
238
|
time: number;
|
|
239
239
|
}
|
|
240
|
+
export interface WsActiveAssetCtx {
|
|
241
|
+
coin: string;
|
|
242
|
+
ctx: {
|
|
243
|
+
dayNtlVlm: number;
|
|
244
|
+
prevDayPx: number;
|
|
245
|
+
markPx: number;
|
|
246
|
+
midPx?: number;
|
|
247
|
+
funding: number;
|
|
248
|
+
openInterest: number;
|
|
249
|
+
oraclePx: number;
|
|
250
|
+
};
|
|
251
|
+
}
|
|
240
252
|
export interface Candle {
|
|
241
253
|
t: number;
|
|
242
254
|
T: number;
|
package/package.json
CHANGED