@n1xyz/nord-ts 0.0.18-8121ed05.0 → 0.0.19

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.
Files changed (77) hide show
  1. package/.claude/settings.local.json +11 -0
  2. package/.local/qa.ts +77 -0
  3. package/.local/test-atomic.ts +112 -0
  4. package/check.sh +4 -0
  5. package/default.nix +47 -0
  6. package/package.json +20 -27
  7. package/src/index.ts +0 -16
  8. package/src/nord/api/actions.ts +131 -9
  9. package/src/nord/api/core.ts +0 -71
  10. package/src/nord/client/Nord.ts +142 -76
  11. package/src/nord/client/NordUser.ts +171 -50
  12. package/src/nord/index.ts +0 -2
  13. package/src/nord/models/Subscriber.ts +2 -2
  14. package/src/types.ts +55 -216
  15. package/src/utils.ts +6 -42
  16. package/src/websocket/NordWebSocketClient.ts +23 -15
  17. package/src/websocket/index.ts +1 -1
  18. package/tests/utils.spec.ts +1 -34
  19. package/dist/bridge/client.d.ts +0 -151
  20. package/dist/bridge/client.js +0 -434
  21. package/dist/bridge/const.d.ts +0 -23
  22. package/dist/bridge/const.js +0 -47
  23. package/dist/bridge/index.d.ts +0 -4
  24. package/dist/bridge/index.js +0 -23
  25. package/dist/bridge/types.d.ts +0 -120
  26. package/dist/bridge/types.js +0 -18
  27. package/dist/bridge/utils.d.ts +0 -64
  28. package/dist/bridge/utils.js +0 -131
  29. package/dist/const.d.ts +0 -8
  30. package/dist/const.js +0 -30
  31. package/dist/gen/common.d.ts +0 -68
  32. package/dist/gen/common.js +0 -215
  33. package/dist/gen/nord.d.ts +0 -853
  34. package/dist/gen/nord.js +0 -6368
  35. package/dist/idl/bridge.d.ts +0 -569
  36. package/dist/idl/bridge.js +0 -8
  37. package/dist/idl/bridge.json +0 -1506
  38. package/dist/idl/index.d.ts +0 -607
  39. package/dist/idl/index.js +0 -8
  40. package/dist/index.d.ts +0 -6
  41. package/dist/index.js +0 -30
  42. package/dist/nord/api/actions.d.ts +0 -106
  43. package/dist/nord/api/actions.js +0 -256
  44. package/dist/nord/api/core.d.ts +0 -49
  45. package/dist/nord/api/core.js +0 -164
  46. package/dist/nord/api/market.d.ts +0 -36
  47. package/dist/nord/api/market.js +0 -96
  48. package/dist/nord/api/metrics.d.ts +0 -67
  49. package/dist/nord/api/metrics.js +0 -229
  50. package/dist/nord/api/queries.d.ts +0 -46
  51. package/dist/nord/api/queries.js +0 -109
  52. package/dist/nord/client/Nord.d.ts +0 -284
  53. package/dist/nord/client/Nord.js +0 -491
  54. package/dist/nord/client/NordUser.d.ts +0 -287
  55. package/dist/nord/client/NordUser.js +0 -595
  56. package/dist/nord/index.d.ts +0 -9
  57. package/dist/nord/index.js +0 -33
  58. package/dist/nord/models/Subscriber.d.ts +0 -37
  59. package/dist/nord/models/Subscriber.js +0 -25
  60. package/dist/nord/utils/NordError.d.ts +0 -35
  61. package/dist/nord/utils/NordError.js +0 -49
  62. package/dist/types.d.ts +0 -407
  63. package/dist/types.js +0 -103
  64. package/dist/utils.d.ts +0 -116
  65. package/dist/utils.js +0 -271
  66. package/dist/websocket/NordWebSocketClient.d.ts +0 -68
  67. package/dist/websocket/NordWebSocketClient.js +0 -338
  68. package/dist/websocket/events.d.ts +0 -19
  69. package/dist/websocket/events.js +0 -2
  70. package/dist/websocket/index.d.ts +0 -2
  71. package/dist/websocket/index.js +0 -5
  72. package/jest.config.ts +0 -9
  73. package/nodemon.json +0 -4
  74. package/protoc-generate.sh +0 -23
  75. package/src/idl/bridge.json +0 -1506
  76. package/src/nord/api/market.ts +0 -122
  77. package/src/nord/api/queries.ts +0 -135
@@ -1,491 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.Nord = void 0;
37
- const events_1 = require("events");
38
- const types_1 = require("../../types");
39
- const proton_1 = require("@n1xyz/proton");
40
- const core = __importStar(require("../api/core"));
41
- const market = __importStar(require("../api/market"));
42
- const metrics = __importStar(require("../api/metrics"));
43
- const queries = __importStar(require("../api/queries"));
44
- const NordError_1 = require("../utils/NordError");
45
- const web3_js_1 = require("@solana/web3.js");
46
- /**
47
- * Main Nord client class for interacting with the Nord API
48
- */
49
- class Nord {
50
- /**
51
- * Create a new Nord client
52
- *
53
- * @param config - Configuration options for the Nord client
54
- * @param config.webServerUrl - Base URL for the Nord web server
55
- * @param config.bridgeVk - Bridge verification key
56
- * @param config.solanaUrl - Solana cluster URL
57
- * @throws {Error} If required configuration is missing
58
- */
59
- constructor({ bridgeVk, solanaUrl, webServerUrl, protonClient, }) {
60
- /** Available markets */
61
- this.markets = [];
62
- /** Available tokens */
63
- this.tokens = [];
64
- /** Map of symbol to market_id */
65
- this.symbolToMarketId = new Map();
66
- this.webServerUrl = webServerUrl;
67
- this.bridgeVk = bridgeVk;
68
- this.solanaUrl = solanaUrl;
69
- this.protonClient = protonClient;
70
- }
71
- /**
72
- * Create a WebSocket client with specific subscriptions
73
- *
74
- * @param options - Subscription options that specify which data streams to subscribe to
75
- * @returns A new WebSocket client with the requested subscriptions
76
- * @throws {NordError} If invalid subscription options are provided
77
- *
78
- * @example
79
- * // Create a client for trades and deltas from one market and an account
80
- * const wsClient = nord.createWebSocketClient({
81
- * trades: ["BTCUSDC"],
82
- * deltas: ["BTCUSDC"],
83
- * accounts: [123]
84
- * });
85
- *
86
- * @example
87
- * // Create a client for trades from multiple markets
88
- * const tradesClient = nord.createWebSocketClient({
89
- * trades: ["BTCUSDC", "ETHUSDC"]
90
- * });
91
- */
92
- createWebSocketClient(options) {
93
- const subscriptions = [];
94
- // Add trade subscriptions
95
- if (options.trades && options.trades.length > 0) {
96
- options.trades.forEach((symbol) => {
97
- subscriptions.push(`trades@${symbol}`);
98
- });
99
- }
100
- // Add delta subscriptions
101
- if (options.deltas && options.deltas.length > 0) {
102
- options.deltas.forEach((symbol) => {
103
- subscriptions.push(`deltas@${symbol}`);
104
- });
105
- }
106
- // Add account subscriptions
107
- if (options.accounts && options.accounts.length > 0) {
108
- options.accounts.forEach((accountId) => {
109
- if (isNaN(accountId) || accountId <= 0) {
110
- throw new NordError_1.NordError(`Invalid account ID: ${accountId}. Must be a positive number.`);
111
- }
112
- subscriptions.push(`account@${accountId}`);
113
- });
114
- }
115
- // Validate that at least one subscription was provided
116
- if (subscriptions.length === 0) {
117
- throw new NordError_1.NordError("At least one subscription must be provided");
118
- }
119
- // Create and return a new WebSocket client
120
- return core.initWebSocketClient(this.webServerUrl, subscriptions);
121
- }
122
- /**
123
- * Get the current timestamp from the Nord server
124
- *
125
- * @returns Current timestamp as a bigint
126
- * @throws {NordError} If the request fails
127
- */
128
- async getTimestamp() {
129
- return core.getTimestamp(this.webServerUrl);
130
- }
131
- /**
132
- * Get the last event nonce from the Nord server
133
- *
134
- * @returns Next action nonce
135
- * @throws {NordError} If the request fails
136
- */
137
- async getActionNonce() {
138
- return core.getLastEventNonce(this.webServerUrl);
139
- }
140
- /**
141
- * Fetch information about Nord markets and tokens
142
- *
143
- * @throws {NordError} If the request fails
144
- */
145
- async fetchNordInfo() {
146
- try {
147
- const info = await core.getInfo(this.webServerUrl);
148
- this.markets = info.markets;
149
- this.tokens = info.tokens;
150
- // Populate the symbolToMarketId map
151
- this.symbolToMarketId.clear();
152
- info.markets.forEach((market) => {
153
- this.symbolToMarketId.set(market.symbol, market.marketId);
154
- });
155
- }
156
- catch (error) {
157
- throw new NordError_1.NordError("Failed to fetch Nord info", { cause: error });
158
- }
159
- }
160
- /**
161
- * Initialize a new Nord client
162
- *
163
- * @param nordConfig - Configuration options for the Nord client
164
- * @param nordConfig.webServerUrl - Base URL for the Nord web server
165
- * @param nordConfig.bridgeVk - Bridge verification key
166
- * @param nordConfig.solanaUrl - Solana cluster URL
167
- * @returns Initialized Nord client
168
- * @throws {NordError} If initialization fails
169
- */
170
- static async initNord({ bridgeVk: bridgeVk_, solanaUrl, webServerUrl, }) {
171
- // TODO: we should parametrize the connectionn not have it done here.
172
- // this is a dogshit api, only here to be compatible with the shitty
173
- // vibecoded code and not break zero one team's workflow.
174
- const connection = new web3_js_1.Connection(solanaUrl, { commitment: "confirmed" });
175
- const bridgeVk = new web3_js_1.PublicKey(bridgeVk_);
176
- const protonClient = await proton_1.ProtonClient.init({
177
- protonUrl: webServerUrl,
178
- bridgeVk,
179
- solConn: connection,
180
- });
181
- const nord = new Nord({
182
- bridgeVk,
183
- protonClient,
184
- solanaUrl,
185
- webServerUrl,
186
- });
187
- await nord.init();
188
- return nord;
189
- }
190
- /**
191
- * Initialize the Nord client
192
- * @private
193
- */
194
- async init() {
195
- await this.fetchNordInfo();
196
- }
197
- /**
198
- * Get market statistics
199
- *
200
- * @returns Market statistics response
201
- * @throws {NordError} If the request fails
202
- */
203
- async marketsStats() {
204
- return market.marketsStats(this.webServerUrl);
205
- }
206
- /**
207
- * Query a specific action
208
- *
209
- * @param query - Action query parameters
210
- * @returns Action response
211
- * @throws {NordError} If the request fails
212
- */
213
- async queryAction(query) {
214
- return queries.queryAction(this.webServerUrl, query);
215
- }
216
- /**
217
- * Query recent actions
218
- *
219
- * @param from - Starting action index
220
- * @param to - Ending action index
221
- * @returns Actions response
222
- * @throws {NordError} If the request fails
223
- */
224
- async queryRecentActions(from, to) {
225
- return queries.queryRecentActions(this.webServerUrl, from, to);
226
- }
227
- /**
228
- * Get the last action ID
229
- *
230
- * @returns Last action ID
231
- * @throws {NordError} If the request fails
232
- */
233
- async getLastActionId() {
234
- return queries.getLastActionId(this.webServerUrl);
235
- }
236
- /**
237
- * Fetch aggregate metrics from the Nord API
238
- *
239
- * @param txPeakTpsPeriod - Period for peak TPS calculation
240
- * @param txPeakTpsPeriodUnit - Unit for peak TPS period
241
- * @returns Aggregate metrics
242
- * @throws {NordError} If the request fails
243
- */
244
- async aggregateMetrics(txPeakTpsPeriod = 1, txPeakTpsPeriodUnit = types_1.PeakTpsPeriodUnit.Day) {
245
- return metrics.aggregateMetrics(this.webServerUrl, txPeakTpsPeriod, txPeakTpsPeriodUnit);
246
- }
247
- /**
248
- * Get current transactions per second
249
- *
250
- * @param period - Time period for the query
251
- * @returns Current TPS value
252
- * @throws {NordError} If the request fails
253
- */
254
- async getCurrentTps(period = "1m") {
255
- return metrics.getCurrentTps(this.webServerUrl, period);
256
- }
257
- /**
258
- * Get peak transactions per second
259
- *
260
- * @param period - Time period for the query
261
- * @returns Peak TPS value
262
- * @throws {NordError} If the request fails
263
- */
264
- async getPeakTps(period = "24h") {
265
- return metrics.getPeakTps(this.webServerUrl, period);
266
- }
267
- /**
268
- * Get median transaction latency
269
- *
270
- * @param period - Time period for the query
271
- * @returns Median latency in milliseconds
272
- * @throws {NordError} If the request fails
273
- */
274
- async getMedianLatency(period = "1m") {
275
- return metrics.getMedianLatency(this.webServerUrl, period);
276
- }
277
- /**
278
- * Get total transaction count
279
- *
280
- * @returns Total transaction count
281
- * @throws {NordError} If the request fails
282
- */
283
- async getTotalTransactions() {
284
- return metrics.getTotalTransactions(this.webServerUrl);
285
- }
286
- /**
287
- * Query an action from Rollman
288
- *
289
- * @param query - Action query parameters
290
- * @returns Rollman action response
291
- * @throws {NordError} If the request fails
292
- */
293
- async actionQueryRollman(query) {
294
- return queries.actionQueryRollman(this.webServerUrl, query);
295
- }
296
- /**
297
- * Query actions from Rollman
298
- *
299
- * @param last_n - Number of recent actions to query
300
- * @returns Rollman actions response
301
- * @throws {NordError} If the request fails
302
- */
303
- async actionsQueryRollman(last_n) {
304
- return queries.actionsQueryRollman(this.webServerUrl, last_n);
305
- }
306
- /**
307
- * Query Prometheus metrics
308
- *
309
- * @param params - Prometheus query parameters
310
- * @returns Query result as a number
311
- * @throws {NordError} If the request fails
312
- */
313
- async queryPrometheus(params) {
314
- return metrics.queryPrometheus(this.webServerUrl, params);
315
- }
316
- /**
317
- * Subscribe to orderbook updates for a market
318
- *
319
- * @param symbol - Market symbol
320
- * @returns Orderbook subscription
321
- * @throws {NordError} If symbol is invalid
322
- */
323
- subscribeOrderbook(symbol) {
324
- if (!symbol || typeof symbol !== "string") {
325
- throw new NordError_1.NordError("Invalid market symbol");
326
- }
327
- const subscription = new events_1.EventEmitter();
328
- const wsClient = this.createWebSocketClient({
329
- deltas: [symbol],
330
- });
331
- const handleDelta = (update) => {
332
- if (update.symbol !== symbol) {
333
- return;
334
- }
335
- subscription.emit("message", update);
336
- };
337
- wsClient.on("delta", handleDelta);
338
- subscription.close = () => {
339
- wsClient.unsubscribe([`deltas@${symbol}`]);
340
- wsClient.removeListener("delta", handleDelta);
341
- subscription.removeAllListeners();
342
- };
343
- return subscription;
344
- }
345
- /**
346
- * Subscribe to trade updates for a market
347
- *
348
- * @param symbol - Market symbol
349
- * @returns Trade subscription
350
- * @throws {NordError} If symbol is invalid
351
- */
352
- subscribeTrades(symbol) {
353
- if (!symbol || typeof symbol !== "string") {
354
- throw new NordError_1.NordError("Invalid market symbol");
355
- }
356
- const subscription = new events_1.EventEmitter();
357
- const wsClient = this.createWebSocketClient({
358
- trades: [symbol],
359
- });
360
- const handleTrade = (update) => {
361
- if (update.symbol !== symbol) {
362
- return;
363
- }
364
- subscription.emit("message", update);
365
- };
366
- wsClient.on("trades", handleTrade);
367
- subscription.close = () => {
368
- wsClient.unsubscribe([`trades@${symbol}`]);
369
- wsClient.removeListener("trades", handleTrade);
370
- subscription.removeAllListeners();
371
- };
372
- return subscription;
373
- }
374
- /**
375
- * Subscribe to account updates
376
- *
377
- * @param accountId - Account ID to subscribe to
378
- * @returns User subscription
379
- * @throws {NordError} If accountId is invalid
380
- */
381
- subscribeAccount(accountId) {
382
- if (isNaN(accountId) || accountId <= 0) {
383
- throw new NordError_1.NordError("Invalid account ID");
384
- }
385
- const subscription = new events_1.EventEmitter();
386
- const wsClient = this.createWebSocketClient({
387
- accounts: [accountId],
388
- });
389
- const handleAccountUpdate = (update) => {
390
- if (update.account_id !== accountId) {
391
- return;
392
- }
393
- subscription.emit("message", update);
394
- };
395
- wsClient.on("account", handleAccountUpdate);
396
- subscription.close = () => {
397
- wsClient.unsubscribe([`account@${accountId}`]);
398
- wsClient.removeListener("account", handleAccountUpdate);
399
- subscription.removeAllListeners();
400
- };
401
- return subscription;
402
- }
403
- /**
404
- * Get trades for a market
405
- *
406
- * @param query - Trades query parameters
407
- * @returns Trades response
408
- * @throws {NordError} If the request fails
409
- */
410
- async getTrades(query) {
411
- return market.getTrades(this.webServerUrl, query);
412
- }
413
- /**
414
- * Get user account IDs
415
- *
416
- * @param query - User account IDs query parameters
417
- * @returns User account IDs response
418
- * @throws {NordError} If the request fails
419
- */
420
- async getUserAccountIds(query) {
421
- return market.getUserAccountIds(this.webServerUrl, query);
422
- }
423
- /**
424
- * Get orderbook for a market
425
- *
426
- * @param query - Orderbook query parameters (either market_id or symbol must be provided)
427
- * @returns Orderbook response
428
- * @throws {NordError} If the request fails or if the market symbol is unknown
429
- * @remarks It's recommended to initialize the Nord client using the static `initNord` method
430
- * to ensure market information is properly loaded before calling this method.
431
- */
432
- async getOrderbook(query) {
433
- // If only symbol is provided, convert it to market_id
434
- if (query.symbol && query.market_id === undefined) {
435
- // If the map is empty, try to fetch market information first
436
- if (this.symbolToMarketId.size === 0) {
437
- await this.fetchNordInfo();
438
- }
439
- const marketId = this.symbolToMarketId.get(query.symbol);
440
- if (marketId === undefined) {
441
- throw new NordError_1.NordError(`Unknown market symbol: ${query.symbol}`);
442
- }
443
- query = { market_id: marketId };
444
- }
445
- return market.getOrderbook(this.webServerUrl, query);
446
- }
447
- /**
448
- * Get information about the Nord server
449
- *
450
- * @returns Information about markets and tokens
451
- * @throws {NordError} If the request fails
452
- */
453
- async getInfo() {
454
- return core.getInfo(this.webServerUrl);
455
- }
456
- /**
457
- * Get account information
458
- *
459
- * @param accountId - Account ID to get information for
460
- * @returns Account information
461
- * @throws {NordError} If the request fails
462
- */
463
- async getAccount(accountId) {
464
- return core.getAccount(this.webServerUrl, accountId);
465
- }
466
- /**
467
- * Get market statistics (alias for marketsStats for backward compatibility)
468
- *
469
- * @deprecated Use marketsStats instead
470
- * @returns Market statistics response
471
- */
472
- async getMarketStats() {
473
- return this.marketsStats();
474
- }
475
- /**
476
- * Check if an account exists for the given address
477
- *
478
- * @param address - The public key address to check
479
- * @returns True if the account exists, false otherwise
480
- */
481
- async accountExists(address) {
482
- try {
483
- await market.getUserAccountIds(this.webServerUrl, { pubkey: address });
484
- return true;
485
- }
486
- catch {
487
- return false;
488
- }
489
- }
490
- }
491
- exports.Nord = Nord;