@n1xyz/nord-ts 0.0.12 → 0.0.14

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 { Account, Info } from "../../types";
1
+ import { Account, Info, SubscriptionPattern } from "../../types";
2
2
  import { NordWebSocketClient } from "../../websocket/index";
3
3
  /**
4
4
  * Get the current timestamp from the Nord server
@@ -36,14 +36,14 @@ export declare function getAccount(webServerUrl: string, accountId: number): Pro
36
36
  /**
37
37
  * Initialize a WebSocket client for Nord
38
38
  *
39
- * Connects to one of the specific Nord WebSocket endpoints:
40
- * - /ws/trades - For trade updates (default)
41
- * - /ws/deltas - For orderbook delta updates
42
- * - /ws/user - For user-specific updates
39
+ * Connects to the Nord WebSocket endpoint with support for multiple subscription types:
40
+ * - trades@SYMBOL - For trade updates
41
+ * - deltas@SYMBOL - For orderbook delta updates
42
+ * - account@ACCOUNT_ID - For user-specific updates
43
43
  *
44
44
  * @param webServerUrl - Base URL for the Nord web server
45
- * @param endpoint - Specific WebSocket endpoint to connect to (trades, deltas, or user)
46
- * @param initialSubscriptions - Optional array of initial subscriptions (e.g., ["trades@BTCUSDC"])
45
+ * @param subscriptions - Array of subscriptions (e.g., ["trades@BTCUSDC", "deltas@BTCUSDC", "account@42"])
47
46
  * @returns WebSocket client
47
+ * @throws {NordError} If initialization fails or invalid subscription is provided
48
48
  */
49
- export declare function initWebSocketClient(webServerUrl: string, endpoint?: "trades" | "deltas" | "user", initialSubscriptions?: string[]): NordWebSocketClient;
49
+ export declare function initWebSocketClient(webServerUrl: string, subscriptions?: SubscriptionPattern[] | "trades" | "delta" | "account", initialSubscriptions?: SubscriptionPattern[]): NordWebSocketClient;
@@ -77,22 +77,46 @@ async function getAccount(webServerUrl, accountId) {
77
77
  /**
78
78
  * Initialize a WebSocket client for Nord
79
79
  *
80
- * Connects to one of the specific Nord WebSocket endpoints:
81
- * - /ws/trades - For trade updates (default)
82
- * - /ws/deltas - For orderbook delta updates
83
- * - /ws/user - For user-specific updates
80
+ * Connects to the Nord WebSocket endpoint with support for multiple subscription types:
81
+ * - trades@SYMBOL - For trade updates
82
+ * - deltas@SYMBOL - For orderbook delta updates
83
+ * - account@ACCOUNT_ID - For user-specific updates
84
84
  *
85
85
  * @param webServerUrl - Base URL for the Nord web server
86
- * @param endpoint - Specific WebSocket endpoint to connect to (trades, deltas, or user)
87
- * @param initialSubscriptions - Optional array of initial subscriptions (e.g., ["trades@BTCUSDC"])
86
+ * @param subscriptions - Array of subscriptions (e.g., ["trades@BTCUSDC", "deltas@BTCUSDC", "account@42"])
88
87
  * @returns WebSocket client
88
+ * @throws {NordError} If initialization fails or invalid subscription is provided
89
89
  */
90
- function initWebSocketClient(webServerUrl, endpoint, initialSubscriptions) {
90
+ function initWebSocketClient(webServerUrl, subscriptions, initialSubscriptions) {
91
91
  try {
92
- // Convert HTTP URL to WebSocket URL with specific endpoint
93
- // If no specific endpoint is provided, we'll connect to trades by default
94
- const specificEndpoint = endpoint || "trades";
95
- const wsUrl = webServerUrl.replace(/^http/, "ws") + `/ws/${specificEndpoint}`;
92
+ // Determine URL and subscriptions based on parameters
93
+ let wsUrl = webServerUrl.replace(/^http/, "ws") + `/ws`;
94
+ let wsSubscriptions = [];
95
+ // Validate subscriptions parameter
96
+ if (typeof subscriptions === "string") {
97
+ // Legacy mode - handle endpoint string
98
+ if (subscriptions === "trades" || subscriptions === "delta" || subscriptions === "account") {
99
+ wsUrl += `/${subscriptions}`;
100
+ // If initialSubscriptions provided, use them
101
+ if (initialSubscriptions && initialSubscriptions.length > 0) {
102
+ // Validate initialSubscriptions
103
+ initialSubscriptions.forEach(validateSubscription);
104
+ wsSubscriptions = initialSubscriptions;
105
+ }
106
+ }
107
+ else {
108
+ throw new NordError_1.NordError(`Invalid endpoint: ${subscriptions}. Must be "trades", "deltas", or "account".`);
109
+ }
110
+ }
111
+ else if (Array.isArray(subscriptions) && subscriptions.length > 0) {
112
+ // New mode - validate and combine subscriptions in URL
113
+ subscriptions.forEach(validateSubscription);
114
+ wsUrl += `/${subscriptions.join("&")}`;
115
+ }
116
+ else {
117
+ // Default to trades endpoint if no subscriptions specified
118
+ wsUrl += `/trades`;
119
+ }
96
120
  console.log(`Initializing WebSocket client with URL: ${wsUrl}`);
97
121
  // Create and connect the WebSocket client
98
122
  const ws = new index_1.NordWebSocketClient(wsUrl);
@@ -103,9 +127,10 @@ function initWebSocketClient(webServerUrl, endpoint, initialSubscriptions) {
103
127
  // Add connected handler for debugging
104
128
  ws.on("connected", () => {
105
129
  console.log("Nord WebSocket connected successfully");
106
- // Subscribe to initial subscriptions if provided
107
- if (initialSubscriptions && initialSubscriptions.length > 0) {
108
- ws.subscribe(initialSubscriptions);
130
+ // Subscribe to additional subscriptions if provided
131
+ // For new format, these are already part of the URL
132
+ if (wsSubscriptions.length > 0) {
133
+ ws.subscribe(wsSubscriptions);
109
134
  }
110
135
  });
111
136
  // Connect the WebSocket
@@ -119,3 +144,19 @@ function initWebSocketClient(webServerUrl, endpoint, initialSubscriptions) {
119
144
  });
120
145
  }
121
146
  }
147
+ /**
148
+ * Validates a subscription string follows the correct format
149
+ *
150
+ * @param subscription - The subscription to validate
151
+ * @throws {NordError} If the subscription format is invalid
152
+ */
153
+ function validateSubscription(subscription) {
154
+ const [type, param] = subscription.split('@');
155
+ if (!type || !param || !['trades', 'deltas', 'account'].includes(type)) {
156
+ throw new NordError_1.NordError(`Invalid subscription format: ${subscription}. Expected format: "trades@SYMBOL", "deltas@SYMBOL", or "account@ID"`);
157
+ }
158
+ // Additional validation for account subscriptions
159
+ if (type === 'account' && isNaN(Number(param))) {
160
+ throw new NordError_1.NordError(`Invalid account ID in subscription: ${subscription}. Account ID must be a number.`);
161
+ }
162
+ }
@@ -208,7 +208,7 @@ async function queryPrometheus(webServerUrl, params) {
208
208
  const data = JSON.parse(text);
209
209
  return data.data.result[0]?.value[1] || 0;
210
210
  }
211
- catch (e) {
211
+ catch (_) {
212
212
  // Try to find a number in the response
213
213
  const numberMatch = text.match(/[\d.]+/);
214
214
  if (numberMatch) {
@@ -8,6 +8,17 @@ import { OrderbookSubscription, TradeSubscription } from "../models/Subscriber";
8
8
  export interface UserSubscription extends EventEmitter {
9
9
  close: () => void;
10
10
  }
11
+ /**
12
+ * WebSocket subscription options interface
13
+ */
14
+ export interface WebSocketSubscriptionOptions {
15
+ /** Market symbols to subscribe to for trade updates */
16
+ trades?: string[];
17
+ /** Market symbols to subscribe to for orderbook delta updates */
18
+ deltas?: string[];
19
+ /** Account IDs to subscribe to for account updates */
20
+ accounts?: number[];
21
+ }
11
22
  /**
12
23
  * Main Nord client class for interacting with the Nord API
13
24
  */
@@ -24,31 +35,6 @@ export declare class Nord {
24
35
  tokens: Token[];
25
36
  /** Map of symbol to market_id */
26
37
  private symbolToMarketId;
27
- /**
28
- * WebSocket client for trades
29
- * @private
30
- */
31
- private tradesWs;
32
- /**
33
- * WebSocket client for orderbook deltas
34
- * @private
35
- */
36
- private deltasWs;
37
- /**
38
- * WebSocket client for user updates
39
- * @private
40
- */
41
- private userWs;
42
- /**
43
- * Initial subscriptions for the trades WebSocket
44
- * @private
45
- */
46
- private tradesSubscriptions?;
47
- /**
48
- * Initial subscriptions for the deltas WebSocket
49
- * @private
50
- */
51
- private deltasSubscriptions?;
52
38
  /**
53
39
  * Create a new Nord client
54
40
  *
@@ -56,25 +42,31 @@ export declare class Nord {
56
42
  * @param config.webServerUrl - Base URL for the Nord web server
57
43
  * @param config.solanaProgramId - Solana program ID
58
44
  * @param config.solanaUrl - Solana cluster URL
59
- * @param config.initWebSockets - Whether to initialize WebSockets on creation, defaults to true
60
- * @param config.tradesSubscriptions - Optional array of trades subscriptions to initialize with (e.g., ["trades@BTCUSDC"])
61
- * @param config.deltasSubscriptions - Optional array of deltas subscriptions to initialize with (e.g., ["deltas@BTCUSDC"])
62
45
  * @throws {Error} If required configuration is missing
63
46
  */
64
- constructor({ webServerUrl, solanaProgramId, solanaUrl, initWebSockets, tradesSubscriptions, deltasSubscriptions, }: NordConfig);
47
+ constructor({ webServerUrl, solanaProgramId, solanaUrl, }: NordConfig);
65
48
  /**
66
- * Initialize WebSocket clients for trades and deltas
49
+ * Create a WebSocket client with specific subscriptions
67
50
  *
68
- * This method can be called manually if websockets were not initialized during construction
69
- * (i.e., if initWebSockets was set to false in the constructor).
51
+ * @param options - Subscription options that specify which data streams to subscribe to
52
+ * @returns A new WebSocket client with the requested subscriptions
53
+ * @throws {NordError} If invalid subscription options are provided
70
54
  *
71
- * It initializes the trades and deltas WebSocket clients. The user WebSocket client
72
- * is initialized on demand when needed.
55
+ * @example
56
+ * // Create a client for trades and deltas from one market and an account
57
+ * const wsClient = nord.createWebSocketClient({
58
+ * trades: ["BTCUSDC"],
59
+ * deltas: ["BTCUSDC"],
60
+ * accounts: [123]
61
+ * });
73
62
  *
74
- * @param tradesSubscriptions - Optional array of trades subscriptions to initialize with
75
- * @param deltasSubscriptions - Optional array of deltas subscriptions to initialize with
63
+ * @example
64
+ * // Create a client for trades from multiple markets
65
+ * const tradesClient = nord.createWebSocketClient({
66
+ * trades: ["BTCUSDC", "ETHUSDC"]
67
+ * });
76
68
  */
77
- initializeWebSockets(tradesSubscriptions?: string[], deltasSubscriptions?: string[]): void;
69
+ createWebSocketClient(options: WebSocketSubscriptionOptions): NordWebSocketClient;
78
70
  /**
79
71
  * Get the current timestamp from the Nord server
80
72
  *
@@ -102,9 +94,6 @@ export declare class Nord {
102
94
  * @param nordConfig.webServerUrl - Base URL for the Nord web server
103
95
  * @param nordConfig.solanaProgramId - Solana program ID
104
96
  * @param nordConfig.solanaUrl - Solana cluster URL
105
- * @param nordConfig.initWebSockets - Whether to initialize WebSockets on creation, defaults to true
106
- * @param nordConfig.tradesSubscriptions - Optional array of trades subscriptions (e.g., ["trades@BTCUSDC"])
107
- * @param nordConfig.deltasSubscriptions - Optional array of deltas subscriptions (e.g., ["deltas@BTCUSDC"])
108
97
  * @returns Initialized Nord client
109
98
  * @throws {NordError} If initialization fails
110
99
  */
@@ -204,39 +193,12 @@ export declare class Nord {
204
193
  * @throws {NordError} If the request fails
205
194
  */
206
195
  queryPrometheus(params: string): Promise<number>;
207
- /**
208
- * Get the trades WebSocket client (default)
209
- * If not already initialized, it will be created
210
- *
211
- * @returns WebSocket client for trades
212
- */
213
- getWebSocketClient(): NordWebSocketClient;
214
- /**
215
- * Get the trades WebSocket client
216
- * If not already initialized, it will be created
217
- *
218
- * @returns WebSocket client for trades
219
- */
220
- getTradesWebSocketClient(): NordWebSocketClient;
221
- /**
222
- * Get the deltas WebSocket client
223
- * If not already initialized, it will be created
224
- *
225
- * @returns WebSocket client for orderbook deltas
226
- */
227
- getDeltasWebSocketClient(): NordWebSocketClient;
228
- /**
229
- * Get the user WebSocket client
230
- * If not already initialized, it will be created
231
- *
232
- * @returns WebSocket client for user updates
233
- */
234
- getUserWebSocketClient(): NordWebSocketClient;
235
196
  /**
236
197
  * Subscribe to orderbook updates for a market
237
198
  *
238
199
  * @param symbol - Market symbol
239
200
  * @returns Orderbook subscription
201
+ * @throws {NordError} If symbol is invalid
240
202
  */
241
203
  subscribeOrderbook(symbol: string): OrderbookSubscription;
242
204
  /**
@@ -244,8 +206,17 @@ export declare class Nord {
244
206
  *
245
207
  * @param symbol - Market symbol
246
208
  * @returns Trade subscription
209
+ * @throws {NordError} If symbol is invalid
247
210
  */
248
211
  subscribeTrades(symbol: string): TradeSubscription;
212
+ /**
213
+ * Subscribe to account updates
214
+ *
215
+ * @param accountId - Account ID to subscribe to
216
+ * @returns User subscription
217
+ * @throws {NordError} If accountId is invalid
218
+ */
219
+ subscribeAccount(accountId: number): UserSubscription;
249
220
  /**
250
221
  * Get trades for a market
251
222
  *
@@ -52,33 +52,15 @@ class Nord {
52
52
  * @param config.webServerUrl - Base URL for the Nord web server
53
53
  * @param config.solanaProgramId - Solana program ID
54
54
  * @param config.solanaUrl - Solana cluster URL
55
- * @param config.initWebSockets - Whether to initialize WebSockets on creation, defaults to true
56
- * @param config.tradesSubscriptions - Optional array of trades subscriptions to initialize with (e.g., ["trades@BTCUSDC"])
57
- * @param config.deltasSubscriptions - Optional array of deltas subscriptions to initialize with (e.g., ["deltas@BTCUSDC"])
58
55
  * @throws {Error} If required configuration is missing
59
56
  */
60
- constructor({ webServerUrl, solanaProgramId, solanaUrl, initWebSockets = true, tradesSubscriptions, deltasSubscriptions, }) {
57
+ constructor({ webServerUrl, solanaProgramId, solanaUrl, }) {
61
58
  /** Available markets */
62
59
  this.markets = [];
63
60
  /** Available tokens */
64
61
  this.tokens = [];
65
62
  /** Map of symbol to market_id */
66
63
  this.symbolToMarketId = new Map();
67
- /**
68
- * WebSocket client for trades
69
- * @private
70
- */
71
- this.tradesWs = null;
72
- /**
73
- * WebSocket client for orderbook deltas
74
- * @private
75
- */
76
- this.deltasWs = null;
77
- /**
78
- * WebSocket client for user updates
79
- * @private
80
- */
81
- this.userWs = null;
82
64
  if (!webServerUrl) {
83
65
  throw new NordError_1.NordError("webServerUrl is required");
84
66
  }
@@ -91,33 +73,57 @@ class Nord {
91
73
  this.webServerUrl = webServerUrl;
92
74
  this.solanaProgramId = solanaProgramId;
93
75
  this.solanaUrl = solanaUrl;
94
- // Store subscription parameters
95
- this.tradesSubscriptions = tradesSubscriptions;
96
- this.deltasSubscriptions = deltasSubscriptions;
97
- // Initialize WebSocket clients only if initWebSockets is true
98
- if (initWebSockets) {
99
- this.initializeWebSockets();
100
- }
101
76
  }
102
77
  /**
103
- * Initialize WebSocket clients for trades and deltas
104
- *
105
- * This method can be called manually if websockets were not initialized during construction
106
- * (i.e., if initWebSockets was set to false in the constructor).
107
- *
108
- * It initializes the trades and deltas WebSocket clients. The user WebSocket client
109
- * is initialized on demand when needed.
110
- *
111
- * @param tradesSubscriptions - Optional array of trades subscriptions to initialize with
112
- * @param deltasSubscriptions - Optional array of deltas subscriptions to initialize with
78
+ * Create a WebSocket client with specific subscriptions
79
+ *
80
+ * @param options - Subscription options that specify which data streams to subscribe to
81
+ * @returns A new WebSocket client with the requested subscriptions
82
+ * @throws {NordError} If invalid subscription options are provided
83
+ *
84
+ * @example
85
+ * // Create a client for trades and deltas from one market and an account
86
+ * const wsClient = nord.createWebSocketClient({
87
+ * trades: ["BTCUSDC"],
88
+ * deltas: ["BTCUSDC"],
89
+ * accounts: [123]
90
+ * });
91
+ *
92
+ * @example
93
+ * // Create a client for trades from multiple markets
94
+ * const tradesClient = nord.createWebSocketClient({
95
+ * trades: ["BTCUSDC", "ETHUSDC"]
96
+ * });
113
97
  */
114
- initializeWebSockets(tradesSubscriptions, deltasSubscriptions) {
115
- // Use provided subscriptions or fall back to stored ones
116
- const trades = tradesSubscriptions || this.tradesSubscriptions;
117
- const deltas = deltasSubscriptions || this.deltasSubscriptions;
118
- // Initialize WebSocket clients for each endpoint with subscriptions
119
- this.tradesWs = core.initWebSocketClient(this.webServerUrl, "trades", trades);
120
- this.deltasWs = core.initWebSocketClient(this.webServerUrl, "deltas", deltas);
98
+ createWebSocketClient(options) {
99
+ const subscriptions = [];
100
+ // Add trade subscriptions
101
+ if (options.trades && options.trades.length > 0) {
102
+ options.trades.forEach(symbol => {
103
+ subscriptions.push(`trades@${symbol}`);
104
+ });
105
+ }
106
+ // Add delta subscriptions
107
+ if (options.deltas && options.deltas.length > 0) {
108
+ options.deltas.forEach(symbol => {
109
+ subscriptions.push(`deltas@${symbol}`);
110
+ });
111
+ }
112
+ // Add account subscriptions
113
+ if (options.accounts && options.accounts.length > 0) {
114
+ options.accounts.forEach(accountId => {
115
+ if (isNaN(accountId) || accountId <= 0) {
116
+ throw new NordError_1.NordError(`Invalid account ID: ${accountId}. Must be a positive number.`);
117
+ }
118
+ subscriptions.push(`account@${accountId}`);
119
+ });
120
+ }
121
+ // Validate that at least one subscription was provided
122
+ if (subscriptions.length === 0) {
123
+ throw new NordError_1.NordError('At least one subscription must be provided');
124
+ }
125
+ // Create and return a new WebSocket client
126
+ return core.initWebSocketClient(this.webServerUrl, subscriptions);
121
127
  }
122
128
  /**
123
129
  * Get the current timestamp from the Nord server
@@ -164,9 +170,6 @@ class Nord {
164
170
  * @param nordConfig.webServerUrl - Base URL for the Nord web server
165
171
  * @param nordConfig.solanaProgramId - Solana program ID
166
172
  * @param nordConfig.solanaUrl - Solana cluster URL
167
- * @param nordConfig.initWebSockets - Whether to initialize WebSockets on creation, defaults to true
168
- * @param nordConfig.tradesSubscriptions - Optional array of trades subscriptions (e.g., ["trades@BTCUSDC"])
169
- * @param nordConfig.deltasSubscriptions - Optional array of deltas subscriptions (e.g., ["deltas@BTCUSDC"])
170
173
  * @returns Initialized Nord client
171
174
  * @throws {NordError} If initialization fails
172
175
  */
@@ -294,79 +297,31 @@ class Nord {
294
297
  async queryPrometheus(params) {
295
298
  return metrics.queryPrometheus(this.webServerUrl, params);
296
299
  }
297
- /**
298
- * Get the trades WebSocket client (default)
299
- * If not already initialized, it will be created
300
- *
301
- * @returns WebSocket client for trades
302
- */
303
- getWebSocketClient() {
304
- if (!this.tradesWs) {
305
- this.initializeWebSockets();
306
- }
307
- return this.tradesWs;
308
- }
309
- /**
310
- * Get the trades WebSocket client
311
- * If not already initialized, it will be created
312
- *
313
- * @returns WebSocket client for trades
314
- */
315
- getTradesWebSocketClient() {
316
- if (!this.tradesWs) {
317
- this.initializeWebSockets();
318
- }
319
- return this.tradesWs;
320
- }
321
- /**
322
- * Get the deltas WebSocket client
323
- * If not already initialized, it will be created
324
- *
325
- * @returns WebSocket client for orderbook deltas
326
- */
327
- getDeltasWebSocketClient() {
328
- if (!this.deltasWs) {
329
- this.initializeWebSockets();
330
- }
331
- return this.deltasWs;
332
- }
333
- /**
334
- * Get the user WebSocket client
335
- * If not already initialized, it will be created
336
- *
337
- * @returns WebSocket client for user updates
338
- */
339
- getUserWebSocketClient() {
340
- if (!this.userWs) {
341
- // Initialize user WebSocket client on demand
342
- this.userWs = core.initWebSocketClient(this.webServerUrl, "user");
343
- return this.userWs;
344
- }
345
- return this.userWs;
346
- }
347
300
  /**
348
301
  * Subscribe to orderbook updates for a market
349
302
  *
350
303
  * @param symbol - Market symbol
351
304
  * @returns Orderbook subscription
305
+ * @throws {NordError} If symbol is invalid
352
306
  */
353
307
  subscribeOrderbook(symbol) {
308
+ if (!symbol || typeof symbol !== 'string') {
309
+ throw new NordError_1.NordError('Invalid market symbol');
310
+ }
354
311
  const subscription = new events_1.EventEmitter();
312
+ const wsClient = this.createWebSocketClient({
313
+ deltas: [symbol]
314
+ });
355
315
  const handleDelta = (update) => {
356
316
  if (update.symbol !== symbol) {
357
317
  return;
358
318
  }
359
319
  subscription.emit("message", update);
360
320
  };
361
- // Initialize deltas websocket if it doesn't exist
362
- if (!this.deltasWs) {
363
- this.initializeWebSockets();
364
- }
365
- this.deltasWs.on("delta", handleDelta);
366
- this.deltasWs.subscribe([`deltas@${symbol}`]);
321
+ wsClient.on("delta", handleDelta);
367
322
  subscription.close = () => {
368
- this.deltasWs.unsubscribe([`deltas@${symbol}`]);
369
- this.deltasWs.removeListener("delta", handleDelta);
323
+ wsClient.unsubscribe([`deltas@${symbol}`]);
324
+ wsClient.removeListener("delta", handleDelta);
370
325
  subscription.removeAllListeners();
371
326
  };
372
327
  return subscription;
@@ -376,24 +331,55 @@ class Nord {
376
331
  *
377
332
  * @param symbol - Market symbol
378
333
  * @returns Trade subscription
334
+ * @throws {NordError} If symbol is invalid
379
335
  */
380
336
  subscribeTrades(symbol) {
337
+ if (!symbol || typeof symbol !== 'string') {
338
+ throw new NordError_1.NordError('Invalid market symbol');
339
+ }
381
340
  const subscription = new events_1.EventEmitter();
341
+ const wsClient = this.createWebSocketClient({
342
+ trades: [symbol]
343
+ });
382
344
  const handleTrade = (update) => {
383
345
  if (update.symbol !== symbol) {
384
346
  return;
385
347
  }
386
348
  subscription.emit("message", update);
387
349
  };
388
- // Initialize trades websocket if it doesn't exist
389
- if (!this.tradesWs) {
390
- this.initializeWebSockets();
350
+ wsClient.on("trades", handleTrade);
351
+ subscription.close = () => {
352
+ wsClient.unsubscribe([`trades@${symbol}`]);
353
+ wsClient.removeListener("trades", handleTrade);
354
+ subscription.removeAllListeners();
355
+ };
356
+ return subscription;
357
+ }
358
+ /**
359
+ * Subscribe to account updates
360
+ *
361
+ * @param accountId - Account ID to subscribe to
362
+ * @returns User subscription
363
+ * @throws {NordError} If accountId is invalid
364
+ */
365
+ subscribeAccount(accountId) {
366
+ if (isNaN(accountId) || accountId <= 0) {
367
+ throw new NordError_1.NordError('Invalid account ID');
391
368
  }
392
- this.tradesWs.on("trade", handleTrade);
393
- this.tradesWs.subscribe([`trades@${symbol}`]);
369
+ const subscription = new events_1.EventEmitter();
370
+ const wsClient = this.createWebSocketClient({
371
+ accounts: [accountId]
372
+ });
373
+ const handleAccountUpdate = (update) => {
374
+ if (update.account_id !== accountId) {
375
+ return;
376
+ }
377
+ subscription.emit("message", update);
378
+ };
379
+ wsClient.on("account", handleAccountUpdate);
394
380
  subscription.close = () => {
395
- this.tradesWs.unsubscribe([`trades@${symbol}`]);
396
- this.tradesWs.removeListener("trade", handleTrade);
381
+ wsClient.unsubscribe([`account@${accountId}`]);
382
+ wsClient.removeListener("account", handleAccountUpdate);
397
383
  subscription.removeAllListeners();
398
384
  };
399
385
  return subscription;
@@ -440,10 +426,6 @@ class Nord {
440
426
  }
441
427
  query = { market_id: marketId };
442
428
  }
443
- // Ensure market_id is provided
444
- if (query.market_id === undefined) {
445
- throw new NordError_1.NordError("market_id is required for orderbook query");
446
- }
447
429
  return market.getOrderbook(this.webServerUrl, query);
448
430
  }
449
431
  /**
@@ -64,15 +64,6 @@ export interface TransferParams {
64
64
  /** Destination account ID */
65
65
  toAccountId: number;
66
66
  }
67
- /**
68
- * Parameters for creating a new account
69
- */
70
- export interface CreateAccountParams {
71
- /** Token ID for initial funding */
72
- tokenId: number;
73
- /** Initial funding amount */
74
- amount: Decimal.Value;
75
- }
76
67
  /**
77
68
  * User class for interacting with the Nord protocol
78
69
  */
@@ -270,14 +261,6 @@ export declare class NordUser {
270
261
  * @throws {NordError} If the operation fails
271
262
  */
272
263
  transferToAccount(params: TransferParams): Promise<void>;
273
- /**
274
- * Create a new account
275
- *
276
- * @param params - Account creation parameters
277
- * @returns New NordUser instance
278
- * @throws {NordError} If the operation fails
279
- */
280
- createAccount(params: CreateAccountParams): Promise<NordUser>;
281
264
  /**
282
265
  * Helper function to retry a promise with exponential backoff
283
266
  *
@@ -547,34 +547,6 @@ class NordUser {
547
547
  throw new NordError_1.NordError("Failed to transfer tokens", { cause: error });
548
548
  }
549
549
  }
550
- /**
551
- * Create a new account
552
- *
553
- * @param params - Account creation parameters
554
- * @returns New NordUser instance
555
- * @throws {NordError} If the operation fails
556
- */
557
- async createAccount(params) {
558
- try {
559
- this.checkSessionValidity();
560
- // Create a new keypair for the account
561
- const keypair = web3_js_1.Keypair.generate();
562
- // Create a new NordUser
563
- const newUser = NordUser.fromPrivateKey(this.nord, keypair.secretKey, this.connection);
564
- // Transfer initial funds
565
- await this.transferToAccount({
566
- to: newUser,
567
- tokenId: params.tokenId,
568
- amount: params.amount,
569
- fromAccountId: (0, utils_2.optExpect)(this.accountIds?.[0], "No account ID"),
570
- toAccountId: (0, utils_2.optExpect)(newUser.accountIds?.[0], "No account ID for new user"),
571
- });
572
- return newUser;
573
- }
574
- catch (error) {
575
- throw new NordError_1.NordError("Failed to create account", { cause: error });
576
- }
577
- }
578
550
  /**
579
551
  * Helper function to retry a promise with exponential backoff
580
552
  *