@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
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
import axios, { AxiosInstance } from "axios";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
acknowledgeNotification,
|
|
5
|
+
changeAccountTier,
|
|
6
|
+
ChangeAccountTierRequest,
|
|
7
|
+
ChangeAccountTierResponse,
|
|
8
|
+
createToken,
|
|
9
|
+
fastWithdraw,
|
|
10
|
+
FastWithdrawRequest,
|
|
11
|
+
FastWithdrawResponse,
|
|
12
|
+
NotificationAckRequest,
|
|
13
|
+
NotificationAckResponse,
|
|
14
|
+
revokeToken,
|
|
15
|
+
sendTransaction,
|
|
16
|
+
sendTransactionBatch,
|
|
17
|
+
SendTransactionBatchRequest,
|
|
18
|
+
SendTransactionRequest,
|
|
19
|
+
sendTx,
|
|
20
|
+
SendTxFormRequest,
|
|
21
|
+
sendTxJson,
|
|
22
|
+
sendTxWithIndices,
|
|
23
|
+
SendTxWithIndicesRequest,
|
|
24
|
+
TokensCreateRequest,
|
|
25
|
+
TokensCreateResponse,
|
|
26
|
+
TokensRevokeRequest,
|
|
27
|
+
TokensRevokeResponse,
|
|
28
|
+
TxHashesResponse,
|
|
29
|
+
TxHashResponse,
|
|
30
|
+
} from "../api/exchange";
|
|
31
|
+
import { TxClient } from "./txClient";
|
|
32
|
+
import { hexToBytes } from "../signer/keyManager";
|
|
33
|
+
import {
|
|
34
|
+
serializeCreateOrder,
|
|
35
|
+
serializeCancelOrder,
|
|
36
|
+
serializeCancelAllOrders,
|
|
37
|
+
serializeTransfer,
|
|
38
|
+
serializeWithdraw,
|
|
39
|
+
serializeUpdateLeverage,
|
|
40
|
+
serializeModifyOrder,
|
|
41
|
+
serializeCreateSubAccount,
|
|
42
|
+
serializeCreatePublicPool,
|
|
43
|
+
serializeUpdatePublicPool,
|
|
44
|
+
serializeMintShares,
|
|
45
|
+
serializeBurnShares,
|
|
46
|
+
serializeUpdateMargin,
|
|
47
|
+
serializeCreateGroupedOrders,
|
|
48
|
+
serializeChangePubKey,
|
|
49
|
+
} from "../types/txInfoSerializer";
|
|
50
|
+
import { TxType } from "../types/constants";
|
|
51
|
+
|
|
52
|
+
export interface ExchangeClientConfig {
|
|
53
|
+
baseURL?: string;
|
|
54
|
+
txClient: TxClient;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const DEFAULT_DEADLINE = 120;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* ExchangeClient - Client for exchange/transaction endpoints
|
|
61
|
+
*
|
|
62
|
+
* Provides access to transaction sending and signing endpoints.
|
|
63
|
+
* Uses TypeScript-based signing via TxClient instead of WASM.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const txClient = createClient(httpClient, privateKey, chainId, apiKeyIndex, accountIndex);
|
|
68
|
+
* const client = new ExchangeClient({
|
|
69
|
+
* baseURL: "https://mainnet.zklighter.elliot.ai",
|
|
70
|
+
* txClient
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* const result = await client.createOrder({
|
|
74
|
+
* marketIndex: 0,
|
|
75
|
+
* clientOrderIndex: 1,
|
|
76
|
+
* baseAmount: 1000000n,
|
|
77
|
+
* price: 50000,
|
|
78
|
+
* isAsk: 0,
|
|
79
|
+
* orderType: 0,
|
|
80
|
+
* timeInForce: 1,
|
|
81
|
+
* reduceOnly: 0,
|
|
82
|
+
* triggerPrice: 0,
|
|
83
|
+
* orderExpiry: BigInt(Date.now() + 3600000),
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export class ExchangeClient {
|
|
88
|
+
private readonly baseURL: string;
|
|
89
|
+
private readonly axiosInstance: AxiosInstance;
|
|
90
|
+
private readonly txClient: TxClient;
|
|
91
|
+
|
|
92
|
+
constructor(config: ExchangeClientConfig) {
|
|
93
|
+
this.baseURL = config?.baseURL || "https://mainnet.zklighter.elliot.ai";
|
|
94
|
+
this.txClient = config.txClient;
|
|
95
|
+
this.axiosInstance = this.createAxiosInstance();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private createAxiosInstance(): AxiosInstance {
|
|
99
|
+
return axios.create({
|
|
100
|
+
baseURL: this.baseURL,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Create an auth token using the TxClient
|
|
106
|
+
*/
|
|
107
|
+
private async createAuthToken(): Promise<string> {
|
|
108
|
+
const deadline = new Date(Date.now() + DEFAULT_DEADLINE * 1000);
|
|
109
|
+
return this.txClient.getAuthToken(deadline);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Get the axios instance for custom requests
|
|
114
|
+
*/
|
|
115
|
+
public getAxiosInstance(): AxiosInstance {
|
|
116
|
+
return this.axiosInstance;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Get the TxClient for direct signing operations
|
|
121
|
+
*/
|
|
122
|
+
public getTxClient(): TxClient {
|
|
123
|
+
return this.txClient;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Send a transaction
|
|
128
|
+
*/
|
|
129
|
+
public async sendTransaction(
|
|
130
|
+
params: SendTransactionRequest,
|
|
131
|
+
): Promise<TxHashResponse> {
|
|
132
|
+
return sendTransaction(this.axiosInstance, params);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Send a transaction with form data
|
|
137
|
+
*/
|
|
138
|
+
public async sendTx(params: SendTxFormRequest): Promise<TxHashResponse> {
|
|
139
|
+
return sendTx(this.axiosInstance, params);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Send a transaction with account and API key indices
|
|
144
|
+
*/
|
|
145
|
+
public async sendTxWithIndices(
|
|
146
|
+
params: SendTxWithIndicesRequest,
|
|
147
|
+
): Promise<TxHashResponse> {
|
|
148
|
+
return sendTxWithIndices(this.axiosInstance, params);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Send a transaction with JSON payload
|
|
153
|
+
*/
|
|
154
|
+
public async sendTxJson(
|
|
155
|
+
params: SendTxWithIndicesRequest,
|
|
156
|
+
): Promise<TxHashResponse> {
|
|
157
|
+
return sendTxJson(this.axiosInstance, params);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Send a batch of transactions
|
|
162
|
+
*/
|
|
163
|
+
public async sendTransactionBatch(
|
|
164
|
+
params: SendTransactionBatchRequest,
|
|
165
|
+
): Promise<TxHashesResponse> {
|
|
166
|
+
return sendTransactionBatch(this.axiosInstance, params);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Create and send an order using TypeScript signing
|
|
171
|
+
*/
|
|
172
|
+
public async createOrder(params: {
|
|
173
|
+
marketIndex: number;
|
|
174
|
+
clientOrderIndex: bigint;
|
|
175
|
+
baseAmount: bigint;
|
|
176
|
+
price: number;
|
|
177
|
+
isAsk: number;
|
|
178
|
+
orderType: number;
|
|
179
|
+
timeInForce: number;
|
|
180
|
+
reduceOnly: number;
|
|
181
|
+
triggerPrice: number;
|
|
182
|
+
orderExpiry: bigint;
|
|
183
|
+
priceProtection?: boolean;
|
|
184
|
+
}): Promise<TxHashResponse> {
|
|
185
|
+
const txInfo = await this.txClient.signCreateOrder({
|
|
186
|
+
marketIndex: params.marketIndex,
|
|
187
|
+
clientOrderIndex: params.clientOrderIndex,
|
|
188
|
+
baseAmount: params.baseAmount,
|
|
189
|
+
price: params.price,
|
|
190
|
+
isAsk: params.isAsk,
|
|
191
|
+
type: params.orderType,
|
|
192
|
+
timeInForce: params.timeInForce,
|
|
193
|
+
reduceOnly: params.reduceOnly,
|
|
194
|
+
triggerPrice: params.triggerPrice,
|
|
195
|
+
orderExpiry: params.orderExpiry,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
return this.sendTx({
|
|
199
|
+
tx_type: TxType.L2CreateOrder,
|
|
200
|
+
tx_info: serializeCreateOrder(txInfo),
|
|
201
|
+
price_protection: params.priceProtection,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Cancel an order using TypeScript signing
|
|
207
|
+
*/
|
|
208
|
+
public async cancelOrder(params: {
|
|
209
|
+
marketIndex: number;
|
|
210
|
+
orderIndex: bigint;
|
|
211
|
+
priceProtection?: boolean;
|
|
212
|
+
}): Promise<TxHashResponse> {
|
|
213
|
+
const txInfo = await this.txClient.signCancelOrder({
|
|
214
|
+
marketIndex: params.marketIndex,
|
|
215
|
+
index: params.orderIndex,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return this.sendTx({
|
|
219
|
+
tx_type: TxType.L2CancelOrder,
|
|
220
|
+
tx_info: serializeCancelOrder(txInfo),
|
|
221
|
+
price_protection: params.priceProtection,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Cancel all orders using TypeScript signing
|
|
227
|
+
*/
|
|
228
|
+
public async cancelAllOrders(params: {
|
|
229
|
+
timeInForce: number;
|
|
230
|
+
time: bigint;
|
|
231
|
+
priceProtection?: boolean;
|
|
232
|
+
}): Promise<TxHashResponse> {
|
|
233
|
+
const txInfo = await this.txClient.signCancelAllOrders({
|
|
234
|
+
timeInForce: params.timeInForce,
|
|
235
|
+
time: params.time,
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
return this.sendTx({
|
|
239
|
+
tx_type: TxType.L2CancelAllOrders,
|
|
240
|
+
tx_info: serializeCancelAllOrders(txInfo),
|
|
241
|
+
price_protection: params.priceProtection,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Transfer funds using TypeScript signing
|
|
247
|
+
*/
|
|
248
|
+
public async transfer(params: {
|
|
249
|
+
toAccountIndex: bigint;
|
|
250
|
+
assetIndex: number;
|
|
251
|
+
fromRouteType: number;
|
|
252
|
+
toRouteType: number;
|
|
253
|
+
amount: bigint;
|
|
254
|
+
usdcFee: bigint;
|
|
255
|
+
memo: string;
|
|
256
|
+
priceProtection?: boolean;
|
|
257
|
+
}): Promise<TxHashResponse> {
|
|
258
|
+
const memoBytes = new TextEncoder().encode(params.memo);
|
|
259
|
+
const txInfo = await this.txClient.signTransfer({
|
|
260
|
+
toAccountIndex: params.toAccountIndex,
|
|
261
|
+
assetIndex: params.assetIndex,
|
|
262
|
+
fromRouteType: params.fromRouteType,
|
|
263
|
+
toRouteType: params.toRouteType,
|
|
264
|
+
amount: params.amount,
|
|
265
|
+
usdcFee: params.usdcFee,
|
|
266
|
+
memo: memoBytes,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return this.sendTx({
|
|
270
|
+
tx_type: TxType.L2Transfer,
|
|
271
|
+
tx_info: serializeTransfer(txInfo),
|
|
272
|
+
price_protection: params.priceProtection,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Withdraw funds using TypeScript signing
|
|
278
|
+
*/
|
|
279
|
+
public async withdraw(params: {
|
|
280
|
+
assetIndex: number;
|
|
281
|
+
routeType: number;
|
|
282
|
+
amount: bigint;
|
|
283
|
+
priceProtection?: boolean;
|
|
284
|
+
}): Promise<TxHashResponse> {
|
|
285
|
+
const txInfo = await this.txClient.signWithdraw({
|
|
286
|
+
assetIndex: params.assetIndex,
|
|
287
|
+
routeType: params.routeType,
|
|
288
|
+
amount: params.amount,
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
return this.sendTx({
|
|
292
|
+
tx_type: TxType.L2Withdraw,
|
|
293
|
+
tx_info: serializeWithdraw(txInfo),
|
|
294
|
+
price_protection: params.priceProtection,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Update leverage using TypeScript signing
|
|
300
|
+
*/
|
|
301
|
+
public async updateLeverage(params: {
|
|
302
|
+
marketIndex: number;
|
|
303
|
+
initialMarginFraction: number;
|
|
304
|
+
marginMode: number;
|
|
305
|
+
priceProtection?: boolean;
|
|
306
|
+
}): Promise<TxHashResponse> {
|
|
307
|
+
const txInfo = await this.txClient.signUpdateLeverage({
|
|
308
|
+
marketIndex: params.marketIndex,
|
|
309
|
+
initialMarginFraction: params.initialMarginFraction,
|
|
310
|
+
marginMode: params.marginMode,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
return this.sendTx({
|
|
314
|
+
tx_type: TxType.L2UpdateLeverage,
|
|
315
|
+
tx_info: serializeUpdateLeverage(txInfo),
|
|
316
|
+
price_protection: params.priceProtection,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Modify an order using TypeScript signing
|
|
322
|
+
*/
|
|
323
|
+
public async modifyOrder(params: {
|
|
324
|
+
marketIndex: number;
|
|
325
|
+
index: bigint;
|
|
326
|
+
baseAmount: bigint;
|
|
327
|
+
price: number;
|
|
328
|
+
triggerPrice: number;
|
|
329
|
+
priceProtection?: boolean;
|
|
330
|
+
}): Promise<TxHashResponse> {
|
|
331
|
+
const txInfo = await this.txClient.signModifyOrder({
|
|
332
|
+
marketIndex: params.marketIndex,
|
|
333
|
+
index: params.index,
|
|
334
|
+
baseAmount: params.baseAmount,
|
|
335
|
+
price: params.price,
|
|
336
|
+
triggerPrice: params.triggerPrice,
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
return this.sendTx({
|
|
340
|
+
tx_type: TxType.L2ModifyOrder,
|
|
341
|
+
tx_info: serializeModifyOrder(txInfo),
|
|
342
|
+
price_protection: params.priceProtection,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Create a subaccount using TypeScript signing
|
|
348
|
+
*/
|
|
349
|
+
public async createSubAccount(params?: {
|
|
350
|
+
priceProtection?: boolean;
|
|
351
|
+
}): Promise<TxHashResponse> {
|
|
352
|
+
const txInfo = await this.txClient.signCreateSubAccount();
|
|
353
|
+
|
|
354
|
+
return this.sendTx({
|
|
355
|
+
tx_type: TxType.L2CreateSubAccount,
|
|
356
|
+
tx_info: serializeCreateSubAccount(txInfo),
|
|
357
|
+
price_protection: params?.priceProtection,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Create a public pool using TypeScript signing
|
|
363
|
+
*/
|
|
364
|
+
public async createPublicPool(params: {
|
|
365
|
+
operatorFee: bigint;
|
|
366
|
+
initialTotalShares: bigint;
|
|
367
|
+
minOperatorShareRate: number;
|
|
368
|
+
priceProtection?: boolean;
|
|
369
|
+
}): Promise<TxHashResponse> {
|
|
370
|
+
const txInfo = await this.txClient.signCreatePublicPool({
|
|
371
|
+
operatorFee: params.operatorFee,
|
|
372
|
+
initialTotalShares: params.initialTotalShares,
|
|
373
|
+
minOperatorShareRate: params.minOperatorShareRate,
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
return this.sendTx({
|
|
377
|
+
tx_type: TxType.L2CreatePublicPool,
|
|
378
|
+
tx_info: serializeCreatePublicPool(txInfo),
|
|
379
|
+
price_protection: params.priceProtection,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Update a public pool using TypeScript signing
|
|
385
|
+
*/
|
|
386
|
+
public async updatePublicPool(params: {
|
|
387
|
+
publicPoolIndex: bigint;
|
|
388
|
+
status: number;
|
|
389
|
+
operatorFee: bigint;
|
|
390
|
+
minOperatorShareRate: number;
|
|
391
|
+
priceProtection?: boolean;
|
|
392
|
+
}): Promise<TxHashResponse> {
|
|
393
|
+
const txInfo = await this.txClient.signUpdatePublicPool({
|
|
394
|
+
publicPoolIndex: params.publicPoolIndex,
|
|
395
|
+
status: params.status,
|
|
396
|
+
operatorFee: params.operatorFee,
|
|
397
|
+
minOperatorShareRate: params.minOperatorShareRate,
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
return this.sendTx({
|
|
401
|
+
tx_type: TxType.L2UpdatePublicPool,
|
|
402
|
+
tx_info: serializeUpdatePublicPool(txInfo),
|
|
403
|
+
price_protection: params.priceProtection,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Mint shares using TypeScript signing
|
|
409
|
+
*/
|
|
410
|
+
public async mintShares(params: {
|
|
411
|
+
publicPoolIndex: bigint;
|
|
412
|
+
shareAmount: bigint;
|
|
413
|
+
priceProtection?: boolean;
|
|
414
|
+
}): Promise<TxHashResponse> {
|
|
415
|
+
const txInfo = await this.txClient.signMintShares({
|
|
416
|
+
publicPoolIndex: params.publicPoolIndex,
|
|
417
|
+
shareAmount: params.shareAmount,
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
return this.sendTx({
|
|
421
|
+
tx_type: TxType.L2MintShares,
|
|
422
|
+
tx_info: serializeMintShares(txInfo),
|
|
423
|
+
price_protection: params.priceProtection,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Burn shares using TypeScript signing
|
|
429
|
+
*/
|
|
430
|
+
public async burnShares(params: {
|
|
431
|
+
publicPoolIndex: bigint;
|
|
432
|
+
shareAmount: bigint;
|
|
433
|
+
priceProtection?: boolean;
|
|
434
|
+
}): Promise<TxHashResponse> {
|
|
435
|
+
const txInfo = await this.txClient.signBurnShares({
|
|
436
|
+
publicPoolIndex: params.publicPoolIndex,
|
|
437
|
+
shareAmount: params.shareAmount,
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
return this.sendTx({
|
|
441
|
+
tx_type: TxType.L2BurnShares,
|
|
442
|
+
tx_info: serializeBurnShares(txInfo),
|
|
443
|
+
price_protection: params.priceProtection,
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Update margin using TypeScript signing
|
|
449
|
+
*/
|
|
450
|
+
public async updateMargin(params: {
|
|
451
|
+
marketIndex: number;
|
|
452
|
+
usdcAmount: bigint;
|
|
453
|
+
direction: number;
|
|
454
|
+
priceProtection?: boolean;
|
|
455
|
+
}): Promise<TxHashResponse> {
|
|
456
|
+
const txInfo = await this.txClient.signUpdateMargin({
|
|
457
|
+
marketIndex: params.marketIndex,
|
|
458
|
+
usdcAmount: params.usdcAmount,
|
|
459
|
+
direction: params.direction,
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
return this.sendTx({
|
|
463
|
+
tx_type: TxType.L2UpdateMargin,
|
|
464
|
+
tx_info: serializeUpdateMargin(txInfo),
|
|
465
|
+
price_protection: params.priceProtection,
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Create grouped orders using TypeScript signing
|
|
471
|
+
*/
|
|
472
|
+
public async createGroupedOrders(params: {
|
|
473
|
+
groupingType: number;
|
|
474
|
+
orders: Array<{
|
|
475
|
+
marketIndex: number;
|
|
476
|
+
clientOrderIndex: bigint;
|
|
477
|
+
baseAmount: bigint;
|
|
478
|
+
price: number;
|
|
479
|
+
isAsk: number;
|
|
480
|
+
type: number;
|
|
481
|
+
timeInForce: number;
|
|
482
|
+
reduceOnly: number;
|
|
483
|
+
triggerPrice: number;
|
|
484
|
+
orderExpiry: bigint;
|
|
485
|
+
}>;
|
|
486
|
+
priceProtection?: boolean;
|
|
487
|
+
}): Promise<TxHashResponse> {
|
|
488
|
+
const txInfo = await this.txClient.signCreateGroupedOrders({
|
|
489
|
+
groupingType: params.groupingType,
|
|
490
|
+
orders: params.orders,
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
return this.sendTx({
|
|
494
|
+
tx_type: TxType.L2CreateGroupedOrders,
|
|
495
|
+
tx_info: serializeCreateGroupedOrders(txInfo),
|
|
496
|
+
price_protection: params.priceProtection,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Create multiple orders in a batch using TypeScript signing
|
|
502
|
+
*/
|
|
503
|
+
public async createBatchOrders(params: {
|
|
504
|
+
orders: Array<{
|
|
505
|
+
marketIndex: number;
|
|
506
|
+
clientOrderIndex: bigint;
|
|
507
|
+
baseAmount: bigint;
|
|
508
|
+
price: number;
|
|
509
|
+
isAsk: number;
|
|
510
|
+
orderType: number;
|
|
511
|
+
timeInForce: number;
|
|
512
|
+
reduceOnly: number;
|
|
513
|
+
triggerPrice: number;
|
|
514
|
+
orderExpiry: bigint;
|
|
515
|
+
}>;
|
|
516
|
+
}): Promise<TxHashesResponse> {
|
|
517
|
+
const txTypes: number[] = [];
|
|
518
|
+
const txInfos: string[] = [];
|
|
519
|
+
|
|
520
|
+
// Sign each order
|
|
521
|
+
for (const order of params.orders) {
|
|
522
|
+
const txInfo = await this.txClient.signCreateOrder({
|
|
523
|
+
marketIndex: order.marketIndex,
|
|
524
|
+
clientOrderIndex: order.clientOrderIndex,
|
|
525
|
+
baseAmount: order.baseAmount,
|
|
526
|
+
price: order.price,
|
|
527
|
+
isAsk: order.isAsk,
|
|
528
|
+
type: order.orderType,
|
|
529
|
+
timeInForce: order.timeInForce,
|
|
530
|
+
reduceOnly: order.reduceOnly,
|
|
531
|
+
triggerPrice: order.triggerPrice,
|
|
532
|
+
orderExpiry: order.orderExpiry,
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
txTypes.push(TxType.L2CreateOrder);
|
|
536
|
+
txInfos.push(serializeCreateOrder(txInfo));
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return this.sendTransactionBatch({
|
|
540
|
+
tx_types: JSON.stringify(txTypes),
|
|
541
|
+
tx_infos: JSON.stringify(txInfos),
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Generate change public key L1 sign message
|
|
547
|
+
*/
|
|
548
|
+
public generateChangePubKeyL1SignMessage(
|
|
549
|
+
pubKeyHex: string,
|
|
550
|
+
nonce: number,
|
|
551
|
+
accountIndex: bigint,
|
|
552
|
+
newApiKeyIndex: number,
|
|
553
|
+
): string {
|
|
554
|
+
const formatHex16Bytes = (value: bigint | number): string => {
|
|
555
|
+
const hex = BigInt(value).toString(16);
|
|
556
|
+
return "0x" + hex.padStart(16, "0");
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
return `Register Lighter Account\n\npubkey: 0x${pubKeyHex.replace("0x", "")}\nnonce: ${formatHex16Bytes(nonce)}\naccount index: ${formatHex16Bytes(accountIndex)}\napi key index: ${formatHex16Bytes(newApiKeyIndex)}\nOnly sign this message for a trusted client!`;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Change public key using TypeScript signing
|
|
564
|
+
*/
|
|
565
|
+
public async changePubKey(params: {
|
|
566
|
+
pubKey: string;
|
|
567
|
+
l1Sig: string;
|
|
568
|
+
priceProtection?: boolean;
|
|
569
|
+
}): Promise<TxHashResponse> {
|
|
570
|
+
const pubKeyBytes = hexToBytes(params.pubKey);
|
|
571
|
+
const txInfo = await this.txClient.signChangePubKey({
|
|
572
|
+
pubKey: pubKeyBytes,
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
// Add L1 signature to the tx info
|
|
576
|
+
const txInfoWithL1Sig = {
|
|
577
|
+
...JSON.parse(serializeChangePubKey(txInfo)),
|
|
578
|
+
L1Sig: params.l1Sig,
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
return this.sendTx({
|
|
582
|
+
tx_type: TxType.L2ChangePubKey,
|
|
583
|
+
tx_info: JSON.stringify(txInfoWithL1Sig),
|
|
584
|
+
price_protection: params.priceProtection,
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Acknowledge a notification
|
|
590
|
+
*/
|
|
591
|
+
public async acknowledgeNotification(
|
|
592
|
+
params: NotificationAckRequest,
|
|
593
|
+
): Promise<NotificationAckResponse> {
|
|
594
|
+
const auth = await this.createAuthToken();
|
|
595
|
+
return acknowledgeNotification(this.axiosInstance, params, auth);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Change account tier
|
|
600
|
+
*/
|
|
601
|
+
public async changeAccountTier(
|
|
602
|
+
params: ChangeAccountTierRequest,
|
|
603
|
+
): Promise<ChangeAccountTierResponse> {
|
|
604
|
+
const auth = await this.createAuthToken();
|
|
605
|
+
return changeAccountTier(this.axiosInstance, params, auth);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Fast withdraw funds
|
|
610
|
+
*/
|
|
611
|
+
public async fastWithdraw(
|
|
612
|
+
params: FastWithdrawRequest,
|
|
613
|
+
): Promise<FastWithdrawResponse> {
|
|
614
|
+
const auth = await this.createAuthToken();
|
|
615
|
+
return fastWithdraw(this.axiosInstance, params, auth);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Create API token
|
|
620
|
+
*/
|
|
621
|
+
public async createToken(
|
|
622
|
+
params: TokensCreateRequest,
|
|
623
|
+
): Promise<TokensCreateResponse> {
|
|
624
|
+
const auth = await this.createAuthToken();
|
|
625
|
+
return createToken(this.axiosInstance, params, auth);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Revoke API token
|
|
630
|
+
*/
|
|
631
|
+
public async revokeToken(
|
|
632
|
+
params: TokensRevokeRequest,
|
|
633
|
+
): Promise<TokensRevokeResponse> {
|
|
634
|
+
const auth = await this.createAuthToken();
|
|
635
|
+
return revokeToken(this.axiosInstance, params, auth);
|
|
636
|
+
}
|
|
637
|
+
}
|