@luxfi/dex 1.3.0 → 2.0.1

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 (37) hide show
  1. package/dist/hooks/index.d.ts +16 -1
  2. package/dist/hooks/index.d.ts.map +1 -1
  3. package/dist/hooks/index.js +20 -1
  4. package/dist/hooks/use-lxbook.d.ts +84 -0
  5. package/dist/hooks/use-lxbook.d.ts.map +1 -0
  6. package/dist/hooks/use-lxbook.js +213 -0
  7. package/dist/hooks/use-lxfeed.d.ts +42 -0
  8. package/dist/hooks/use-lxfeed.d.ts.map +1 -0
  9. package/dist/hooks/use-lxfeed.js +152 -0
  10. package/dist/hooks/use-lxvault.d.ts +75 -0
  11. package/dist/hooks/use-lxvault.d.ts.map +1 -0
  12. package/dist/hooks/use-lxvault.js +227 -0
  13. package/dist/index.d.ts +38 -21
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +57 -26
  16. package/dist/precompile/abis.d.ts +593 -2
  17. package/dist/precompile/abis.d.ts.map +1 -1
  18. package/dist/precompile/abis.js +458 -2
  19. package/dist/precompile/addresses.d.ts +89 -25
  20. package/dist/precompile/addresses.d.ts.map +1 -1
  21. package/dist/precompile/addresses.js +86 -21
  22. package/dist/precompile/index.d.ts +13 -2
  23. package/dist/precompile/index.d.ts.map +1 -1
  24. package/dist/precompile/index.js +13 -2
  25. package/dist/precompile/types.d.ts +171 -1
  26. package/dist/precompile/types.d.ts.map +1 -1
  27. package/dist/precompile/types.js +67 -0
  28. package/package.json +2 -2
  29. package/src/hooks/index.ts +24 -1
  30. package/src/hooks/use-lxbook.ts +343 -0
  31. package/src/hooks/use-lxfeed.ts +179 -0
  32. package/src/hooks/use-lxvault.ts +318 -0
  33. package/src/index.ts +92 -26
  34. package/src/precompile/abis.ts +466 -2
  35. package/src/precompile/addresses.ts +109 -28
  36. package/src/precompile/index.ts +13 -2
  37. package/src/precompile/types.ts +200 -1
@@ -0,0 +1,318 @@
1
+ /**
2
+ * LXVault Hooks (LP-9030)
3
+ * React hooks for custody, margin, and positions via LXVault precompile
4
+ */
5
+ import { useCallback } from 'react'
6
+ import { useReadContract, useWriteContract, useWaitForTransactionReceipt, useAccount } from 'wagmi'
7
+ import type { Address } from 'viem'
8
+ import { LX } from '../precompile/addresses'
9
+ import { LX_VAULT_ABI } from '../precompile/abis'
10
+ import type { LXAccount, LXPosition, LXMarginInfo } from '../precompile/types'
11
+
12
+ /**
13
+ * Build account tuple from address and subaccount
14
+ */
15
+ function buildAccount(main: Address, subaccountId: number = 0): LXAccount {
16
+ return { main, subaccountId }
17
+ }
18
+
19
+ /**
20
+ * Hook to get token balance in vault
21
+ */
22
+ export function useLXVaultBalance(token: Address, subaccountId: number = 0): any {
23
+ const { address } = useAccount()
24
+
25
+ const { data, isLoading, error, refetch } = useReadContract({
26
+ address: LX.LX_VAULT,
27
+ abi: LX_VAULT_ABI,
28
+ functionName: 'getBalance',
29
+ args: address ? [buildAccount(address, subaccountId), token] : undefined,
30
+ query: { enabled: !!address },
31
+ })
32
+
33
+ return {
34
+ balance: data as bigint | undefined,
35
+ isLoading,
36
+ error,
37
+ refetch,
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Hook to get position for a market
43
+ */
44
+ export function useLXVaultPosition(marketId: number, subaccountId: number = 0): any {
45
+ const { address } = useAccount()
46
+
47
+ const { data, isLoading, error, refetch } = useReadContract({
48
+ address: LX.LX_VAULT,
49
+ abi: LX_VAULT_ABI,
50
+ functionName: 'getPosition',
51
+ args: address ? [buildAccount(address, subaccountId), marketId] : undefined,
52
+ query: { enabled: !!address },
53
+ })
54
+
55
+ return {
56
+ position: data as LXPosition | undefined,
57
+ isLoading,
58
+ error,
59
+ refetch,
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Hook to get margin info (free margin, used margin, etc.)
65
+ */
66
+ export function useLXVaultMargin(subaccountId: number = 0): any {
67
+ const { address } = useAccount()
68
+
69
+ const { data, isLoading, error, refetch } = useReadContract({
70
+ address: LX.LX_VAULT,
71
+ abi: LX_VAULT_ABI,
72
+ functionName: 'getMargin',
73
+ args: address ? [buildAccount(address, subaccountId)] : undefined,
74
+ query: { enabled: !!address },
75
+ })
76
+
77
+ return {
78
+ margin: data as LXMarginInfo | undefined,
79
+ isLoading,
80
+ error,
81
+ refetch,
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Hook to check if account is liquidatable
87
+ */
88
+ export function useLXVaultLiquidatable(account?: Address, subaccountId: number = 0): any {
89
+ const { address: connectedAddress } = useAccount()
90
+ const targetAddress = account ?? connectedAddress
91
+
92
+ const { data, isLoading, error, refetch } = useReadContract({
93
+ address: LX.LX_VAULT,
94
+ abi: LX_VAULT_ABI,
95
+ functionName: 'isLiquidatable',
96
+ args: targetAddress ? [buildAccount(targetAddress, subaccountId)] : undefined,
97
+ query: { enabled: !!targetAddress },
98
+ })
99
+
100
+ const result = data as [boolean, bigint] | undefined
101
+
102
+ return {
103
+ liquidatable: result?.[0],
104
+ shortfall: result?.[1],
105
+ isLoading,
106
+ error,
107
+ refetch,
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Hook to get funding rate for a market
113
+ */
114
+ export function useLXVaultFundingRate(marketId: number): any {
115
+ const { data, isLoading, error, refetch } = useReadContract({
116
+ address: LX.LX_VAULT,
117
+ abi: LX_VAULT_ABI,
118
+ functionName: 'getFundingRate',
119
+ args: [marketId],
120
+ })
121
+
122
+ const result = data as [bigint, bigint] | undefined
123
+
124
+ return {
125
+ rateX18: result?.[0],
126
+ nextFundingTime: result?.[1],
127
+ isLoading,
128
+ error,
129
+ refetch,
130
+ }
131
+ }
132
+
133
+ interface UseLXVaultDepositResult {
134
+ deposit: (token: Address, amount: bigint, subaccountId?: number) => void
135
+ hash: `0x${string}` | undefined
136
+ isPending: boolean
137
+ isConfirming: boolean
138
+ isSuccess: boolean
139
+ error: Error | null
140
+ }
141
+
142
+ /**
143
+ * Hook for depositing tokens into vault
144
+ */
145
+ export function useLXVaultDeposit(): UseLXVaultDepositResult {
146
+ const { writeContract, data: hash, isPending, error } = useWriteContract()
147
+ const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash })
148
+
149
+ const deposit = useCallback(
150
+ (token: Address, amount: bigint, subaccountId: number = 0) => {
151
+ writeContract({
152
+ address: LX.LX_VAULT,
153
+ abi: LX_VAULT_ABI,
154
+ functionName: 'deposit',
155
+ args: [token, amount, subaccountId],
156
+ })
157
+ },
158
+ [writeContract]
159
+ )
160
+
161
+ return {
162
+ deposit,
163
+ hash,
164
+ isPending,
165
+ isConfirming,
166
+ isSuccess,
167
+ error,
168
+ }
169
+ }
170
+
171
+ interface UseLXVaultWithdrawResult {
172
+ withdraw: (token: Address, amount: bigint, subaccountId?: number) => void
173
+ hash: `0x${string}` | undefined
174
+ isPending: boolean
175
+ isConfirming: boolean
176
+ isSuccess: boolean
177
+ error: Error | null
178
+ }
179
+
180
+ /**
181
+ * Hook for withdrawing tokens from vault
182
+ */
183
+ export function useLXVaultWithdraw(): UseLXVaultWithdrawResult {
184
+ const { writeContract, data: hash, isPending, error } = useWriteContract()
185
+ const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash })
186
+
187
+ const withdraw = useCallback(
188
+ (token: Address, amount: bigint, subaccountId: number = 0) => {
189
+ writeContract({
190
+ address: LX.LX_VAULT,
191
+ abi: LX_VAULT_ABI,
192
+ functionName: 'withdraw',
193
+ args: [token, amount, subaccountId],
194
+ })
195
+ },
196
+ [writeContract]
197
+ )
198
+
199
+ return {
200
+ withdraw,
201
+ hash,
202
+ isPending,
203
+ isConfirming,
204
+ isSuccess,
205
+ error,
206
+ }
207
+ }
208
+
209
+ interface UseLXVaultTransferResult {
210
+ transfer: (token: Address, amount: bigint, fromSubaccount: number, toSubaccount: number) => void
211
+ hash: `0x${string}` | undefined
212
+ isPending: boolean
213
+ isConfirming: boolean
214
+ isSuccess: boolean
215
+ error: Error | null
216
+ }
217
+
218
+ /**
219
+ * Hook for transferring between subaccounts
220
+ */
221
+ export function useLXVaultTransfer(): UseLXVaultTransferResult {
222
+ const { writeContract, data: hash, isPending, error } = useWriteContract()
223
+ const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash })
224
+
225
+ const transfer = useCallback(
226
+ (
227
+ token: Address,
228
+ amount: bigint,
229
+ fromSubaccount: number,
230
+ toSubaccount: number
231
+ ) => {
232
+ writeContract({
233
+ address: LX.LX_VAULT,
234
+ abi: LX_VAULT_ABI,
235
+ functionName: 'transfer',
236
+ args: [token, amount, fromSubaccount, toSubaccount],
237
+ })
238
+ },
239
+ [writeContract]
240
+ )
241
+
242
+ return {
243
+ transfer,
244
+ hash,
245
+ isPending,
246
+ isConfirming,
247
+ isSuccess,
248
+ error,
249
+ }
250
+ }
251
+
252
+ interface UseLXVaultLiquidateResult {
253
+ liquidate: (targetAccount: Address, targetSubaccount: number, marketId: number, sizeX18: bigint) => void
254
+ hash: `0x${string}` | undefined
255
+ isPending: boolean
256
+ isConfirming: boolean
257
+ isSuccess: boolean
258
+ error: Error | null
259
+ }
260
+
261
+ /**
262
+ * Hook for liquidating underwater positions
263
+ */
264
+ export function useLXVaultLiquidate(): UseLXVaultLiquidateResult {
265
+ const { writeContract, data: hash, isPending, error } = useWriteContract()
266
+ const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash })
267
+
268
+ const liquidate = useCallback(
269
+ (
270
+ targetAccount: Address,
271
+ targetSubaccount: number,
272
+ marketId: number,
273
+ sizeX18: bigint
274
+ ) => {
275
+ writeContract({
276
+ address: LX.LX_VAULT,
277
+ abi: LX_VAULT_ABI,
278
+ functionName: 'liquidate',
279
+ args: [buildAccount(targetAccount, targetSubaccount), marketId, sizeX18],
280
+ })
281
+ },
282
+ [writeContract]
283
+ )
284
+
285
+ return {
286
+ liquidate,
287
+ hash,
288
+ isPending,
289
+ isConfirming,
290
+ isSuccess,
291
+ error,
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Combined hook for common vault operations
297
+ */
298
+ export function useLXVault(subaccountId: number = 0): any {
299
+ const { address } = useAccount()
300
+ const margin = useLXVaultMargin(subaccountId)
301
+ const { deposit, isPending: isDepositing } = useLXVaultDeposit()
302
+ const { withdraw, isPending: isWithdrawing } = useLXVaultWithdraw()
303
+ const { transfer, isPending: isTransferring } = useLXVaultTransfer()
304
+
305
+ return {
306
+ address,
307
+ subaccountId,
308
+ margin: margin.margin,
309
+ isLoadingMargin: margin.isLoading,
310
+ deposit,
311
+ withdraw,
312
+ transfer,
313
+ isDepositing,
314
+ isWithdrawing,
315
+ isTransferring,
316
+ refetchMargin: margin.refetch,
317
+ }
318
+ }
package/src/index.ts CHANGED
@@ -1,36 +1,56 @@
1
1
  /**
2
2
  * @luxfi/dex
3
3
  *
4
- * DEX Integration Package
4
+ * LX Integration Package
5
5
  *
6
- * Provides routing between:
7
- * - CLOB (Central Limit Order Book) via ~/work/lux/dex
8
- * - AMM Precompiles (0x0400-0x0403) for native swaps
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
9
15
  *
10
16
  * Architecture:
11
17
  * ```
12
- * ┌────────────────────────────────────────┐
13
- * │ Omnichain Router
14
- * │ Best execution between CLOB & AMM
15
- * └─────────────────┬──────────────────────┘
16
- *
17
- * ┌─────────────┴─────────────┐
18
- *
19
- *
20
- * ┌─────────┐ ┌───────────────┐
21
- * │ CLOB AMM Precompile
22
- * │ Client (0x0400)
23
- * │
24
- * │ • Limit │ • Swap
25
- * │ • HFT │ • Liquidity
26
- * │ • Perps │ • Flash │
27
- * └─────────┘ └───────────────┘
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
+ * └─────────────┘
28
45
  * ```
29
46
  */
30
47
 
31
- // Precompile types and ABIs (AMM)
48
+ // =============================================================================
49
+ // Precompile Types, ABIs, and Addresses
50
+ // =============================================================================
51
+
32
52
  export {
33
- // Types
53
+ // AMM Types (LP-9010)
34
54
  type Currency,
35
55
  type PoolKey,
36
56
  type BalanceDelta,
@@ -41,17 +61,57 @@ export {
41
61
  NATIVE_LUX,
42
62
  sortCurrencies,
43
63
  createPoolKey,
44
- // ABIs
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
45
89
  POOL_MANAGER_ABI,
46
90
  SWAP_ROUTER_ABI,
47
91
  HOOKS_REGISTRY_ABI,
48
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
+
49
100
  // Addresses
101
+ LX,
50
102
  DEX_PRECOMPILES,
103
+ type LxdexPrecompile,
51
104
  type DexPrecompile,
105
+ fromLP,
106
+ toLP,
107
+ isDEXPrecompile,
108
+ isBridgePrecompile,
52
109
  } from './precompile'
53
110
 
54
- // CLOB client (exports Position as CLOBPosition to avoid conflict)
111
+ // =============================================================================
112
+ // CLOB Client (External ~/work/lux/dex integration)
113
+ // =============================================================================
114
+
55
115
  export {
56
116
  type OrderSide,
57
117
  type OrderType,
@@ -69,8 +129,14 @@ export {
69
129
  createCLOBClient,
70
130
  } from './client'
71
131
 
72
- // Omnichain router
132
+ // =============================================================================
133
+ // Omnichain Router
134
+ // =============================================================================
135
+
73
136
  export * from './router'
74
137
 
75
- // React hooks
138
+ // =============================================================================
139
+ // React Hooks
140
+ // =============================================================================
141
+
76
142
  export * from './hooks'