@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.
- package/README.md +381 -0
- package/dist/index.d.mts +3686 -0
- package/dist/index.d.ts +3686 -0
- package/dist/index.js +6168 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +5939 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +57 -0
- package/src/api/exchange/change-account-tier.ts +38 -0
- package/src/api/exchange/fastwithdraw.ts +38 -0
- package/src/api/exchange/index.ts +7 -0
- package/src/api/exchange/notification-ack.ts +36 -0
- package/src/api/exchange/send-tx-batch.ts +53 -0
- package/src/api/exchange/send-tx.ts +105 -0
- package/src/api/exchange/tokens-create.ts +50 -0
- package/src/api/exchange/tokens-revoke.ts +38 -0
- package/src/api/index.ts +3 -0
- package/src/api/info/account-by-l1-address.ts +23 -0
- package/src/api/info/account.ts +80 -0
- package/src/api/info/announcement.ts +24 -0
- package/src/api/info/api-keys.ts +32 -0
- package/src/api/info/asset-details.ts +42 -0
- package/src/api/info/candles.ts +31 -0
- package/src/api/info/exchange-stats.ts +16 -0
- package/src/api/info/fastbridge-info.ts +15 -0
- package/src/api/info/funding-rates.ts +22 -0
- package/src/api/info/fundings.ts +29 -0
- package/src/api/info/index.ts +20 -0
- package/src/api/info/next-nonce.ts +26 -0
- package/src/api/info/order-book-details.ts +125 -0
- package/src/api/info/order-books.ts +23 -0
- package/src/api/info/recent-trades.ts +24 -0
- package/src/api/info/root-info.ts +13 -0
- package/src/api/info/root-status.ts +13 -0
- package/src/api/info/tx-from-l1-hash.ts +20 -0
- package/src/api/info/tx.ts +45 -0
- package/src/api/info/txs.ts +19 -0
- package/src/api/info/withdrawal-delay.ts +13 -0
- package/src/api/info-private/account-active-orders.ts +31 -0
- package/src/api/info-private/account-inactive-orders.ts +83 -0
- package/src/api/info-private/account-limits.ts +35 -0
- package/src/api/info-private/account-metadata.ts +43 -0
- package/src/api/info-private/deposit-history.ts +49 -0
- package/src/api/info-private/export.ts +41 -0
- package/src/api/info-private/fastwithdraw-info.ts +35 -0
- package/src/api/info-private/index.ts +18 -0
- package/src/api/info-private/l1-metadata.ts +35 -0
- package/src/api/info-private/liquidations.ts +96 -0
- package/src/api/info-private/pnl.ts +52 -0
- package/src/api/info-private/position-funding.ts +54 -0
- package/src/api/info-private/public-pools-metadata.ts +46 -0
- package/src/api/info-private/referral-points.ts +43 -0
- package/src/api/info-private/tokens.ts +44 -0
- package/src/api/info-private/trades.ts +66 -0
- package/src/api/info-private/transfer-fee-info.ts +37 -0
- package/src/api/info-private/transfer-history.ts +54 -0
- package/src/api/info-private/withdraw-history.ts +49 -0
- package/src/client/clientManager.ts +121 -0
- package/src/client/exchange-client.ts +637 -0
- package/src/client/http/client.ts +137 -0
- package/src/client/http/index.ts +6 -0
- package/src/client/http/interface.ts +89 -0
- package/src/client/index.ts +11 -0
- package/src/client/info-client.ts +383 -0
- package/src/client/info-private-client.ts +444 -0
- package/src/client/txClient.ts +597 -0
- package/src/client/ws-client.ts +457 -0
- package/src/crypto/ecgfp5.ts +722 -0
- package/src/crypto/goldilocks.ts +136 -0
- package/src/crypto/gorand.ts +777 -0
- package/src/crypto/index.ts +6 -0
- package/src/crypto/poseidon2.ts +365 -0
- package/src/crypto/scalar.ts +375 -0
- package/src/index.ts +112 -0
- package/src/signer/index.ts +5 -0
- package/src/signer/keyManager.ts +132 -0
- package/src/types/bridge.ts +24 -0
- package/src/types/constants.ts +252 -0
- package/src/types/errors.ts +168 -0
- package/src/types/index.ts +12 -0
- package/src/types/requests.ts +197 -0
- package/src/types/txInfo.ts +1277 -0
- package/src/types/txInfoPools.ts +502 -0
- package/src/types/txInfoSerializer.ts +348 -0
- package/src/types/ws.ts +407 -0
package/README.md
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
# lighter-ts-sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for Lighter - signing & hashing of Lighter transactions, plus HTTP and WebSocket clients.
|
|
4
|
+
|
|
5
|
+
This is the TypeScript equivalent of the [lighter-go](https://github.com/elliottech/lighter-go) SDK.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install lighter-ts-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import {
|
|
17
|
+
createClient,
|
|
18
|
+
generateApiKey,
|
|
19
|
+
newHttpClient,
|
|
20
|
+
InfoClient,
|
|
21
|
+
ExchangeClient,
|
|
22
|
+
OrderType,
|
|
23
|
+
TimeInForce,
|
|
24
|
+
} from 'lighter-ts-sdk';
|
|
25
|
+
|
|
26
|
+
// Generate a new API key pair (optionally with a seed for deterministic generation)
|
|
27
|
+
const [privateKey, publicKey] = generateApiKey();
|
|
28
|
+
// Or with a seed:
|
|
29
|
+
// const [privateKey, publicKey] = generateApiKey('my-seed-string');
|
|
30
|
+
console.log('Private Key:', privateKey);
|
|
31
|
+
console.log('Public Key:', publicKey);
|
|
32
|
+
|
|
33
|
+
// Create an HTTP client (for automatic nonce fetching)
|
|
34
|
+
const httpClient = newHttpClient('https://mainnet.zklighter.elliot.ai');
|
|
35
|
+
|
|
36
|
+
// Create a TxClient for signing
|
|
37
|
+
const txClient = createClient(
|
|
38
|
+
httpClient,
|
|
39
|
+
privateKey,
|
|
40
|
+
1, // chainId
|
|
41
|
+
0, // apiKeyIndex
|
|
42
|
+
1n // accountIndex
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// Use ExchangeClient to create and send orders
|
|
46
|
+
const exchangeClient = new ExchangeClient({
|
|
47
|
+
baseURL: 'https://mainnet.zklighter.elliot.ai',
|
|
48
|
+
txClient,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Create and send an order
|
|
52
|
+
const result = await exchangeClient.createOrder({
|
|
53
|
+
marketIndex: 0,
|
|
54
|
+
clientOrderIndex: 1n,
|
|
55
|
+
baseAmount: 1000000n,
|
|
56
|
+
price: 50000,
|
|
57
|
+
isAsk: 0,
|
|
58
|
+
orderType: OrderType.Limit,
|
|
59
|
+
timeInForce: TimeInForce.GoodTillTime,
|
|
60
|
+
reduceOnly: 0,
|
|
61
|
+
triggerPrice: 0,
|
|
62
|
+
orderExpiry: BigInt(Date.now() + 3600000), // 1 hour
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
console.log('Transaction hash:', result.tx_hash);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Clients
|
|
69
|
+
|
|
70
|
+
The SDK provides several client classes for different purposes:
|
|
71
|
+
|
|
72
|
+
### InfoClient - Public API
|
|
73
|
+
|
|
74
|
+
Access public market data without authentication:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { InfoClient } from 'lighter-ts-sdk';
|
|
78
|
+
|
|
79
|
+
const infoClient = new InfoClient({
|
|
80
|
+
baseURL: 'https://mainnet.zklighter.elliot.ai',
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Get order books
|
|
84
|
+
const orderBooks = await infoClient.getOrderBooks();
|
|
85
|
+
|
|
86
|
+
// Get account info
|
|
87
|
+
const account = await infoClient.getAccount({ l1_address: '0x...' });
|
|
88
|
+
|
|
89
|
+
// Get candles
|
|
90
|
+
const candles = await infoClient.getCandles({
|
|
91
|
+
market_id: 0,
|
|
92
|
+
resolution: '1h',
|
|
93
|
+
start_time: Date.now() - 86400000,
|
|
94
|
+
end_time: Date.now(),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Get recent trades
|
|
98
|
+
const trades = await infoClient.getRecentTrades({ market_id: 0 });
|
|
99
|
+
|
|
100
|
+
// Get funding rates
|
|
101
|
+
const fundingRates = await infoClient.getFundingRates({ market_id: 0 });
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### InfoPrivateClient - Authenticated API
|
|
105
|
+
|
|
106
|
+
Access private account data with authentication:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { InfoPrivateClient, createClient, newHttpClient } from 'lighter-ts-sdk';
|
|
110
|
+
|
|
111
|
+
const httpClient = newHttpClient('https://mainnet.zklighter.elliot.ai');
|
|
112
|
+
const txClient = createClient(httpClient, privateKey, 1, 0, 1n);
|
|
113
|
+
|
|
114
|
+
const privateClient = new InfoPrivateClient({
|
|
115
|
+
baseURL: 'https://mainnet.zklighter.elliot.ai',
|
|
116
|
+
txClient,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Get active orders
|
|
120
|
+
const orders = await privateClient.getAccountActiveOrders({ account_index: 1 });
|
|
121
|
+
|
|
122
|
+
// Get trade history
|
|
123
|
+
const trades = await privateClient.getTrades({
|
|
124
|
+
account_index: 1,
|
|
125
|
+
start_time: Date.now() - 86400000,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Get PnL
|
|
129
|
+
const pnl = await privateClient.getPnl({ account_index: 1 });
|
|
130
|
+
|
|
131
|
+
// Get deposit history
|
|
132
|
+
const deposits = await privateClient.getDepositHistory({ account_index: 1 });
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### ExchangeClient - Transactions
|
|
136
|
+
|
|
137
|
+
Sign and send transactions:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { ExchangeClient, createClient, newHttpClient } from 'lighter-ts-sdk';
|
|
141
|
+
|
|
142
|
+
const httpClient = newHttpClient('https://mainnet.zklighter.elliot.ai');
|
|
143
|
+
const txClient = createClient(httpClient, privateKey, 1, 0, 1n);
|
|
144
|
+
|
|
145
|
+
const exchangeClient = new ExchangeClient({
|
|
146
|
+
baseURL: 'https://mainnet.zklighter.elliot.ai',
|
|
147
|
+
txClient,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Create order
|
|
151
|
+
const orderResult = await exchangeClient.createOrder({
|
|
152
|
+
marketIndex: 0,
|
|
153
|
+
clientOrderIndex: 1n,
|
|
154
|
+
baseAmount: 1000000n,
|
|
155
|
+
price: 50000,
|
|
156
|
+
isAsk: 0,
|
|
157
|
+
orderType: 1, // Limit
|
|
158
|
+
timeInForce: 1, // GoodTillTime
|
|
159
|
+
reduceOnly: 0,
|
|
160
|
+
triggerPrice: 0,
|
|
161
|
+
orderExpiry: BigInt(Date.now() + 3600000),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Cancel order
|
|
165
|
+
const cancelResult = await exchangeClient.cancelOrder({
|
|
166
|
+
marketIndex: 0,
|
|
167
|
+
orderIndex: 123n,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Cancel all orders
|
|
171
|
+
const cancelAllResult = await exchangeClient.cancelAllOrders({
|
|
172
|
+
timeInForce: 1,
|
|
173
|
+
time: BigInt(Date.now()),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Withdraw
|
|
177
|
+
const withdrawResult = await exchangeClient.withdraw({
|
|
178
|
+
assetIndex: 0,
|
|
179
|
+
routeType: 0,
|
|
180
|
+
amount: 1000000n,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Transfer
|
|
184
|
+
const transferResult = await exchangeClient.transfer({
|
|
185
|
+
toAccountIndex: 2n,
|
|
186
|
+
assetIndex: 0,
|
|
187
|
+
fromRouteType: 0,
|
|
188
|
+
toRouteType: 0,
|
|
189
|
+
amount: 1000000n,
|
|
190
|
+
usdcFee: 0n,
|
|
191
|
+
memo: 'test transfer',
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Update leverage
|
|
195
|
+
const leverageResult = await exchangeClient.updateLeverage({
|
|
196
|
+
marketIndex: 0,
|
|
197
|
+
initialMarginFraction: 100,
|
|
198
|
+
marginMode: 0,
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### WsClient - WebSocket
|
|
203
|
+
|
|
204
|
+
Real-time data streams with handler callbacks. Subscription methods return an unsubscribe function for easy cleanup:
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import { WsClient, createClient, newHttpClient } from 'lighter-ts-sdk';
|
|
208
|
+
|
|
209
|
+
const httpClient = newHttpClient('https://mainnet.zklighter.elliot.ai');
|
|
210
|
+
const txClient = createClient(httpClient, privateKey, 1, 0, 1n);
|
|
211
|
+
|
|
212
|
+
const wsClient = new WsClient({
|
|
213
|
+
url: 'wss://mainnet.zklighter.elliot.ai/stream',
|
|
214
|
+
onOpen: () => console.log('Connected'),
|
|
215
|
+
onClose: () => console.log('Disconnected'),
|
|
216
|
+
onError: (error) => console.error('Error:', error),
|
|
217
|
+
onMessage: (data) => console.log('Raw message:', data),
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Connect
|
|
221
|
+
await wsClient.connect();
|
|
222
|
+
|
|
223
|
+
// Public subscriptions with handler callbacks (returns unsubscribe function)
|
|
224
|
+
const unsubOrderBook = wsClient.subscribeOrderBook(0, (update) => {
|
|
225
|
+
console.log('Order book:', update.order_book);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
const unsubMarketStats = wsClient.subscribeMarketStats('all', (update) => {
|
|
229
|
+
console.log('Market stats:', update.market_stats);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const unsubTrades = wsClient.subscribeTrades(0, (update) => {
|
|
233
|
+
console.log('New trades:', update.trades);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const unsubHeight = wsClient.subscribeHeight((update) => {
|
|
237
|
+
console.log('Block height:', update.height);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Authenticated subscriptions (generate auth token manually)
|
|
241
|
+
const deadline = new Date(Date.now() + 7 * 60 * 60 * 1000); // 7 hours
|
|
242
|
+
const authToken = await txClient.getAuthToken(deadline);
|
|
243
|
+
|
|
244
|
+
const unsubAccountAll = wsClient.subscribeAccountAll(1, (update) => {
|
|
245
|
+
console.log('Account data:', update);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const unsubAccountMarket = wsClient.subscribeAccountMarket(0, 1, authToken, (update) => {
|
|
249
|
+
console.log('Account market:', update);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const unsubUserStats = wsClient.subscribeUserStats(1, (update) => {
|
|
253
|
+
console.log('User stats:', update.stats);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const unsubAccountOrders = wsClient.subscribeAccountAllOrders(1, authToken, (update) => {
|
|
257
|
+
console.log('All orders:', update.orders);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const unsubAccountAssets = wsClient.subscribeAccountAllAssets(1, authToken, (update) => {
|
|
261
|
+
console.log('All assets:', update.assets);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const unsubNotifications = wsClient.subscribeNotifications(1, authToken, (update) => {
|
|
265
|
+
console.log('Notifications:', update.notifs);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Unsubscribe by calling the returned function
|
|
269
|
+
unsubOrderBook();
|
|
270
|
+
unsubMarketStats();
|
|
271
|
+
|
|
272
|
+
// Or disconnect entirely
|
|
273
|
+
wsClient.disconnect();
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Direct Signing (Low-Level)
|
|
277
|
+
|
|
278
|
+
For advanced use cases, you can sign transactions directly without sending:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
import { createClient, newHttpClient } from 'lighter-ts-sdk';
|
|
282
|
+
|
|
283
|
+
const httpClient = newHttpClient('https://mainnet.zklighter.elliot.ai');
|
|
284
|
+
const txClient = createClient(httpClient, privateKey, 1, 0, 1n);
|
|
285
|
+
|
|
286
|
+
// Sign a create order transaction
|
|
287
|
+
const signedOrder = await txClient.signCreateOrder({
|
|
288
|
+
marketIndex: 0,
|
|
289
|
+
clientOrderIndex: 1n,
|
|
290
|
+
baseAmount: 1000000n,
|
|
291
|
+
price: 50000,
|
|
292
|
+
isAsk: 0,
|
|
293
|
+
type: 1,
|
|
294
|
+
timeInForce: 1,
|
|
295
|
+
reduceOnly: 0,
|
|
296
|
+
triggerPrice: 0,
|
|
297
|
+
orderExpiry: BigInt(Date.now() + 3600000),
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
console.log('Signed hash:', signedOrder.signedHash);
|
|
301
|
+
console.log('Signature:', signedOrder.signature);
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## API Key Generation
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
import { generateApiKey } from 'lighter-ts-sdk';
|
|
308
|
+
|
|
309
|
+
// Generate random API key pair
|
|
310
|
+
const [privateKey, publicKey] = generateApiKey();
|
|
311
|
+
|
|
312
|
+
// Generate deterministic API key from seed
|
|
313
|
+
const [privKey, pubKey] = generateApiKey('my-seed-string');
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Auth Tokens
|
|
317
|
+
|
|
318
|
+
Auth tokens are used for authenticated HTTP & WebSocket endpoints:
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
// Create an auth token valid for 7 hours
|
|
322
|
+
const token = await txClient.getAuthToken(new Date(Date.now() + 7 * 60 * 60 * 1000));
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Transaction Options
|
|
326
|
+
|
|
327
|
+
Signing methods accept an optional `TransactOpts` object:
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
const tx = await txClient.signCreateOrder(orderReq, {
|
|
331
|
+
nonce: 42n, // If undefined, fetched from server
|
|
332
|
+
expiredAt: BigInt(Date.now() + 600000), // 10 minutes
|
|
333
|
+
apiKeyIndex: 0,
|
|
334
|
+
fromAccountIndex: 1n,
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Constants
|
|
339
|
+
|
|
340
|
+
```typescript
|
|
341
|
+
import {
|
|
342
|
+
OrderType,
|
|
343
|
+
TimeInForce,
|
|
344
|
+
GroupingType,
|
|
345
|
+
AssetRouteType,
|
|
346
|
+
MarginMode,
|
|
347
|
+
MarginDirection,
|
|
348
|
+
TxType,
|
|
349
|
+
} from 'lighter-ts-sdk';
|
|
350
|
+
|
|
351
|
+
// Order types
|
|
352
|
+
OrderType.Market // 0
|
|
353
|
+
OrderType.Limit // 1
|
|
354
|
+
OrderType.StopLoss // 2
|
|
355
|
+
OrderType.TakeProfit // 3
|
|
356
|
+
|
|
357
|
+
// Time in force
|
|
358
|
+
TimeInForce.ImmediateOrCancel // 0
|
|
359
|
+
TimeInForce.GoodTillTime // 1
|
|
360
|
+
TimeInForce.PostOnly // 2
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
## Development
|
|
364
|
+
|
|
365
|
+
```bash
|
|
366
|
+
# Install dependencies
|
|
367
|
+
npm install
|
|
368
|
+
|
|
369
|
+
# Build
|
|
370
|
+
npm run build
|
|
371
|
+
|
|
372
|
+
# Run tests
|
|
373
|
+
npm test
|
|
374
|
+
|
|
375
|
+
# Type check
|
|
376
|
+
npm run typecheck
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
## License
|
|
380
|
+
|
|
381
|
+
MIT
|