@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
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LXBook Hooks (LP-9020)
|
|
3
|
+
* React hooks for CLOB trading via LXBook precompile
|
|
4
|
+
*/
|
|
5
|
+
import { useCallback } from 'react';
|
|
6
|
+
import { useReadContract, useWriteContract, useWaitForTransactionReceipt } from 'wagmi';
|
|
7
|
+
import { encodeFunctionData } from 'viem';
|
|
8
|
+
import { LX } from '../precompile/addresses';
|
|
9
|
+
import { LX_BOOK_ABI } from '../precompile/abis';
|
|
10
|
+
import { ActionType, TIF, OrderKind, } from '../precompile/types';
|
|
11
|
+
/**
|
|
12
|
+
* Hook to get L1 (best bid/ask) for a market
|
|
13
|
+
*/
|
|
14
|
+
export function useLXBookL1(marketId) {
|
|
15
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
16
|
+
address: LX.LX_BOOK,
|
|
17
|
+
abi: LX_BOOK_ABI,
|
|
18
|
+
functionName: 'getL1',
|
|
19
|
+
args: [marketId],
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
l1: data,
|
|
23
|
+
isLoading,
|
|
24
|
+
error,
|
|
25
|
+
refetch,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Hook to get market config
|
|
30
|
+
*/
|
|
31
|
+
export function useLXBookMarket(marketId) {
|
|
32
|
+
const { data, isLoading, error } = useReadContract({
|
|
33
|
+
address: LX.LX_BOOK,
|
|
34
|
+
abi: LX_BOOK_ABI,
|
|
35
|
+
functionName: 'getMarketConfig',
|
|
36
|
+
args: [marketId],
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
config: data,
|
|
40
|
+
isLoading,
|
|
41
|
+
error,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Hook to get market status
|
|
46
|
+
*/
|
|
47
|
+
export function useLXBookMarketStatus(marketId) {
|
|
48
|
+
const { data, isLoading, error } = useReadContract({
|
|
49
|
+
address: LX.LX_BOOK,
|
|
50
|
+
abi: LX_BOOK_ABI,
|
|
51
|
+
functionName: 'getMarketStatus',
|
|
52
|
+
args: [marketId],
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
status: data,
|
|
56
|
+
isLoading,
|
|
57
|
+
error,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Build action payload for execute()
|
|
62
|
+
*/
|
|
63
|
+
function buildAction(actionType, nonce, expiresAfter, data) {
|
|
64
|
+
return {
|
|
65
|
+
actionType,
|
|
66
|
+
nonce,
|
|
67
|
+
expiresAfter,
|
|
68
|
+
data,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Encode order data for placement
|
|
73
|
+
*/
|
|
74
|
+
function encodeOrderData(order) {
|
|
75
|
+
// Pack order into bytes - this matches the Go/Solidity struct layout
|
|
76
|
+
const encoded = encodeFunctionData({
|
|
77
|
+
abi: [{
|
|
78
|
+
type: 'function',
|
|
79
|
+
name: 'encodeOrder',
|
|
80
|
+
inputs: [{
|
|
81
|
+
name: 'order',
|
|
82
|
+
type: 'tuple',
|
|
83
|
+
components: [
|
|
84
|
+
{ name: 'marketId', type: 'uint32' },
|
|
85
|
+
{ name: 'isBuy', type: 'bool' },
|
|
86
|
+
{ name: 'kind', type: 'uint8' },
|
|
87
|
+
{ name: 'sizeX18', type: 'uint128' },
|
|
88
|
+
{ name: 'limitPxX18', type: 'uint128' },
|
|
89
|
+
{ name: 'triggerPxX18', type: 'uint128' },
|
|
90
|
+
{ name: 'reduceOnly', type: 'bool' },
|
|
91
|
+
{ name: 'tif', type: 'uint8' },
|
|
92
|
+
{ name: 'cloid', type: 'bytes16' },
|
|
93
|
+
{ name: 'groupId', type: 'bytes16' },
|
|
94
|
+
{ name: 'groupType', type: 'uint8' },
|
|
95
|
+
],
|
|
96
|
+
}],
|
|
97
|
+
outputs: [],
|
|
98
|
+
stateMutability: 'pure',
|
|
99
|
+
}],
|
|
100
|
+
functionName: 'encodeOrder',
|
|
101
|
+
args: [order],
|
|
102
|
+
});
|
|
103
|
+
// Strip function selector (first 4 bytes)
|
|
104
|
+
return `0x${encoded.slice(10)}`;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Hook for placing orders via LXBook execute()
|
|
108
|
+
*/
|
|
109
|
+
export function useLXBookExecute() {
|
|
110
|
+
const { writeContract, data: hash, isPending, error } = useWriteContract();
|
|
111
|
+
const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
|
|
112
|
+
const execute = useCallback((action) => {
|
|
113
|
+
writeContract({
|
|
114
|
+
address: LX.LX_BOOK,
|
|
115
|
+
abi: LX_BOOK_ABI,
|
|
116
|
+
functionName: 'execute',
|
|
117
|
+
args: [action],
|
|
118
|
+
});
|
|
119
|
+
}, [writeContract]);
|
|
120
|
+
const executeBatch = useCallback((actions) => {
|
|
121
|
+
writeContract({
|
|
122
|
+
address: LX.LX_BOOK,
|
|
123
|
+
abi: LX_BOOK_ABI,
|
|
124
|
+
functionName: 'executeBatch',
|
|
125
|
+
args: [actions],
|
|
126
|
+
});
|
|
127
|
+
}, [writeContract]);
|
|
128
|
+
return {
|
|
129
|
+
execute,
|
|
130
|
+
executeBatch,
|
|
131
|
+
hash,
|
|
132
|
+
isPending,
|
|
133
|
+
isConfirming,
|
|
134
|
+
isSuccess,
|
|
135
|
+
error,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Hook for placing limit orders
|
|
140
|
+
*/
|
|
141
|
+
export function useLXBookPlaceOrder() {
|
|
142
|
+
const { execute, hash, isPending, isConfirming, isSuccess, error } = useLXBookExecute();
|
|
143
|
+
const placeOrder = useCallback(async (params) => {
|
|
144
|
+
const order = {
|
|
145
|
+
marketId: params.marketId,
|
|
146
|
+
isBuy: params.isBuy,
|
|
147
|
+
kind: OrderKind.LIMIT,
|
|
148
|
+
sizeX18: params.size,
|
|
149
|
+
limitPxX18: params.price,
|
|
150
|
+
triggerPxX18: 0n,
|
|
151
|
+
reduceOnly: params.reduceOnly ?? false,
|
|
152
|
+
tif: params.tif ?? TIF.GTC,
|
|
153
|
+
cloid: params.cloid ?? '0x00000000000000000000000000000000',
|
|
154
|
+
groupId: '0x00000000000000000000000000000000',
|
|
155
|
+
groupType: 0,
|
|
156
|
+
};
|
|
157
|
+
const action = buildAction(ActionType.PLACE, BigInt(Date.now()), BigInt(Math.floor(Date.now() / 1000) + 300), // 5 min expiry
|
|
158
|
+
encodeOrderData(order));
|
|
159
|
+
return execute(action);
|
|
160
|
+
}, [execute]);
|
|
161
|
+
const placeMarketOrder = useCallback(async (params) => {
|
|
162
|
+
const order = {
|
|
163
|
+
marketId: params.marketId,
|
|
164
|
+
isBuy: params.isBuy,
|
|
165
|
+
kind: OrderKind.MARKET,
|
|
166
|
+
sizeX18: params.size,
|
|
167
|
+
limitPxX18: 0n, // Market order - no limit
|
|
168
|
+
triggerPxX18: 0n,
|
|
169
|
+
reduceOnly: params.reduceOnly ?? false,
|
|
170
|
+
tif: TIF.IOC,
|
|
171
|
+
cloid: '0x00000000000000000000000000000000',
|
|
172
|
+
groupId: '0x00000000000000000000000000000000',
|
|
173
|
+
groupType: 0,
|
|
174
|
+
};
|
|
175
|
+
const action = buildAction(ActionType.PLACE, BigInt(Date.now()), BigInt(Math.floor(Date.now() / 1000) + 60), // 1 min expiry for market
|
|
176
|
+
encodeOrderData(order));
|
|
177
|
+
return execute(action);
|
|
178
|
+
}, [execute]);
|
|
179
|
+
return {
|
|
180
|
+
placeOrder,
|
|
181
|
+
placeMarketOrder,
|
|
182
|
+
hash,
|
|
183
|
+
isPending,
|
|
184
|
+
isConfirming,
|
|
185
|
+
isSuccess,
|
|
186
|
+
error,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Hook for canceling orders
|
|
191
|
+
*/
|
|
192
|
+
export function useLXBookCancelOrder() {
|
|
193
|
+
const { execute, hash, isPending, isConfirming, isSuccess, error } = useLXBookExecute();
|
|
194
|
+
const cancelByOid = useCallback(async (oid) => {
|
|
195
|
+
// Encode oid as bytes
|
|
196
|
+
const data = `0x${oid.toString(16).padStart(64, '0')}`;
|
|
197
|
+
const action = buildAction(ActionType.CANCEL, BigInt(Date.now()), BigInt(Math.floor(Date.now() / 1000) + 60), data);
|
|
198
|
+
return execute(action);
|
|
199
|
+
}, [execute]);
|
|
200
|
+
const cancelByCloid = useCallback(async (cloid) => {
|
|
201
|
+
const action = buildAction(ActionType.CANCEL_BY_CLOID, BigInt(Date.now()), BigInt(Math.floor(Date.now() / 1000) + 60), cloid);
|
|
202
|
+
return execute(action);
|
|
203
|
+
}, [execute]);
|
|
204
|
+
return {
|
|
205
|
+
cancelByOid,
|
|
206
|
+
cancelByCloid,
|
|
207
|
+
hash,
|
|
208
|
+
isPending,
|
|
209
|
+
isConfirming,
|
|
210
|
+
isSuccess,
|
|
211
|
+
error,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { Address } from 'viem';
|
|
2
|
+
import type { LXMarkPrice } from '../precompile/types';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to get mark price for a market
|
|
5
|
+
*/
|
|
6
|
+
export declare function useLXFeedMarkPrice(marketId: number): {
|
|
7
|
+
markPrice: LXMarkPrice | undefined;
|
|
8
|
+
isLoading: boolean;
|
|
9
|
+
error: import("viem").ReadContractErrorType | null;
|
|
10
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<{
|
|
11
|
+
indexPxX18: bigint;
|
|
12
|
+
markPxX18: bigint;
|
|
13
|
+
premiumX18: bigint;
|
|
14
|
+
timestamp: bigint;
|
|
15
|
+
}, import("viem").ReadContractErrorType>>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Hook to get index price for a market
|
|
19
|
+
*/
|
|
20
|
+
export declare function useLXFeedIndexPrice(marketId: number): {
|
|
21
|
+
priceX18: bigint | undefined;
|
|
22
|
+
timestamp: bigint | undefined;
|
|
23
|
+
isLoading: boolean;
|
|
24
|
+
error: import("viem").ReadContractErrorType | null;
|
|
25
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<readonly [bigint, timestamp: bigint], import("viem").ReadContractErrorType>>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Hook to get funding rate for a market
|
|
29
|
+
*/
|
|
30
|
+
export declare function useLXFeedFundingRate(marketId: number): {
|
|
31
|
+
rateX18: bigint | undefined;
|
|
32
|
+
nextFundingTime: bigint | undefined;
|
|
33
|
+
isLoading: boolean;
|
|
34
|
+
error: import("viem").ReadContractErrorType | null;
|
|
35
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<readonly [bigint, bigint], import("viem").ReadContractErrorType>>;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Hook to get trigger price for stop/take orders
|
|
39
|
+
*/
|
|
40
|
+
export declare function useLXFeedTriggerPrice(marketId: number, isBuy: boolean): {
|
|
41
|
+
triggerPriceX18: bigint | undefined;
|
|
42
|
+
isLoading: boolean;
|
|
43
|
+
error: import("viem").ReadContractErrorType | null;
|
|
44
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<bigint, import("viem").ReadContractErrorType>>;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Hook to get oracle price for token pair
|
|
48
|
+
*/
|
|
49
|
+
export declare function useLXOraclePrice(baseToken: Address, quoteToken: Address): {
|
|
50
|
+
price: {
|
|
51
|
+
price: bigint;
|
|
52
|
+
confidence: bigint;
|
|
53
|
+
timestamp: bigint;
|
|
54
|
+
source: number;
|
|
55
|
+
expo: number;
|
|
56
|
+
} | undefined;
|
|
57
|
+
isLoading: boolean;
|
|
58
|
+
error: import("viem").ReadContractErrorType | null;
|
|
59
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<{
|
|
60
|
+
price: bigint;
|
|
61
|
+
confidence: bigint;
|
|
62
|
+
timestamp: bigint;
|
|
63
|
+
source: number;
|
|
64
|
+
expo: number;
|
|
65
|
+
}, import("viem").ReadContractErrorType>>;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Hook to get aggregated oracle price with deviation info
|
|
69
|
+
*/
|
|
70
|
+
export declare function useLXOracleAggregatedPrice(baseToken: Address, quoteToken: Address, maxStaleness?: bigint): {
|
|
71
|
+
aggregated: {
|
|
72
|
+
price: bigint;
|
|
73
|
+
minPrice: bigint;
|
|
74
|
+
maxPrice: bigint;
|
|
75
|
+
deviation: bigint;
|
|
76
|
+
numSources: bigint;
|
|
77
|
+
timestamp: bigint;
|
|
78
|
+
} | undefined;
|
|
79
|
+
isLoading: boolean;
|
|
80
|
+
error: import("viem").ReadContractErrorType | null;
|
|
81
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<{
|
|
82
|
+
price: bigint;
|
|
83
|
+
minPrice: bigint;
|
|
84
|
+
maxPrice: bigint;
|
|
85
|
+
deviation: bigint;
|
|
86
|
+
numSources: bigint;
|
|
87
|
+
timestamp: bigint;
|
|
88
|
+
}, import("viem").ReadContractErrorType>>;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Hook to check if oracle price is fresh
|
|
92
|
+
*/
|
|
93
|
+
export declare function useLXOraclePriceFresh(baseToken: Address, quoteToken: Address, maxStaleness?: bigint): {
|
|
94
|
+
isFresh: boolean | undefined;
|
|
95
|
+
isLoading: boolean;
|
|
96
|
+
error: import("viem").ReadContractErrorType | null;
|
|
97
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<boolean, import("viem").ReadContractErrorType>>;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Combined hook for market price data
|
|
101
|
+
*/
|
|
102
|
+
export declare function useLXMarketPrices(marketId: number): {
|
|
103
|
+
markPrice: LXMarkPrice | undefined;
|
|
104
|
+
indexPrice: bigint | undefined;
|
|
105
|
+
fundingRate: bigint | undefined;
|
|
106
|
+
nextFundingTime: bigint | undefined;
|
|
107
|
+
isLoading: boolean;
|
|
108
|
+
error: import("viem").ReadContractErrorType | null;
|
|
109
|
+
refetch: () => void;
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=use-lxfeed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-lxfeed.d.ts","sourceRoot":"","sources":["../../src/hooks/use-lxfeed.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAGnC,OAAO,KAAK,EAAE,WAAW,EAAiB,MAAM,qBAAqB,CAAA;AAErE;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM;eAS5B,WAAW,GAAG,SAAS;;;;;;;;;EAK7C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM;;;;;;EAiBnD;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM;;;;;;EAiBpD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;qBASzC,MAAM,GAAG,SAAS;;;;EAK9C;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO;;;;;;;;;;;;;;;;;EAcvE;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,OAAO,EAClB,UAAU,EAAE,OAAO,EACnB,YAAY,GAAE,MAAc;;;;;;;;;;;;;;;;;;;EAe7B;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,SAAS,EAAE,OAAO,EAClB,UAAU,EAAE,OAAO,EACnB,YAAY,GAAE,MAAc;aAUT,OAAO,GAAG,SAAS;;;;EAKvC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM;;;;;;;;EAkBjD"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LXFeed Hooks (LP-9040)
|
|
3
|
+
* React hooks for price feeds, mark price, and funding rates
|
|
4
|
+
*/
|
|
5
|
+
import { useReadContract } from 'wagmi';
|
|
6
|
+
import { LX } from '../precompile/addresses';
|
|
7
|
+
import { LX_FEED_ABI, LX_ORACLE_ABI } from '../precompile/abis';
|
|
8
|
+
/**
|
|
9
|
+
* Hook to get mark price for a market
|
|
10
|
+
*/
|
|
11
|
+
export function useLXFeedMarkPrice(marketId) {
|
|
12
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
13
|
+
address: LX.LX_FEED,
|
|
14
|
+
abi: LX_FEED_ABI,
|
|
15
|
+
functionName: 'getMarkPrice',
|
|
16
|
+
args: [marketId],
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
markPrice: data,
|
|
20
|
+
isLoading,
|
|
21
|
+
error,
|
|
22
|
+
refetch,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Hook to get index price for a market
|
|
27
|
+
*/
|
|
28
|
+
export function useLXFeedIndexPrice(marketId) {
|
|
29
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
30
|
+
address: LX.LX_FEED,
|
|
31
|
+
abi: LX_FEED_ABI,
|
|
32
|
+
functionName: 'getIndexPrice',
|
|
33
|
+
args: [marketId],
|
|
34
|
+
});
|
|
35
|
+
const result = data;
|
|
36
|
+
return {
|
|
37
|
+
priceX18: result?.[0],
|
|
38
|
+
timestamp: result?.[1],
|
|
39
|
+
isLoading,
|
|
40
|
+
error,
|
|
41
|
+
refetch,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Hook to get funding rate for a market
|
|
46
|
+
*/
|
|
47
|
+
export function useLXFeedFundingRate(marketId) {
|
|
48
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
49
|
+
address: LX.LX_FEED,
|
|
50
|
+
abi: LX_FEED_ABI,
|
|
51
|
+
functionName: 'getFundingRate',
|
|
52
|
+
args: [marketId],
|
|
53
|
+
});
|
|
54
|
+
const result = data;
|
|
55
|
+
return {
|
|
56
|
+
rateX18: result?.[0],
|
|
57
|
+
nextFundingTime: result?.[1],
|
|
58
|
+
isLoading,
|
|
59
|
+
error,
|
|
60
|
+
refetch,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Hook to get trigger price for stop/take orders
|
|
65
|
+
*/
|
|
66
|
+
export function useLXFeedTriggerPrice(marketId, isBuy) {
|
|
67
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
68
|
+
address: LX.LX_FEED,
|
|
69
|
+
abi: LX_FEED_ABI,
|
|
70
|
+
functionName: 'getTriggerPrice',
|
|
71
|
+
args: [marketId, isBuy],
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
triggerPriceX18: data,
|
|
75
|
+
isLoading,
|
|
76
|
+
error,
|
|
77
|
+
refetch,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Hook to get oracle price for token pair
|
|
82
|
+
*/
|
|
83
|
+
export function useLXOraclePrice(baseToken, quoteToken) {
|
|
84
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
85
|
+
address: LX.LX_ORACLE,
|
|
86
|
+
abi: LX_ORACLE_ABI,
|
|
87
|
+
functionName: 'getPrice',
|
|
88
|
+
args: [baseToken, quoteToken],
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
price: data,
|
|
92
|
+
isLoading,
|
|
93
|
+
error,
|
|
94
|
+
refetch,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Hook to get aggregated oracle price with deviation info
|
|
99
|
+
*/
|
|
100
|
+
export function useLXOracleAggregatedPrice(baseToken, quoteToken, maxStaleness = 3600n // 1 hour default
|
|
101
|
+
) {
|
|
102
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
103
|
+
address: LX.LX_ORACLE,
|
|
104
|
+
abi: LX_ORACLE_ABI,
|
|
105
|
+
functionName: 'getAggregatedPrice',
|
|
106
|
+
args: [baseToken, quoteToken, maxStaleness],
|
|
107
|
+
});
|
|
108
|
+
return {
|
|
109
|
+
aggregated: data,
|
|
110
|
+
isLoading,
|
|
111
|
+
error,
|
|
112
|
+
refetch,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Hook to check if oracle price is fresh
|
|
117
|
+
*/
|
|
118
|
+
export function useLXOraclePriceFresh(baseToken, quoteToken, maxStaleness = 3600n) {
|
|
119
|
+
const { data, isLoading, error, refetch } = useReadContract({
|
|
120
|
+
address: LX.LX_ORACLE,
|
|
121
|
+
abi: LX_ORACLE_ABI,
|
|
122
|
+
functionName: 'isPriceFresh',
|
|
123
|
+
args: [baseToken, quoteToken, maxStaleness],
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
isFresh: data,
|
|
127
|
+
isLoading,
|
|
128
|
+
error,
|
|
129
|
+
refetch,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Combined hook for market price data
|
|
134
|
+
*/
|
|
135
|
+
export function useLXMarketPrices(marketId) {
|
|
136
|
+
const mark = useLXFeedMarkPrice(marketId);
|
|
137
|
+
const index = useLXFeedIndexPrice(marketId);
|
|
138
|
+
const funding = useLXFeedFundingRate(marketId);
|
|
139
|
+
return {
|
|
140
|
+
markPrice: mark.markPrice,
|
|
141
|
+
indexPrice: index.priceX18,
|
|
142
|
+
fundingRate: funding.rateX18,
|
|
143
|
+
nextFundingTime: funding.nextFundingTime,
|
|
144
|
+
isLoading: mark.isLoading || index.isLoading || funding.isLoading,
|
|
145
|
+
error: mark.error || index.error || funding.error,
|
|
146
|
+
refetch: () => {
|
|
147
|
+
mark.refetch();
|
|
148
|
+
index.refetch();
|
|
149
|
+
funding.refetch();
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Address } from 'viem';
|
|
2
|
+
import type { LXPosition, LXMarginInfo } from '../precompile/types';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to get token balance in vault
|
|
5
|
+
*/
|
|
6
|
+
export declare function useLXVaultBalance(token: Address, subaccountId?: number): {
|
|
7
|
+
balance: bigint | undefined;
|
|
8
|
+
isLoading: boolean;
|
|
9
|
+
error: import("viem").ReadContractErrorType | null;
|
|
10
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<bigint, import("viem").ReadContractErrorType>>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Hook to get position for a market
|
|
14
|
+
*/
|
|
15
|
+
export declare function useLXVaultPosition(marketId: number, subaccountId?: number): {
|
|
16
|
+
position: LXPosition | undefined;
|
|
17
|
+
isLoading: boolean;
|
|
18
|
+
error: import("viem").ReadContractErrorType | null;
|
|
19
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<{
|
|
20
|
+
marketId: number;
|
|
21
|
+
side: number;
|
|
22
|
+
sizeX18: bigint;
|
|
23
|
+
entryPxX18: bigint;
|
|
24
|
+
unrealizedPnlX18: bigint;
|
|
25
|
+
accumulatedFundingX18: bigint;
|
|
26
|
+
lastFundingTime: bigint;
|
|
27
|
+
}, import("viem").ReadContractErrorType>>;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Hook to get margin info (free margin, used margin, etc.)
|
|
31
|
+
*/
|
|
32
|
+
export declare function useLXVaultMargin(subaccountId?: number): {
|
|
33
|
+
margin: LXMarginInfo | undefined;
|
|
34
|
+
isLoading: boolean;
|
|
35
|
+
error: import("viem").ReadContractErrorType | null;
|
|
36
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<{
|
|
37
|
+
totalCollateralX18: bigint;
|
|
38
|
+
usedMarginX18: bigint;
|
|
39
|
+
freeMarginX18: bigint;
|
|
40
|
+
marginRatioX18: bigint;
|
|
41
|
+
maintenanceMarginX18: bigint;
|
|
42
|
+
liquidatable: boolean;
|
|
43
|
+
}, import("viem").ReadContractErrorType>>;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Hook to check if account is liquidatable
|
|
47
|
+
*/
|
|
48
|
+
export declare function useLXVaultLiquidatable(account?: Address, subaccountId?: number): {
|
|
49
|
+
liquidatable: boolean | undefined;
|
|
50
|
+
shortfall: bigint | undefined;
|
|
51
|
+
isLoading: boolean;
|
|
52
|
+
error: import("viem").ReadContractErrorType | null;
|
|
53
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<readonly [boolean, bigint], import("viem").ReadContractErrorType>>;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Hook to get funding rate for a market
|
|
57
|
+
*/
|
|
58
|
+
export declare function useLXVaultFundingRate(marketId: number): {
|
|
59
|
+
rateX18: bigint | undefined;
|
|
60
|
+
nextFundingTime: bigint | undefined;
|
|
61
|
+
isLoading: boolean;
|
|
62
|
+
error: import("viem").ReadContractErrorType | null;
|
|
63
|
+
refetch: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<readonly [bigint, bigint], import("viem").ReadContractErrorType>>;
|
|
64
|
+
};
|
|
65
|
+
interface UseLXVaultDepositResult {
|
|
66
|
+
deposit: (token: Address, amount: bigint, subaccountId?: number) => void;
|
|
67
|
+
hash: `0x${string}` | undefined;
|
|
68
|
+
isPending: boolean;
|
|
69
|
+
isConfirming: boolean;
|
|
70
|
+
isSuccess: boolean;
|
|
71
|
+
error: Error | null;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Hook for depositing tokens into vault
|
|
75
|
+
*/
|
|
76
|
+
export declare function useLXVaultDeposit(): UseLXVaultDepositResult;
|
|
77
|
+
interface UseLXVaultWithdrawResult {
|
|
78
|
+
withdraw: (token: Address, amount: bigint, subaccountId?: number) => void;
|
|
79
|
+
hash: `0x${string}` | undefined;
|
|
80
|
+
isPending: boolean;
|
|
81
|
+
isConfirming: boolean;
|
|
82
|
+
isSuccess: boolean;
|
|
83
|
+
error: Error | null;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Hook for withdrawing tokens from vault
|
|
87
|
+
*/
|
|
88
|
+
export declare function useLXVaultWithdraw(): UseLXVaultWithdrawResult;
|
|
89
|
+
interface UseLXVaultTransferResult {
|
|
90
|
+
transfer: (token: Address, amount: bigint, fromSubaccount: number, toSubaccount: number) => void;
|
|
91
|
+
hash: `0x${string}` | undefined;
|
|
92
|
+
isPending: boolean;
|
|
93
|
+
isConfirming: boolean;
|
|
94
|
+
isSuccess: boolean;
|
|
95
|
+
error: Error | null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Hook for transferring between subaccounts
|
|
99
|
+
*/
|
|
100
|
+
export declare function useLXVaultTransfer(): UseLXVaultTransferResult;
|
|
101
|
+
interface UseLXVaultLiquidateResult {
|
|
102
|
+
liquidate: (targetAccount: Address, targetSubaccount: number, marketId: number, sizeX18: bigint) => void;
|
|
103
|
+
hash: `0x${string}` | undefined;
|
|
104
|
+
isPending: boolean;
|
|
105
|
+
isConfirming: boolean;
|
|
106
|
+
isSuccess: boolean;
|
|
107
|
+
error: Error | null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Hook for liquidating underwater positions
|
|
111
|
+
*/
|
|
112
|
+
export declare function useLXVaultLiquidate(): UseLXVaultLiquidateResult;
|
|
113
|
+
/**
|
|
114
|
+
* Combined hook for common vault operations
|
|
115
|
+
*/
|
|
116
|
+
export declare function useLXVault(subaccountId?: number): {
|
|
117
|
+
address: `0x${string}` | undefined;
|
|
118
|
+
subaccountId: number;
|
|
119
|
+
margin: LXMarginInfo | undefined;
|
|
120
|
+
isLoadingMargin: boolean;
|
|
121
|
+
deposit: (token: Address, amount: bigint, subaccountId?: number) => void;
|
|
122
|
+
withdraw: (token: Address, amount: bigint, subaccountId?: number) => void;
|
|
123
|
+
transfer: (token: Address, amount: bigint, fromSubaccount: number, toSubaccount: number) => void;
|
|
124
|
+
isDepositing: boolean;
|
|
125
|
+
isWithdrawing: boolean;
|
|
126
|
+
isTransferring: boolean;
|
|
127
|
+
refetchMargin: (options?: import("@tanstack/query-core").RefetchOptions) => Promise<import("@tanstack/query-core").QueryObserverResult<{
|
|
128
|
+
totalCollateralX18: bigint;
|
|
129
|
+
usedMarginX18: bigint;
|
|
130
|
+
freeMarginX18: bigint;
|
|
131
|
+
marginRatioX18: bigint;
|
|
132
|
+
maintenanceMarginX18: bigint;
|
|
133
|
+
liquidatable: boolean;
|
|
134
|
+
}, import("viem").ReadContractErrorType>>;
|
|
135
|
+
};
|
|
136
|
+
export {};
|
|
137
|
+
//# sourceMappingURL=use-lxvault.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-lxvault.d.ts","sourceRoot":"","sources":["../../src/hooks/use-lxvault.ts"],"names":[],"mappings":"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"}
|