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
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* WASM Signer Client for Lighter Protocol
|
|
4
|
+
*
|
|
5
|
+
* This module provides a TypeScript wrapper for the Go WASM signer,
|
|
6
|
+
* enabling cryptographic operations in the browser and Node.js environments.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.createWasmSignerClient = exports.WasmSignerClient = void 0;
|
|
10
|
+
class WasmSignerClient {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.wasmModule = null;
|
|
13
|
+
this.isInitialized = false;
|
|
14
|
+
this.config = config;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initialize the WASM module
|
|
18
|
+
*/
|
|
19
|
+
async initialize() {
|
|
20
|
+
if (this.isInitialized) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
// Load the Go WASM runtime
|
|
25
|
+
const wasmExecPath = this.config.wasmExecPath || this.config.wasmPath.replace('.wasm', '_exec.js');
|
|
26
|
+
await this.loadScript(wasmExecPath);
|
|
27
|
+
// Load the WASM binary
|
|
28
|
+
const wasmBytes = await this.loadWasmBinary(this.config.wasmPath);
|
|
29
|
+
// Initialize the Go runtime
|
|
30
|
+
const go = new window.Go();
|
|
31
|
+
const result = await WebAssembly.instantiate(wasmBytes, go.importObject);
|
|
32
|
+
go.run(result.instance);
|
|
33
|
+
this.wasmModule = {
|
|
34
|
+
generateAPIKey: window.generateAPIKey,
|
|
35
|
+
createClient: window.createClient,
|
|
36
|
+
signCreateOrder: window.signCreateOrder,
|
|
37
|
+
signCancelOrder: window.signCancelOrder,
|
|
38
|
+
signCancelAllOrders: window.signCancelAllOrders,
|
|
39
|
+
signTransfer: window.signTransfer,
|
|
40
|
+
signUpdateLeverage: window.signUpdateLeverage,
|
|
41
|
+
createAuthToken: window.createAuthToken,
|
|
42
|
+
};
|
|
43
|
+
this.isInitialized = true;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error(`Failed to initialize WASM signer: ${error}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Generate a new API key pair
|
|
51
|
+
*/
|
|
52
|
+
async generateAPIKey(seed) {
|
|
53
|
+
await this.ensureInitialized();
|
|
54
|
+
const result = this.wasmModule.generateAPIKey(seed);
|
|
55
|
+
if (result.error) {
|
|
56
|
+
throw new Error(`Failed to generate API key: ${result.error}`);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
privateKey: result.privateKey,
|
|
60
|
+
publicKey: result.publicKey,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create a client for signing transactions
|
|
65
|
+
*/
|
|
66
|
+
async createClient(params) {
|
|
67
|
+
await this.ensureInitialized();
|
|
68
|
+
const result = this.wasmModule.createClient(params.url, params.privateKey, params.chainId, params.apiKeyIndex, params.accountIndex);
|
|
69
|
+
if (result.error) {
|
|
70
|
+
throw new Error(`Failed to create client: ${result.error}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Sign a create order transaction
|
|
75
|
+
*/
|
|
76
|
+
async signCreateOrder(params) {
|
|
77
|
+
await this.ensureInitialized();
|
|
78
|
+
const result = this.wasmModule.signCreateOrder(params.marketIndex, params.clientOrderIndex, params.baseAmount, params.price, params.isAsk, params.orderType, params.timeInForce, params.reduceOnly, params.triggerPrice, params.orderExpiry, params.nonce);
|
|
79
|
+
if (result.error) {
|
|
80
|
+
throw new Error(`Failed to sign create order: ${result.error}`);
|
|
81
|
+
}
|
|
82
|
+
return result.txInfo;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Sign a cancel order transaction
|
|
86
|
+
*/
|
|
87
|
+
async signCancelOrder(params) {
|
|
88
|
+
await this.ensureInitialized();
|
|
89
|
+
const result = this.wasmModule.signCancelOrder(params.marketIndex, params.orderIndex, params.nonce);
|
|
90
|
+
if (result.error) {
|
|
91
|
+
throw new Error(`Failed to sign cancel order: ${result.error}`);
|
|
92
|
+
}
|
|
93
|
+
return result.txInfo;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Sign a cancel all orders transaction
|
|
97
|
+
*/
|
|
98
|
+
async signCancelAllOrders(params) {
|
|
99
|
+
await this.ensureInitialized();
|
|
100
|
+
const result = this.wasmModule.signCancelAllOrders(params.timeInForce, params.time, params.nonce);
|
|
101
|
+
if (result.error) {
|
|
102
|
+
return { txInfo: '', error: result.error };
|
|
103
|
+
}
|
|
104
|
+
return { txInfo: result.txInfo };
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sign a transfer transaction
|
|
108
|
+
*/
|
|
109
|
+
async signTransfer(params) {
|
|
110
|
+
await this.ensureInitialized();
|
|
111
|
+
const result = this.wasmModule.signTransfer(params.toAccountIndex, params.usdcAmount, params.fee, params.memo, params.nonce);
|
|
112
|
+
if (result.error) {
|
|
113
|
+
return { txInfo: '', error: result.error };
|
|
114
|
+
}
|
|
115
|
+
return { txInfo: result.txInfo };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Sign an update leverage transaction
|
|
119
|
+
*/
|
|
120
|
+
async signUpdateLeverage(params) {
|
|
121
|
+
await this.ensureInitialized();
|
|
122
|
+
const result = this.wasmModule.signUpdateLeverage(params.marketIndex, params.fraction, params.marginMode, params.nonce);
|
|
123
|
+
if (result.error) {
|
|
124
|
+
return { txInfo: '', error: result.error };
|
|
125
|
+
}
|
|
126
|
+
return { txInfo: result.txInfo };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Create an authentication token
|
|
130
|
+
*/
|
|
131
|
+
async createAuthToken(deadline) {
|
|
132
|
+
await this.ensureInitialized();
|
|
133
|
+
const result = this.wasmModule.createAuthToken(deadline);
|
|
134
|
+
if (result.error) {
|
|
135
|
+
throw new Error(`Failed to create auth token: ${result.error}`);
|
|
136
|
+
}
|
|
137
|
+
return result.authToken;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Ensure the WASM module is initialized
|
|
141
|
+
*/
|
|
142
|
+
async ensureInitialized() {
|
|
143
|
+
if (!this.isInitialized) {
|
|
144
|
+
await this.initialize();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Load a script dynamically
|
|
149
|
+
*/
|
|
150
|
+
loadScript(src) {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const script = document.createElement('script');
|
|
153
|
+
script.src = src;
|
|
154
|
+
script.onload = () => resolve();
|
|
155
|
+
script.onerror = () => reject(new Error(`Failed to load script: ${src}`));
|
|
156
|
+
document.head.appendChild(script);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Load WASM binary
|
|
161
|
+
*/
|
|
162
|
+
async loadWasmBinary(path) {
|
|
163
|
+
const response = await fetch(path);
|
|
164
|
+
if (!response.ok) {
|
|
165
|
+
throw new Error(`Failed to load WASM binary: ${response.statusText}`);
|
|
166
|
+
}
|
|
167
|
+
return response.arrayBuffer();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.WasmSignerClient = WasmSignerClient;
|
|
171
|
+
/**
|
|
172
|
+
* Create a WASM signer client instance
|
|
173
|
+
*/
|
|
174
|
+
function createWasmSignerClient(config) {
|
|
175
|
+
return new WasmSignerClient(config);
|
|
176
|
+
}
|
|
177
|
+
exports.createWasmSignerClient = createWasmSignerClient;
|
|
178
|
+
//# sourceMappingURL=wasm-signer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm-signer.js","sourceRoot":"","sources":["../../src/utils/wasm-signer.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAmEH,MAAa,gBAAgB;IAK3B,YAAY,MAAwB;QAJ5B,eAAU,GAAQ,IAAI,CAAC;QACvB,kBAAa,GAAG,KAAK,CAAC;QAI5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU;QACd,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACvB,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,2BAA2B;YAC3B,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YACnG,MAAM,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;YAEpC,uBAAuB;YACvB,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAElE,4BAA4B;YAC5B,MAAM,EAAE,GAAG,IAAK,MAAc,CAAC,EAAE,EAAE,CAAC;YACpC,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,WAAW,CAAC,SAAS,EAAE,EAAE,CAAC,YAAY,C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IAAI,CAAC,CAAC;QACnC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,+BAA+B,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;QACxE,CAAC;QACD,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC;IAChC,CAAC;CACF;AA5OD,4CA4OC;AAED;;GAEG;AACH,SAAgB,sBAAsB,CAAC,MAAwB;IAC7D,OAAO,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC;AAFD,wDAEC"}
|
package/docs/API.md
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
This section contains detailed documentation for all API classes, methods, and types in the Lighter TypeScript SDK.
|
|
4
|
+
|
|
5
|
+
## Core Classes
|
|
6
|
+
|
|
7
|
+
### [SignerClient](SignerClient.md)
|
|
8
|
+
The main class for trading operations, order management, and account operations.
|
|
9
|
+
|
|
10
|
+
**Key Methods:**
|
|
11
|
+
- `createOrder()` - Create limit orders
|
|
12
|
+
- `createMarketOrder()` - Create market orders
|
|
13
|
+
- `cancelOrder()` - Cancel existing orders
|
|
14
|
+
- `cancelAllOrders()` - Cancel all orders
|
|
15
|
+
- `transfer()` - Transfer USDC between accounts
|
|
16
|
+
- `updateLeverage()` - Update leverage settings
|
|
17
|
+
- `generateAPIKey()` - Generate new API keys
|
|
18
|
+
|
|
19
|
+
### [ApiClient](ApiClient.md)
|
|
20
|
+
Base HTTP client for API communication.
|
|
21
|
+
|
|
22
|
+
### [WsClient](WsClient.md)
|
|
23
|
+
WebSocket client for real-time data streaming.
|
|
24
|
+
|
|
25
|
+
## API Classes
|
|
26
|
+
|
|
27
|
+
### [AccountApi](AccountApi.md)
|
|
28
|
+
Account information and management.
|
|
29
|
+
|
|
30
|
+
**Methods:**
|
|
31
|
+
- `getAccount()` - Get account information
|
|
32
|
+
- `getAccountsByL1Address()` - Get accounts by L1 address
|
|
33
|
+
- `getApiKeys()` - Get API keys
|
|
34
|
+
- `getPnL()` - Get profit/loss information
|
|
35
|
+
- `getPublicPools()` - Get public pool information
|
|
36
|
+
|
|
37
|
+
### [OrderApi](OrderApi.md)
|
|
38
|
+
Order book and trading data.
|
|
39
|
+
|
|
40
|
+
**Methods:**
|
|
41
|
+
- `getExchangeStats()` - Get exchange statistics
|
|
42
|
+
- `getOrderBookDetails()` - Get order book details
|
|
43
|
+
- `getOrderBooks()` - Get all order books
|
|
44
|
+
- `getRecentTrades()` - Get recent trades
|
|
45
|
+
|
|
46
|
+
### [TransactionApi](TransactionApi.md)
|
|
47
|
+
Transaction management and history.
|
|
48
|
+
|
|
49
|
+
**Methods:**
|
|
50
|
+
- `getTransaction()` - Get specific transaction
|
|
51
|
+
- `getTransactions()` - Get transaction list
|
|
52
|
+
- `getBlockTransactions()` - Get block transactions
|
|
53
|
+
- `getAccountTransactions()` - Get account transactions
|
|
54
|
+
- `getNextNonce()` - Get next nonce
|
|
55
|
+
- `sendTx()` - Send transaction
|
|
56
|
+
- `sendTxBatch()` - Send batch transactions
|
|
57
|
+
|
|
58
|
+
### [BlockApi](BlockApi.md)
|
|
59
|
+
Block information and data.
|
|
60
|
+
|
|
61
|
+
**Methods:**
|
|
62
|
+
- `getBlock()` - Get block information
|
|
63
|
+
- `getBlocks()` - Get block list
|
|
64
|
+
- `getCurrentHeight()` - Get current block height
|
|
65
|
+
|
|
66
|
+
### [RootApi](RootApi.md)
|
|
67
|
+
System information and status.
|
|
68
|
+
|
|
69
|
+
**Methods:**
|
|
70
|
+
- `getInfo()` - Get system information
|
|
71
|
+
- `getStatus()` - Get system status
|
|
72
|
+
|
|
73
|
+
## Type Definitions
|
|
74
|
+
|
|
75
|
+
### Core Types
|
|
76
|
+
|
|
77
|
+
#### [SignerConfig](types/SignerConfig.md)
|
|
78
|
+
Configuration for SignerClient initialization.
|
|
79
|
+
|
|
80
|
+
#### [WasmSignerConfig](types/WasmSignerConfig.md)
|
|
81
|
+
Configuration for WASM signer setup.
|
|
82
|
+
|
|
83
|
+
#### [ApiKeyPair](types/ApiKeyPair.md)
|
|
84
|
+
Generated API key pair structure.
|
|
85
|
+
|
|
86
|
+
### Order Types
|
|
87
|
+
|
|
88
|
+
#### [CreateOrderParams](types/CreateOrderParams.md)
|
|
89
|
+
Parameters for creating limit orders.
|
|
90
|
+
|
|
91
|
+
#### [MarketOrderParams](types/MarketOrderParams.md)
|
|
92
|
+
Parameters for creating market orders.
|
|
93
|
+
|
|
94
|
+
#### [CancelOrderParams](types/CancelOrderParams.md)
|
|
95
|
+
Parameters for canceling orders.
|
|
96
|
+
|
|
97
|
+
#### [CancelAllOrdersParams](types/CancelAllOrdersParams.md)
|
|
98
|
+
Parameters for canceling all orders.
|
|
99
|
+
|
|
100
|
+
### Transaction Types
|
|
101
|
+
|
|
102
|
+
#### [TransferParams](types/TransferParams.md)
|
|
103
|
+
Parameters for USDC transfers.
|
|
104
|
+
|
|
105
|
+
#### [UpdateLeverageParams](types/UpdateLeverageParams.md)
|
|
106
|
+
Parameters for updating leverage.
|
|
107
|
+
|
|
108
|
+
### Data Types
|
|
109
|
+
|
|
110
|
+
#### [Account](types/Account.md)
|
|
111
|
+
Account information structure.
|
|
112
|
+
|
|
113
|
+
#### [Order](types/Order.md)
|
|
114
|
+
Order information structure.
|
|
115
|
+
|
|
116
|
+
#### [Transaction](types/Transaction.md)
|
|
117
|
+
Transaction information structure.
|
|
118
|
+
|
|
119
|
+
#### [OrderBook](types/OrderBook.md)
|
|
120
|
+
Order book data structure.
|
|
121
|
+
|
|
122
|
+
#### [Trade](types/Trade.md)
|
|
123
|
+
Trade information structure.
|
|
124
|
+
|
|
125
|
+
## Constants
|
|
126
|
+
|
|
127
|
+
### Order Types
|
|
128
|
+
- `ORDER_TYPE_LIMIT = 0` - Limit order
|
|
129
|
+
- `ORDER_TYPE_MARKET = 1` - Market order
|
|
130
|
+
|
|
131
|
+
### Time in Force
|
|
132
|
+
- `ORDER_TIME_IN_FORCE_IMMEDIATE_OR_CANCEL = 0` - IOC
|
|
133
|
+
- `ORDER_TIME_IN_FORCE_GOOD_TILL_TIME = 1` - GTT
|
|
134
|
+
- `ORDER_TIME_IN_FORCE_FILL_OR_KILL = 2` - FOK
|
|
135
|
+
|
|
136
|
+
### Margin Modes
|
|
137
|
+
- `CROSS_MARGIN_MODE = 0` - Cross margin
|
|
138
|
+
- `ISOLATED_MARGIN_MODE = 1` - Isolated margin
|
|
139
|
+
|
|
140
|
+
### Transaction Types
|
|
141
|
+
- `TX_TYPE_CREATE_ORDER = 1` - Create order
|
|
142
|
+
- `TX_TYPE_CANCEL_ORDER = 2` - Cancel order
|
|
143
|
+
- `TX_TYPE_CANCEL_ALL_ORDERS = 3` - Cancel all orders
|
|
144
|
+
- `TX_TYPE_TRANSFER = 4` - Transfer
|
|
145
|
+
- `TX_TYPE_UPDATE_LEVERAGE = 20` - Update leverage
|
|
146
|
+
|
|
147
|
+
## Error Handling
|
|
148
|
+
|
|
149
|
+
All SDK methods use consistent error handling patterns:
|
|
150
|
+
|
|
151
|
+
### SignerClient Methods
|
|
152
|
+
Return tuples with error information:
|
|
153
|
+
```typescript
|
|
154
|
+
const [result, txHash, error] = await client.createOrder(params);
|
|
155
|
+
if (error) {
|
|
156
|
+
console.error('Operation failed:', error);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### API Methods
|
|
162
|
+
Throw exceptions for errors:
|
|
163
|
+
```typescript
|
|
164
|
+
try {
|
|
165
|
+
const account = await accountApi.getAccount(params);
|
|
166
|
+
console.log('Account:', account);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error('API call failed:', error.message);
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### WebSocket Methods
|
|
173
|
+
Use event handlers for errors:
|
|
174
|
+
```typescript
|
|
175
|
+
wsClient.on('error', (error) => {
|
|
176
|
+
console.error('WebSocket error:', error);
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Examples
|
|
181
|
+
|
|
182
|
+
See the [examples directory](../examples/) for comprehensive usage examples:
|
|
183
|
+
|
|
184
|
+
- [Basic Trading](examples/create_market_order.ts)
|
|
185
|
+
- [Order Management](examples/create_cancel_order.ts)
|
|
186
|
+
- [Account Setup](examples/system_setup.ts)
|
|
187
|
+
- [Real-time Data](examples/ws.ts)
|
|
188
|
+
- [Advanced Operations](examples/transfer_update_leverage.ts)
|
|
189
|
+
|
|
190
|
+
## Getting Help
|
|
191
|
+
|
|
192
|
+
- [Getting Started Guide](GettingStarted.md)
|
|
193
|
+
- [Examples](../examples/)
|
|
194
|
+
- [Discord Community](https://discord.gg/lighter)
|
|
195
|
+
- [Documentation Site](https://docs.lighter.xyz)
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# AccountApi
|
|
2
|
+
|
|
3
|
+
The `AccountApi` class provides methods for retrieving account information, API keys, PnL data, and public pools.
|
|
4
|
+
|
|
5
|
+
## Constructor
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
new AccountApi(client: ApiClient)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Methods
|
|
12
|
+
|
|
13
|
+
### getAccount(params: AccountParams)
|
|
14
|
+
|
|
15
|
+
Gets account information by index or L1 address.
|
|
16
|
+
|
|
17
|
+
**Parameters:**
|
|
18
|
+
- `by: 'index' | 'l1_address'` - Search by account index or L1 address
|
|
19
|
+
- `value: string` - The account index or L1 address value
|
|
20
|
+
|
|
21
|
+
**Returns:** `Promise<Account>` - Account information
|
|
22
|
+
|
|
23
|
+
**Example:**
|
|
24
|
+
```typescript
|
|
25
|
+
const accountApi = new AccountApi(client);
|
|
26
|
+
|
|
27
|
+
// Get account by index
|
|
28
|
+
const account = await accountApi.getAccount({ by: 'index', value: '123' });
|
|
29
|
+
|
|
30
|
+
// Get account by L1 address
|
|
31
|
+
const account = await accountApi.getAccount({
|
|
32
|
+
by: 'l1_address',
|
|
33
|
+
value: '0x1234567890123456789012345678901234567890'
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### getAccountsByL1Address(l1Address: string)
|
|
38
|
+
|
|
39
|
+
Gets all accounts associated with an L1 address.
|
|
40
|
+
|
|
41
|
+
**Parameters:**
|
|
42
|
+
- `l1Address: string` - Ethereum address
|
|
43
|
+
|
|
44
|
+
**Returns:** `Promise<Account[]>` - Array of accounts
|
|
45
|
+
|
|
46
|
+
**Example:**
|
|
47
|
+
```typescript
|
|
48
|
+
const accounts = await accountApi.getAccountsByL1Address(
|
|
49
|
+
'0x1234567890123456789012345678901234567890'
|
|
50
|
+
);
|
|
51
|
+
console.log(`Found ${accounts.length} accounts`);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### getApiKeys(accountIndex: number, limit?: number)
|
|
55
|
+
|
|
56
|
+
Gets API keys for an account.
|
|
57
|
+
|
|
58
|
+
**Parameters:**
|
|
59
|
+
- `accountIndex: number` - Account index
|
|
60
|
+
- `limit?: number` - Maximum number of API keys to return (default: 10)
|
|
61
|
+
|
|
62
|
+
**Returns:** `Promise<AccountApiKeys>` - API keys information
|
|
63
|
+
|
|
64
|
+
**Example:**
|
|
65
|
+
```typescript
|
|
66
|
+
const apiKeys = await accountApi.getApiKeys(123, 5);
|
|
67
|
+
console.log('API keys:', apiKeys.api_keys);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### getPnL(params: PnLParams)
|
|
71
|
+
|
|
72
|
+
Gets PnL (Profit and Loss) information for an account.
|
|
73
|
+
|
|
74
|
+
**Parameters:**
|
|
75
|
+
- `accountIndex: number` - Account index
|
|
76
|
+
- `marketIndex?: number` - Optional market index filter
|
|
77
|
+
- `limit?: number` - Maximum number of entries to return
|
|
78
|
+
|
|
79
|
+
**Returns:** `Promise<AccountPnL>` - PnL information
|
|
80
|
+
|
|
81
|
+
**Example:**
|
|
82
|
+
```typescript
|
|
83
|
+
const pnl = await accountApi.getPnL({
|
|
84
|
+
accountIndex: 123,
|
|
85
|
+
marketIndex: 0, // ETH/USDC
|
|
86
|
+
limit: 100
|
|
87
|
+
});
|
|
88
|
+
console.log('PnL entries:', pnl.entries);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### getPublicPools(filter?: string, limit?: number, index?: number)
|
|
92
|
+
|
|
93
|
+
Gets public pool information.
|
|
94
|
+
|
|
95
|
+
**Parameters:**
|
|
96
|
+
- `filter?: string` - Filter type ('all', 'active', etc.)
|
|
97
|
+
- `limit?: number` - Maximum number of pools to return
|
|
98
|
+
- `index?: number` - Starting index for pagination
|
|
99
|
+
|
|
100
|
+
**Returns:** `Promise<PublicPool[]>` - Array of public pools
|
|
101
|
+
|
|
102
|
+
**Example:**
|
|
103
|
+
```typescript
|
|
104
|
+
const pools = await accountApi.getPublicPools('all', 10, 0);
|
|
105
|
+
console.log(`Found ${pools.length} public pools`);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Types
|
|
109
|
+
|
|
110
|
+
### AccountParams
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
interface AccountParams {
|
|
114
|
+
by: 'index' | 'l1_address';
|
|
115
|
+
value: string;
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### PnLParams
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
interface PnLParams {
|
|
123
|
+
accountIndex: number;
|
|
124
|
+
marketIndex?: number;
|
|
125
|
+
limit?: number;
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Account
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
interface Account {
|
|
133
|
+
index: string;
|
|
134
|
+
l1_address: string;
|
|
135
|
+
l2_address: string;
|
|
136
|
+
// ... other account properties
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### AccountApiKeys
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
interface AccountApiKeys {
|
|
144
|
+
api_keys: ApiKey[];
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### AccountPnL
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
interface AccountPnL {
|
|
152
|
+
entries: PnLEntry[];
|
|
153
|
+
// ... other PnL properties
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### PublicPool
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
interface PublicPool {
|
|
161
|
+
pool_id: string;
|
|
162
|
+
name: string;
|
|
163
|
+
// ... other pool properties
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Error Handling
|
|
168
|
+
|
|
169
|
+
All methods throw errors for invalid parameters or network issues:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
try {
|
|
173
|
+
const account = await accountApi.getAccount({ by: 'index', value: '123' });
|
|
174
|
+
console.log('Account:', account);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.error('Failed to get account:', error.message);
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Complete Example
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { ApiClient, AccountApi } from '@lighter/typescript-sdk';
|
|
184
|
+
|
|
185
|
+
async function main() {
|
|
186
|
+
const client = new ApiClient({ host: 'https://mainnet.zklighter.elliot.ai' });
|
|
187
|
+
const accountApi = new AccountApi(client);
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
// Get account by index
|
|
191
|
+
const account = await accountApi.getAccount({ by: 'index', value: '123' });
|
|
192
|
+
console.log('Account:', account);
|
|
193
|
+
|
|
194
|
+
// Get all accounts for an L1 address
|
|
195
|
+
const accounts = await accountApi.getAccountsByL1Address(
|
|
196
|
+
'0x1234567890123456789012345678901234567890'
|
|
197
|
+
);
|
|
198
|
+
console.log(`Found ${accounts.length} accounts`);
|
|
199
|
+
|
|
200
|
+
// Get API keys
|
|
201
|
+
const apiKeys = await accountApi.getApiKeys(123);
|
|
202
|
+
console.log('API keys:', apiKeys.api_keys);
|
|
203
|
+
|
|
204
|
+
// Get PnL
|
|
205
|
+
const pnl = await accountApi.getPnL({ accountIndex: 123 });
|
|
206
|
+
console.log('PnL entries:', pnl.entries);
|
|
207
|
+
|
|
208
|
+
// Get public pools
|
|
209
|
+
const pools = await accountApi.getPublicPools('all', 10);
|
|
210
|
+
console.log(`Found ${pools.length} public pools`);
|
|
211
|
+
|
|
212
|
+
} catch (error) {
|
|
213
|
+
console.error('Error:', error.message);
|
|
214
|
+
} finally {
|
|
215
|
+
await client.close();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
main().catch(console.error);
|
|
220
|
+
```
|