@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.
Files changed (74) hide show
  1. package/dist/client/clob.d.ts +52 -0
  2. package/dist/client/clob.d.ts.map +1 -0
  3. package/dist/client/clob.js +196 -0
  4. package/dist/client/index.d.ts +7 -0
  5. package/dist/client/index.d.ts.map +1 -0
  6. package/dist/client/index.js +6 -0
  7. package/dist/client/types.d.ts +126 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +5 -0
  10. package/dist/hooks/index.d.ts +22 -0
  11. package/dist/hooks/index.d.ts.map +1 -0
  12. package/dist/hooks/index.js +25 -0
  13. package/dist/hooks/use-lxbook.d.ts +95 -0
  14. package/dist/hooks/use-lxbook.d.ts.map +1 -0
  15. package/dist/hooks/use-lxbook.js +213 -0
  16. package/dist/hooks/use-lxfeed.d.ts +111 -0
  17. package/dist/hooks/use-lxfeed.d.ts.map +1 -0
  18. package/dist/hooks/use-lxfeed.js +152 -0
  19. package/dist/hooks/use-lxvault.d.ts +137 -0
  20. package/dist/hooks/use-lxvault.d.ts.map +1 -0
  21. package/dist/hooks/use-lxvault.js +227 -0
  22. package/dist/hooks/use-quote.d.ts +18 -0
  23. package/dist/hooks/use-quote.d.ts.map +1 -0
  24. package/dist/hooks/use-quote.js +65 -0
  25. package/dist/hooks/use-swap.d.ts +17 -0
  26. package/dist/hooks/use-swap.d.ts.map +1 -0
  27. package/dist/hooks/use-swap.js +75 -0
  28. package/dist/index.d.ts +50 -115
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +72 -225
  31. package/dist/precompile/abis.d.ts +991 -0
  32. package/dist/precompile/abis.d.ts.map +1 -0
  33. package/dist/precompile/abis.js +743 -0
  34. package/dist/precompile/addresses.d.ts +129 -0
  35. package/dist/precompile/addresses.d.ts.map +1 -0
  36. package/dist/precompile/addresses.js +117 -0
  37. package/dist/precompile/index.d.ts +19 -0
  38. package/dist/precompile/index.d.ts.map +1 -0
  39. package/dist/precompile/index.js +18 -0
  40. package/dist/precompile/types.d.ts +246 -0
  41. package/dist/precompile/types.d.ts.map +1 -0
  42. package/dist/precompile/types.js +84 -0
  43. package/dist/router/index.d.ts +7 -0
  44. package/dist/router/index.d.ts.map +1 -0
  45. package/dist/router/index.js +6 -0
  46. package/dist/router/router.d.ts +58 -0
  47. package/dist/router/router.d.ts.map +1 -0
  48. package/dist/router/router.js +272 -0
  49. package/dist/router/types.d.ts +76 -0
  50. package/dist/router/types.d.ts.map +1 -0
  51. package/dist/router/types.js +1 -0
  52. package/package.json +55 -29
  53. package/src/client/clob.ts +256 -0
  54. package/src/client/index.ts +6 -0
  55. package/src/client/types.ts +148 -0
  56. package/src/hooks/index.ts +29 -0
  57. package/src/hooks/use-lxbook.ts +343 -0
  58. package/src/hooks/use-lxfeed.ts +179 -0
  59. package/src/hooks/use-lxvault.ts +318 -0
  60. package/src/hooks/use-quote.ts +92 -0
  61. package/src/hooks/use-swap.ts +103 -0
  62. package/src/index.ts +142 -309
  63. package/src/precompile/abis.ts +755 -0
  64. package/src/precompile/addresses.ts +153 -0
  65. package/src/precompile/index.ts +18 -0
  66. package/src/precompile/types.ts +295 -0
  67. package/src/router/index.ts +6 -0
  68. package/src/router/router.ts +338 -0
  69. package/src/router/types.ts +87 -0
  70. package/dist/marketData.d.ts +0 -152
  71. package/dist/marketData.d.ts.map +0 -1
  72. package/dist/marketData.js +0 -253
  73. package/src/marketData.ts +0 -351
  74. package/tsconfig.json +0 -19
package/src/index.ts CHANGED
@@ -1,309 +1,142 @@
1
- import axios, { AxiosInstance } from 'axios';
2
- import WebSocket from 'ws';
3
- import {
4
- MarketDataClient,
5
- LiquidationMonitor,
6
- MarketDataSource,
7
- LiquidationInfo,
8
- SettlementBatch,
9
- MarginInfo,
10
- MarketDataProviders
11
- } from './marketData';
12
-
13
- // Types
14
- export enum OrderType {
15
- LIMIT = 0,
16
- MARKET = 1,
17
- STOP = 2,
18
- STOP_LIMIT = 3,
19
- ICEBERG = 4,
20
- PEG = 5
21
- }
22
-
23
- export enum OrderSide {
24
- BUY = 0,
25
- SELL = 1
26
- }
27
-
28
- export enum OrderStatus {
29
- OPEN = 'open',
30
- PARTIAL = 'partial',
31
- FILLED = 'filled',
32
- CANCELLED = 'cancelled',
33
- REJECTED = 'rejected'
34
- }
35
-
36
- export enum TimeInForce {
37
- GTC = 'GTC',
38
- IOC = 'IOC',
39
- FOK = 'FOK',
40
- DAY = 'DAY'
41
- }
42
-
43
- export interface Order {
44
- orderId?: number;
45
- symbol: string;
46
- type: OrderType;
47
- side: OrderSide;
48
- price: number;
49
- size: number;
50
- userID?: string;
51
- clientID?: string;
52
- timeInForce?: TimeInForce;
53
- postOnly?: boolean;
54
- reduceOnly?: boolean;
55
- status?: OrderStatus;
56
- filled?: number;
57
- remaining?: number;
58
- timestamp?: number;
59
- }
60
-
61
- export interface Trade {
62
- tradeId: number;
63
- symbol: string;
64
- price: number;
65
- size: number;
66
- side: OrderSide;
67
- buyOrderId: number;
68
- sellOrderId: number;
69
- buyerId: string;
70
- sellerId: string;
71
- timestamp: number;
72
- }
73
-
74
- export interface OrderBookLevel {
75
- price: number;
76
- size: number;
77
- count?: number;
78
- }
79
-
80
- export interface OrderBook {
81
- symbol: string;
82
- bids: OrderBookLevel[];
83
- asks: OrderBookLevel[];
84
- timestamp: number;
85
- }
86
-
87
- export interface NodeInfo {
88
- version: string;
89
- network: string;
90
- orderCount: number;
91
- tradeCount: number;
92
- timestamp: number;
93
- }
94
-
95
- export interface LXDexConfig {
96
- jsonRpcUrl?: string;
97
- wsUrl?: string;
98
- grpcUrl?: string;
99
- apiKey?: string;
100
- }
101
-
102
- // JSON-RPC Client
103
- class JSONRPCClient {
104
- private axios: AxiosInstance;
105
- private idCounter = 1;
106
-
107
- constructor(baseURL: string) {
108
- this.axios = axios.create({
109
- baseURL,
110
- headers: {
111
- 'Content-Type': 'application/json'
112
- }
113
- });
114
- }
115
-
116
- async call(method: string, params: any = {}): Promise<any> {
117
- const response = await this.axios.post('/rpc', {
118
- jsonrpc: '2.0',
119
- method,
120
- params,
121
- id: this.idCounter++
122
- });
123
-
124
- if (response.data.error) {
125
- throw new Error(response.data.error.message);
126
- }
127
-
128
- return response.data.result;
129
- }
130
- }
131
-
132
- // Main SDK Client
133
- export class LXDexClient {
134
- private jsonRpc: JSONRPCClient;
135
- private ws: WebSocket | null = null;
136
- private wsCallbacks: Map<string, Function[]> = new Map();
137
- private config: LXDexConfig;
138
- public marketData: MarketDataClient;
139
- public liquidationMonitor: LiquidationMonitor;
140
-
141
- constructor(config: LXDexConfig = {}) {
142
- this.config = {
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'