@rabby-wallet/hyperliquid-sdk 1.0.0-beta.4 → 1.0.0-beta.6

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.
@@ -11,6 +11,8 @@ export interface InfoClientConfig {
11
11
  export declare class InfoClient {
12
12
  private readonly httpClient;
13
13
  private readonly masterAddress;
14
+ private metaAndAssetCtxsCache;
15
+ private metaAndAssetCtxsInFlight;
14
16
  constructor(config: InfoClientConfig);
15
17
  /**
16
18
  * Get all mid prices for all assets
@@ -19,7 +21,7 @@ export declare class InfoClient {
19
21
  /**
20
22
  * Get metadata and asset contexts
21
23
  */
22
- metaAndAssetCtxs(): Promise<[Meta, AssetCtx[]]>;
24
+ metaAndAssetCtxs(canUseCache?: boolean): Promise<[Meta, AssetCtx[]]>;
23
25
  getClearingHouseState(address?: string): Promise<ClearinghouseState>;
24
26
  getFrontendOpenOrders(address?: string): Promise<OpenOrder[]>;
25
27
  getUserNonFundingLedgerUpdates(address?: string, startTime?: number, endTime?: number): Promise<UserNonFundingLedgerUpdates[]>;
@@ -18,6 +18,8 @@ const constants_1 = require("../types/constants");
18
18
  */
19
19
  class InfoClient {
20
20
  constructor(config) {
21
+ this.metaAndAssetCtxsCache = null;
22
+ this.metaAndAssetCtxsInFlight = null;
21
23
  this.masterAddress = config.masterAddress;
22
24
  this.httpClient = new http_client_1.HttpClient({
23
25
  isTestnet: config.isTestnet,
@@ -38,10 +40,27 @@ class InfoClient {
38
40
  * Get metadata and asset contexts
39
41
  */
40
42
  metaAndAssetCtxs() {
41
- return __awaiter(this, void 0, void 0, function* () {
42
- return this.httpClient.info({
43
- type: constants_1.InfoType.META_AND_ASSET_CTXS,
44
- });
43
+ return __awaiter(this, arguments, void 0, function* (canUseCache = true) {
44
+ if (canUseCache && this.metaAndAssetCtxsCache) {
45
+ return this.metaAndAssetCtxsCache;
46
+ }
47
+ // De-duplicate concurrent requests
48
+ if (this.metaAndAssetCtxsInFlight) {
49
+ return this.metaAndAssetCtxsInFlight;
50
+ }
51
+ this.metaAndAssetCtxsInFlight = (() => __awaiter(this, void 0, void 0, function* () {
52
+ try {
53
+ const [meta, assetCtxs] = yield this.httpClient.info({
54
+ type: constants_1.InfoType.META_AND_ASSET_CTXS,
55
+ });
56
+ this.metaAndAssetCtxsCache = [meta, assetCtxs];
57
+ return [meta, assetCtxs];
58
+ }
59
+ finally {
60
+ this.metaAndAssetCtxsInFlight = null;
61
+ }
62
+ }))();
63
+ return this.metaAndAssetCtxsInFlight;
45
64
  });
46
65
  }
47
66
  getClearingHouseState(address) {
@@ -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;
@@ -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(({ id, message, callback }) => {
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.ws.send(JSON.stringify(message));
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
- if (data.method === 'subscription') {
76
- const subscriptionId = data.subscription;
77
- const callback = this.subscriptions.get(subscriptionId);
78
- if (callback) {
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(id, callback);
124
- this.ws.send(JSON.stringify(Object.assign(Object.assign({}, subscriptionMessage), { method: 'subscribe' })));
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({ id, message: subscriptionMessage, callback });
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(id);
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({}, subscriptionMessage), { method: 'unsubscribe' })));
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.id !== id);
137
+ this.pendingSubscriptions = this.pendingSubscriptions.filter(sub => sub.message.subscription.type !== message.subscription.type);
143
138
  },
144
139
  };
145
140
  }
@@ -196,7 +191,7 @@ class WebSocketClient {
196
191
  */
197
192
  subscribeToUserOrders(callback) {
198
193
  return this.subscribe({
199
- subscription: { type: 'userEvents', user: this.config.masterAddress },
194
+ subscription: { type: 'orderUpdates', user: this.config.masterAddress },
200
195
  }, callback);
201
196
  }
202
197
  /**
@@ -46,8 +46,4 @@ export declare class HyperliquidSDK {
46
46
  * Quick helper: Get account summary with essential info
47
47
  */
48
48
  getAccountSummary(): Promise<ClearinghouseState>;
49
- /**
50
- * Quick helper: Get market overview
51
- */
52
- getMarketOverview(): Promise<any>;
53
49
  }
@@ -87,18 +87,5 @@ class HyperliquidSDK {
87
87
  return this.info.getClearingHouseState();
88
88
  });
89
89
  }
90
- /**
91
- * Quick helper: Get market overview
92
- */
93
- getMarketOverview() {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- const [meta, assetCtxs] = yield this.info.metaAndAssetCtxs();
96
- return {
97
- assets: meta.universe,
98
- assetContexts: assetCtxs,
99
- marginTables: meta.marginTables,
100
- };
101
- });
102
- }
103
90
  }
104
91
  exports.HyperliquidSDK = HyperliquidSDK;
@@ -4,6 +4,11 @@ export type TriggerType = 'tp' | 'sl';
4
4
  export type Side = 'A' | 'B';
5
5
  export type AssetId = number;
6
6
  export type OrderId = number;
7
+ export interface MarketOverview {
8
+ assets: AssetInfo[];
9
+ assetContexts: AssetCtx[];
10
+ marginTables: [number, MarginTable][];
11
+ }
7
12
  export interface OpenOrder {
8
13
  coin: string;
9
14
  side: Side;
@@ -52,6 +57,7 @@ export type OrderType = LimitOrderType | MarketOrderType | StopOrderType;
52
57
  export interface AssetInfo {
53
58
  name: string;
54
59
  szDecimals: number;
60
+ marginTableId: number;
55
61
  maxLeverage: number;
56
62
  onlyIsolated?: boolean;
57
63
  isDelisted?: boolean;
@@ -151,6 +157,7 @@ export interface OrderResponse {
151
157
  filled?: {
152
158
  totalSz: string;
153
159
  avgPx: string;
160
+ oid: number;
154
161
  };
155
162
  error?: string;
156
163
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/hyperliquid-sdk",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0-beta.6",
4
4
  "description": "Simplified Hyperliquid Perpetuals Trading SDK for Frontend Applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",