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

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 (48) hide show
  1. package/.local/qa.ts +77 -0
  2. package/.local/test-atomic.ts +112 -0
  3. package/check.sh +4 -0
  4. package/default.nix +47 -0
  5. package/dist/api/client.d.ts +14 -0
  6. package/dist/api/client.js +45 -0
  7. package/dist/gen/nord.d.ts +52 -23
  8. package/dist/gen/nord.js +322 -170
  9. package/dist/gen/openapi.d.ts +2244 -0
  10. package/dist/gen/openapi.js +6 -0
  11. package/dist/index.d.ts +0 -1
  12. package/dist/index.js +0 -9
  13. package/dist/nord/api/actions.d.ts +30 -1
  14. package/dist/nord/api/actions.js +60 -3
  15. package/dist/nord/api/core.d.ts +1 -34
  16. package/dist/nord/api/core.js +0 -71
  17. package/dist/nord/client/Nord.d.ts +31 -33
  18. package/dist/nord/client/Nord.js +100 -60
  19. package/dist/nord/client/NordUser.d.ts +64 -11
  20. package/dist/nord/client/NordUser.js +90 -33
  21. package/dist/nord/index.d.ts +0 -2
  22. package/dist/nord/index.js +0 -2
  23. package/dist/nord/models/Subscriber.d.ts +2 -2
  24. package/dist/types.d.ts +43 -190
  25. package/dist/utils.d.ts +1 -19
  26. package/dist/utils.js +5 -39
  27. package/dist/websocket/NordWebSocketClient.js +18 -13
  28. package/dist/websocket/index.d.ts +1 -1
  29. package/package.json +20 -27
  30. package/src/index.ts +0 -16
  31. package/src/nord/api/actions.ts +131 -9
  32. package/src/nord/api/core.ts +0 -71
  33. package/src/nord/client/Nord.ts +142 -76
  34. package/src/nord/client/NordUser.ts +171 -50
  35. package/src/nord/index.ts +0 -2
  36. package/src/nord/models/Subscriber.ts +2 -2
  37. package/src/types.ts +55 -216
  38. package/src/utils.ts +6 -42
  39. package/src/websocket/NordWebSocketClient.ts +23 -15
  40. package/src/websocket/index.ts +1 -1
  41. package/tests/utils.spec.ts +1 -34
  42. package/dist/idl/bridge.json +0 -1506
  43. package/jest.config.ts +0 -9
  44. package/nodemon.json +0 -4
  45. package/protoc-generate.sh +0 -23
  46. package/src/idl/bridge.json +0 -1506
  47. package/src/nord/api/market.ts +0 -122
  48. package/src/nord/api/queries.ts +0 -135
package/src/utils.ts CHANGED
@@ -25,6 +25,12 @@ export function panic(message: string): never {
25
25
  throw new Error(message);
26
26
  }
27
27
 
28
+ export function isRfc3339(s: string): boolean {
29
+ const REGEX =
30
+ /^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$/;
31
+ return REGEX.test(s);
32
+ }
33
+
28
34
  export function assert(predicate: boolean, message?: string): void {
29
35
  if (!predicate) panic(message ?? "Assertion violated");
30
36
  }
@@ -38,26 +44,6 @@ export function optExpect<T>(value: T | undefined, message: string): T {
38
44
  if (value === undefined) throw new Error(message);
39
45
  return value as T;
40
46
  }
41
- /**
42
- * Unwraps optional value with default error message
43
- * @param value
44
- * @returns
45
- */
46
- export function optUnwrap<T>(value: T | undefined): T {
47
- return optExpect(value, "Optional contains no value");
48
- }
49
- /**
50
- * Applies function to value if it's defined, or passes `undefined` through
51
- * @param value Optional value to map
52
- * @param mapFn Mapper function
53
- * @returns Either mapped value or undefined
54
- */
55
- export function optMap<T, U>(
56
- value: T | undefined,
57
- mapFn: (arg: T) => U,
58
- ): U | undefined {
59
- return value !== undefined ? mapFn(value) : undefined;
60
- }
61
47
  /** Behaves same as `node-fetch/fetch` but throws if response is a failure
62
48
  *
63
49
  * @param url Request HTTP URL
@@ -190,28 +176,6 @@ export const toScaledU128 = makeToScaledBigUint({
190
176
  exponent: 56,
191
177
  });
192
178
 
193
- const U64_MAX = (1n << 64n) - 1n;
194
- const U128_MAX = (1n << 128n) - 1n;
195
- /**
196
- * Converts U128 into pair of U64 numbers, to pass it through protobuf
197
- * @param value integer, must fit U128 limits
198
- * @returns Pair of U64 integers which represent original number split in two
199
- */
200
- export function bigIntToProtoU128(value: bigint): proto.U128 {
201
- if (value < 0n) {
202
- throw new Error(`Negative number (${value})`);
203
- }
204
-
205
- if (value > U128_MAX) {
206
- throw new Error(`U128 overflow (${value})`);
207
- }
208
-
209
- return {
210
- lo: value & U64_MAX,
211
- hi: (value >> 64n) & U64_MAX,
212
- };
213
- }
214
-
215
179
  /**
216
180
  * Encodes any protobuf message into a length-delimited format,
217
181
  * i.e. prefixed with its length encoded as varint
@@ -1,12 +1,12 @@
1
- import WebSocket from "ws";
2
1
  import { EventEmitter } from "events";
2
+ import WebSocket from "ws";
3
3
  import {
4
+ WebSocketAccountUpdate,
5
+ WebSocketDeltaUpdate,
4
6
  WebSocketMessage,
5
7
  WebSocketMessageType,
6
8
  WebSocketSubscription,
7
9
  WebSocketTradeUpdate,
8
- WebSocketDeltaUpdate,
9
- WebSocketAccountUpdate,
10
10
  } from "../types";
11
11
  import { NordWebSocketClientEvents } from "./events";
12
12
 
@@ -394,19 +394,27 @@ export class NordWebSocketClient
394
394
  * @param message WebSocket message
395
395
  */
396
396
  private handleMessage(message: WebSocketMessage): void {
397
- switch (message.e) {
398
- case WebSocketMessageType.TradeUpdate:
399
- this.emit("trades", message as WebSocketTradeUpdate);
400
- break;
401
- case WebSocketMessageType.DeltaUpdate:
402
- this.emit("delta", message as WebSocketDeltaUpdate);
403
- break;
404
- case WebSocketMessageType.AccountUpdate:
405
- this.emit("account", message as WebSocketAccountUpdate);
406
- break;
407
- default:
408
- this.emit("error", new Error(`Unknown message type: ${message.e}`));
397
+ if (!message || typeof message !== "object") {
398
+ this.emit("error", new Error(`Unexpected message type: ${message}`));
399
+ return;
409
400
  }
401
+
402
+ const hasOwn = (k: string) =>
403
+ Object.prototype.hasOwnProperty.call(message, k);
404
+ if (hasOwn("trades")) {
405
+ this.emit("trades", message as WebSocketTradeUpdate);
406
+ return;
407
+ }
408
+ if (hasOwn("delta")) {
409
+ this.emit("delta", message as WebSocketDeltaUpdate);
410
+ return;
411
+ }
412
+ if (hasOwn("account")) {
413
+ this.emit("account", message as WebSocketAccountUpdate);
414
+ return;
415
+ }
416
+
417
+ this.emit("error", new Error(`Unexpected message type: ${message}`));
410
418
  }
411
419
 
412
420
  /**
@@ -1,2 +1,2 @@
1
1
  export { NordWebSocketClient } from "./NordWebSocketClient";
2
- export { NordWebSocketEvents, NordWebSocketClientEvents } from "./events";
2
+ export type { NordWebSocketEvents, NordWebSocketClientEvents } from "./events";
@@ -1,6 +1,5 @@
1
1
  import { describe, it, expect } from "@jest/globals";
2
2
  import {
3
- bigIntToProtoU128,
4
3
  toScaledU64,
5
4
  toScaledU128,
6
5
  encodeLengthDelimited,
@@ -91,38 +90,6 @@ describe("toScaledU128", () => {
91
90
  });
92
91
  });
93
92
 
94
- const U64_MAX = (1n << 64n) - 1n;
95
- const U128_MAX = (1n << 128n) - 1n;
96
-
97
- describe("bigIntToProtoU128", () => {
98
- const success: Array<[bigint, { lo: bigint; hi: bigint }]> = [
99
- [0n, { lo: 0n, hi: 0n }],
100
- [1n, { lo: 1n, hi: 0n }],
101
- [U64_MAX - 1n, { lo: U64_MAX - 1n, hi: 0n }],
102
- [U64_MAX, { lo: U64_MAX, hi: 0n }],
103
- [U64_MAX + 1n, { lo: 0n, hi: 1n }],
104
- [U128_MAX - 1n, { lo: U64_MAX - 1n, hi: U64_MAX }],
105
- [U128_MAX, { lo: U64_MAX, hi: U64_MAX }],
106
- ];
107
-
108
- success.forEach((sample, index) => {
109
- it(`Conversion to Protobuf's U128 should succeed: case ${index}`, () => {
110
- expect(bigIntToProtoU128(sample[0])).toEqual(sample[1]);
111
- });
112
- });
113
-
114
- const failure: Array<[bigint, string]> = [
115
- [-1n, "Negative number"],
116
- [U128_MAX + 1n, "U128 overflow"],
117
- ];
118
-
119
- failure.forEach((sample, index) => {
120
- it(`Conversion to Protobuf's U128 should fail: case ${index}`, () => {
121
- expect(() => bigIntToProtoU128(sample[0])).toThrow(sample[1]);
122
- });
123
- });
124
- });
125
-
126
93
  describe("proto.Action encode-decode loop", () => {
127
94
  const action: proto.Action = {
128
95
  currentTimestamp: 0n,
@@ -137,7 +104,7 @@ describe("proto.Action encode-decode loop", () => {
137
104
  isReduceOnly: false,
138
105
  price: 12n,
139
106
  size: 39n,
140
- quoteSize: { lo: 54n, hi: 55n },
107
+ quoteSize: { size: 54n, price: 55n },
141
108
  senderAccountId: undefined,
142
109
  delegatorAccountId: undefined,
143
110
  clientOrderId: 350n,