@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/dist/index.js
CHANGED
|
@@ -1,225 +1,72 @@
|
|
|
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
|
-
throw new Error(response.data.error.message);
|
|
74
|
-
}
|
|
75
|
-
return response.data.result;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
// Main SDK Client
|
|
79
|
-
class LXDexClient {
|
|
80
|
-
constructor(config = {}) {
|
|
81
|
-
this.ws = null;
|
|
82
|
-
this.wsCallbacks = new Map();
|
|
83
|
-
this.config = {
|
|
84
|
-
jsonRpcUrl: config.jsonRpcUrl || 'http://localhost:8080',
|
|
85
|
-
wsUrl: config.wsUrl || 'ws://localhost:8081',
|
|
86
|
-
...config
|
|
87
|
-
};
|
|
88
|
-
this.jsonRpc = new JSONRPCClient(this.config.jsonRpcUrl);
|
|
89
|
-
// Initialize sub-clients
|
|
90
|
-
this.marketData = new marketData_1.MarketDataClient(this.jsonRpc);
|
|
91
|
-
this.liquidationMonitor = new marketData_1.LiquidationMonitor(null);
|
|
92
|
-
}
|
|
93
|
-
// Connection Management
|
|
94
|
-
async connect() {
|
|
95
|
-
if (this.ws)
|
|
96
|
-
return;
|
|
97
|
-
return new Promise((resolve, reject) => {
|
|
98
|
-
this.ws = new ws_1.default(this.config.wsUrl);
|
|
99
|
-
this.ws.on('open', () => {
|
|
100
|
-
console.log('WebSocket connected');
|
|
101
|
-
// Set WebSocket for liquidation monitor
|
|
102
|
-
this.liquidationMonitor.setWebSocket(this.ws);
|
|
103
|
-
resolve();
|
|
104
|
-
});
|
|
105
|
-
this.ws.on('message', (data) => {
|
|
106
|
-
try {
|
|
107
|
-
const msg = JSON.parse(data);
|
|
108
|
-
this.handleWebSocketMessage(msg);
|
|
109
|
-
}
|
|
110
|
-
catch (err) {
|
|
111
|
-
console.error('Failed to parse WebSocket message:', err);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
this.ws.on('error', (err) => {
|
|
115
|
-
console.error('WebSocket error:', err);
|
|
116
|
-
reject(err);
|
|
117
|
-
});
|
|
118
|
-
this.ws.on('close', () => {
|
|
119
|
-
console.log('WebSocket disconnected');
|
|
120
|
-
this.ws = null;
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
disconnect() {
|
|
125
|
-
if (this.ws) {
|
|
126
|
-
this.ws.close();
|
|
127
|
-
this.ws = null;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
// Order Management
|
|
131
|
-
async placeOrder(order) {
|
|
132
|
-
return this.jsonRpc.call('lx_placeOrder', order);
|
|
133
|
-
}
|
|
134
|
-
async cancelOrder(orderId) {
|
|
135
|
-
return this.jsonRpc.call('lx_cancelOrder', { orderId });
|
|
136
|
-
}
|
|
137
|
-
async getOrder(orderId) {
|
|
138
|
-
return this.jsonRpc.call('lx_getOrder', { orderId });
|
|
139
|
-
}
|
|
140
|
-
// Market Data
|
|
141
|
-
async getOrderBook(symbol = 'BTC-USD', depth = 10) {
|
|
142
|
-
return this.jsonRpc.call('lx_getOrderBook', { symbol, depth });
|
|
143
|
-
}
|
|
144
|
-
async getBestBid(symbol = 'BTC-USD') {
|
|
145
|
-
const result = await this.jsonRpc.call('lx_getBestBid', { symbol });
|
|
146
|
-
return result.price;
|
|
147
|
-
}
|
|
148
|
-
async getBestAsk(symbol = 'BTC-USD') {
|
|
149
|
-
const result = await this.jsonRpc.call('lx_getBestAsk', { symbol });
|
|
150
|
-
return result.price;
|
|
151
|
-
}
|
|
152
|
-
async getTrades(symbol = 'BTC-USD', limit = 100) {
|
|
153
|
-
return this.jsonRpc.call('lx_getTrades', { symbol, limit });
|
|
154
|
-
}
|
|
155
|
-
// Node Information
|
|
156
|
-
async getInfo() {
|
|
157
|
-
return this.jsonRpc.call('lx_getInfo');
|
|
158
|
-
}
|
|
159
|
-
async ping() {
|
|
160
|
-
return this.jsonRpc.call('lx_ping');
|
|
161
|
-
}
|
|
162
|
-
// WebSocket Subscriptions
|
|
163
|
-
subscribe(channel, callback) {
|
|
164
|
-
if (!this.wsCallbacks.has(channel)) {
|
|
165
|
-
this.wsCallbacks.set(channel, []);
|
|
166
|
-
}
|
|
167
|
-
this.wsCallbacks.get(channel).push(callback);
|
|
168
|
-
// Send subscription message
|
|
169
|
-
if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
|
|
170
|
-
this.ws.send(JSON.stringify({
|
|
171
|
-
type: 'subscribe',
|
|
172
|
-
channel
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
unsubscribe(channel, callback) {
|
|
177
|
-
if (callback) {
|
|
178
|
-
const callbacks = this.wsCallbacks.get(channel);
|
|
179
|
-
if (callbacks) {
|
|
180
|
-
const index = callbacks.indexOf(callback);
|
|
181
|
-
if (index > -1) {
|
|
182
|
-
callbacks.splice(index, 1);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
this.wsCallbacks.delete(channel);
|
|
188
|
-
}
|
|
189
|
-
// Send unsubscribe message
|
|
190
|
-
if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
|
|
191
|
-
this.ws.send(JSON.stringify({
|
|
192
|
-
type: 'unsubscribe',
|
|
193
|
-
channel
|
|
194
|
-
}));
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
subscribeOrderBook(symbol, callback) {
|
|
198
|
-
this.subscribe(`orderbook:${symbol}`, callback);
|
|
199
|
-
}
|
|
200
|
-
subscribeTrades(symbol, callback) {
|
|
201
|
-
this.subscribe(`trades:${symbol}`, callback);
|
|
202
|
-
}
|
|
203
|
-
// Private methods
|
|
204
|
-
handleWebSocketMessage(msg) {
|
|
205
|
-
const { channel, data } = msg;
|
|
206
|
-
const callbacks = this.wsCallbacks.get(channel);
|
|
207
|
-
if (callbacks) {
|
|
208
|
-
callbacks.forEach(cb => cb(data));
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
// Utility methods
|
|
212
|
-
static formatPrice(price, decimals = 2) {
|
|
213
|
-
return price.toFixed(decimals);
|
|
214
|
-
}
|
|
215
|
-
static formatSize(size, decimals = 8) {
|
|
216
|
-
return size.toFixed(decimals);
|
|
217
|
-
}
|
|
218
|
-
static calculateTotal(price, size) {
|
|
219
|
-
return price * size;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
exports.LXDexClient = LXDexClient;
|
|
223
|
-
// Export everything
|
|
224
|
-
exports.default = LXDexClient;
|
|
225
|
-
__exportStar(require("./marketData"), exports);
|
|
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
|
+
// Precompile Types, ABIs, and Addresses
|
|
49
|
+
// =============================================================================
|
|
50
|
+
export { NATIVE_LUX, sortCurrencies, createPoolKey,
|
|
51
|
+
// LXBook Types (LP-9020)
|
|
52
|
+
TIF, OrderKind, GroupType, ActionType,
|
|
53
|
+
// LXVault Types (LP-9030)
|
|
54
|
+
MarginMode, PositionSide,
|
|
55
|
+
// AMM ABIs
|
|
56
|
+
POOL_MANAGER_ABI, SWAP_ROUTER_ABI, HOOKS_REGISTRY_ABI, FLASH_LOAN_ABI,
|
|
57
|
+
// LX* ABIs
|
|
58
|
+
LX_BOOK_ABI, LX_VAULT_ABI, LX_FEED_ABI, LX_ORACLE_ABI,
|
|
59
|
+
// Addresses
|
|
60
|
+
LX, DEX_PRECOMPILES, fromLP, toLP, isDEXPrecompile, isBridgePrecompile, } from './precompile';
|
|
61
|
+
// =============================================================================
|
|
62
|
+
// CLOB Client (External ~/work/lux/dex integration)
|
|
63
|
+
// =============================================================================
|
|
64
|
+
export { CLOBClient, createCLOBClient, } from './client';
|
|
65
|
+
// =============================================================================
|
|
66
|
+
// Omnichain Router
|
|
67
|
+
// =============================================================================
|
|
68
|
+
export * from './router';
|
|
69
|
+
// =============================================================================
|
|
70
|
+
// React Hooks
|
|
71
|
+
// =============================================================================
|
|
72
|
+
export * from './hooks';
|