@lightconexyz/lightcone-sdk 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 (158) hide show
  1. package/README.md +232 -0
  2. package/dist/api/client.d.ts +225 -0
  3. package/dist/api/client.d.ts.map +1 -0
  4. package/dist/api/client.js +452 -0
  5. package/dist/api/client.js.map +1 -0
  6. package/dist/api/error.d.ts +58 -0
  7. package/dist/api/error.d.ts.map +1 -0
  8. package/dist/api/error.js +98 -0
  9. package/dist/api/error.js.map +1 -0
  10. package/dist/api/index.d.ts +23 -0
  11. package/dist/api/index.d.ts.map +1 -0
  12. package/dist/api/index.js +51 -0
  13. package/dist/api/index.js.map +1 -0
  14. package/dist/api/types/admin.d.ts +49 -0
  15. package/dist/api/types/admin.d.ts.map +1 -0
  16. package/dist/api/types/admin.js +13 -0
  17. package/dist/api/types/admin.js.map +1 -0
  18. package/dist/api/types/index.d.ts +14 -0
  19. package/dist/api/types/index.d.ts.map +1 -0
  20. package/dist/api/types/index.js +13 -0
  21. package/dist/api/types/index.js.map +1 -0
  22. package/dist/api/types/market.d.ts +186 -0
  23. package/dist/api/types/market.d.ts.map +1 -0
  24. package/dist/api/types/market.js +6 -0
  25. package/dist/api/types/market.js.map +1 -0
  26. package/dist/api/types/order.d.ts +190 -0
  27. package/dist/api/types/order.d.ts.map +1 -0
  28. package/dist/api/types/order.js +6 -0
  29. package/dist/api/types/order.js.map +1 -0
  30. package/dist/api/types/orderbook.d.ts +36 -0
  31. package/dist/api/types/orderbook.d.ts.map +1 -0
  32. package/dist/api/types/orderbook.js +6 -0
  33. package/dist/api/types/orderbook.js.map +1 -0
  34. package/dist/api/types/position.d.ts +60 -0
  35. package/dist/api/types/position.d.ts.map +1 -0
  36. package/dist/api/types/position.js +6 -0
  37. package/dist/api/types/position.js.map +1 -0
  38. package/dist/api/types/price_history.d.ts +68 -0
  39. package/dist/api/types/price_history.d.ts.map +1 -0
  40. package/dist/api/types/price_history.js +13 -0
  41. package/dist/api/types/price_history.js.map +1 -0
  42. package/dist/api/types/trade.d.ts +67 -0
  43. package/dist/api/types/trade.d.ts.map +1 -0
  44. package/dist/api/types/trade.js +13 -0
  45. package/dist/api/types/trade.js.map +1 -0
  46. package/dist/api/validation.d.ts +24 -0
  47. package/dist/api/validation.d.ts.map +1 -0
  48. package/dist/api/validation.js +53 -0
  49. package/dist/api/validation.js.map +1 -0
  50. package/dist/auth.d.ts +80 -0
  51. package/dist/auth.d.ts.map +1 -0
  52. package/dist/auth.js +149 -0
  53. package/dist/auth.js.map +1 -0
  54. package/dist/index.d.ts +55 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +107 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/network.d.ts +5 -0
  59. package/dist/network.d.ts.map +1 -0
  60. package/dist/network.js +8 -0
  61. package/dist/network.js.map +1 -0
  62. package/dist/program/accounts.d.ts +98 -0
  63. package/dist/program/accounts.d.ts.map +1 -0
  64. package/dist/program/accounts.js +319 -0
  65. package/dist/program/accounts.js.map +1 -0
  66. package/dist/program/builder.d.ts +94 -0
  67. package/dist/program/builder.d.ts.map +1 -0
  68. package/dist/program/builder.js +175 -0
  69. package/dist/program/builder.js.map +1 -0
  70. package/dist/program/client.d.ts +56 -0
  71. package/dist/program/client.d.ts.map +1 -0
  72. package/dist/program/client.js +288 -0
  73. package/dist/program/client.js.map +1 -0
  74. package/dist/program/constants.d.ts +108 -0
  75. package/dist/program/constants.d.ts.map +1 -0
  76. package/dist/program/constants.js +112 -0
  77. package/dist/program/constants.js.map +1 -0
  78. package/dist/program/index.d.ts +14 -0
  79. package/dist/program/index.d.ts.map +1 -0
  80. package/dist/program/index.js +149 -0
  81. package/dist/program/index.js.map +1 -0
  82. package/dist/program/instructions.d.ts +248 -0
  83. package/dist/program/instructions.d.ts.map +1 -0
  84. package/dist/program/instructions.js +692 -0
  85. package/dist/program/instructions.js.map +1 -0
  86. package/dist/program/orders.d.ts +151 -0
  87. package/dist/program/orders.d.ts.map +1 -0
  88. package/dist/program/orders.js +417 -0
  89. package/dist/program/orders.js.map +1 -0
  90. package/dist/program/pda.d.ts +73 -0
  91. package/dist/program/pda.d.ts.map +1 -0
  92. package/dist/program/pda.js +131 -0
  93. package/dist/program/pda.js.map +1 -0
  94. package/dist/program/types.d.ts +380 -0
  95. package/dist/program/types.d.ts.map +1 -0
  96. package/dist/program/types.js +27 -0
  97. package/dist/program/types.js.map +1 -0
  98. package/dist/program/utils.d.ts +91 -0
  99. package/dist/program/utils.d.ts.map +1 -0
  100. package/dist/program/utils.js +219 -0
  101. package/dist/program/utils.js.map +1 -0
  102. package/dist/shared/index.d.ts +8 -0
  103. package/dist/shared/index.d.ts.map +1 -0
  104. package/dist/shared/index.js +18 -0
  105. package/dist/shared/index.js.map +1 -0
  106. package/dist/shared/price.d.ts +41 -0
  107. package/dist/shared/price.d.ts.map +1 -0
  108. package/dist/shared/price.js +57 -0
  109. package/dist/shared/price.js.map +1 -0
  110. package/dist/shared/scaling.d.ts +45 -0
  111. package/dist/shared/scaling.d.ts.map +1 -0
  112. package/dist/shared/scaling.js +84 -0
  113. package/dist/shared/scaling.js.map +1 -0
  114. package/dist/shared/types.d.ts +19 -0
  115. package/dist/shared/types.d.ts.map +1 -0
  116. package/dist/shared/types.js +23 -0
  117. package/dist/shared/types.js.map +1 -0
  118. package/dist/websocket/client.d.ts +238 -0
  119. package/dist/websocket/client.d.ts.map +1 -0
  120. package/dist/websocket/client.js +580 -0
  121. package/dist/websocket/client.js.map +1 -0
  122. package/dist/websocket/error.d.ts +47 -0
  123. package/dist/websocket/error.d.ts.map +1 -0
  124. package/dist/websocket/error.js +83 -0
  125. package/dist/websocket/error.js.map +1 -0
  126. package/dist/websocket/handlers.d.ts +97 -0
  127. package/dist/websocket/handlers.d.ts.map +1 -0
  128. package/dist/websocket/handlers.js +277 -0
  129. package/dist/websocket/handlers.js.map +1 -0
  130. package/dist/websocket/index.d.ts +38 -0
  131. package/dist/websocket/index.d.ts.map +1 -0
  132. package/dist/websocket/index.js +75 -0
  133. package/dist/websocket/index.js.map +1 -0
  134. package/dist/websocket/state/index.d.ts +7 -0
  135. package/dist/websocket/state/index.d.ts.map +1 -0
  136. package/dist/websocket/state/index.js +14 -0
  137. package/dist/websocket/state/index.js.map +1 -0
  138. package/dist/websocket/state/orderbook.d.ts +107 -0
  139. package/dist/websocket/state/orderbook.d.ts.map +1 -0
  140. package/dist/websocket/state/orderbook.js +293 -0
  141. package/dist/websocket/state/orderbook.js.map +1 -0
  142. package/dist/websocket/state/price.d.ts +108 -0
  143. package/dist/websocket/state/price.d.ts.map +1 -0
  144. package/dist/websocket/state/price.js +243 -0
  145. package/dist/websocket/state/price.js.map +1 -0
  146. package/dist/websocket/state/user.d.ts +83 -0
  147. package/dist/websocket/state/user.d.ts.map +1 -0
  148. package/dist/websocket/state/user.js +228 -0
  149. package/dist/websocket/state/user.js.map +1 -0
  150. package/dist/websocket/subscriptions.d.ts +143 -0
  151. package/dist/websocket/subscriptions.d.ts.map +1 -0
  152. package/dist/websocket/subscriptions.js +244 -0
  153. package/dist/websocket/subscriptions.js.map +1 -0
  154. package/dist/websocket/types.d.ts +417 -0
  155. package/dist/websocket/types.d.ts.map +1 -0
  156. package/dist/websocket/types.js +195 -0
  157. package/dist/websocket/types.js.map +1 -0
  158. package/package.json +75 -0
@@ -0,0 +1,84 @@
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.ScalingError = void 0;
7
+ exports.scalePriceSize = scalePriceSize;
8
+ const decimal_js_1 = __importDefault(require("decimal.js"));
9
+ const types_1 = require("../program/types");
10
+ /**
11
+ * Error thrown during price/size scaling
12
+ */
13
+ class ScalingError extends Error {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = "ScalingError";
17
+ }
18
+ static zeroPriceOrSize() {
19
+ return new ScalingError("Price and size must be non-zero");
20
+ }
21
+ static negativeValue(field) {
22
+ return new ScalingError(`${field} must be positive`);
23
+ }
24
+ static overflow(field) {
25
+ return new ScalingError(`${field} exceeds u64 range`);
26
+ }
27
+ }
28
+ exports.ScalingError = ScalingError;
29
+ const U64_MAX = (1n << 64n) - 1n;
30
+ /**
31
+ * Scale price and size to maker_amount and taker_amount using exact decimal arithmetic.
32
+ *
33
+ * For BID orders (buying base with quote):
34
+ * - maker_amount = price * size * 10^quoteDecimals (what maker gives in quote tokens)
35
+ * - taker_amount = size * 10^baseDecimals (what maker receives in base tokens)
36
+ *
37
+ * For ASK orders (selling base for quote):
38
+ * - maker_amount = size * 10^baseDecimals (what maker gives in base tokens)
39
+ * - taker_amount = price * size * 10^quoteDecimals (what maker receives in quote tokens)
40
+ *
41
+ * @param priceStr - Price as a decimal string (e.g., "0.75")
42
+ * @param sizeStr - Size as a decimal string (e.g., "100")
43
+ * @param side - Order side (BID or ASK)
44
+ * @param decimals - Orderbook decimal configuration
45
+ * @returns Scaled maker_amount and taker_amount as bigints
46
+ */
47
+ function scalePriceSize(priceStr, sizeStr, side, decimals) {
48
+ const price = new decimal_js_1.default(priceStr);
49
+ const size = new decimal_js_1.default(sizeStr);
50
+ if (price.isZero() || size.isZero()) {
51
+ throw ScalingError.zeroPriceOrSize();
52
+ }
53
+ if (price.isNegative()) {
54
+ throw ScalingError.negativeValue("price");
55
+ }
56
+ if (size.isNegative()) {
57
+ throw ScalingError.negativeValue("size");
58
+ }
59
+ const baseScale = new decimal_js_1.default(10).pow(decimals.baseDecimals);
60
+ const quoteScale = new decimal_js_1.default(10).pow(decimals.quoteDecimals);
61
+ const baseAmount = size.mul(baseScale).floor();
62
+ const quoteAmount = price.mul(size).mul(quoteScale).floor();
63
+ const baseAmountBigInt = BigInt(baseAmount.toFixed(0));
64
+ const quoteAmountBigInt = BigInt(quoteAmount.toFixed(0));
65
+ if (baseAmountBigInt > U64_MAX) {
66
+ throw ScalingError.overflow("base amount");
67
+ }
68
+ if (quoteAmountBigInt > U64_MAX) {
69
+ throw ScalingError.overflow("quote amount");
70
+ }
71
+ if (side === types_1.OrderSide.BID) {
72
+ return {
73
+ makerAmount: quoteAmountBigInt,
74
+ takerAmount: baseAmountBigInt,
75
+ };
76
+ }
77
+ else {
78
+ return {
79
+ makerAmount: baseAmountBigInt,
80
+ takerAmount: quoteAmountBigInt,
81
+ };
82
+ }
83
+ }
84
+ //# sourceMappingURL=scaling.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scaling.js","sourceRoot":"","sources":["../../src/shared/scaling.ts"],"names":[],"mappings":";;;;;;AA8DA,wCA8CC;AA5GD,4DAAiC;AACjC,4CAA6C;AAoB7C;;GAEG;AACH,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;IAED,MAAM,CAAC,eAAe;QACpB,OAAO,IAAI,YAAY,CAAC,iCAAiC,CAAC,CAAC;IAC7D,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,KAAa;QAChC,OAAO,IAAI,YAAY,CAAC,GAAG,KAAK,mBAAmB,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,CAAC,QAAQ,CAAC,KAAa;QAC3B,OAAO,IAAI,YAAY,CAAC,GAAG,KAAK,oBAAoB,CAAC,CAAC;IACxD,CAAC;CACF;AAjBD,oCAiBC;AAED,MAAM,OAAO,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;AAEjC;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,cAAc,CAC5B,QAAgB,EAChB,OAAe,EACf,IAAe,EACf,QAA2B;IAE3B,MAAM,KAAK,GAAG,IAAI,oBAAO,CAAC,QAAQ,CAAC,CAAC;IACpC,MAAM,IAAI,GAAG,IAAI,oBAAO,CAAC,OAAO,CAAC,CAAC;IAElC,IAAI,KAAK,CAA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AE,iBAAiB;YAC9B,WAAW,EAAE,gBAAgB;SAC9B,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,OAAO;YACL,WAAW,EAAE,gBAAgB;YAC7B,WAAW,EAAE,iBAAiB;SAC/B,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import Decimal from \"decimal.js\";\nimport { OrderSide } from \"../program/types\";\n\n/**\n * Orderbook decimal configuration\n */\nexport interface OrderbookDecimals {\n orderbookId: string;\n baseDecimals: number;\n quoteDecimals: number;\n priceDecimals: number;\n}\n\n/**\n * Scaled amounts result\n */\nexport interface ScaledAmounts {\n makerAmount: bigint;\n takerAmount: bigint;\n}\n\n/**\n * Error thrown during price/size scaling\n */\nexport class ScalingError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"ScalingError\";\n }\n\n static zeroPriceOrSize(): ScalingError {\n return new ScalingError(\"Price and size must be non-zero\");\n }\n\n static negativeValue(field: string): ScalingError {\n return new ScalingError(`${field} must be positive`);\n }\n\n static overflow(field: string): ScalingError {\n return new ScalingError(`${field} exceeds u64 range`);\n }\n}\n\nconst U64_MAX = (1n << 64n) - 1n;\n\n/**\n * Scale price and size to maker_amount and taker_amount using exact decimal arithmetic.\n *\n * For BID orders (buying base with quote):\n * - maker_amount = price * size * 10^quoteDecimals (what maker gives in quote tokens)\n * - taker_amount = size * 10^baseDecimals (what maker receives in base tokens)\n *\n * For ASK orders (selling base for quote):\n * - maker_amount = size * 10^baseDecimals (what maker gives in base tokens)\n * - taker_amount = price * size * 10^quoteDecimals (what maker receives in quote tokens)\n *\n * @param priceStr - Price as a decimal string (e.g., \"0.75\")\n * @param sizeStr - Size as a decimal string (e.g., \"100\")\n * @param side - Order side (BID or ASK)\n * @param decimals - Orderbook decimal configuration\n * @returns Scaled maker_amount and taker_amount as bigints\n */\nexport function scalePriceSize(\n priceStr: string,\n sizeStr: string,\n side: OrderSide,\n decimals: OrderbookDecimals\n): ScaledAmounts {\n const price = new Decimal(priceStr);\n const size = new Decimal(sizeStr);\n\n if (price.isZero() || size.isZero()) {\n throw ScalingError.zeroPriceOrSize();\n }\n if (price.isNegative()) {\n throw ScalingError.negativeValue(\"price\");\n }\n if (size.isNegative()) {\n throw ScalingError.negativeValue(\"size\");\n }\n\n const baseScale = new Decimal(10).pow(decimals.baseDecimals);\n const quoteScale = new Decimal(10).pow(decimals.quoteDecimals);\n\n const baseAmount = size.mul(baseScale).floor();\n const quoteAmount = price.mul(size).mul(quoteScale).floor();\n\n const baseAmountBigInt = BigInt(baseAmount.toFixed(0));\n const quoteAmountBigInt = BigInt(quoteAmount.toFixed(0));\n\n if (baseAmountBigInt > U64_MAX) {\n throw ScalingError.overflow(\"base amount\");\n }\n if (quoteAmountBigInt > U64_MAX) {\n throw ScalingError.overflow(\"quote amount\");\n }\n\n if (side === OrderSide.BID) {\n return {\n makerAmount: quoteAmountBigInt,\n takerAmount: baseAmountBigInt,\n };\n } else {\n return {\n makerAmount: baseAmountBigInt,\n takerAmount: quoteAmountBigInt,\n };\n }\n}\n"]}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Price history candle resolution.
3
+ * Used by both REST API and WebSocket for price history queries.
4
+ */
5
+ export declare enum Resolution {
6
+ /** 1 minute candles */
7
+ OneMinute = "1m",
8
+ /** 5 minute candles */
9
+ FiveMinutes = "5m",
10
+ /** 15 minute candles */
11
+ FifteenMinutes = "15m",
12
+ /** 1 hour candles */
13
+ OneHour = "1h",
14
+ /** 4 hour candles */
15
+ FourHours = "4h",
16
+ /** 1 day candles */
17
+ OneDay = "1d"
18
+ }
19
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,oBAAY,UAAU;IACpB,uBAAuB;IACvB,SAAS,OAAO;IAChB,uBAAuB;IACvB,WAAW,OAAO;IAClB,wBAAwB;IACxB,cAAc,QAAQ;IACtB,qBAAqB;IACrB,OAAO,OAAO;IACd,qBAAqB;IACrB,SAAS,OAAO;IAChB,oBAAoB;IACpB,MAAM,OAAO;CACd"}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Resolution = void 0;
4
+ /**
5
+ * Price history candle resolution.
6
+ * Used by both REST API and WebSocket for price history queries.
7
+ */
8
+ var Resolution;
9
+ (function (Resolution) {
10
+ /** 1 minute candles */
11
+ Resolution["OneMinute"] = "1m";
12
+ /** 5 minute candles */
13
+ Resolution["FiveMinutes"] = "5m";
14
+ /** 15 minute candles */
15
+ Resolution["FifteenMinutes"] = "15m";
16
+ /** 1 hour candles */
17
+ Resolution["OneHour"] = "1h";
18
+ /** 4 hour candles */
19
+ Resolution["FourHours"] = "4h";
20
+ /** 1 day candles */
21
+ Resolution["OneDay"] = "1d";
22
+ })(Resolution || (exports.Resolution = Resolution = {}));
23
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,IAAY,UAaX;AAbD,WAAY,UAAU;IACpB,uBAAuB;IACvB,8BAAgB,CAAA;IAChB,uBAAuB;IACvB,gCAAkB,CAAA;IAClB,wBAAwB;IACxB,oCAAsB,CAAA;IACtB,qBAAqB;IACrB,4BAAc,CAAA;IACd,qBAAqB;IACrB,8BAAgB,CAAA;IAChB,oBAAoB;IACpB,2BAAa,CAAA;AACf,CAAC,EAbW,UAAU,0BAAV,UAAU,QAarB","sourcesContent":["/**\n * Price history candle resolution.\n * Used by both REST API and WebSocket for price history queries.\n */\nexport enum Resolution {\n /** 1 minute candles */\n OneMinute = \"1m\",\n /** 5 minute candles */\n FiveMinutes = \"5m\",\n /** 15 minute candles */\n FifteenMinutes = \"15m\",\n /** 1 hour candles */\n OneHour = \"1h\",\n /** 4 hour candles */\n FourHours = \"4h\",\n /** 1 day candles */\n OneDay = \"1d\",\n}\n"]}
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Main WebSocket client implementation.
3
+ *
4
+ * Provides a WebSocket client for real-time data streaming.
5
+ */
6
+ import { Keypair } from "@solana/web3.js";
7
+ import type { LocalOrderbook, UserState, PriceHistory } from "./state";
8
+ import type { WsEvent } from "./types";
9
+ import { type AuthCredentials } from "../auth";
10
+ export { DEFAULT_WS_URL } from "../network";
11
+ /**
12
+ * WebSocket client configuration.
13
+ */
14
+ export interface WebSocketConfig {
15
+ /** Number of reconnect attempts before giving up */
16
+ reconnectAttempts?: number;
17
+ /** Base delay for exponential backoff (ms) */
18
+ baseDelayMs?: number;
19
+ /** Maximum delay for exponential backoff (ms) */
20
+ maxDelayMs?: number;
21
+ /** Interval for client ping (seconds) */
22
+ pingIntervalSecs?: number;
23
+ /** Timeout for pong response (seconds) - if no pong received within this time, reconnect */
24
+ pongTimeoutSecs?: number;
25
+ /** Whether to automatically reconnect on disconnect */
26
+ autoReconnect?: boolean;
27
+ /** Whether to automatically re-subscribe after reconnect */
28
+ autoResubscribe?: boolean;
29
+ /** Optional authentication token for private user streams */
30
+ authToken?: string;
31
+ /** Connection timeout in milliseconds (default: 30000) */
32
+ connectionTimeoutMs?: number;
33
+ }
34
+ /**
35
+ * Connection state.
36
+ */
37
+ export type ConnectionState = "Disconnected" | "Connecting" | "Connected" | "Reconnecting" | "Disconnecting";
38
+ /**
39
+ * Event callback type.
40
+ */
41
+ export type EventCallback = (event: WsEvent) => void | Promise<void>;
42
+ /**
43
+ * Main WebSocket client for Lightcone.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * import { LightconeWebSocketClient } from "@lightcone/sdk/websocket";
48
+ *
49
+ * const client = new LightconeWebSocketClient();
50
+ * await client.connect();
51
+ *
52
+ * client.on((event) => {
53
+ * if (event.type === "BookUpdate") {
54
+ * const book = client.getOrderbook(event.orderbookId);
55
+ * console.log("Best bid:", book?.bestBid());
56
+ * }
57
+ * });
58
+ *
59
+ * await client.subscribeBookUpdates(["market1:ob1"]);
60
+ * ```
61
+ */
62
+ export declare class LightconeWebSocketClient {
63
+ private url;
64
+ private config;
65
+ private state;
66
+ private ws;
67
+ private subscriptions;
68
+ private handler;
69
+ private reconnectAttempt;
70
+ private pingInterval;
71
+ private eventCallbacks;
72
+ private authCredentials?;
73
+ private subscribedUser;
74
+ private lastPong;
75
+ private awaitingPong;
76
+ private isReconnecting;
77
+ constructor(url?: string, config?: WebSocketConfig);
78
+ /**
79
+ * Create a client connected to the default URL.
80
+ */
81
+ static connectDefault(): Promise<LightconeWebSocketClient>;
82
+ /**
83
+ * Create a client connected to a custom URL.
84
+ */
85
+ static connect(url: string, config?: WebSocketConfig): Promise<LightconeWebSocketClient>;
86
+ /**
87
+ * Create an authenticated client connected to the default URL.
88
+ */
89
+ static connectAuthenticated(keypair: Keypair): Promise<LightconeWebSocketClient>;
90
+ /**
91
+ * Create an authenticated client with custom config.
92
+ */
93
+ static connectAuthenticatedWithConfig(keypair: Keypair, config: WebSocketConfig): Promise<LightconeWebSocketClient>;
94
+ /**
95
+ * Connect to the WebSocket server.
96
+ */
97
+ connect(): Promise<void>;
98
+ /**
99
+ * Establish the WebSocket connection.
100
+ */
101
+ private establishConnection;
102
+ /**
103
+ * Attempt to reconnect.
104
+ */
105
+ private attemptReconnect;
106
+ /**
107
+ * Start the ping interval.
108
+ */
109
+ private startPingInterval;
110
+ /**
111
+ * Stop the ping interval.
112
+ */
113
+ private stopPingInterval;
114
+ /**
115
+ * Sleep for a given number of milliseconds.
116
+ */
117
+ private sleep;
118
+ /**
119
+ * Send a WebSocket message.
120
+ */
121
+ private send;
122
+ /**
123
+ * Emit an event to all callbacks.
124
+ */
125
+ private emitEvent;
126
+ /**
127
+ * Subscribe to orderbook updates.
128
+ */
129
+ subscribeBookUpdates(orderbookIds: string[]): void;
130
+ /**
131
+ * Subscribe to trade executions.
132
+ */
133
+ subscribeTrades(orderbookIds: string[]): void;
134
+ /**
135
+ * Subscribe to user events.
136
+ */
137
+ subscribeUser(user: string): void;
138
+ /**
139
+ * Subscribe to price history.
140
+ */
141
+ subscribePriceHistory(orderbookId: string, resolution: string, includeOhlcv: boolean): void;
142
+ /**
143
+ * Subscribe to market events.
144
+ */
145
+ subscribeMarket(marketPubkey: string): void;
146
+ /**
147
+ * Unsubscribe from orderbook updates.
148
+ */
149
+ unsubscribeBookUpdates(orderbookIds: string[]): void;
150
+ /**
151
+ * Unsubscribe from trades.
152
+ */
153
+ unsubscribeTrades(orderbookIds: string[]): void;
154
+ /**
155
+ * Unsubscribe from user events.
156
+ */
157
+ unsubscribeUser(user: string): void;
158
+ /**
159
+ * Unsubscribe from price history.
160
+ */
161
+ unsubscribePriceHistory(orderbookId: string, resolution: string): void;
162
+ /**
163
+ * Unsubscribe from market events.
164
+ */
165
+ unsubscribeMarket(marketPubkey: string): void;
166
+ /**
167
+ * Send a ping request.
168
+ */
169
+ ping(): void;
170
+ /**
171
+ * Disconnect from the server.
172
+ * Returns a Promise that resolves when the connection is fully closed.
173
+ */
174
+ disconnect(): Promise<void>;
175
+ /**
176
+ * Check if connected.
177
+ */
178
+ isConnected(): boolean;
179
+ /**
180
+ * Check if authenticated.
181
+ */
182
+ isAuthenticated(): boolean;
183
+ /**
184
+ * Check if the WebSocket connection is healthy.
185
+ */
186
+ isHealthy(): boolean;
187
+ /**
188
+ * Get detailed connection status.
189
+ */
190
+ getConnectionStatus(): {
191
+ state: ConnectionState;
192
+ isHealthy: boolean;
193
+ lastPongMs: number;
194
+ awaitingPong: boolean;
195
+ wsReadyState: number | null;
196
+ };
197
+ /**
198
+ * Get the current connection state.
199
+ */
200
+ connectionState(): ConnectionState;
201
+ /**
202
+ * Get orderbook state.
203
+ */
204
+ getOrderbook(orderbookId: string): LocalOrderbook | undefined;
205
+ /**
206
+ * Get user state.
207
+ */
208
+ getUserState(user: string): UserState | undefined;
209
+ /**
210
+ * Get price history state.
211
+ */
212
+ getPriceHistory(orderbookId: string, resolution: string): PriceHistory | undefined;
213
+ /**
214
+ * Get auth credentials (if authenticated).
215
+ */
216
+ getAuthCredentials(): AuthCredentials | undefined;
217
+ /**
218
+ * Get user public key (if authenticated).
219
+ */
220
+ userPubkey(): string | undefined;
221
+ /**
222
+ * Register an event callback.
223
+ */
224
+ on(callback: EventCallback): void;
225
+ /**
226
+ * Remove an event callback.
227
+ */
228
+ off(callback: EventCallback): void;
229
+ /**
230
+ * Get the WebSocket URL.
231
+ */
232
+ getUrl(): string;
233
+ /**
234
+ * Get the configuration.
235
+ */
236
+ getConfig(): Required<WebSocketConfig>;
237
+ }
238
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/websocket/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAI1C,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvE,OAAO,KAAK,EAAa,OAAO,EAAE,MAAM,SAAS,CAAC;AAWlD,OAAO,EAAgB,KAAK,eAAe,EAAE,MAAM,SAAS,CAAC;AAG7D,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,oDAAoD;IACpD,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,8CAA8C;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iDAAiD;IACjD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,yCAAyC;IACzC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,4FAA4F;IAC5F,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,uDAAuD;IACvD,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,4DAA4D;IAC5D,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,6DAA6D;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,0DAA0D;IA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