@andy-liquid-labs/lighter-ts-sdk 1.0.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 (85) hide show
  1. package/README.md +381 -0
  2. package/dist/index.d.mts +3686 -0
  3. package/dist/index.d.ts +3686 -0
  4. package/dist/index.js +6168 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +5939 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +57 -0
  9. package/src/api/exchange/change-account-tier.ts +38 -0
  10. package/src/api/exchange/fastwithdraw.ts +38 -0
  11. package/src/api/exchange/index.ts +7 -0
  12. package/src/api/exchange/notification-ack.ts +36 -0
  13. package/src/api/exchange/send-tx-batch.ts +53 -0
  14. package/src/api/exchange/send-tx.ts +105 -0
  15. package/src/api/exchange/tokens-create.ts +50 -0
  16. package/src/api/exchange/tokens-revoke.ts +38 -0
  17. package/src/api/index.ts +3 -0
  18. package/src/api/info/account-by-l1-address.ts +23 -0
  19. package/src/api/info/account.ts +80 -0
  20. package/src/api/info/announcement.ts +24 -0
  21. package/src/api/info/api-keys.ts +32 -0
  22. package/src/api/info/asset-details.ts +42 -0
  23. package/src/api/info/candles.ts +31 -0
  24. package/src/api/info/exchange-stats.ts +16 -0
  25. package/src/api/info/fastbridge-info.ts +15 -0
  26. package/src/api/info/funding-rates.ts +22 -0
  27. package/src/api/info/fundings.ts +29 -0
  28. package/src/api/info/index.ts +20 -0
  29. package/src/api/info/next-nonce.ts +26 -0
  30. package/src/api/info/order-book-details.ts +125 -0
  31. package/src/api/info/order-books.ts +23 -0
  32. package/src/api/info/recent-trades.ts +24 -0
  33. package/src/api/info/root-info.ts +13 -0
  34. package/src/api/info/root-status.ts +13 -0
  35. package/src/api/info/tx-from-l1-hash.ts +20 -0
  36. package/src/api/info/tx.ts +45 -0
  37. package/src/api/info/txs.ts +19 -0
  38. package/src/api/info/withdrawal-delay.ts +13 -0
  39. package/src/api/info-private/account-active-orders.ts +31 -0
  40. package/src/api/info-private/account-inactive-orders.ts +83 -0
  41. package/src/api/info-private/account-limits.ts +35 -0
  42. package/src/api/info-private/account-metadata.ts +43 -0
  43. package/src/api/info-private/deposit-history.ts +49 -0
  44. package/src/api/info-private/export.ts +41 -0
  45. package/src/api/info-private/fastwithdraw-info.ts +35 -0
  46. package/src/api/info-private/index.ts +18 -0
  47. package/src/api/info-private/l1-metadata.ts +35 -0
  48. package/src/api/info-private/liquidations.ts +96 -0
  49. package/src/api/info-private/pnl.ts +52 -0
  50. package/src/api/info-private/position-funding.ts +54 -0
  51. package/src/api/info-private/public-pools-metadata.ts +46 -0
  52. package/src/api/info-private/referral-points.ts +43 -0
  53. package/src/api/info-private/tokens.ts +44 -0
  54. package/src/api/info-private/trades.ts +66 -0
  55. package/src/api/info-private/transfer-fee-info.ts +37 -0
  56. package/src/api/info-private/transfer-history.ts +54 -0
  57. package/src/api/info-private/withdraw-history.ts +49 -0
  58. package/src/client/clientManager.ts +121 -0
  59. package/src/client/exchange-client.ts +637 -0
  60. package/src/client/http/client.ts +137 -0
  61. package/src/client/http/index.ts +6 -0
  62. package/src/client/http/interface.ts +89 -0
  63. package/src/client/index.ts +11 -0
  64. package/src/client/info-client.ts +383 -0
  65. package/src/client/info-private-client.ts +444 -0
  66. package/src/client/txClient.ts +597 -0
  67. package/src/client/ws-client.ts +457 -0
  68. package/src/crypto/ecgfp5.ts +722 -0
  69. package/src/crypto/goldilocks.ts +136 -0
  70. package/src/crypto/gorand.ts +777 -0
  71. package/src/crypto/index.ts +6 -0
  72. package/src/crypto/poseidon2.ts +365 -0
  73. package/src/crypto/scalar.ts +375 -0
  74. package/src/index.ts +112 -0
  75. package/src/signer/index.ts +5 -0
  76. package/src/signer/keyManager.ts +132 -0
  77. package/src/types/bridge.ts +24 -0
  78. package/src/types/constants.ts +252 -0
  79. package/src/types/errors.ts +168 -0
  80. package/src/types/index.ts +12 -0
  81. package/src/types/requests.ts +197 -0
  82. package/src/types/txInfo.ts +1277 -0
  83. package/src/types/txInfoPools.ts +502 -0
  84. package/src/types/txInfoSerializer.ts +348 -0
  85. package/src/types/ws.ts +407 -0
@@ -0,0 +1,348 @@
1
+ /**
2
+ * Transaction Info Serializer
3
+ *
4
+ * Converts TypeScript TxInfo objects to JSON format compatible with the Lighter API.
5
+ * The API expects PascalCase field names matching the Go struct definitions.
6
+ */
7
+
8
+ import * as types from "./index";
9
+
10
+ /**
11
+ * Convert Uint8Array to base64 string (Go json.Marshal format for []byte)
12
+ */
13
+ function uint8ArrayToBase64(bytes: Uint8Array): string {
14
+ if (typeof Buffer !== "undefined") {
15
+ return Buffer.from(bytes).toString("base64");
16
+ }
17
+ // Browser fallback
18
+ let binary = "";
19
+ for (let i = 0; i < bytes.length; i++) {
20
+ binary += String.fromCharCode(bytes[i]);
21
+ }
22
+ return btoa(binary);
23
+ }
24
+
25
+ /**
26
+ * Serialize L2CreateOrderTxInfo to JSON
27
+ */
28
+ export function serializeCreateOrder(
29
+ txInfo: types.L2CreateOrderTxInfo,
30
+ ): string {
31
+ return JSON.stringify({
32
+ AccountIndex: Number(txInfo.accountIndex),
33
+ ApiKeyIndex: txInfo.apiKeyIndex,
34
+ MarketIndex: txInfo.orderInfo.marketIndex,
35
+ ClientOrderIndex: Number(txInfo.orderInfo.clientOrderIndex),
36
+ BaseAmount: Number(txInfo.orderInfo.baseAmount),
37
+ Price: txInfo.orderInfo.price,
38
+ IsAsk: txInfo.orderInfo.isAsk,
39
+ Type: txInfo.orderInfo.type,
40
+ TimeInForce: txInfo.orderInfo.timeInForce,
41
+ ReduceOnly: txInfo.orderInfo.reduceOnly,
42
+ TriggerPrice: txInfo.orderInfo.triggerPrice,
43
+ OrderExpiry: Number(txInfo.orderInfo.orderExpiry),
44
+ ExpiredAt: Number(txInfo.expiredAt),
45
+ Nonce: Number(txInfo.nonce),
46
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
47
+ });
48
+ }
49
+
50
+ /**
51
+ * Serialize L2CancelOrderTxInfo to JSON
52
+ */
53
+ export function serializeCancelOrder(
54
+ txInfo: types.L2CancelOrderTxInfo,
55
+ ): string {
56
+ return JSON.stringify({
57
+ AccountIndex: Number(txInfo.accountIndex),
58
+ ApiKeyIndex: txInfo.apiKeyIndex,
59
+ MarketIndex: txInfo.marketIndex,
60
+ Index: Number(txInfo.index),
61
+ ExpiredAt: Number(txInfo.expiredAt),
62
+ Nonce: Number(txInfo.nonce),
63
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
64
+ });
65
+ }
66
+
67
+ /**
68
+ * Serialize L2ModifyOrderTxInfo to JSON
69
+ */
70
+ export function serializeModifyOrder(
71
+ txInfo: types.L2ModifyOrderTxInfo,
72
+ ): string {
73
+ return JSON.stringify({
74
+ AccountIndex: Number(txInfo.accountIndex),
75
+ ApiKeyIndex: txInfo.apiKeyIndex,
76
+ MarketIndex: txInfo.marketIndex,
77
+ Index: Number(txInfo.index),
78
+ BaseAmount: Number(txInfo.baseAmount),
79
+ Price: txInfo.price,
80
+ TriggerPrice: txInfo.triggerPrice,
81
+ ExpiredAt: Number(txInfo.expiredAt),
82
+ Nonce: Number(txInfo.nonce),
83
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
84
+ });
85
+ }
86
+
87
+ /**
88
+ * Serialize L2CancelAllOrdersTxInfo to JSON
89
+ */
90
+ export function serializeCancelAllOrders(
91
+ txInfo: types.L2CancelAllOrdersTxInfo,
92
+ ): string {
93
+ return JSON.stringify({
94
+ AccountIndex: Number(txInfo.accountIndex),
95
+ ApiKeyIndex: txInfo.apiKeyIndex,
96
+ TimeInForce: txInfo.timeInForce,
97
+ Time: Number(txInfo.time),
98
+ ExpiredAt: Number(txInfo.expiredAt),
99
+ Nonce: Number(txInfo.nonce),
100
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
101
+ });
102
+ }
103
+
104
+ /**
105
+ * Serialize L2WithdrawTxInfo to JSON
106
+ */
107
+ export function serializeWithdraw(txInfo: types.L2WithdrawTxInfo): string {
108
+ return JSON.stringify({
109
+ FromAccountIndex: Number(txInfo.fromAccountIndex),
110
+ ApiKeyIndex: txInfo.apiKeyIndex,
111
+ AssetIndex: txInfo.assetIndex,
112
+ RouteType: txInfo.routeType,
113
+ Amount: Number(txInfo.amount),
114
+ ExpiredAt: Number(txInfo.expiredAt),
115
+ Nonce: Number(txInfo.nonce),
116
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Serialize L2TransferTxInfo to JSON
122
+ */
123
+ export function serializeTransfer(txInfo: types.L2TransferTxInfo): string {
124
+ // Convert memo bytes to string if it's a Uint8Array
125
+
126
+ return JSON.stringify({
127
+ FromAccountIndex: Number(txInfo.fromAccountIndex),
128
+ ApiKeyIndex: txInfo.apiKeyIndex,
129
+ ToAccountIndex: Number(txInfo.toAccountIndex),
130
+ AssetIndex: txInfo.assetIndex,
131
+ FromRouteType: txInfo.fromRouteType,
132
+ ToRouteType: txInfo.toRouteType,
133
+ Amount: Number(txInfo.amount),
134
+ USDCFee: Number(txInfo.usdcFee),
135
+ Memo: Array.from(txInfo.memo),
136
+ ExpiredAt: Number(txInfo.expiredAt),
137
+ Nonce: Number(txInfo.nonce),
138
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
139
+ });
140
+ }
141
+
142
+ /**
143
+ * Serialize L2ChangePubKeyTxInfo to JSON
144
+ */
145
+ export function serializeChangePubKey(
146
+ txInfo: types.L2ChangePubKeyTxInfo,
147
+ ): string {
148
+ // Convert pubKey bytes to hex string
149
+ const pubKeyHex = Array.from(txInfo.pubKey)
150
+ .map((b) => b.toString(16).padStart(2, "0"))
151
+ .join("");
152
+
153
+ return JSON.stringify({
154
+ AccountIndex: Number(txInfo.accountIndex),
155
+ ApiKeyIndex: txInfo.apiKeyIndex,
156
+ PubKey: pubKeyHex,
157
+ ExpiredAt: Number(txInfo.expiredAt),
158
+ Nonce: Number(txInfo.nonce),
159
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
160
+ });
161
+ }
162
+
163
+ /**
164
+ * Serialize L2CreateSubAccountTxInfo to JSON
165
+ */
166
+ export function serializeCreateSubAccount(
167
+ txInfo: types.L2CreateSubAccountTxInfo,
168
+ ): string {
169
+ return JSON.stringify({
170
+ AccountIndex: Number(txInfo.accountIndex),
171
+ ApiKeyIndex: txInfo.apiKeyIndex,
172
+ ExpiredAt: Number(txInfo.expiredAt),
173
+ Nonce: Number(txInfo.nonce),
174
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
175
+ });
176
+ }
177
+
178
+ /**
179
+ * Serialize L2UpdateLeverageTxInfo to JSON
180
+ */
181
+ export function serializeUpdateLeverage(
182
+ txInfo: types.L2UpdateLeverageTxInfo,
183
+ ): string {
184
+ return JSON.stringify({
185
+ AccountIndex: Number(txInfo.accountIndex),
186
+ ApiKeyIndex: txInfo.apiKeyIndex,
187
+ MarketIndex: txInfo.marketIndex,
188
+ InitialMarginFraction: txInfo.initialMarginFraction,
189
+ MarginMode: txInfo.marginMode,
190
+ ExpiredAt: Number(txInfo.expiredAt),
191
+ Nonce: Number(txInfo.nonce),
192
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
193
+ });
194
+ }
195
+
196
+ /**
197
+ * Serialize L2UpdateMarginTxInfo to JSON
198
+ */
199
+ export function serializeUpdateMargin(
200
+ txInfo: types.L2UpdateMarginTxInfo,
201
+ ): string {
202
+ return JSON.stringify({
203
+ AccountIndex: Number(txInfo.accountIndex),
204
+ ApiKeyIndex: txInfo.apiKeyIndex,
205
+ MarketIndex: txInfo.marketIndex,
206
+ UsdcAmount: Number(txInfo.usdcAmount),
207
+ Direction: txInfo.direction,
208
+ ExpiredAt: Number(txInfo.expiredAt),
209
+ Nonce: Number(txInfo.nonce),
210
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
211
+ });
212
+ }
213
+
214
+ /**
215
+ * Serialize L2CreatePublicPoolTxInfo to JSON
216
+ */
217
+ export function serializeCreatePublicPool(
218
+ txInfo: types.L2CreatePublicPoolTxInfo,
219
+ ): string {
220
+ return JSON.stringify({
221
+ AccountIndex: Number(txInfo.accountIndex),
222
+ ApiKeyIndex: txInfo.apiKeyIndex,
223
+ OperatorFee: Number(txInfo.operatorFee),
224
+ InitialTotalShares: Number(txInfo.initialTotalShares),
225
+ MinOperatorShareRate: txInfo.minOperatorShareRate,
226
+ ExpiredAt: Number(txInfo.expiredAt),
227
+ Nonce: Number(txInfo.nonce),
228
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
229
+ });
230
+ }
231
+
232
+ /**
233
+ * Serialize L2UpdatePublicPoolTxInfo to JSON
234
+ */
235
+ export function serializeUpdatePublicPool(
236
+ txInfo: types.L2UpdatePublicPoolTxInfo,
237
+ ): string {
238
+ return JSON.stringify({
239
+ AccountIndex: Number(txInfo.accountIndex),
240
+ ApiKeyIndex: txInfo.apiKeyIndex,
241
+ PublicPoolIndex: Number(txInfo.publicPoolIndex),
242
+ Status: txInfo.status,
243
+ OperatorFee: Number(txInfo.operatorFee),
244
+ MinOperatorShareRate: txInfo.minOperatorShareRate,
245
+ ExpiredAt: Number(txInfo.expiredAt),
246
+ Nonce: Number(txInfo.nonce),
247
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
248
+ });
249
+ }
250
+
251
+ /**
252
+ * Serialize L2MintSharesTxInfo to JSON
253
+ */
254
+ export function serializeMintShares(txInfo: types.L2MintSharesTxInfo): string {
255
+ return JSON.stringify({
256
+ AccountIndex: Number(txInfo.accountIndex),
257
+ ApiKeyIndex: txInfo.apiKeyIndex,
258
+ PublicPoolIndex: Number(txInfo.publicPoolIndex),
259
+ ShareAmount: Number(txInfo.shareAmount),
260
+ ExpiredAt: Number(txInfo.expiredAt),
261
+ Nonce: Number(txInfo.nonce),
262
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
263
+ });
264
+ }
265
+
266
+ /**
267
+ * Serialize L2BurnSharesTxInfo to JSON
268
+ */
269
+ export function serializeBurnShares(txInfo: types.L2BurnSharesTxInfo): string {
270
+ return JSON.stringify({
271
+ AccountIndex: Number(txInfo.accountIndex),
272
+ ApiKeyIndex: txInfo.apiKeyIndex,
273
+ PublicPoolIndex: Number(txInfo.publicPoolIndex),
274
+ ShareAmount: Number(txInfo.shareAmount),
275
+ ExpiredAt: Number(txInfo.expiredAt),
276
+ Nonce: Number(txInfo.nonce),
277
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
278
+ });
279
+ }
280
+
281
+ /**
282
+ * Serialize L2CreateGroupedOrdersTxInfo to JSON
283
+ */
284
+ export function serializeCreateGroupedOrders(
285
+ txInfo: types.L2CreateGroupedOrdersTxInfo,
286
+ ): string {
287
+ const orders = txInfo.orders.map((order) => ({
288
+ MarketIndex: order.marketIndex,
289
+ ClientOrderIndex: Number(order.clientOrderIndex),
290
+ BaseAmount: Number(order.baseAmount),
291
+ Price: order.price,
292
+ IsAsk: order.isAsk,
293
+ Type: order.type,
294
+ TimeInForce: order.timeInForce,
295
+ ReduceOnly: order.reduceOnly,
296
+ TriggerPrice: order.triggerPrice,
297
+ OrderExpiry: Number(order.orderExpiry),
298
+ }));
299
+
300
+ return JSON.stringify({
301
+ AccountIndex: Number(txInfo.accountIndex),
302
+ ApiKeyIndex: txInfo.apiKeyIndex,
303
+ GroupingType: txInfo.groupingType,
304
+ Orders: orders,
305
+ ExpiredAt: Number(txInfo.expiredAt),
306
+ Nonce: Number(txInfo.nonce),
307
+ Sig: txInfo.signature ? uint8ArrayToBase64(txInfo.signature) : null,
308
+ });
309
+ }
310
+
311
+ /**
312
+ * Generic serializer - determines the type and calls the appropriate serializer
313
+ */
314
+ export function serializeTxInfo(txInfo: types.TxInfo): string {
315
+ if (txInfo instanceof types.L2CreateOrderTxInfo) {
316
+ return serializeCreateOrder(txInfo);
317
+ } else if (txInfo instanceof types.L2CancelOrderTxInfo) {
318
+ return serializeCancelOrder(txInfo);
319
+ } else if (txInfo instanceof types.L2ModifyOrderTxInfo) {
320
+ return serializeModifyOrder(txInfo);
321
+ } else if (txInfo instanceof types.L2CancelAllOrdersTxInfo) {
322
+ return serializeCancelAllOrders(txInfo);
323
+ } else if (txInfo instanceof types.L2WithdrawTxInfo) {
324
+ return serializeWithdraw(txInfo);
325
+ } else if (txInfo instanceof types.L2TransferTxInfo) {
326
+ return serializeTransfer(txInfo);
327
+ } else if (txInfo instanceof types.L2ChangePubKeyTxInfo) {
328
+ return serializeChangePubKey(txInfo);
329
+ } else if (txInfo instanceof types.L2CreateSubAccountTxInfo) {
330
+ return serializeCreateSubAccount(txInfo);
331
+ } else if (txInfo instanceof types.L2UpdateLeverageTxInfo) {
332
+ return serializeUpdateLeverage(txInfo);
333
+ } else if (txInfo instanceof types.L2UpdateMarginTxInfo) {
334
+ return serializeUpdateMargin(txInfo);
335
+ } else if (txInfo instanceof types.L2CreatePublicPoolTxInfo) {
336
+ return serializeCreatePublicPool(txInfo);
337
+ } else if (txInfo instanceof types.L2UpdatePublicPoolTxInfo) {
338
+ return serializeUpdatePublicPool(txInfo);
339
+ } else if (txInfo instanceof types.L2MintSharesTxInfo) {
340
+ return serializeMintShares(txInfo);
341
+ } else if (txInfo instanceof types.L2BurnSharesTxInfo) {
342
+ return serializeBurnShares(txInfo);
343
+ } else if (txInfo instanceof types.L2CreateGroupedOrdersTxInfo) {
344
+ return serializeCreateGroupedOrders(txInfo);
345
+ }
346
+
347
+ throw new Error(`Unknown TxInfo type: ${txInfo.constructor.name}`);
348
+ }