@n1xyz/nord-ts 0.1.6 → 0.1.8

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 (85) hide show
  1. package/dist/actions.d.ts +57 -0
  2. package/dist/actions.js +186 -0
  3. package/dist/bundle.js +79181 -0
  4. package/dist/{nord/client → client}/Nord.d.ts +90 -107
  5. package/dist/{nord/client → client}/Nord.js +157 -218
  6. package/dist/{nord/client → client}/NordAdmin.d.ts +68 -102
  7. package/dist/{nord/client → client}/NordAdmin.js +109 -140
  8. package/dist/{nord/client → client}/NordUser.d.ts +86 -98
  9. package/dist/{nord/client → client}/NordUser.js +204 -271
  10. package/dist/const.js +5 -8
  11. package/dist/{nord/utils/NordError.js → error.js} +7 -5
  12. package/dist/gen/nord_pb.js +88 -92
  13. package/dist/gen/openapi.d.ts +39 -0
  14. package/dist/gen/openapi.js +1 -2
  15. package/dist/index.d.ts +6 -1
  16. package/dist/index.js +10 -21
  17. package/dist/types.d.ts +5 -50
  18. package/dist/types.js +21 -83
  19. package/dist/utils.d.ts +8 -11
  20. package/dist/utils.js +55 -90
  21. package/dist/websocket/NordWebSocketClient.js +12 -17
  22. package/dist/{nord/models → websocket}/Subscriber.d.ts +1 -1
  23. package/dist/{nord/models → websocket}/Subscriber.js +6 -7
  24. package/dist/websocket/events.js +1 -2
  25. package/dist/websocket/index.d.ts +19 -2
  26. package/dist/websocket/index.js +80 -5
  27. package/package.json +2 -3
  28. package/dist/api/client.d.ts +0 -14
  29. package/dist/api/client.js +0 -45
  30. package/dist/bridge/client.d.ts +0 -151
  31. package/dist/bridge/client.js +0 -434
  32. package/dist/bridge/const.d.ts +0 -23
  33. package/dist/bridge/const.js +0 -47
  34. package/dist/bridge/index.d.ts +0 -4
  35. package/dist/bridge/index.js +0 -23
  36. package/dist/bridge/types.d.ts +0 -120
  37. package/dist/bridge/types.js +0 -18
  38. package/dist/bridge/utils.d.ts +0 -64
  39. package/dist/bridge/utils.js +0 -131
  40. package/dist/gen/common.d.ts +0 -68
  41. package/dist/gen/common.js +0 -215
  42. package/dist/gen/nord.d.ts +0 -882
  43. package/dist/gen/nord.js +0 -6520
  44. package/dist/idl/bridge.d.ts +0 -569
  45. package/dist/idl/bridge.js +0 -8
  46. package/dist/idl/bridge.json +0 -1506
  47. package/dist/idl/index.d.ts +0 -607
  48. package/dist/idl/index.js +0 -8
  49. package/dist/nord/api/actions.d.ts +0 -126
  50. package/dist/nord/api/actions.js +0 -397
  51. package/dist/nord/api/core.d.ts +0 -16
  52. package/dist/nord/api/core.js +0 -81
  53. package/dist/nord/api/market.d.ts +0 -36
  54. package/dist/nord/api/market.js +0 -96
  55. package/dist/nord/api/metrics.d.ts +0 -67
  56. package/dist/nord/api/metrics.js +0 -229
  57. package/dist/nord/api/queries.d.ts +0 -46
  58. package/dist/nord/api/queries.js +0 -109
  59. package/dist/nord/api/triggers.d.ts +0 -7
  60. package/dist/nord/api/triggers.js +0 -38
  61. package/dist/nord/client/NordClient.d.ts +0 -33
  62. package/dist/nord/client/NordClient.js +0 -45
  63. package/dist/nord/index.d.ts +0 -11
  64. package/dist/nord/index.js +0 -36
  65. package/src/const.ts +0 -34
  66. package/src/gen/.gitkeep +0 -0
  67. package/src/gen/nord_pb.ts +0 -5053
  68. package/src/gen/openapi.ts +0 -2864
  69. package/src/index.ts +0 -5
  70. package/src/nord/api/actions.ts +0 -648
  71. package/src/nord/api/core.ts +0 -96
  72. package/src/nord/api/metrics.ts +0 -269
  73. package/src/nord/client/Nord.ts +0 -937
  74. package/src/nord/client/NordAdmin.ts +0 -514
  75. package/src/nord/client/NordClient.ts +0 -79
  76. package/src/nord/client/NordUser.ts +0 -1211
  77. package/src/nord/index.ts +0 -25
  78. package/src/nord/models/Subscriber.ts +0 -56
  79. package/src/nord/utils/NordError.ts +0 -76
  80. package/src/types.ts +0 -377
  81. package/src/utils.ts +0 -269
  82. package/src/websocket/NordWebSocketClient.ts +0 -316
  83. package/src/websocket/events.ts +0 -31
  84. package/src/websocket/index.ts +0 -2
  85. /package/dist/{nord/utils/NordError.d.ts → error.d.ts} +0 -0
@@ -1,11 +1,5 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.NordWebSocketClient = void 0;
7
- const events_1 = require("events");
8
- const ws_1 = __importDefault(require("ws"));
1
+ import { EventEmitter } from "events";
2
+ import WebSocket from "ws";
9
3
  const VALID_STREAM_TYPES = ["trades", "delta", "account"];
10
4
  /**
11
5
  * WebSocket client for Nord exchange
@@ -16,19 +10,21 @@ const VALID_STREAM_TYPES = ["trades", "delta", "account"];
16
10
  * the endpoint type (e.g., only 'trades@BTCUSDC' subscriptions are valid on
17
11
  * the /ws/trades endpoint).
18
12
  */
19
- class NordWebSocketClient extends events_1.EventEmitter {
13
+ export class NordWebSocketClient extends EventEmitter {
14
+ ws = null;
15
+ url;
16
+ reconnectAttempts = 0;
17
+ maxReconnectAttempts = 5;
18
+ reconnectDelay = 1000;
19
+ pingInterval = null;
20
+ pingTimeout = null;
21
+ isBrowser;
20
22
  /**
21
23
  * Create a new NordWebSocketClient
22
24
  * @param url WebSocket server URL
23
25
  */
24
26
  constructor(url) {
25
27
  super();
26
- this.ws = null;
27
- this.reconnectAttempts = 0;
28
- this.maxReconnectAttempts = 5;
29
- this.reconnectDelay = 1000;
30
- this.pingInterval = null;
31
- this.pingTimeout = null;
32
28
  this.url = url;
33
29
  // Check if we're in a browser environment
34
30
  // The most reliable way is to check for Node.js process
@@ -101,7 +97,7 @@ class NordWebSocketClient extends events_1.EventEmitter {
101
97
  }
102
98
  else {
103
99
  // In Node.js
104
- return ws_1.default;
100
+ return WebSocket;
105
101
  }
106
102
  }
107
103
  /**
@@ -244,4 +240,3 @@ class NordWebSocketClient extends events_1.EventEmitter {
244
240
  }, this.reconnectDelay);
245
241
  }
246
242
  }
247
- exports.NordWebSocketClient = NordWebSocketClient;
@@ -1,5 +1,5 @@
1
1
  import { EventEmitter } from "events";
2
- import { Account, DeltaEvent, OrderbookResponse, SubscriberConfig, StreamTrade, Trades } from "../../types";
2
+ import { Account, DeltaEvent, OrderbookResponse, SubscriberConfig, StreamTrade, Trades } from "../types";
3
3
  /**
4
4
  * Subscriber class for handling WebSocket subscriptions
5
5
  */
@@ -1,11 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Subscriber = void 0;
4
- const utils_1 = require("../../utils");
1
+ import { MAX_BUFFER_LEN } from "../utils";
5
2
  /**
6
3
  * Subscriber class for handling WebSocket subscriptions
7
4
  */
8
- class Subscriber {
5
+ export class Subscriber {
6
+ streamURL;
7
+ buffer;
8
+ maxBufferLen;
9
9
  /**
10
10
  * Create a new Subscriber instance
11
11
  * @param config Subscriber configuration
@@ -13,7 +13,7 @@ class Subscriber {
13
13
  constructor(config) {
14
14
  this.streamURL = config.streamURL;
15
15
  this.buffer = [];
16
- this.maxBufferLen = config.maxBufferLen ?? utils_1.MAX_BUFFER_LEN;
16
+ this.maxBufferLen = config.maxBufferLen ?? MAX_BUFFER_LEN;
17
17
  }
18
18
  /**
19
19
  * Subscribe to WebSocket events
@@ -22,4 +22,3 @@ class Subscriber {
22
22
  // TODO: Implement subscription logic
23
23
  }
24
24
  }
25
- exports.Subscriber = Subscriber;
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,2 +1,19 @@
1
- export { NordWebSocketClient } from "./NordWebSocketClient";
2
- export type { NordWebSocketEvents, NordWebSocketClientEvents } from "./events";
1
+ import { NordWebSocketClient } from "./NordWebSocketClient";
2
+ import type { SubscriptionPattern } from "../types";
3
+ import type { NordWebSocketEvents, NordWebSocketClientEvents } from "./events";
4
+ import { Subscriber } from "./Subscriber";
5
+ export { NordWebSocketClient, NordWebSocketEvents, NordWebSocketClientEvents, Subscriber, };
6
+ /**
7
+ * Initialize a WebSocket client for Nord
8
+ *
9
+ * Connects to the Nord WebSocket endpoint with support for multiple subscription types:
10
+ * - trades@SYMBOL - For trade updates
11
+ * - deltas@SYMBOL - For orderbook delta updates
12
+ * - account@ACCOUNT_ID - For user-specific updates
13
+ *
14
+ * @param webServerUrl - Base URL for the Nord web server
15
+ * @param subscriptions - Array of subscriptions (e.g., ["trades@BTCUSDC", "deltas@BTCUSDC", "account@42"])
16
+ * @returns WebSocket client
17
+ * @throws {NordError} If initialization fails or invalid subscription is provided
18
+ */
19
+ export declare function initWebSocketClient(webServerUrl: string, subscriptions?: SubscriptionPattern[] | "trades" | "delta" | "account"): NordWebSocketClient;
@@ -1,5 +1,80 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NordWebSocketClient = void 0;
4
- var NordWebSocketClient_1 = require("./NordWebSocketClient");
5
- Object.defineProperty(exports, "NordWebSocketClient", { enumerable: true, get: function () { return NordWebSocketClient_1.NordWebSocketClient; } });
1
+ import { NordWebSocketClient } from "./NordWebSocketClient";
2
+ import { NordError } from "../error";
3
+ import { Subscriber } from "./Subscriber";
4
+ export { NordWebSocketClient, Subscriber, };
5
+ /**
6
+ * Initialize a WebSocket client for Nord
7
+ *
8
+ * Connects to the Nord WebSocket endpoint with support for multiple subscription types:
9
+ * - trades@SYMBOL - For trade updates
10
+ * - deltas@SYMBOL - For orderbook delta updates
11
+ * - account@ACCOUNT_ID - For user-specific updates
12
+ *
13
+ * @param webServerUrl - Base URL for the Nord web server
14
+ * @param subscriptions - Array of subscriptions (e.g., ["trades@BTCUSDC", "deltas@BTCUSDC", "account@42"])
15
+ * @returns WebSocket client
16
+ * @throws {NordError} If initialization fails or invalid subscription is provided
17
+ */
18
+ export function initWebSocketClient(webServerUrl, subscriptions) {
19
+ try {
20
+ // Determine URL and subscriptions based on parameters
21
+ let wsUrl = webServerUrl.replace(/^http/, "ws") + `/ws`;
22
+ // Validate subscriptions parameter
23
+ if (typeof subscriptions === "string") {
24
+ // Legacy mode - handle endpoint string
25
+ if (subscriptions === "trades" ||
26
+ subscriptions === "delta" ||
27
+ subscriptions === "account") {
28
+ wsUrl += `/${subscriptions}`;
29
+ }
30
+ else {
31
+ throw new NordError(`Invalid endpoint: ${subscriptions}. Must be "trades", "deltas", or "account".`);
32
+ }
33
+ }
34
+ else if (Array.isArray(subscriptions) && subscriptions.length > 0) {
35
+ // New mode - validate and combine subscriptions in URL
36
+ subscriptions.forEach(validateSubscription);
37
+ wsUrl += `/${subscriptions.join("&")}`;
38
+ }
39
+ else {
40
+ // Default to trades endpoint if no subscriptions specified
41
+ wsUrl += `/trades`;
42
+ }
43
+ console.log(`Initializing WebSocket client with URL: ${wsUrl}`);
44
+ // Create and connect the WebSocket client
45
+ const ws = new NordWebSocketClient(wsUrl);
46
+ // Add error handler
47
+ ws.on("error", (error) => {
48
+ console.error("Nord WebSocket error:", error);
49
+ });
50
+ // Add connected handler for debugging
51
+ ws.on("connected", () => {
52
+ console.log("Nord WebSocket connected successfully");
53
+ });
54
+ // Connect the WebSocket
55
+ ws.connect();
56
+ return ws;
57
+ }
58
+ catch (error) {
59
+ console.error("Failed to initialize WebSocket client:", error);
60
+ throw new NordError("Failed to initialize WebSocket client", {
61
+ cause: error,
62
+ });
63
+ }
64
+ }
65
+ /**
66
+ * Validates a subscription string follows the correct format
67
+ *
68
+ * @param subscription - The subscription to validate
69
+ * @throws {NordError} If the subscription format is invalid
70
+ */
71
+ function validateSubscription(subscription) {
72
+ const [type, param] = subscription.split("@");
73
+ if (!type || !param || !["trades", "deltas", "account"].includes(type)) {
74
+ throw new NordError(`Invalid subscription format: ${subscription}. Expected format: "trades@SYMBOL", "deltas@SYMBOL", or "account@ID"`);
75
+ }
76
+ // Additional validation for account subscriptions
77
+ if (type === "account" && isNaN(Number(param))) {
78
+ throw new NordError(`Invalid account ID in subscription: ${subscription}. Account ID must be a number.`);
79
+ }
80
+ }
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@n1xyz/nord-ts",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Typescript for Nord",
5
5
  "keywords": [],
6
6
  "author": "",
7
7
  "license": "ISC",
8
+ "type": "module",
8
9
  "main": "dist/index.js",
9
10
  "types": "dist/index.d.ts",
10
11
  "files": [
11
12
  "dist/",
12
- "src/",
13
13
  "package.json",
14
14
  "README.md"
15
15
  ],
@@ -30,7 +30,6 @@
30
30
  "@bufbuild/protoplugin": "^2.6.3",
31
31
  "@types/bun": "latest",
32
32
  "@types/google-protobuf": "^3.15.12",
33
- "@types/node": "^22.17.1",
34
33
  "@types/node-fetch": "^2.6.13",
35
34
  "@types/ws": "^8.18.1",
36
35
  "@typescript-eslint/eslint-plugin": "^8.39.1",
@@ -1,14 +0,0 @@
1
- import type { paths } from "../gen/openapi";
2
- export interface ClientConfig {
3
- baseUrl: string;
4
- }
5
- export declare function createApiClient(config: ClientConfig): import("openapi-fetch").Client<paths, `${string}/${string}`>;
6
- export declare function handleApiResponse<T>(response: Promise<{
7
- data: T;
8
- error?: unknown;
9
- response: Response;
10
- } | {
11
- data?: never;
12
- error?: unknown;
13
- response: Response;
14
- }>): Promise<T>;
@@ -1,45 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createApiClient = createApiClient;
7
- exports.handleApiResponse = handleApiResponse;
8
- const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
9
- const NordError_1 = require("../nord/utils/NordError");
10
- function createApiClient(config) {
11
- const client = (0, openapi_fetch_1.default)({
12
- baseUrl: config.baseUrl,
13
- });
14
- client.use({
15
- onResponse({ response }) {
16
- if (!response.ok) {
17
- throw new NordError_1.NordError(`HTTP ${response.status}: ${response.statusText}`, {
18
- statusCode: response.status,
19
- details: { url: response.url }
20
- });
21
- }
22
- return response;
23
- },
24
- });
25
- return client;
26
- }
27
- async function handleApiResponse(response) {
28
- const result = await response;
29
- if (result.error || !result.response.ok) {
30
- const errorMessage = result.error
31
- ? `HTTP ${result.response.status}: ${result.response.statusText}`
32
- : `Request failed: ${result.response.statusText}`;
33
- throw new NordError_1.NordError(errorMessage, {
34
- statusCode: result.response.status,
35
- details: { url: result.response.url }
36
- });
37
- }
38
- if (!('data' in result)) {
39
- throw new NordError_1.NordError("No data in response", {
40
- statusCode: 500,
41
- details: { url: result.response.url }
42
- });
43
- }
44
- return result.data;
45
- }
@@ -1,151 +0,0 @@
1
- import * as anchor from "@coral-xyz/anchor";
2
- import { AnchorProvider } from "@coral-xyz/anchor";
3
- import { Connection, Keypair, PublicKey } from "@solana/web3.js";
4
- import { BlockFacts, DepositSplParams, PdaSeedType, SolanaBridgeConfig, WithdrawalParams } from "./types";
5
- /**
6
- * Solana Bridge Client for interacting with the bridge program
7
- */
8
- export declare class SolanaBridgeClient {
9
- /** Anchor program instance */
10
- program: any;
11
- /** Solana connection */
12
- connection: Connection;
13
- /** Program ID */
14
- programId: PublicKey;
15
- /** Configuration */
16
- config: SolanaBridgeConfig;
17
- /** Anchor provider */
18
- provider: AnchorProvider;
19
- bridge: PublicKey;
20
- /**
21
- * Create a new Solana Bridge Client
22
- *
23
- * @param config Bridge configuration
24
- * @param wallet Anchor wallet for signing transactions
25
- */
26
- constructor(config: SolanaBridgeConfig, wallet: anchor.Wallet);
27
- /**
28
- * Derive a PDA (Program Derived Address) for the given seeds
29
- *
30
- * Seeds can be of type:
31
- * - Buffer: raw bytes
32
- * - PublicKey: Solana public key
33
- * - string: string encoded as UTF-8 bytes
34
- * - number: u64 encoded as little-endian 8 bytes
35
- *
36
- * @param type PDA seed type
37
- * @param seeds Additional seeds
38
- * @returns [PDA, bump]
39
- */
40
- findPda(type: PdaSeedType, ...seeds: any[]): Promise<[PublicKey, number]>;
41
- /**
42
- * Find the asset config PDA for a token mint
43
- *
44
- * @param mint Token mint address
45
- * @returns [PDA, bump]
46
- */
47
- findAssetConfigPda(mint: PublicKey): Promise<[PublicKey, number]>;
48
- /**
49
- * Find the deposit storage PDA for a deposit index
50
- *
51
- * @param depositIndex Deposit index
52
- * @returns [PDA, bump]
53
- */
54
- findDepositStoragePda(depositIndex: number): Promise<[PublicKey, number]>;
55
- /**
56
- * Find the block storage PDA for a block ID
57
- *
58
- * @param blockId Block ID
59
- * @returns [PDA, bump]
60
- */
61
- findBlockStoragePda(blockId: number): Promise<[PublicKey, number]>;
62
- /**
63
- * Find the withdrawal nullifier PDA
64
- *
65
- * @param blockId Block ID
66
- * @param leafIndex Leaf index
67
- * @returns [PDA, bump]
68
- */
69
- findWithdrawalNullifierPda(blockId: number, leafIndex: number): Promise<[PublicKey, number]>;
70
- /**
71
- * Find the authority PDA
72
- *
73
- * @returns [PDA, bump]
74
- */
75
- findAuthorityPda(): Promise<[PublicKey, number]>;
76
- /**
77
- * Helper method to sign and send a transaction
78
- *
79
- * @param transaction Transaction to sign and send
80
- * @param signers Additional signers (beyond the provider's wallet)
81
- * @returns Transaction signature
82
- */
83
- private signAndSendTransaction;
84
- /**
85
- * Deposit SPL tokens to the bridge
86
- *
87
- * @param params Deposit parameters
88
- * @param signer Signer keypair
89
- * @returns Transaction signature
90
- */
91
- depositSpl(params: DepositSplParams): Promise<string>;
92
- /**
93
- * Withdraw tokens from the bridge
94
- *
95
- * @param params Withdrawal parameters
96
- * @param signer Signer keypair
97
- * @returns Transaction signature
98
- */
99
- withdraw(params: WithdrawalParams, signer: Keypair): Promise<string>;
100
- /**
101
- * Whitelist an asset (token) for use with the bridge
102
- *
103
- * @param mint Token mint address
104
- * @param signer Operator keypair
105
- * @returns Transaction signature
106
- */
107
- whitelistAsset(mint: PublicKey, signer: Keypair): Promise<string>;
108
- /**
109
- * Propose a new block
110
- *
111
- * @param facts Block facts
112
- * @param signer Operator keypair
113
- * @returns Transaction signature
114
- */
115
- proposeBlock(facts: BlockFacts, signer: Keypair): Promise<string>;
116
- /**
117
- * Finalize a block
118
- *
119
- * @param blockId Block ID
120
- * @param stateUpdateId State update ID
121
- * @param signer Payer keypair
122
- * @returns Transaction signature
123
- */
124
- finalizeBlock(blockId: number, stateUpdateId: number, signer: Keypair): Promise<string>;
125
- /**
126
- * Finalize a DA fact
127
- *
128
- * @param fact DA fact (32-byte array)
129
- * @param signer Payer keypair
130
- * @returns Transaction signature
131
- */
132
- finalizeDaFact(fact: Buffer, signer: Keypair): Promise<string>;
133
- /**
134
- * Initialize the bridge contract
135
- *
136
- * @param operator Operator public key
137
- * @param initialAppStateCommitment Initial app state commitment (32-byte array)
138
- * @param signer Payer keypair
139
- * @returns Transaction signature
140
- */
141
- initialize(operator: PublicKey, initialAppStateCommitment: Buffer, signer: Keypair): Promise<string>;
142
- /**
143
- * Create an associated token account if it doesn't exist
144
- *
145
- * @param mint Token mint
146
- * @param owner Account owner
147
- * @param payer Transaction payer
148
- * @returns Associated token account address
149
- */
150
- createTokenAccountIfNeeded(mint: PublicKey, owner: PublicKey, payer: Keypair): Promise<PublicKey>;
151
- }