@n1xyz/nord-ts 0.0.17 → 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 +91 -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 -28
  26. package/dist/utils.js +5 -58
  27. package/dist/websocket/NordWebSocketClient.js +18 -13
  28. package/dist/websocket/index.d.ts +1 -1
  29. package/package.json +23 -31
  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 -51
  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 -63
  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
@@ -9,7 +9,6 @@ import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
9
9
  import { ethers } from "ethers";
10
10
  import fetch from "node-fetch";
11
11
  import { RequestInfo, RequestInit, Response } from "node-fetch";
12
- import { BN } from "@coral-xyz/anchor";
13
12
  import { Keypair } from "@solana/web3.js";
14
13
  import bs58 from "bs58";
15
14
 
@@ -26,6 +25,12 @@ export function panic(message: string): never {
26
25
  throw new Error(message);
27
26
  }
28
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
+
29
34
  export function assert(predicate: boolean, message?: string): void {
30
35
  if (!predicate) panic(message ?? "Assertion violated");
31
36
  }
@@ -39,26 +44,6 @@ export function optExpect<T>(value: T | undefined, message: string): T {
39
44
  if (value === undefined) throw new Error(message);
40
45
  return value as T;
41
46
  }
42
- /**
43
- * Unwraps optional value with default error message
44
- * @param value
45
- * @returns
46
- */
47
- export function optUnwrap<T>(value: T | undefined): T {
48
- return optExpect(value, "Optional contains no value");
49
- }
50
- /**
51
- * Applies function to value if it's defined, or passes `undefined` through
52
- * @param value Optional value to map
53
- * @param mapFn Mapper function
54
- * @returns Either mapped value or undefined
55
- */
56
- export function optMap<T, U>(
57
- value: T | undefined,
58
- mapFn: (arg: T) => U,
59
- ): U | undefined {
60
- return value !== undefined ? mapFn(value) : undefined;
61
- }
62
47
  /** Behaves same as `node-fetch/fetch` but throws if response is a failure
63
48
  *
64
49
  * @param url Request HTTP URL
@@ -191,28 +176,6 @@ export const toScaledU128 = makeToScaledBigUint({
191
176
  exponent: 56,
192
177
  });
193
178
 
194
- const U64_MAX = (1n << 64n) - 1n;
195
- const U128_MAX = (1n << 128n) - 1n;
196
- /**
197
- * Converts U128 into pair of U64 numbers, to pass it through protobuf
198
- * @param value integer, must fit U128 limits
199
- * @returns Pair of U64 integers which represent original number split in two
200
- */
201
- export function bigIntToProtoU128(value: bigint): proto.U128 {
202
- if (value < 0n) {
203
- throw new Error(`Negative number (${value})`);
204
- }
205
-
206
- if (value > U128_MAX) {
207
- throw new Error(`U128 overflow (${value})`);
208
- }
209
-
210
- return {
211
- lo: value & U64_MAX,
212
- hi: (value >> 64n) & U64_MAX,
213
- };
214
- }
215
-
216
179
  /**
217
180
  * Encodes any protobuf message into a length-delimited format,
218
181
  * i.e. prefixed with its length encoded as varint
@@ -302,26 +265,6 @@ export function findToken(tokens: Token[], tokenId: number): Token {
302
265
  return tokens[tokenId];
303
266
  }
304
267
 
305
- /**
306
- * Convert a number to a BN with the specified number of decimals
307
- *
308
- * @param amount Amount as a number
309
- * @param decimals Number of decimal places
310
- * @returns Amount as a BN
311
- */
312
- export function toBN(amount: number, decimals: number): BN {
313
- const amountString = amount.toFixed(decimals);
314
- const [whole, fraction] = amountString.split(".");
315
-
316
- // Convert to smallest units (no decimals)
317
- const wholeBN = new BN(whole).mul(new BN(10).pow(new BN(decimals)));
318
- const fractionBN = fraction
319
- ? new BN(fraction.padEnd(decimals, "0").slice(0, decimals))
320
- : new BN(0);
321
-
322
- return wholeBN.add(fractionBN);
323
- }
324
-
325
268
  export function keypairFromPrivateKey(
326
269
  privateKey: string | Uint8Array,
327
270
  ): Keypair {
@@ -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,