@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
@@ -0,0 +1,153 @@
1
+ /**
2
+ * LX Precompile Addresses (LP-Aligned)
3
+ * Native Go implementation at EVM level
4
+ *
5
+ * LX is the umbrella name for the Lux DEX stack:
6
+ * - AMM (Lux v4-style)
7
+ * - CLOB (Hyperliquid-style)
8
+ * - Vaults, Feeds, Routing
9
+ *
10
+ * LP-Aligned Address Format (LP-9015):
11
+ * - Address = 0x0000000000000000000000000000000000LPNUM
12
+ * - LP number directly visible as trailing hex digits
13
+ * - Example: LP-9010 → 0x0000...009010
14
+ *
15
+ * @see LP-9015 (Precompile Registry) for canonical spec
16
+ * @see ~/work/lux/precompile/dex/module.go for Go implementation
17
+ * @see ~/work/lux/lps/LPs/lp-9015-precompile-registry.md
18
+ */
19
+ import type { Address } from 'viem'
20
+
21
+ /**
22
+ * LX Precompile addresses (LP-9xxx range)
23
+ * LX prefix naming convention for developer-facing contracts
24
+ * Address format: 0x0000000000000000000000000000000000LPNUM
25
+ */
26
+ export const LX = {
27
+ /**
28
+ * LXPool (LP-9010)
29
+ * v4 PoolManager-compatible core AMM
30
+ * - Initialize pools
31
+ * - Execute swaps
32
+ * - Modify liquidity
33
+ * - Flash accounting settlement
34
+ */
35
+ LX_POOL: '0x0000000000000000000000000000000000009010' as Address,
36
+
37
+ /**
38
+ * LXOracle (LP-9011)
39
+ * Multi-source price aggregation
40
+ * - Chainlink, Pyth, TWAP aggregation
41
+ * - Price feed validation
42
+ */
43
+ LX_ORACLE: '0x0000000000000000000000000000000000009011' as Address,
44
+
45
+ /**
46
+ * LXRouter (LP-9012)
47
+ * Optimized swap routing
48
+ * - exactInputSingle / exactOutputSingle
49
+ * - Multi-hop swaps
50
+ * - Native LUX support
51
+ */
52
+ LX_ROUTER: '0x0000000000000000000000000000000000009012' as Address,
53
+
54
+ /**
55
+ * LXHooks (LP-9013)
56
+ * Hook contract registry
57
+ * - Register hook contracts
58
+ * - Query hook permissions
59
+ */
60
+ LX_HOOKS: '0x0000000000000000000000000000000000009013' as Address,
61
+
62
+ /**
63
+ * LXFlash (LP-9014)
64
+ * Flash loan facility
65
+ * - Borrow any token
66
+ * - Repay in same transaction
67
+ */
68
+ LX_FLASH: '0x0000000000000000000000000000000000009014' as Address,
69
+
70
+ /**
71
+ * LXBook (LP-9020)
72
+ * Permissionless orderbooks + matching + advanced orders
73
+ * - Market factory (createMarket)
74
+ * - Order lifecycle (place/cancel/modify)
75
+ * - Advanced orders (trigger, TWAP)
76
+ * - Book views (L1, order info)
77
+ */
78
+ LX_BOOK: '0x0000000000000000000000000000000000009020' as Address,
79
+
80
+ /**
81
+ * LXVault (LP-9030)
82
+ * Balances, margin, collateral, liquidations
83
+ * - Token custody
84
+ * - Margin requirements
85
+ * - Position liquidations
86
+ */
87
+ LX_VAULT: '0x0000000000000000000000000000000000009030' as Address,
88
+
89
+ /**
90
+ * LXFeed (LP-9040)
91
+ * Price feed aggregator
92
+ */
93
+ LX_FEED: '0x0000000000000000000000000000000000009040' as Address,
94
+
95
+ /**
96
+ * Teleport Bridge (LP-6010)
97
+ * Cross-chain asset teleportation
98
+ */
99
+ TELEPORT: '0x0000000000000000000000000000000000006010' as Address,
100
+ } as const
101
+
102
+ /**
103
+ * Backwards compatibility - old naming convention
104
+ * @deprecated Use LX export instead
105
+ */
106
+ export const DEX_PRECOMPILES = {
107
+ POOL_MANAGER: LX.LX_POOL,
108
+ ORACLE_HUB: LX.LX_ORACLE,
109
+ SWAP_ROUTER: LX.LX_ROUTER,
110
+ HOOKS_REGISTRY: LX.LX_HOOKS,
111
+ FLASH_LOAN: LX.LX_FLASH,
112
+ CLOB: LX.LX_BOOK,
113
+ VAULT: LX.LX_VAULT,
114
+ PRICE_FEED: LX.LX_FEED,
115
+ TELEPORT: LX.TELEPORT,
116
+ } as const
117
+
118
+ export type LxdexPrecompile = keyof typeof LX
119
+ export type DexPrecompile = keyof typeof DEX_PRECOMPILES
120
+
121
+ /**
122
+ * Generate precompile address from LP number
123
+ * @param lpNumber - The LP number (e.g., 9010)
124
+ * @returns The precompile address
125
+ */
126
+ export function fromLP(lpNumber: number): Address {
127
+ return `0x${lpNumber.toString(16).padStart(40, '0')}` as Address
128
+ }
129
+
130
+ /**
131
+ * Extract LP number from precompile address
132
+ * @param address - The precompile address
133
+ * @returns The LP number
134
+ */
135
+ export function toLP(address: Address): number {
136
+ return parseInt(address.slice(-4), 16)
137
+ }
138
+
139
+ /**
140
+ * Check if address is a DEX precompile (LP-9xxx range)
141
+ */
142
+ export function isDEXPrecompile(address: Address): boolean {
143
+ const lp = toLP(address)
144
+ return lp >= 9000 && lp < 10000
145
+ }
146
+
147
+ /**
148
+ * Check if address is a Bridge precompile (LP-6xxx range)
149
+ */
150
+ export function isBridgePrecompile(address: Address): boolean {
151
+ const lp = toLP(address)
152
+ return lp >= 6000 && lp < 7000
153
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * LX Precompile Exports (LP-Aligned)
3
+ *
4
+ * Native precompiles for Lux DEX stack:
5
+ * - LXPool (LP-9010): v4-style AMM PoolManager
6
+ * - LXOracle (LP-9011): Multi-source price aggregation
7
+ * - LXRouter (LP-9012): Optimized swap routing
8
+ * - LXHooks (LP-9013): Hook contract registry
9
+ * - LXFlash (LP-9014): Flash loan facility
10
+ * - LXBook (LP-9020): CLOB matching engine
11
+ * - LXVault (LP-9030): Custody and margin engine
12
+ * - LXFeed (LP-9040): Mark price and funding feeds
13
+ *
14
+ * Address format: 0x0000000000000000000000000000000000LPNUM
15
+ */
16
+ export * from './types'
17
+ export * from './abis'
18
+ export * from './addresses'
@@ -0,0 +1,295 @@
1
+ /**
2
+ * DEX Precompile Types
3
+ * Native Lux v4-style AMM implementation
4
+ */
5
+ import type { Address } from 'viem'
6
+
7
+ /**
8
+ * Currency type - address(0) = native LUX
9
+ */
10
+ export interface Currency {
11
+ address: Address
12
+ }
13
+
14
+ /**
15
+ * Pool key uniquely identifies a pool
16
+ */
17
+ export interface PoolKey {
18
+ currency0: Address // Lower address token (sorted)
19
+ currency1: Address // Higher address token (sorted)
20
+ fee: number // Fee in basis points (3000 = 0.30%)
21
+ tickSpacing: number // Tick spacing for concentrated liquidity
22
+ hooks: Address // Hook contract (address(0) = no hooks)
23
+ }
24
+
25
+ /**
26
+ * Balance delta from swap/liquidity operations
27
+ * Positive = user owes pool, Negative = pool owes user
28
+ */
29
+ export interface BalanceDelta {
30
+ amount0: bigint
31
+ amount1: bigint
32
+ }
33
+
34
+ /**
35
+ * Swap parameters
36
+ */
37
+ export interface SwapParams {
38
+ zeroForOne: boolean // true = swap token0 for token1
39
+ amountSpecified: bigint // Positive = exact input, Negative = exact output
40
+ sqrtPriceLimitX96: bigint // Price limit (0 = no limit)
41
+ }
42
+
43
+ /**
44
+ * Modify liquidity parameters
45
+ */
46
+ export interface ModifyLiquidityParams {
47
+ tickLower: number
48
+ tickUpper: number
49
+ liquidityDelta: bigint
50
+ salt: `0x${string}`
51
+ }
52
+
53
+ /**
54
+ * Pool state
55
+ */
56
+ export interface PoolState {
57
+ sqrtPriceX96: bigint
58
+ tick: number
59
+ protocolFee: number
60
+ lpFee: number
61
+ }
62
+
63
+ /**
64
+ * Position info
65
+ */
66
+ export interface Position {
67
+ liquidity: bigint
68
+ feeGrowthInside0LastX128: bigint
69
+ feeGrowthInside1LastX128: bigint
70
+ }
71
+
72
+ /**
73
+ * Native LUX currency constant
74
+ */
75
+ export const NATIVE_LUX: Address = '0x0000000000000000000000000000000000000000'
76
+
77
+ /**
78
+ * Sort currencies for pool key creation
79
+ */
80
+ export function sortCurrencies(a: Address, b: Address): [Address, Address] {
81
+ return a.toLowerCase() < b.toLowerCase() ? [a, b] : [b, a]
82
+ }
83
+
84
+ /**
85
+ * Create a pool key from two currencies
86
+ */
87
+ export function createPoolKey(
88
+ tokenA: Address,
89
+ tokenB: Address,
90
+ fee: number = 3000,
91
+ tickSpacing: number = 60,
92
+ hooks: Address = '0x0000000000000000000000000000000000000000'
93
+ ): PoolKey {
94
+ const [currency0, currency1] = sortCurrencies(tokenA, tokenB)
95
+ return { currency0, currency1, fee, tickSpacing, hooks }
96
+ }
97
+
98
+ // ============================================================================
99
+ // LXBook Types (LP-9020) - CLOB Matching Engine
100
+ // ============================================================================
101
+
102
+ /**
103
+ * Order time-in-force
104
+ */
105
+ export enum TIF {
106
+ GTC = 0, // Good-til-canceled (resting)
107
+ IOC = 1, // Immediate-or-cancel
108
+ ALO = 2, // Add-liquidity-only (post-only)
109
+ }
110
+
111
+ /**
112
+ * Order kind
113
+ */
114
+ export enum OrderKind {
115
+ LIMIT = 0,
116
+ MARKET = 1,
117
+ STOP_MARKET = 2,
118
+ STOP_LIMIT = 3,
119
+ TAKE_MARKET = 4,
120
+ TAKE_LIMIT = 5,
121
+ }
122
+
123
+ /**
124
+ * Order group type
125
+ */
126
+ export enum GroupType {
127
+ NONE = 0,
128
+ OCO = 1, // One-cancels-other
129
+ BRACKET = 2, // Bracket order
130
+ }
131
+
132
+ /**
133
+ * Action type for execute() endpoint
134
+ */
135
+ export enum ActionType {
136
+ PLACE = 0,
137
+ CANCEL = 1,
138
+ CANCEL_BY_CLOID = 2,
139
+ MODIFY = 3,
140
+ TWAP_CREATE = 4,
141
+ TWAP_CANCEL = 5,
142
+ SCHEDULE_CANCEL = 6,
143
+ NOOP = 7,
144
+ RESERVE_WEIGHT = 8,
145
+ }
146
+
147
+ /**
148
+ * LXBook Order (LP-9020)
149
+ */
150
+ export interface LXOrder {
151
+ marketId: number
152
+ isBuy: boolean
153
+ kind: OrderKind
154
+ sizeX18: bigint
155
+ limitPxX18: bigint
156
+ triggerPxX18: bigint
157
+ reduceOnly: boolean
158
+ tif: TIF
159
+ cloid: `0x${string}`
160
+ groupId: `0x${string}`
161
+ groupType: GroupType
162
+ }
163
+
164
+ /**
165
+ * LXBook Action (execute() payload)
166
+ */
167
+ export interface LXAction {
168
+ actionType: ActionType
169
+ nonce: bigint
170
+ expiresAfter: bigint
171
+ data: `0x${string}`
172
+ }
173
+
174
+ /**
175
+ * LXBook PlaceResult
176
+ */
177
+ export interface LXPlaceResult {
178
+ oid: bigint
179
+ status: number // 0=rejected, 1=filled, 2=resting, 3=partial
180
+ filledSizeX18: bigint
181
+ avgPxX18: bigint
182
+ }
183
+
184
+ /**
185
+ * LXBook L1 (best bid/ask)
186
+ */
187
+ export interface LXL1 {
188
+ bestBidPxX18: bigint
189
+ bestBidSzX18: bigint
190
+ bestAskPxX18: bigint
191
+ bestAskSzX18: bigint
192
+ lastTradePxX18: bigint
193
+ }
194
+
195
+ // ============================================================================
196
+ // LXVault Types (LP-9030) - Custody and Risk Engine
197
+ // ============================================================================
198
+
199
+ /**
200
+ * Margin mode
201
+ */
202
+ export enum MarginMode {
203
+ CROSS = 0,
204
+ ISOLATED = 1,
205
+ }
206
+
207
+ /**
208
+ * Position side
209
+ */
210
+ export enum PositionSide {
211
+ LONG = 0,
212
+ SHORT = 1,
213
+ }
214
+
215
+ /**
216
+ * LXVault Account identifier
217
+ */
218
+ export interface LXAccount {
219
+ main: Address
220
+ subaccountId: number
221
+ }
222
+
223
+ /**
224
+ * LXVault Position (LP-9030)
225
+ */
226
+ export interface LXPosition {
227
+ marketId: number
228
+ side: PositionSide
229
+ sizeX18: bigint
230
+ entryPxX18: bigint
231
+ unrealizedPnlX18: bigint
232
+ accumulatedFundingX18: bigint
233
+ lastFundingTime: bigint
234
+ }
235
+
236
+ /**
237
+ * LXVault MarginInfo
238
+ */
239
+ export interface LXMarginInfo {
240
+ totalCollateralX18: bigint
241
+ usedMarginX18: bigint
242
+ freeMarginX18: bigint
243
+ marginRatioX18: bigint
244
+ maintenanceMarginX18: bigint
245
+ liquidatable: boolean
246
+ }
247
+
248
+ /**
249
+ * LXVault Settlement (LXBook → LXVault)
250
+ */
251
+ export interface LXSettlement {
252
+ maker: LXAccount
253
+ taker: LXAccount
254
+ marketId: number
255
+ takerIsBuy: boolean
256
+ sizeX18: bigint
257
+ priceX18: bigint
258
+ makerFeeX18: bigint
259
+ takerFeeX18: bigint
260
+ }
261
+
262
+ /**
263
+ * LXVault LiquidationResult
264
+ */
265
+ export interface LXLiquidationResult {
266
+ liquidated: LXAccount
267
+ liquidator: LXAccount
268
+ marketId: number
269
+ sizeX18: bigint
270
+ priceX18: bigint
271
+ penaltyX18: bigint
272
+ adlTriggered: boolean
273
+ }
274
+
275
+ // ============================================================================
276
+ // LXFeed Types (LP-9040) - Price Feeds
277
+ // ============================================================================
278
+
279
+ /**
280
+ * LXFeed Mark price
281
+ */
282
+ export interface LXMarkPrice {
283
+ indexPxX18: bigint
284
+ markPxX18: bigint
285
+ premiumX18: bigint
286
+ timestamp: bigint
287
+ }
288
+
289
+ /**
290
+ * LXFeed Funding rate
291
+ */
292
+ export interface LXFundingRate {
293
+ rateX18: bigint
294
+ nextFundingTime: bigint
295
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Router Exports
3
+ * Omnichain routing between CLOB and AMM
4
+ */
5
+ export * from './types'
6
+ export * from './router'