@luxfi/dex 1.2.1 → 2.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/dist/client/clob.d.ts +52 -0
- package/dist/client/clob.d.ts.map +1 -0
- package/dist/client/clob.js +196 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +6 -0
- package/dist/client/types.d.ts +126 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +5 -0
- package/dist/hooks/index.d.ts +22 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +25 -0
- package/dist/hooks/use-lxbook.d.ts +95 -0
- package/dist/hooks/use-lxbook.d.ts.map +1 -0
- package/dist/hooks/use-lxbook.js +213 -0
- package/dist/hooks/use-lxfeed.d.ts +111 -0
- package/dist/hooks/use-lxfeed.d.ts.map +1 -0
- package/dist/hooks/use-lxfeed.js +152 -0
- package/dist/hooks/use-lxvault.d.ts +137 -0
- package/dist/hooks/use-lxvault.d.ts.map +1 -0
- package/dist/hooks/use-lxvault.js +227 -0
- package/dist/hooks/use-quote.d.ts +18 -0
- package/dist/hooks/use-quote.d.ts.map +1 -0
- package/dist/hooks/use-quote.js +65 -0
- package/dist/hooks/use-swap.d.ts +17 -0
- package/dist/hooks/use-swap.d.ts.map +1 -0
- package/dist/hooks/use-swap.js +75 -0
- package/dist/index.d.ts +50 -115
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +72 -225
- package/dist/precompile/abis.d.ts +991 -0
- package/dist/precompile/abis.d.ts.map +1 -0
- package/dist/precompile/abis.js +743 -0
- package/dist/precompile/addresses.d.ts +129 -0
- package/dist/precompile/addresses.d.ts.map +1 -0
- package/dist/precompile/addresses.js +117 -0
- package/dist/precompile/index.d.ts +19 -0
- package/dist/precompile/index.d.ts.map +1 -0
- package/dist/precompile/index.js +18 -0
- package/dist/precompile/types.d.ts +246 -0
- package/dist/precompile/types.d.ts.map +1 -0
- package/dist/precompile/types.js +84 -0
- package/dist/router/index.d.ts +7 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +6 -0
- package/dist/router/router.d.ts +58 -0
- package/dist/router/router.d.ts.map +1 -0
- package/dist/router/router.js +272 -0
- package/dist/router/types.d.ts +76 -0
- package/dist/router/types.d.ts.map +1 -0
- package/dist/router/types.js +1 -0
- package/package.json +55 -29
- package/src/client/clob.ts +256 -0
- package/src/client/index.ts +6 -0
- package/src/client/types.ts +148 -0
- package/src/hooks/index.ts +29 -0
- package/src/hooks/use-lxbook.ts +343 -0
- package/src/hooks/use-lxfeed.ts +179 -0
- package/src/hooks/use-lxvault.ts +318 -0
- package/src/hooks/use-quote.ts +92 -0
- package/src/hooks/use-swap.ts +103 -0
- package/src/index.ts +142 -309
- package/src/precompile/abis.ts +755 -0
- package/src/precompile/addresses.ts +153 -0
- package/src/precompile/index.ts +18 -0
- package/src/precompile/types.ts +295 -0
- package/src/router/index.ts +6 -0
- package/src/router/router.ts +338 -0
- package/src/router/types.ts +87 -0
- package/dist/marketData.d.ts +0 -152
- package/dist/marketData.d.ts.map +0 -1
- package/dist/marketData.js +0 -253
- package/src/marketData.ts +0 -351
- package/tsconfig.json +0 -19
package/src/index.ts
CHANGED
|
@@ -1,309 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
//
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
jsonRpcUrl: config.jsonRpcUrl || 'http://localhost:8080',
|
|
144
|
-
wsUrl: config.wsUrl || 'ws://localhost:8081',
|
|
145
|
-
...config
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
this.jsonRpc = new JSONRPCClient(this.config.jsonRpcUrl!);
|
|
149
|
-
|
|
150
|
-
// Initialize sub-clients
|
|
151
|
-
this.marketData = new MarketDataClient(this.jsonRpc);
|
|
152
|
-
this.liquidationMonitor = new LiquidationMonitor(null);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Connection Management
|
|
156
|
-
async connect(): Promise<void> {
|
|
157
|
-
if (this.ws) return;
|
|
158
|
-
|
|
159
|
-
return new Promise((resolve, reject) => {
|
|
160
|
-
this.ws = new WebSocket(this.config.wsUrl!);
|
|
161
|
-
|
|
162
|
-
this.ws.on('open', () => {
|
|
163
|
-
console.log('WebSocket connected');
|
|
164
|
-
// Set WebSocket for liquidation monitor
|
|
165
|
-
this.liquidationMonitor.setWebSocket(this.ws!);
|
|
166
|
-
resolve();
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
this.ws.on('message', (data: string) => {
|
|
170
|
-
try {
|
|
171
|
-
const msg = JSON.parse(data);
|
|
172
|
-
this.handleWebSocketMessage(msg);
|
|
173
|
-
} catch (err) {
|
|
174
|
-
console.error('Failed to parse WebSocket message:', err);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
this.ws.on('error', (err) => {
|
|
179
|
-
console.error('WebSocket error:', err);
|
|
180
|
-
reject(err);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
this.ws.on('close', () => {
|
|
184
|
-
console.log('WebSocket disconnected');
|
|
185
|
-
this.ws = null;
|
|
186
|
-
});
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
disconnect(): void {
|
|
191
|
-
if (this.ws) {
|
|
192
|
-
this.ws.close();
|
|
193
|
-
this.ws = null;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// Order Management
|
|
198
|
-
async placeOrder(order: Partial<Order>): Promise<{ orderId: number; status: string }> {
|
|
199
|
-
return this.jsonRpc.call('lx_placeOrder', order);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async cancelOrder(orderId: number): Promise<{ success: boolean; message: string }> {
|
|
203
|
-
return this.jsonRpc.call('lx_cancelOrder', { orderId });
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async getOrder(orderId: number): Promise<Order> {
|
|
207
|
-
return this.jsonRpc.call('lx_getOrder', { orderId });
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Market Data
|
|
211
|
-
async getOrderBook(symbol: string = 'BTC-USD', depth: number = 10): Promise<OrderBook> {
|
|
212
|
-
return this.jsonRpc.call('lx_getOrderBook', { symbol, depth });
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
async getBestBid(symbol: string = 'BTC-USD'): Promise<number> {
|
|
216
|
-
const result = await this.jsonRpc.call('lx_getBestBid', { symbol });
|
|
217
|
-
return result.price;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async getBestAsk(symbol: string = 'BTC-USD'): Promise<number> {
|
|
221
|
-
const result = await this.jsonRpc.call('lx_getBestAsk', { symbol });
|
|
222
|
-
return result.price;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async getTrades(symbol: string = 'BTC-USD', limit: number = 100): Promise<Trade[]> {
|
|
226
|
-
return this.jsonRpc.call('lx_getTrades', { symbol, limit });
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// Node Information
|
|
230
|
-
async getInfo(): Promise<NodeInfo> {
|
|
231
|
-
return this.jsonRpc.call('lx_getInfo');
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async ping(): Promise<string> {
|
|
235
|
-
return this.jsonRpc.call('lx_ping');
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// WebSocket Subscriptions
|
|
239
|
-
subscribe(channel: string, callback: Function): void {
|
|
240
|
-
if (!this.wsCallbacks.has(channel)) {
|
|
241
|
-
this.wsCallbacks.set(channel, []);
|
|
242
|
-
}
|
|
243
|
-
this.wsCallbacks.get(channel)!.push(callback);
|
|
244
|
-
|
|
245
|
-
// Send subscription message
|
|
246
|
-
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
247
|
-
this.ws.send(JSON.stringify({
|
|
248
|
-
type: 'subscribe',
|
|
249
|
-
channel
|
|
250
|
-
}));
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
unsubscribe(channel: string, callback?: Function): void {
|
|
255
|
-
if (callback) {
|
|
256
|
-
const callbacks = this.wsCallbacks.get(channel);
|
|
257
|
-
if (callbacks) {
|
|
258
|
-
const index = callbacks.indexOf(callback);
|
|
259
|
-
if (index > -1) {
|
|
260
|
-
callbacks.splice(index, 1);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
} else {
|
|
264
|
-
this.wsCallbacks.delete(channel);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// Send unsubscribe message
|
|
268
|
-
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
269
|
-
this.ws.send(JSON.stringify({
|
|
270
|
-
type: 'unsubscribe',
|
|
271
|
-
channel
|
|
272
|
-
}));
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
subscribeOrderBook(symbol: string, callback: (book: OrderBook) => void): void {
|
|
277
|
-
this.subscribe(`orderbook:${symbol}`, callback);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
subscribeTrades(symbol: string, callback: (trade: Trade) => void): void {
|
|
281
|
-
this.subscribe(`trades:${symbol}`, callback);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// Private methods
|
|
285
|
-
private handleWebSocketMessage(msg: any): void {
|
|
286
|
-
const { channel, data } = msg;
|
|
287
|
-
const callbacks = this.wsCallbacks.get(channel);
|
|
288
|
-
if (callbacks) {
|
|
289
|
-
callbacks.forEach(cb => cb(data));
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// Utility methods
|
|
294
|
-
static formatPrice(price: number, decimals: number = 2): string {
|
|
295
|
-
return price.toFixed(decimals);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
static formatSize(size: number, decimals: number = 8): string {
|
|
299
|
-
return size.toFixed(decimals);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
static calculateTotal(price: number, size: number): number {
|
|
303
|
-
return price * size;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// Export everything
|
|
308
|
-
export default LXDexClient;
|
|
309
|
-
export * from './marketData';
|
|
1
|
+
/**
|
|
2
|
+
* @luxfi/dex
|
|
3
|
+
*
|
|
4
|
+
* LX Integration Package
|
|
5
|
+
*
|
|
6
|
+
* Native precompile integration for Lux DEX stack:
|
|
7
|
+
* - LXPool (LP-9010): v4-style AMM PoolManager
|
|
8
|
+
* - LXOracle (LP-9011): Multi-source price aggregation
|
|
9
|
+
* - LXRouter (LP-9012): Optimized swap routing
|
|
10
|
+
* - LXHooks (LP-9013): Hook contract registry
|
|
11
|
+
* - LXFlash (LP-9014): Flash loan facility
|
|
12
|
+
* - LXBook (LP-9020): CLOB matching engine
|
|
13
|
+
* - LXVault (LP-9030): Custody and margin engine
|
|
14
|
+
* - LXFeed (LP-9040): Mark price and funding feeds
|
|
15
|
+
*
|
|
16
|
+
* Architecture:
|
|
17
|
+
* ```
|
|
18
|
+
* ┌─────────────────────────────────────────────────────────────┐
|
|
19
|
+
* │ Omnichain Router │
|
|
20
|
+
* │ Best execution between CLOB & AMM │
|
|
21
|
+
* └─────────────────────────┬───────────────────────────────────┘
|
|
22
|
+
* │
|
|
23
|
+
* ┌───────────────────┼───────────────────┐
|
|
24
|
+
* │ │ │
|
|
25
|
+
* ▼ ▼ ▼
|
|
26
|
+
* ┌───────────┐ ┌───────────┐ ┌───────────┐
|
|
27
|
+
* │ LXBook │ │ LXPool │ │ LXVault │
|
|
28
|
+
* │ (LP-9020) │ │ (LP-9010) │ │ (LP-9030) │
|
|
29
|
+
* │ │ │ │ │ │
|
|
30
|
+
* │ • Orders │ │ • Swaps │ │ • Custody │
|
|
31
|
+
* │ • CLOB │ │ • AMM │ │ • Margin │
|
|
32
|
+
* │ • Perps │ │ • Flash │ │ • Liq. │
|
|
33
|
+
* └───────────┘ └───────────┘ └───────────┘
|
|
34
|
+
* │ │ │
|
|
35
|
+
* └───────────────────┴───────────────────┘
|
|
36
|
+
* │
|
|
37
|
+
* ┌──────┴──────┐
|
|
38
|
+
* │ LXFeed │
|
|
39
|
+
* │ (LP-9040) │
|
|
40
|
+
* │ │
|
|
41
|
+
* │ • Mark Px │
|
|
42
|
+
* │ • Index Px │
|
|
43
|
+
* │ • Funding │
|
|
44
|
+
* └─────────────┘
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
// =============================================================================
|
|
49
|
+
// Precompile Types, ABIs, and Addresses
|
|
50
|
+
// =============================================================================
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
// AMM Types (LP-9010)
|
|
54
|
+
type Currency,
|
|
55
|
+
type PoolKey,
|
|
56
|
+
type BalanceDelta,
|
|
57
|
+
type SwapParams,
|
|
58
|
+
type ModifyLiquidityParams,
|
|
59
|
+
type PoolState,
|
|
60
|
+
type Position as AMMPosition,
|
|
61
|
+
NATIVE_LUX,
|
|
62
|
+
sortCurrencies,
|
|
63
|
+
createPoolKey,
|
|
64
|
+
|
|
65
|
+
// LXBook Types (LP-9020)
|
|
66
|
+
TIF,
|
|
67
|
+
OrderKind,
|
|
68
|
+
GroupType,
|
|
69
|
+
ActionType,
|
|
70
|
+
type LXOrder,
|
|
71
|
+
type LXAction,
|
|
72
|
+
type LXPlaceResult,
|
|
73
|
+
type LXL1,
|
|
74
|
+
|
|
75
|
+
// LXVault Types (LP-9030)
|
|
76
|
+
MarginMode,
|
|
77
|
+
PositionSide,
|
|
78
|
+
type LXAccount,
|
|
79
|
+
type LXPosition,
|
|
80
|
+
type LXMarginInfo,
|
|
81
|
+
type LXSettlement,
|
|
82
|
+
type LXLiquidationResult,
|
|
83
|
+
|
|
84
|
+
// LXFeed Types (LP-9040)
|
|
85
|
+
type LXMarkPrice,
|
|
86
|
+
type LXFundingRate,
|
|
87
|
+
|
|
88
|
+
// AMM ABIs
|
|
89
|
+
POOL_MANAGER_ABI,
|
|
90
|
+
SWAP_ROUTER_ABI,
|
|
91
|
+
HOOKS_REGISTRY_ABI,
|
|
92
|
+
FLASH_LOAN_ABI,
|
|
93
|
+
|
|
94
|
+
// LX* ABIs
|
|
95
|
+
LX_BOOK_ABI,
|
|
96
|
+
LX_VAULT_ABI,
|
|
97
|
+
LX_FEED_ABI,
|
|
98
|
+
LX_ORACLE_ABI,
|
|
99
|
+
|
|
100
|
+
// Addresses
|
|
101
|
+
LX,
|
|
102
|
+
DEX_PRECOMPILES,
|
|
103
|
+
type LxdexPrecompile,
|
|
104
|
+
type DexPrecompile,
|
|
105
|
+
fromLP,
|
|
106
|
+
toLP,
|
|
107
|
+
isDEXPrecompile,
|
|
108
|
+
isBridgePrecompile,
|
|
109
|
+
} from './precompile'
|
|
110
|
+
|
|
111
|
+
// =============================================================================
|
|
112
|
+
// CLOB Client (External ~/work/lux/dex integration)
|
|
113
|
+
// =============================================================================
|
|
114
|
+
|
|
115
|
+
export {
|
|
116
|
+
type OrderSide,
|
|
117
|
+
type OrderType,
|
|
118
|
+
type OrderStatus,
|
|
119
|
+
type TimeInForce,
|
|
120
|
+
type OrderRequest,
|
|
121
|
+
type Order,
|
|
122
|
+
type OrderBookEntry,
|
|
123
|
+
type OrderBook,
|
|
124
|
+
type Trade,
|
|
125
|
+
type Position as CLOBPosition,
|
|
126
|
+
type Balance,
|
|
127
|
+
type ICLOBClient,
|
|
128
|
+
CLOBClient,
|
|
129
|
+
createCLOBClient,
|
|
130
|
+
} from './client'
|
|
131
|
+
|
|
132
|
+
// =============================================================================
|
|
133
|
+
// Omnichain Router
|
|
134
|
+
// =============================================================================
|
|
135
|
+
|
|
136
|
+
export * from './router'
|
|
137
|
+
|
|
138
|
+
// =============================================================================
|
|
139
|
+
// React Hooks
|
|
140
|
+
// =============================================================================
|
|
141
|
+
|
|
142
|
+
export * from './hooks'
|