@n1xyz/nord-ts 0.0.21 → 0.1.0

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 (46) hide show
  1. package/README.md +20 -16
  2. package/dist/api/client.d.ts +14 -0
  3. package/dist/api/client.js +45 -0
  4. package/dist/bridge/client.d.ts +151 -0
  5. package/dist/bridge/client.js +434 -0
  6. package/dist/bridge/const.d.ts +23 -0
  7. package/dist/bridge/const.js +47 -0
  8. package/dist/bridge/index.d.ts +4 -0
  9. package/dist/bridge/index.js +23 -0
  10. package/dist/bridge/types.d.ts +120 -0
  11. package/dist/bridge/types.js +18 -0
  12. package/dist/bridge/utils.d.ts +64 -0
  13. package/dist/bridge/utils.js +131 -0
  14. package/dist/gen/common.d.ts +68 -0
  15. package/dist/gen/common.js +215 -0
  16. package/dist/gen/nord_pb.d.ts +3719 -0
  17. package/dist/gen/nord_pb.js +945 -0
  18. package/dist/gen/openapi.d.ts +268 -4
  19. package/dist/idl/bridge.d.ts +569 -0
  20. package/dist/idl/bridge.js +8 -0
  21. package/dist/idl/bridge.json +1506 -0
  22. package/dist/idl/index.d.ts +607 -0
  23. package/dist/idl/index.js +8 -0
  24. package/dist/nord/api/actions.d.ts +31 -72
  25. package/dist/nord/api/actions.js +199 -201
  26. package/dist/nord/api/market.d.ts +36 -0
  27. package/dist/nord/api/market.js +96 -0
  28. package/dist/nord/api/queries.d.ts +46 -0
  29. package/dist/nord/api/queries.js +109 -0
  30. package/dist/nord/client/Nord.js +3 -3
  31. package/dist/nord/client/NordUser.d.ts +26 -13
  32. package/dist/nord/client/NordUser.js +13 -10
  33. package/dist/types.d.ts +12 -1
  34. package/dist/types.js +29 -2
  35. package/dist/utils.d.ts +6 -20
  36. package/dist/utils.js +17 -35
  37. package/dist/websocket/NordWebSocketClient.js +2 -6
  38. package/package.json +26 -23
  39. package/src/gen/nord_pb.ts +4257 -0
  40. package/src/gen/openapi.ts +268 -4
  41. package/src/nord/api/actions.ts +278 -369
  42. package/src/nord/client/Nord.ts +3 -3
  43. package/src/nord/client/NordUser.ts +40 -19
  44. package/src/types.ts +32 -1
  45. package/src/utils.ts +24 -43
  46. package/src/websocket/NordWebSocketClient.ts +2 -8
package/dist/utils.js CHANGED
@@ -11,7 +11,6 @@ exports.optExpect = optExpect;
11
11
  exports.checkedFetch = checkedFetch;
12
12
  exports.signAction = signAction;
13
13
  exports.makeWalletSignFn = makeWalletSignFn;
14
- exports.encodeLengthDelimited = encodeLengthDelimited;
15
14
  exports.decodeLengthDelimited = decodeLengthDelimited;
16
15
  exports.checkPubKeyLength = checkPubKeyLength;
17
16
  exports.findMarket = findMarket;
@@ -24,6 +23,7 @@ const secp256k1_1 = require("@noble/curves/secp256k1");
24
23
  const sha256_1 = require("@noble/hashes/sha256");
25
24
  const types_1 = require("./types");
26
25
  const wire_1 = require("@bufbuild/protobuf/wire");
26
+ const protobuf_1 = require("@bufbuild/protobuf");
27
27
  const ethers_1 = require("ethers");
28
28
  const node_fetch_1 = __importDefault(require("node-fetch"));
29
29
  const web3_js_1 = require("@solana/web3.js");
@@ -101,6 +101,7 @@ function makeWalletSignFn(walletKey) {
101
101
  const signingKey = new ethers_1.ethers.SigningKey(walletKey);
102
102
  return async (message) => signingKey.sign(ethers_1.ethers.hashMessage(message)).serialized;
103
103
  }
104
+ // Returned numbers do fit into specified bits range, or error is thrown.
104
105
  function makeToScaledBigUint(params) {
105
106
  const Dec = decimal_js_1.Decimal.clone({
106
107
  precision: params.precision,
@@ -159,47 +160,28 @@ exports.toScaledU128 = makeToScaledBigUint({
159
160
  precision: 40,
160
161
  exponent: 56,
161
162
  });
162
- /**
163
- * Encodes any protobuf message into a length-delimited format,
164
- * i.e. prefixed with its length encoded as varint
165
- * @param message message object
166
- * @param coder associated coder object which implements `MessageFns` interface
167
- * @returns Encoded message as Uint8Array, prefixed with its length
168
- */
169
- function encodeLengthDelimited(message, coder) {
170
- const encoded = coder.encode(message).finish();
171
- if (encoded.byteLength > MAX_PAYLOAD_SIZE) {
172
- throw new Error(`Encoded message size (${encoded.byteLength} bytes) is greater than max payload size (${MAX_PAYLOAD_SIZE} bytes).`);
173
- }
174
- const encodedLength = new wire_1.BinaryWriter().uint32(encoded.byteLength).finish();
175
- return new Uint8Array([...encodedLength, ...encoded]);
176
- }
177
163
  /**
178
164
  * Decodes any protobuf message from a length-delimited format,
179
165
  * i.e. prefixed with its length encoded as varint
180
166
  *
181
- * NB: Please note that due to limitations of Typescript type inference
182
- * it requires to specify variable type explicitly:
183
- *
184
- * ```
185
- * const foo: proto.Bar = decodeLengthDelimited(bytes, proto.Bar);
186
- * ```
187
- *
188
- * @param bytes Byte array with encoded message
189
- * @param coder associated coder object which implements `MessageFns` interface
190
- * @returns Decoded Action as Uint8Array.
167
+ * @param bytes Byte array with encoded message
168
+ * @param schema Message schema for decoding
169
+ * @returns Decoded message
191
170
  */
192
- function decodeLengthDelimited(bytes, coder) {
193
- const lengthReader = new wire_1.BinaryReader(bytes);
194
- const msgLength = lengthReader.uint32();
195
- const startsAt = lengthReader.pos;
196
- if (msgLength > MAX_PAYLOAD_SIZE) {
197
- throw new Error(`Encoded message size (${msgLength} bytes) is greater than max payload size (${MAX_PAYLOAD_SIZE} bytes).`);
171
+ function decodeLengthDelimited(bytes, schema) {
172
+ // use sizeDelimitedPeek to extract the message length and offset
173
+ const peekResult = (0, wire_1.sizeDelimitedPeek)(bytes);
174
+ if (peekResult.size === null || peekResult.offset === null) {
175
+ throw new Error("Failed to parse size-delimited message");
176
+ }
177
+ if (peekResult.size > MAX_PAYLOAD_SIZE) {
178
+ throw new Error(`Encoded message size (${peekResult.size} bytes) is greater than max payload size (${MAX_PAYLOAD_SIZE} bytes).`);
198
179
  }
199
- if (startsAt + msgLength > bytes.byteLength) {
200
- throw new Error(`Encoded message size (${msgLength} bytes) is greater than remaining buffer size (${bytes.byteLength - startsAt} bytes).`);
180
+ if (peekResult.offset + peekResult.size > bytes.length) {
181
+ throw new Error(`Encoded message size (${peekResult.size} bytes) is greater than remaining buffer size (${bytes.length - peekResult.offset} bytes).`);
201
182
  }
202
- return coder.decode(bytes.slice(startsAt, startsAt + msgLength));
183
+ // decode the message using the offset and size from peek
184
+ return (0, protobuf_1.fromBinary)(schema, bytes.slice(peekResult.offset, peekResult.offset + peekResult.size));
203
185
  }
204
186
  function checkPubKeyLength(keyType, len) {
205
187
  if (keyType === types_1.KeyType.Bls12_381) {
@@ -7,8 +7,6 @@ exports.NordWebSocketClient = void 0;
7
7
  const events_1 = require("events");
8
8
  const ws_1 = __importDefault(require("ws"));
9
9
  const VALID_STREAM_TYPES = ["trades", "delta", "account"];
10
- // Constants for WebSocket readyState
11
- const WS_OPEN = 1;
12
10
  /**
13
11
  * WebSocket client for Nord exchange
14
12
  *
@@ -132,9 +130,7 @@ class NordWebSocketClient extends events_1.EventEmitter {
132
130
  this.emit("error", new Error(`Failed to parse message: ${error instanceof Error ? error.message : String(error)}`));
133
131
  }
134
132
  };
135
- this.ws.onclose = (event) => {
136
- const reason = event && event.reason ? ` Reason: ${event.reason}` : "";
137
- const code = event && event.code ? ` Code: ${event.code}` : "";
133
+ this.ws.onclose = (_event) => {
138
134
  this.emit("disconnected");
139
135
  this.reconnect();
140
136
  };
@@ -162,7 +158,7 @@ class NordWebSocketClient extends events_1.EventEmitter {
162
158
  this.emit("error", new Error(`Failed to parse message: ${error instanceof Error ? error.message : String(error)}`));
163
159
  }
164
160
  });
165
- nodeWs.on("close", (code, reason) => {
161
+ nodeWs.on("close", (_code, _reason) => {
166
162
  this.emit("disconnected");
167
163
  if (this.pingInterval) {
168
164
  clearInterval(this.pingInterval);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1xyz/nord-ts",
3
- "version": "0.0.21",
3
+ "version": "0.1.0",
4
4
  "description": "Typescript for Nord",
5
5
  "keywords": [],
6
6
  "author": "",
@@ -14,46 +14,49 @@
14
14
  "README.md"
15
15
  ],
16
16
  "scripts": {
17
- "gen:proto": "nix run ..#nord-ts-proto ../engine/nord.proto src/gen/",
18
- "gen:api": "nix run ..#nord-openapi | bunx openapi-typescript > src/gen/openapi.ts",
17
+ "gen:proto": "bunx @bufbuild/buf generate ../engine",
18
+ "gen:api": "nix run ..#openapi | bunx openapi-typescript > src/gen/openapi.ts",
19
19
  "gen": "bun run gen:proto && bun run gen:api",
20
20
  "build": "bun run gen && bunx tsc && bun run docs",
21
21
  "docs": "bunx typedoc --out ./docs ./src",
22
22
  "compile": "tsc",
23
23
  "clean": "rm -rf ./src/gen ./dist ./docs",
24
24
  "fmt": "prettier --write src tests",
25
- "ci": "bunx eslint && prettier --check src tests"
25
+ "ci": "bunx eslint && prettier --check src tests",
26
+ "all": "bun install && bun run build && bun run ci && bun fmt"
26
27
  },
27
28
  "devDependencies": {
29
+ "@bufbuild/protoc-gen-es": "^2.6.3",
30
+ "@bufbuild/protoplugin": "^2.6.3",
28
31
  "@types/bun": "latest",
29
- "@types/google-protobuf": "^3.15.10",
30
- "@types/node": "^22.15.29",
31
- "@types/node-fetch": "^2.6.9",
32
- "@types/ws": "^8.5.10",
33
- "@typescript-eslint/eslint-plugin": "^8.26.1",
34
- "@typescript-eslint/parser": "^8.26.1",
35
- "eslint": "^9.22.0",
36
- "globals": "^16.0.0",
37
- "openapi-typescript": "^7.8.0",
38
- "prettier": "^3.5.3",
32
+ "@types/google-protobuf": "^3.15.12",
33
+ "@types/node": "^22.17.1",
34
+ "@types/node-fetch": "^2.6.13",
35
+ "@types/ws": "^8.18.1",
36
+ "@typescript-eslint/eslint-plugin": "^8.39.1",
37
+ "@typescript-eslint/parser": "^8.39.1",
38
+ "eslint": "^9.33.0",
39
+ "globals": "^16.3.0",
40
+ "openapi-typescript": "^7.9.1",
41
+ "prettier": "^3.6.2",
39
42
  "ts-node": "^10.9.2",
40
43
  "typedoc": "^0.27.9",
41
- "typescript": "*"
44
+ "typescript": "^5.9.2"
42
45
  },
43
46
  "dependencies": {
44
- "@bufbuild/protobuf": "^2.0.0",
47
+ "@bufbuild/protobuf": "^2.6.3",
45
48
  "@n1xyz/proton": "0.0.2",
46
- "@noble/curves": "^1.3.0",
47
- "@noble/ed25519": "^2.2.3",
48
- "@noble/hashes": "^1.3.2",
49
+ "@noble/curves": "^1.9.6",
50
+ "@noble/ed25519": "^2.3.0",
51
+ "@noble/hashes": "^1.8.0",
49
52
  "@solana/spl-token": "^0.4.13",
50
- "@solana/web3.js": "^1.98.2",
53
+ "@solana/web3.js": "^1.98.4",
51
54
  "bs58": "^6.0.0",
52
- "decimal.js": "^10.4.3",
53
- "ethers": "^6.11.1",
55
+ "decimal.js": "^10.6.0",
56
+ "ethers": "^6.15.0",
54
57
  "node-fetch": "2.6.13",
55
58
  "openapi-fetch": "^0.14.0",
56
59
  "tweetnacl": "^1.0.3",
57
- "ws": "^8.16.0"
60
+ "ws": "^8.18.3"
58
61
  }
59
62
  }