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/LICENSE +21 -0
- package/README.md +440 -0
- package/dist/api/account-api.d.ts +89 -0
- package/dist/api/account-api.d.ts.map +1 -0
- package/dist/api/account-api.js +61 -0
- package/dist/api/account-api.js.map +1 -0
- package/dist/api/api-client.d.ts +23 -0
- package/dist/api/api-client.d.ts.map +1 -0
- package/dist/api/api-client.js +129 -0
- package/dist/api/api-client.js.map +1 -0
- package/dist/api/block-api.d.ts +24 -0
- package/dist/api/block-api.d.ts.map +1 -0
- package/dist/api/block-api.js +22 -0
- package/dist/api/block-api.js.map +1 -0
- package/dist/api/candlestick-api.d.ts +27 -0
- package/dist/api/candlestick-api.d.ts.map +1 -0
- package/dist/api/candlestick-api.js +18 -0
- package/dist/api/candlestick-api.js.map +1 -0
- package/dist/api/order-api.d.ts +76 -0
- package/dist/api/order-api.d.ts.map +1 -0
- package/dist/api/order-api.js +102 -0
- package/dist/api/order-api.js.map +1 -0
- package/dist/api/root-api.d.ts +8 -0
- package/dist/api/root-api.d.ts.map +1 -0
- package/dist/api/root-api.js +14 -0
- package/dist/api/root-api.js.map +1 -0
- package/dist/api/transaction-api.d.ts +58 -0
- package/dist/api/transaction-api.d.ts.map +1 -0
- package/dist/api/transaction-api.js +141 -0
- package/dist/api/transaction-api.js.map +1 -0
- package/dist/api/ws-client.d.ts +21 -0
- package/dist/api/ws-client.d.ts.map +1 -0
- package/dist/api/ws-client.js +139 -0
- package/dist/api/ws-client.js.map +1 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +99 -0
- package/dist/index.js.map +1 -0
- package/dist/signer/signer-client.d.ts +79 -0
- package/dist/signer/signer-client.d.ts.map +1 -0
- package/dist/signer/signer-client.js +213 -0
- package/dist/signer/signer-client.js.map +1 -0
- package/dist/signer/wasm-signer-client.d.ts +129 -0
- package/dist/signer/wasm-signer-client.d.ts.map +1 -0
- package/dist/signer/wasm-signer-client.js +607 -0
- package/dist/signer/wasm-signer-client.js.map +1 -0
- package/dist/types/index.d.ts +130 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/api-key-utils.d.ts +7 -0
- package/dist/utils/api-key-utils.d.ts.map +1 -0
- package/dist/utils/api-key-utils.js +31 -0
- package/dist/utils/api-key-utils.js.map +1 -0
- package/dist/utils/configuration.d.ts +19 -0
- package/dist/utils/configuration.d.ts.map +1 -0
- package/dist/utils/configuration.js +55 -0
- package/dist/utils/configuration.js.map +1 -0
- package/dist/utils/exceptions.d.ts +33 -0
- package/dist/utils/exceptions.d.ts.map +1 -0
- package/dist/utils/exceptions.js +76 -0
- package/dist/utils/exceptions.js.map +1 -0
- package/dist/utils/node-wasm-signer.d.ts +126 -0
- package/dist/utils/node-wasm-signer.d.ts.map +1 -0
- package/dist/utils/node-wasm-signer.js +286 -0
- package/dist/utils/node-wasm-signer.js.map +1 -0
- package/dist/utils/signer-server.d.ts +18 -0
- package/dist/utils/signer-server.d.ts.map +1 -0
- package/dist/utils/signer-server.js +79 -0
- package/dist/utils/signer-server.js.map +1 -0
- package/dist/utils/signer.d.ts +15 -0
- package/dist/utils/signer.d.ts.map +1 -0
- package/dist/utils/signer.js +68 -0
- package/dist/utils/signer.js.map +1 -0
- package/dist/utils/wasm-signer.d.ts +130 -0
- package/dist/utils/wasm-signer.d.ts.map +1 -0
- package/dist/utils/wasm-signer.js +178 -0
- package/dist/utils/wasm-signer.js.map +1 -0
- package/docs/API.md +195 -0
- package/docs/AccountApi.md +220 -0
- package/docs/GettingStarted.md +261 -0
- package/docs/OrderApi.md +199 -0
- package/docs/SignerClient.md +326 -0
- package/docs/TransactionApi.md +312 -0
- package/docs/WsClient.md +275 -0
- package/docs/types/Account.md +67 -0
- package/docs/types/ApiKeyPair.md +33 -0
- package/docs/types/CreateOrderParams.md +47 -0
- package/docs/types/MarketOrderParams.md +36 -0
- package/docs/types/SignerConfig.md +40 -0
- package/docs/types/WasmSignerConfig.md +30 -0
- package/examples/README.md +171 -0
- package/examples/account_info.ts +12 -0
- package/examples/create_cancel_order.ts +74 -0
- package/examples/create_market_order.ts +42 -0
- package/examples/create_market_order_max_slippage.ts +43 -0
- package/examples/create_sl_tp.ts +61 -0
- package/examples/create_with_multiple_keys.ts +58 -0
- package/examples/get_info.ts +58 -0
- package/examples/send_tx_batch.ts +87 -0
- package/examples/system_setup.ts +96 -0
- package/examples/transfer_update_leverage.ts +53 -0
- package/examples/ws.ts +24 -0
- package/examples/ws_async.ts +39 -0
- package/examples/ws_send_batch_tx.ts +113 -0
- package/examples/ws_send_tx.ts +94 -0
- package/package.json +71 -0
- package/wasm/lighter-signer.wasm +0 -0
- package/wasm/wasm_exec.js +402 -0
- package/wasm/wasm_exec_nodejs.js +402 -0
package/docs/WsClient.md
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
# WsClient
|
|
2
|
+
|
|
3
|
+
The `WsClient` class provides real-time WebSocket connectivity for order book updates, account changes, and other live data from the Lighter Protocol.
|
|
4
|
+
|
|
5
|
+
## Constructor
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
new WsClient(config: WsConfig)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### WsConfig
|
|
12
|
+
|
|
13
|
+
| Property | Type | Required | Description |
|
|
14
|
+
|----------|------|----------|-------------|
|
|
15
|
+
| `url` | `string` | Yes | WebSocket URL (e.g., `wss://mainnet.zklighter.elliot.ai/ws`) |
|
|
16
|
+
| `accountIndex` | `number` | Yes | Your account index |
|
|
17
|
+
| `apiKeyIndex` | `number` | Yes | Your API key index |
|
|
18
|
+
| `privateKey` | `string` | Yes | Your API key private key |
|
|
19
|
+
| `reconnectInterval` | `number` | No | Reconnection interval in ms (default: 5000) |
|
|
20
|
+
| `maxReconnectAttempts` | `number` | No | Maximum reconnection attempts (default: 10) |
|
|
21
|
+
|
|
22
|
+
## Methods
|
|
23
|
+
|
|
24
|
+
### connect()
|
|
25
|
+
|
|
26
|
+
Establishes a WebSocket connection to the Lighter server.
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
await wsClient.connect();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### disconnect()
|
|
33
|
+
|
|
34
|
+
Closes the WebSocket connection.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
await wsClient.disconnect();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### subscribeOrderBook(marketIndex: number, callback: (data: OrderBookData) => void)
|
|
41
|
+
|
|
42
|
+
Subscribes to order book updates for a specific market.
|
|
43
|
+
|
|
44
|
+
**Parameters:**
|
|
45
|
+
- `marketIndex: number` - Market index (0 for ETH/USDC)
|
|
46
|
+
- `callback: (data: OrderBookData) => void` - Callback function for order book updates
|
|
47
|
+
|
|
48
|
+
**Example:**
|
|
49
|
+
```typescript
|
|
50
|
+
wsClient.subscribeOrderBook(0, (data) => {
|
|
51
|
+
console.log('ETH/USDC Order Book Update:', data);
|
|
52
|
+
console.log('Best Bid:', data.bestBid);
|
|
53
|
+
console.log('Best Ask:', data.bestAsk);
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### subscribeAccount(callback: (data: AccountData) => void)
|
|
58
|
+
|
|
59
|
+
Subscribes to account updates.
|
|
60
|
+
|
|
61
|
+
**Parameters:**
|
|
62
|
+
- `callback: (data: AccountData) => void` - Callback function for account updates
|
|
63
|
+
|
|
64
|
+
**Example:**
|
|
65
|
+
```typescript
|
|
66
|
+
wsClient.subscribeAccount((data) => {
|
|
67
|
+
console.log('Account Update:', data);
|
|
68
|
+
console.log('Balance:', data.balance);
|
|
69
|
+
console.log('Positions:', data.positions);
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### subscribeTrades(marketIndex: number, callback: (data: TradeData) => void)
|
|
74
|
+
|
|
75
|
+
Subscribes to trade updates for a specific market.
|
|
76
|
+
|
|
77
|
+
**Parameters:**
|
|
78
|
+
- `marketIndex: number` - Market index
|
|
79
|
+
- `callback: (data: TradeData) => void` - Callback function for trade updates
|
|
80
|
+
|
|
81
|
+
**Example:**
|
|
82
|
+
```typescript
|
|
83
|
+
wsClient.subscribeTrades(0, (data) => {
|
|
84
|
+
console.log('New Trade:', data);
|
|
85
|
+
console.log('Price:', data.price);
|
|
86
|
+
console.log('Size:', data.size);
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### sendTransaction(txType: number, txInfo: string)
|
|
91
|
+
|
|
92
|
+
Sends a transaction through the WebSocket connection.
|
|
93
|
+
|
|
94
|
+
**Parameters:**
|
|
95
|
+
- `txType: number` - Transaction type (use `SignerClient.TX_TYPE_*` constants)
|
|
96
|
+
- `txInfo: string` - Transaction information as JSON string
|
|
97
|
+
|
|
98
|
+
**Returns:** `Promise<string>` - Transaction hash
|
|
99
|
+
|
|
100
|
+
**Example:**
|
|
101
|
+
```typescript
|
|
102
|
+
const txHash = await wsClient.sendTransaction(
|
|
103
|
+
SignerClient.TX_TYPE_CREATE_ORDER,
|
|
104
|
+
JSON.stringify(orderData)
|
|
105
|
+
);
|
|
106
|
+
console.log('Transaction sent:', txHash);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Event Handling
|
|
110
|
+
|
|
111
|
+
The WebSocket client emits events for connection status:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
wsClient.on('connected', () => {
|
|
115
|
+
console.log('WebSocket connected');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
wsClient.on('disconnected', () => {
|
|
119
|
+
console.log('WebSocket disconnected');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
wsClient.on('error', (error) => {
|
|
123
|
+
console.error('WebSocket error:', error);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
wsClient.on('reconnecting', (attempt) => {
|
|
127
|
+
console.log(`Reconnecting... attempt ${attempt}`);
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Types
|
|
132
|
+
|
|
133
|
+
### OrderBookData
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
interface OrderBookData {
|
|
137
|
+
marketIndex: number;
|
|
138
|
+
bids: PriceLevel[];
|
|
139
|
+
asks: PriceLevel[];
|
|
140
|
+
bestBid: string;
|
|
141
|
+
bestAsk: string;
|
|
142
|
+
timestamp: number;
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### AccountData
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
interface AccountData {
|
|
150
|
+
accountIndex: number;
|
|
151
|
+
balance: string;
|
|
152
|
+
positions: AccountPosition[];
|
|
153
|
+
orders: Order[];
|
|
154
|
+
timestamp: number;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### TradeData
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
interface TradeData {
|
|
162
|
+
tradeId: string;
|
|
163
|
+
marketIndex: number;
|
|
164
|
+
price: string;
|
|
165
|
+
size: string;
|
|
166
|
+
side: 'buy' | 'sell';
|
|
167
|
+
timestamp: number;
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### PriceLevel
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
interface PriceLevel {
|
|
175
|
+
price: string;
|
|
176
|
+
size: string;
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Complete Example
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { WsClient, SignerClient } from '@lighter/typescript-sdk';
|
|
184
|
+
|
|
185
|
+
async function main() {
|
|
186
|
+
const wsClient = new WsClient({
|
|
187
|
+
url: 'wss://mainnet.zklighter.elliot.ai/ws',
|
|
188
|
+
accountIndex: 123,
|
|
189
|
+
apiKeyIndex: 0,
|
|
190
|
+
privateKey: 'your-api-key-private-key',
|
|
191
|
+
reconnectInterval: 5000,
|
|
192
|
+
maxReconnectAttempts: 10
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Set up event handlers
|
|
196
|
+
wsClient.on('connected', () => {
|
|
197
|
+
console.log('WebSocket connected');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
wsClient.on('disconnected', () => {
|
|
201
|
+
console.log('WebSocket disconnected');
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
wsClient.on('error', (error) => {
|
|
205
|
+
console.error('WebSocket error:', error);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
// Connect to WebSocket
|
|
210
|
+
await wsClient.connect();
|
|
211
|
+
|
|
212
|
+
// Subscribe to order book updates
|
|
213
|
+
wsClient.subscribeOrderBook(0, (data) => {
|
|
214
|
+
console.log('ETH/USDC Order Book:');
|
|
215
|
+
console.log(`Best Bid: ${data.bestBid}`);
|
|
216
|
+
console.log(`Best Ask: ${data.bestAsk}`);
|
|
217
|
+
console.log(`Bid Depth: ${data.bids.length} levels`);
|
|
218
|
+
console.log(`Ask Depth: ${data.asks.length} levels`);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Subscribe to account updates
|
|
222
|
+
wsClient.subscribeAccount((data) => {
|
|
223
|
+
console.log('Account Update:');
|
|
224
|
+
console.log(`Balance: ${data.balance} USDC`);
|
|
225
|
+
console.log(`Positions: ${data.positions.length}`);
|
|
226
|
+
console.log(`Open Orders: ${data.orders.length}`);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Subscribe to trade updates
|
|
230
|
+
wsClient.subscribeTrades(0, (data) => {
|
|
231
|
+
console.log(`New Trade: ${data.size} @ ${data.price} (${data.side})`);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// Keep the connection alive
|
|
235
|
+
await new Promise(() => {}); // Keep running
|
|
236
|
+
|
|
237
|
+
} catch (error) {
|
|
238
|
+
console.error('Error:', error.message);
|
|
239
|
+
} finally {
|
|
240
|
+
await wsClient.disconnect();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
main().catch(console.error);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Error Handling
|
|
248
|
+
|
|
249
|
+
The WebSocket client includes automatic reconnection and error handling:
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
wsClient.on('error', (error) => {
|
|
253
|
+
console.error('WebSocket error:', error);
|
|
254
|
+
// The client will automatically attempt to reconnect
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
wsClient.on('reconnecting', (attempt) => {
|
|
258
|
+
console.log(`Reconnection attempt ${attempt}/${wsClient.maxReconnectAttempts}`);
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Best Practices
|
|
263
|
+
|
|
264
|
+
1. **Always handle connection events** - Monitor connection status
|
|
265
|
+
2. **Use appropriate callbacks** - Keep callback functions lightweight
|
|
266
|
+
3. **Handle errors gracefully** - The client will auto-reconnect, but you should handle errors
|
|
267
|
+
4. **Clean up resources** - Always call `disconnect()` when done
|
|
268
|
+
5. **Monitor performance** - WebSocket connections can generate high-frequency updates
|
|
269
|
+
|
|
270
|
+
## Limitations
|
|
271
|
+
|
|
272
|
+
- WebSocket connections are not persistent across browser refreshes
|
|
273
|
+
- Rate limiting may apply to high-frequency subscriptions
|
|
274
|
+
- Some data may be delayed during high network congestion
|
|
275
|
+
- Connection will be lost if the server restarts
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Account
|
|
2
|
+
|
|
3
|
+
Account information returned by the `AccountApi.getAccount()` method.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
| Property | Type | Description |
|
|
8
|
+
|----------|------|-------------|
|
|
9
|
+
| `index` | `string` | Account index |
|
|
10
|
+
| `l1_address` | `string` | Ethereum L1 address |
|
|
11
|
+
| `l2_address` | `string` | Lighter L2 address |
|
|
12
|
+
| `sub_accounts` | `SubAccount[]` | Sub-accounts associated with this account |
|
|
13
|
+
| `positions` | `AccountPosition[]` | Current positions |
|
|
14
|
+
| `trades` | `Trade[]` | Recent trades |
|
|
15
|
+
|
|
16
|
+
## Example
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { AccountApi } from '@lighter/typescript-sdk';
|
|
20
|
+
|
|
21
|
+
const accountApi = new AccountApi(client);
|
|
22
|
+
const account = await accountApi.getAccount({ by: 'index', value: '123' });
|
|
23
|
+
|
|
24
|
+
console.log('Account Index:', account.index);
|
|
25
|
+
console.log('L1 Address:', account.l1_address);
|
|
26
|
+
console.log('L2 Address:', account.l2_address);
|
|
27
|
+
console.log('Positions:', account.positions.length);
|
|
28
|
+
console.log('Recent Trades:', account.trades.length);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Related Types
|
|
32
|
+
|
|
33
|
+
### SubAccount
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
interface SubAccount {
|
|
37
|
+
index: string;
|
|
38
|
+
l1_address: string;
|
|
39
|
+
l2_address: string;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### AccountPosition
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
interface AccountPosition {
|
|
47
|
+
market_id: number;
|
|
48
|
+
side: 'long' | 'short';
|
|
49
|
+
size: string;
|
|
50
|
+
entry_price: string;
|
|
51
|
+
mark_price: string;
|
|
52
|
+
pnl: string;
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Trade
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
interface Trade {
|
|
60
|
+
trade_id: string;
|
|
61
|
+
market_index: number;
|
|
62
|
+
price: string;
|
|
63
|
+
size: string;
|
|
64
|
+
timestamp: string;
|
|
65
|
+
side: 'buy' | 'sell';
|
|
66
|
+
}
|
|
67
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# ApiKeyPair
|
|
2
|
+
|
|
3
|
+
Structure representing a generated API key pair.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
| Property | Type | Description |
|
|
8
|
+
|----------|------|-------------|
|
|
9
|
+
| `privateKey` | `string` | The private key for the API key |
|
|
10
|
+
| `publicKey` | `string` | The public key for the API key |
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { SignerClient } from '@lighter/typescript-sdk';
|
|
16
|
+
|
|
17
|
+
const client = new SignerClient(config);
|
|
18
|
+
await client.initialize();
|
|
19
|
+
await client.ensureWasmClient();
|
|
20
|
+
|
|
21
|
+
const apiKeyPair = await client.generateAPIKey();
|
|
22
|
+
if (apiKeyPair) {
|
|
23
|
+
console.log('Private Key:', apiKeyPair.privateKey);
|
|
24
|
+
console.log('Public Key:', apiKeyPair.publicKey);
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Notes
|
|
29
|
+
|
|
30
|
+
- The `privateKey` should be kept secure and not shared
|
|
31
|
+
- The `publicKey` can be used for verification purposes
|
|
32
|
+
- Both keys are returned as hexadecimal strings
|
|
33
|
+
- The `generateAPIKey()` method returns `null` if generation fails
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# CreateOrderParams
|
|
2
|
+
|
|
3
|
+
Parameters for creating a limit order using the `SignerClient.createOrder()` method.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
| Property | Type | Required | Description |
|
|
8
|
+
|----------|------|----------|-------------|
|
|
9
|
+
| `marketIndex` | `number` | Yes | Market index (0 for ETH/USDC) |
|
|
10
|
+
| `clientOrderIndex` | `number` | Yes | Unique client order index |
|
|
11
|
+
| `baseAmount` | `number` | Yes | Base amount in smallest unit |
|
|
12
|
+
| `price` | `number` | Yes | Order price in smallest unit |
|
|
13
|
+
| `isAsk` | `boolean` | Yes | True for sell orders, false for buy orders |
|
|
14
|
+
| `orderType` | `number` | Yes | Order type (use `SignerClient.ORDER_TYPE_LIMIT`) |
|
|
15
|
+
| `timeInForce` | `number` | Yes | Time in force (use `SignerClient.ORDER_TIME_IN_FORCE_*`) |
|
|
16
|
+
| `reduceOnly` | `boolean` | Yes | Whether this is a reduce-only order |
|
|
17
|
+
| `triggerPrice` | `number` | Yes | Trigger price for conditional orders |
|
|
18
|
+
| `orderExpiry` | `number` | Yes | Order expiry timestamp |
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { SignerClient } from '@lighter/typescript-sdk';
|
|
24
|
+
|
|
25
|
+
const orderParams: CreateOrderParams = {
|
|
26
|
+
marketIndex: 0, // ETH/USDC
|
|
27
|
+
clientOrderIndex: Date.now(),
|
|
28
|
+
baseAmount: 1000000, // 1 ETH in smallest unit
|
|
29
|
+
price: 300000000, // $3000 in smallest unit
|
|
30
|
+
isAsk: true, // Sell order
|
|
31
|
+
orderType: SignerClient.ORDER_TYPE_LIMIT,
|
|
32
|
+
timeInForce: SignerClient.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
|
|
33
|
+
reduceOnly: false,
|
|
34
|
+
triggerPrice: SignerClient.NIL_TRIGGER_PRICE,
|
|
35
|
+
orderExpiry: SignerClient.DEFAULT_28_DAY_ORDER_EXPIRY
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const [tx, txHash, err] = await client.createOrder(orderParams);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Notes
|
|
42
|
+
|
|
43
|
+
- `baseAmount` and `price` are in the smallest units (e.g., wei for ETH, micro-USDC for USDC)
|
|
44
|
+
- `clientOrderIndex` should be unique per client to avoid conflicts
|
|
45
|
+
- `triggerPrice` is used for conditional orders (stop loss, take profit)
|
|
46
|
+
- `orderExpiry` can be set to `SignerClient.DEFAULT_28_DAY_ORDER_EXPIRY` for 28-day expiry
|
|
47
|
+
- Use `SignerClient.NIL_TRIGGER_PRICE` for regular limit orders
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# MarketOrderParams
|
|
2
|
+
|
|
3
|
+
Parameters for creating a market order using the `SignerClient.createMarketOrder()` method.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
| Property | Type | Required | Description |
|
|
8
|
+
|----------|------|----------|-------------|
|
|
9
|
+
| `marketIndex` | `number` | Yes | Market index (0 for ETH/USDC) |
|
|
10
|
+
| `clientOrderIndex` | `number` | Yes | Unique client order index |
|
|
11
|
+
| `baseAmount` | `number` | Yes | Base amount in smallest unit |
|
|
12
|
+
| `avgExecutionPrice` | `number` | Yes | Maximum average execution price |
|
|
13
|
+
| `isAsk` | `boolean` | Yes | True for sell orders, false for buy orders |
|
|
14
|
+
|
|
15
|
+
## Example
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { SignerClient } from '@lighter/typescript-sdk';
|
|
19
|
+
|
|
20
|
+
const marketOrderParams: MarketOrderParams = {
|
|
21
|
+
marketIndex: 0, // ETH/USDC
|
|
22
|
+
clientOrderIndex: Date.now(),
|
|
23
|
+
baseAmount: 1000000, // 1 ETH in smallest unit
|
|
24
|
+
avgExecutionPrice: 300000000, // Max $3000 average price
|
|
25
|
+
isAsk: true // Sell order
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const [tx, txHash, err] = await client.createMarketOrder(marketOrderParams);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Notes
|
|
32
|
+
|
|
33
|
+
- `baseAmount` is in the smallest units (e.g., wei for ETH)
|
|
34
|
+
- `avgExecutionPrice` acts as a price limit to prevent excessive slippage
|
|
35
|
+
- Market orders are executed immediately at the best available price
|
|
36
|
+
- Use `avgExecutionPrice` to set a maximum price you're willing to pay/receive
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# SignerConfig
|
|
2
|
+
|
|
3
|
+
Configuration object for the `SignerClient` class.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
| Property | Type | Required | Description |
|
|
8
|
+
|----------|------|----------|-------------|
|
|
9
|
+
| `url` | `string` | Yes | The Lighter API URL (e.g., `https://mainnet.zklighter.elliot.ai`) |
|
|
10
|
+
| `privateKey` | `string` | Yes | Your API key private key |
|
|
11
|
+
| `accountIndex` | `number` | Yes | Your account index |
|
|
12
|
+
| `apiKeyIndex` | `number` | Yes | Your API key index |
|
|
13
|
+
| `signerServerUrl` | `string` | No | URL of the signer server (alternative to WASM) |
|
|
14
|
+
| `wasmConfig` | `WasmSignerConfig` | No | Configuration for WASM signer |
|
|
15
|
+
|
|
16
|
+
## Example
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { SignerClient } from '@lighter/typescript-sdk';
|
|
20
|
+
|
|
21
|
+
const config: SignerConfig = {
|
|
22
|
+
url: 'https://mainnet.zklighter.elliot.ai',
|
|
23
|
+
privateKey: '0x1234567890abcdef...',
|
|
24
|
+
accountIndex: 123,
|
|
25
|
+
apiKeyIndex: 0,
|
|
26
|
+
wasmConfig: {
|
|
27
|
+
wasmPath: 'wasm/lighter-signer.wasm',
|
|
28
|
+
wasmExecPath: 'wasm/wasm_exec.js' // optional
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const client = new SignerClient(config);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Notes
|
|
36
|
+
|
|
37
|
+
- Either `signerServerUrl` or `wasmConfig` must be provided
|
|
38
|
+
- The `privateKey` should be your API key private key, not your Ethereum private key
|
|
39
|
+
- The `accountIndex` and `apiKeyIndex` can be obtained from the system setup process
|
|
40
|
+
- The `wasmPath` should point to the compiled WASM binary file
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# WasmSignerConfig
|
|
2
|
+
|
|
3
|
+
Configuration object for the WASM signer client.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
| Property | Type | Required | Description |
|
|
8
|
+
|----------|------|----------|-------------|
|
|
9
|
+
| `wasmPath` | `string` | Yes | Path to the WASM binary file |
|
|
10
|
+
| `wasmExecPath` | `string` | No | Path to wasm_exec.js runtime (optional) |
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { WasmSignerClient } from '@lighter/typescript-sdk';
|
|
16
|
+
|
|
17
|
+
const config: WasmSignerConfig = {
|
|
18
|
+
wasmPath: 'wasm/lighter-signer.wasm',
|
|
19
|
+
wasmExecPath: 'wasm/wasm_exec.js' // optional, will auto-detect if not provided
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const wasmClient = new WasmSignerClient(config);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Notes
|
|
26
|
+
|
|
27
|
+
- The `wasmPath` should point to the compiled WASM binary file
|
|
28
|
+
- The `wasmExecPath` is optional and will be auto-detected if not provided
|
|
29
|
+
- For Node.js environments, the runtime will look for wasm_exec.js in common locations
|
|
30
|
+
- For browser environments, the runtime will look for wasm_exec.js in the same directory as the WASM file
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Lighter TypeScript SDK Examples
|
|
2
|
+
|
|
3
|
+
This directory contains comprehensive examples demonstrating how to use the Lighter TypeScript SDK.
|
|
4
|
+
|
|
5
|
+
## Setup Instructions
|
|
6
|
+
|
|
7
|
+
### Testnet Setup
|
|
8
|
+
1. Go to https://testnet.app.lighter.xyz/ and connect a wallet to receive $500
|
|
9
|
+
2. Run `system_setup.ts` with the correct ETH Private key configured
|
|
10
|
+
- Set an API key index which is not 0, so you won't override the one used by [app.lighter.xyz](https://app.lighter.xyz/)
|
|
11
|
+
- This will require you to enter your Ethereum private key
|
|
12
|
+
- The ETH private key will only be used in the TypeScript SDK to sign a message
|
|
13
|
+
- The ETH private key is not required in order to trade on the platform
|
|
14
|
+
- The ETH private key is not passed to the WASM binary
|
|
15
|
+
- Copy the output of the script and post it into `create_cancel_order.ts`
|
|
16
|
+
- The output should look like:
|
|
17
|
+
```
|
|
18
|
+
BASE_URL = 'https://testnet.zklighter.elliot.ai'
|
|
19
|
+
API_KEY_PRIVATE_KEY = '0xea5d2eca5be67eca056752eaf27b173518b8a5550117c09d2b58c7ea7d306cc4426f913ccf27ab19'
|
|
20
|
+
ACCOUNT_INDEX = 595
|
|
21
|
+
API_KEY_INDEX = 1
|
|
22
|
+
```
|
|
23
|
+
3. Start trading using:
|
|
24
|
+
- `create_cancel_order.ts` has an example which creates an order on testnet & cancels it
|
|
25
|
+
- You'll need to set up both your account index, API key index & API Key private key
|
|
26
|
+
|
|
27
|
+
### Mainnet Setup
|
|
28
|
+
1. Deposit money on Lighter to create an account first
|
|
29
|
+
2. Change the URL to `mainnet.zklighter.elliot.ai`
|
|
30
|
+
3. Repeat setup steps
|
|
31
|
+
|
|
32
|
+
## Examples Overview
|
|
33
|
+
|
|
34
|
+
### Basic Trading Examples
|
|
35
|
+
|
|
36
|
+
#### [Create Market Order](create_market_order.ts)
|
|
37
|
+
Demonstrates how to create a market order using the WASM signer.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npx ts-node examples/create_market_order.ts
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### [Create & Cancel Orders](create_cancel_order.ts)
|
|
44
|
+
Shows how to create limit orders and cancel them.
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npx ts-node examples/create_cancel_order.ts
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### [Create Market Order with Max Slippage](create_market_order_max_slippage.ts)
|
|
51
|
+
Example of creating market orders with price protection.
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npx ts-node examples/create_market_order_max_slippage.ts
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### [Create Stop Loss & Take Profit Orders](create_sl_tp.ts)
|
|
58
|
+
Demonstrates advanced order types with trigger prices.
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npx ts-node examples/create_sl_tp.ts
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Account Management Examples
|
|
65
|
+
|
|
66
|
+
#### [System Setup](system_setup.ts)
|
|
67
|
+
Complete setup process for creating API keys and configuring accounts.
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npx ts-node examples/system_setup.ts
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### [Transfer & Update Leverage](transfer_update_leverage.ts)
|
|
74
|
+
Shows how to transfer USDC between accounts and update leverage settings.
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npx ts-node examples/transfer_update_leverage.ts
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### [Create Orders with Multiple Keys](create_with_multiple_keys.ts)
|
|
81
|
+
Demonstrates using multiple API keys for trading.
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npx ts-node examples/create_with_multiple_keys.ts
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### API Information Examples
|
|
88
|
+
|
|
89
|
+
#### [Get Information](get_info.ts)
|
|
90
|
+
Comprehensive example showing all available API endpoints.
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
npx ts-node examples/get_info.ts
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### WebSocket Examples
|
|
97
|
+
|
|
98
|
+
#### [WebSocket Sync](ws.ts)
|
|
99
|
+
Real-time order book and account synchronization.
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
npx ts-node examples/ws.ts
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### [WebSocket Async](ws_async.ts)
|
|
106
|
+
Asynchronous WebSocket connection handling.
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
npx ts-node examples/ws_async.ts
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### [WebSocket Send Transaction](ws_send_tx.ts)
|
|
113
|
+
Sending transactions through WebSocket connection.
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
npx ts-node examples/ws_send_tx.ts
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### [WebSocket Send Batch Transaction](ws_send_batch_tx.ts)
|
|
120
|
+
Sending multiple transactions in a batch via WebSocket.
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
npx ts-node examples/ws_send_batch_tx.ts
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Advanced Examples
|
|
127
|
+
|
|
128
|
+
#### [Send Transaction Batch](send_tx_batch.ts)
|
|
129
|
+
Batch transaction processing for improved efficiency.
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
npx ts-node examples/send_tx_batch.ts
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Environment Variables
|
|
136
|
+
|
|
137
|
+
Create a `.env` file in your project root:
|
|
138
|
+
|
|
139
|
+
```env
|
|
140
|
+
BASE_URL=https://mainnet.zklighter.elliot.ai
|
|
141
|
+
PRIVATE_KEY=your-api-key-private-key
|
|
142
|
+
ACCOUNT_INDEX=123
|
|
143
|
+
API_KEY_INDEX=0
|
|
144
|
+
ETH_PRIVATE_KEY=your-ethereum-private-key
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Running Examples
|
|
148
|
+
|
|
149
|
+
All examples can be run using:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
npx ts-node examples/[example-name].ts
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Make sure you have:
|
|
156
|
+
1. Installed dependencies: `npm install`
|
|
157
|
+
2. Set up your `.env` file with correct credentials
|
|
158
|
+
3. Built the WASM signer (if using signer examples)
|
|
159
|
+
|
|
160
|
+
## Error Handling
|
|
161
|
+
|
|
162
|
+
All examples include proper error handling and will display helpful error messages if something goes wrong. Common issues include:
|
|
163
|
+
|
|
164
|
+
- Invalid API credentials
|
|
165
|
+
- Insufficient balance
|
|
166
|
+
- Invalid order parameters
|
|
167
|
+
- Network connectivity issues
|
|
168
|
+
|
|
169
|
+
## Support
|
|
170
|
+
|
|
171
|
+
For questions about the examples or the SDK, please visit our [documentation](https://docs.lighter.xyz) or join our [Discord community](https://discord.gg/lighter).
|