timelock-sdk 0.0.16 → 0.0.17
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/abis.cjs +1 -0
- package/dist/abis.d.cts +1143 -0
- package/dist/abis.d.ts +1143 -0
- package/dist/abis.js +1 -0
- package/dist/client.cjs +4 -728
- package/dist/client.d.cts +62 -67
- package/dist/client.d.ts +62 -67
- package/dist/client.js +4 -699
- package/dist/{index-D65Sb-VK.d.ts → index-CnaCnYLR.d.cts} +520 -6911
- package/dist/{index-Dy2IJz9u.d.cts → index-DyZhOGnP.d.ts} +568 -6959
- package/dist/numberUtils-B7BwWYfb.cjs +1 -0
- package/dist/numberUtils-DP-pDRRs.js +1 -0
- package/dist/optionsMarket-BsJTpwtl.js +1 -0
- package/dist/optionsMarket-BsK9z8z4.cjs +1 -0
- package/dist/package.cjs +1 -39
- package/dist/package.d.cts +3 -2
- package/dist/package.d.ts +3 -2
- package/dist/package.js +1 -3
- package/dist/uniswapMathLens-CsZDhwII.d.cts +1808 -0
- package/dist/uniswapMathLens-SWLgAikn.d.ts +1808 -0
- package/dist/uniswapV3Pool-Copswrde.js +1 -0
- package/dist/uniswapV3Pool-D9Vqrkmz.cjs +1 -0
- package/package.json +23 -10
- package/dist/numberUtils-DB5N1e6G.js +0 -4652
- package/dist/numberUtils-DRGhFDHZ.cjs +0 -4907
package/dist/client.js
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { getErc20, getPriceAtTick, getTimelockLens, getTimelockMarket, getUniswapMathLens, getUniswapPool, lensAbi, optionsMarketAbi, roundTickDown, singleOwnerVaultAbi, timelockLenses, token0ToToken1, token1ToToken0, uniswapMathLenses, wrapAmount, wrapPrice } from "./numberUtils-DB5N1e6G.js";
|
|
5
|
-
import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
|
|
6
|
-
import React, { createContext, useContext, useMemo } from "react";
|
|
7
|
-
import { useAccount, useChainId, useClient, useReadContract, useWaitForTransactionReceipt, useWriteContract } from "wagmi";
|
|
8
|
-
import { GraphQLClient } from "graphql-request";
|
|
9
|
-
import gql from "graphql-tag";
|
|
10
|
-
import { waitForTransactionReceipt } from "viem/actions";
|
|
11
|
-
import { useQuery } from "@tanstack/react-query";
|
|
12
|
-
|
|
13
|
-
//#region src/generated/graphql.ts
|
|
14
|
-
const GetActiveUserOptionsDocument = gql`
|
|
1
|
+
"use client";import{lensAbi as e,optionsMarketAbi as t}from"./optionsMarket-BsJTpwtl.js";import{getErc20 as n,getPriceAtTick as r,getTimelockLens as i,getTimelockMarket as a,getUniswapMathLens as o,roundTickDown as s,timelockLenses as c,token0ToToken1 as l,token1ToToken0 as u,uniswapMathLenses as d,wrapAmount as f,wrapPrice as p}from"./numberUtils-DP-pDRRs.js";import{singleOwnerVaultAbi as m,uniswapV3PoolAbi as h}from"./uniswapV3Pool-Copswrde.js";import{encodeFunctionData as g,erc20Abi as _,maxUint256 as v}from"viem";import y,{createContext as b,useContext as x,useMemo as S}from"react";import{useAccount as C,useChainId as w,useClient as T,useReadContract as E,useWaitForTransactionReceipt as D,useWriteContract as O}from"wagmi";import{GraphQLClient as k}from"graphql-request";import A from"graphql-tag";import{waitForTransactionReceipt as j}from"viem/actions";import{useQuery as M}from"@tanstack/react-query";const N=A`
|
|
15
2
|
query GetActiveUserOptions($user: String!) {
|
|
16
3
|
UserOption(
|
|
17
4
|
where: {ownerAddr: {_eq: $user}, fullyExercised: {_eq: false}}
|
|
@@ -42,8 +29,7 @@ const GetActiveUserOptionsDocument = gql`
|
|
|
42
29
|
fullyExercised
|
|
43
30
|
}
|
|
44
31
|
}
|
|
45
|
-
|
|
46
|
-
const GetClosedUserOptionsDocument = gql`
|
|
32
|
+
`,P=A`
|
|
47
33
|
query GetClosedUserOptions($user: String!) {
|
|
48
34
|
UserOption(
|
|
49
35
|
where: {ownerAddr: {_eq: $user}, fullyExercised: {_eq: true}}
|
|
@@ -74,8 +60,7 @@ const GetClosedUserOptionsDocument = gql`
|
|
|
74
60
|
fullyExercised
|
|
75
61
|
}
|
|
76
62
|
}
|
|
77
|
-
|
|
78
|
-
const GetMarketDataDocument = gql`
|
|
63
|
+
`,F=A`
|
|
79
64
|
query GetMarketData($marketAddr: String!) {
|
|
80
65
|
TimelockMarket(where: {id: {_eq: $marketAddr}}, limit: 1) {
|
|
81
66
|
id
|
|
@@ -95,684 +80,4 @@ const GetMarketDataDocument = gql`
|
|
|
95
80
|
payoutAssetName
|
|
96
81
|
}
|
|
97
82
|
}
|
|
98
|
-
`;
|
|
99
|
-
const defaultWrapper = (action, _operationName, _operationType, _variables) => action();
|
|
100
|
-
function getSdk(client, withWrapper = defaultWrapper) {
|
|
101
|
-
return {
|
|
102
|
-
GetActiveUserOptions(variables, requestHeaders, signal) {
|
|
103
|
-
return withWrapper((wrappedRequestHeaders) => client.request({
|
|
104
|
-
document: GetActiveUserOptionsDocument,
|
|
105
|
-
variables,
|
|
106
|
-
requestHeaders: {
|
|
107
|
-
...requestHeaders,
|
|
108
|
-
...wrappedRequestHeaders
|
|
109
|
-
},
|
|
110
|
-
signal
|
|
111
|
-
}), "GetActiveUserOptions", "query", variables);
|
|
112
|
-
},
|
|
113
|
-
GetClosedUserOptions(variables, requestHeaders, signal) {
|
|
114
|
-
return withWrapper((wrappedRequestHeaders) => client.request({
|
|
115
|
-
document: GetClosedUserOptionsDocument,
|
|
116
|
-
variables,
|
|
117
|
-
requestHeaders: {
|
|
118
|
-
...requestHeaders,
|
|
119
|
-
...wrappedRequestHeaders
|
|
120
|
-
},
|
|
121
|
-
signal
|
|
122
|
-
}), "GetClosedUserOptions", "query", variables);
|
|
123
|
-
},
|
|
124
|
-
GetMarketData(variables, requestHeaders, signal) {
|
|
125
|
-
return withWrapper((wrappedRequestHeaders) => client.request({
|
|
126
|
-
document: GetMarketDataDocument,
|
|
127
|
-
variables,
|
|
128
|
-
requestHeaders: {
|
|
129
|
-
...requestHeaders,
|
|
130
|
-
...wrappedRequestHeaders
|
|
131
|
-
},
|
|
132
|
-
signal
|
|
133
|
-
}), "GetMarketData", "query", variables);
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
//#endregion
|
|
139
|
-
//#region src/providers/TimelockMarketProvider.tsx
|
|
140
|
-
const TimelockMarketContext = createContext(void 0);
|
|
141
|
-
const TimelockMarketProvider = ({ children, marketData, envioGraphqlUrl }) => {
|
|
142
|
-
const chainId = useChainId();
|
|
143
|
-
const lensAddr = timelockLenses[chainId];
|
|
144
|
-
const uniswapMathLensAddr = uniswapMathLenses[chainId];
|
|
145
|
-
return /* @__PURE__ */ React.createElement(TimelockMarketContext.Provider, { value: {
|
|
146
|
-
marketData: marketData || {},
|
|
147
|
-
lensAddr,
|
|
148
|
-
uniswapMathLensAddr,
|
|
149
|
-
envioGraphqlUrl
|
|
150
|
-
} }, children);
|
|
151
|
-
};
|
|
152
|
-
const useCurrentMarket = () => {
|
|
153
|
-
const context = useContext(TimelockMarketContext);
|
|
154
|
-
if (context === void 0) throw new Error("useCurrentMarket must be used within a TimelockMarketProvider");
|
|
155
|
-
return context.marketData;
|
|
156
|
-
};
|
|
157
|
-
const useTimelockConfig = () => {
|
|
158
|
-
const context = useContext(TimelockMarketContext);
|
|
159
|
-
if (context === void 0) throw new Error("useConfig must be used within a TimelockMarketProvider");
|
|
160
|
-
return {
|
|
161
|
-
lensAddr: context.lensAddr,
|
|
162
|
-
uniswapMathLensAddr: context.uniswapMathLensAddr,
|
|
163
|
-
envioGraphqlUrl: context.envioGraphqlUrl,
|
|
164
|
-
graphqlClient: context.envioGraphqlUrl ? getSdk(new GraphQLClient(context.envioGraphqlUrl)) : void 0
|
|
165
|
-
};
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
//#endregion
|
|
169
|
-
//#region src/hooks/market/useMarketData.ts
|
|
170
|
-
const useMarketData = (market) => {
|
|
171
|
-
const { graphqlClient } = useTimelockConfig();
|
|
172
|
-
const marketAddr = typeof market === "string" ? market : market === null || market === void 0 ? void 0 : market.address;
|
|
173
|
-
const { data } = useQuery({
|
|
174
|
-
queryKey: ["marketData", marketAddr || "--"],
|
|
175
|
-
queryFn: () => graphqlClient.GetMarketData({ marketAddr: marketAddr.toLowerCase() }),
|
|
176
|
-
select: (data$1) => ({
|
|
177
|
-
...data$1.TimelockMarket[0],
|
|
178
|
-
pool: data$1.TimelockMarket[0].pool,
|
|
179
|
-
vault: data$1.TimelockMarket[0].vault,
|
|
180
|
-
optionAsset: data$1.TimelockMarket[0].optionAsset,
|
|
181
|
-
payoutAsset: data$1.TimelockMarket[0].payoutAsset,
|
|
182
|
-
optionsCount: BigInt(data$1.TimelockMarket[0].optionsCount),
|
|
183
|
-
tradersCount: BigInt(data$1.TimelockMarket[0].tradersCount)
|
|
184
|
-
}),
|
|
185
|
-
enabled: !!marketAddr && !!graphqlClient
|
|
186
|
-
});
|
|
187
|
-
return data || {};
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
//#endregion
|
|
191
|
-
//#region src/hooks/market/useExerciseOption.ts
|
|
192
|
-
const useExerciseOption = (market) => {
|
|
193
|
-
const { vault } = useMarketData(market);
|
|
194
|
-
const client = useClient();
|
|
195
|
-
const { data: lowestTick } = useReadContract({
|
|
196
|
-
address: vault,
|
|
197
|
-
abi: singleOwnerVaultAbi,
|
|
198
|
-
functionName: "lowestTick"
|
|
199
|
-
});
|
|
200
|
-
const { writeContractAsync, data: hash, isPending, error } = useWriteContract();
|
|
201
|
-
const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
|
|
202
|
-
const marketAddr = typeof market === "string" ? market : market === null || market === void 0 ? void 0 : market.address;
|
|
203
|
-
const exerciseOption = async (optionId, liquidities) => {
|
|
204
|
-
if (!client) throw new Error("Wallet not connected");
|
|
205
|
-
if (lowestTick === void 0 || !marketAddr) throw new Error("Lowest tick lower not available");
|
|
206
|
-
const hash$1 = await writeContractAsync({
|
|
207
|
-
address: marketAddr,
|
|
208
|
-
abi: optionsMarketAbi,
|
|
209
|
-
functionName: "exerciseOption",
|
|
210
|
-
args: [
|
|
211
|
-
optionId,
|
|
212
|
-
liquidities,
|
|
213
|
-
0n,
|
|
214
|
-
lowestTick
|
|
215
|
-
]
|
|
216
|
-
});
|
|
217
|
-
await waitForTransactionReceipt(client, { hash: hash$1 });
|
|
218
|
-
return hash$1;
|
|
219
|
-
};
|
|
220
|
-
return {
|
|
221
|
-
exerciseOption,
|
|
222
|
-
hash,
|
|
223
|
-
isPending,
|
|
224
|
-
isConfirming,
|
|
225
|
-
isSuccess,
|
|
226
|
-
error,
|
|
227
|
-
isLoading: isPending || isConfirming
|
|
228
|
-
};
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
//#endregion
|
|
232
|
-
//#region src/hooks/useLens.ts
|
|
233
|
-
const useLens = () => {
|
|
234
|
-
const client = useClient();
|
|
235
|
-
const timelockLens = client ? getTimelockLens(client) : void 0;
|
|
236
|
-
return {
|
|
237
|
-
uniswapLens: client ? getUniswapMathLens(client) : void 0,
|
|
238
|
-
timelockLens
|
|
239
|
-
};
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
//#endregion
|
|
243
|
-
//#region src/hooks/market/useMaxPositionSize.ts
|
|
244
|
-
const useMaxPositionSize = (market, strikeTick, maxBorrowableRange = 100) => {
|
|
245
|
-
const { timelockLens } = useLens();
|
|
246
|
-
const { optionAssetDecimals } = useMarketData(market);
|
|
247
|
-
const marketAddr = typeof market === "string" ? market : market === null || market === void 0 ? void 0 : market.address;
|
|
248
|
-
const { data: data0, refetch: refetch0 } = useReadContract({
|
|
249
|
-
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
250
|
-
abi: lensAbi,
|
|
251
|
-
functionName: "getMaxPositionSizeAtCurrentTick",
|
|
252
|
-
args: [marketAddr, maxBorrowableRange],
|
|
253
|
-
query: { enabled: !!marketAddr && !!timelockLens },
|
|
254
|
-
gas: 100000000n
|
|
255
|
-
});
|
|
256
|
-
const { data: data1, refetch: refetch1 } = useReadContract({
|
|
257
|
-
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
258
|
-
abi: lensAbi,
|
|
259
|
-
functionName: "getMaxPositionSize",
|
|
260
|
-
args: [
|
|
261
|
-
marketAddr,
|
|
262
|
-
strikeTick,
|
|
263
|
-
maxBorrowableRange
|
|
264
|
-
],
|
|
265
|
-
query: { enabled: !!marketAddr && !!timelockLens && strikeTick !== void 0 },
|
|
266
|
-
gas: 100000000n
|
|
267
|
-
});
|
|
268
|
-
const data = strikeTick !== void 0 ? data1 : data0;
|
|
269
|
-
const { maxCallSize, maxPutSize } = useMemo(() => data && optionAssetDecimals ? {
|
|
270
|
-
maxCallSize: wrapAmount(data[0], optionAssetDecimals),
|
|
271
|
-
maxPutSize: wrapAmount(data[1], optionAssetDecimals)
|
|
272
|
-
} : {}, [data, optionAssetDecimals]);
|
|
273
|
-
const refetch = () => {
|
|
274
|
-
refetch0();
|
|
275
|
-
refetch1();
|
|
276
|
-
};
|
|
277
|
-
return {
|
|
278
|
-
maxCallSize,
|
|
279
|
-
maxPutSize,
|
|
280
|
-
refetch
|
|
281
|
-
};
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
//#endregion
|
|
285
|
-
//#region src/hooks/pool/useCurrentTick.ts
|
|
286
|
-
const getCurrentTick = async (pool) => {
|
|
287
|
-
const [tickSpacing, { 1: exact }] = await Promise.all([pool.read.tickSpacing(), pool.read.slot0()]);
|
|
288
|
-
return {
|
|
289
|
-
exact,
|
|
290
|
-
rounded: Math.floor(exact / tickSpacing) * tickSpacing
|
|
291
|
-
};
|
|
292
|
-
};
|
|
293
|
-
const useCurrentTick = (pool) => {
|
|
294
|
-
const client = useClient();
|
|
295
|
-
const { data: currentTick } = useQuery({
|
|
296
|
-
queryKey: ["currentTick", typeof pool !== "string" ? (pool === null || pool === void 0 ? void 0 : pool.address) || "-" : pool],
|
|
297
|
-
queryFn: () => getCurrentTick(typeof pool === "string" ? getUniswapPool(pool, client) : pool),
|
|
298
|
-
enabled: !!pool && !!client,
|
|
299
|
-
refetchInterval: 5e3
|
|
300
|
-
});
|
|
301
|
-
return currentTick || {};
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
//#endregion
|
|
305
|
-
//#region src/hooks/pool/usePoolData.ts
|
|
306
|
-
const usePoolData = (pool) => {
|
|
307
|
-
const poolAddress = typeof pool === "string" ? pool : pool === null || pool === void 0 ? void 0 : pool.address;
|
|
308
|
-
const { timelockLens } = useLens();
|
|
309
|
-
const client = useClient();
|
|
310
|
-
const { data } = useReadContract({
|
|
311
|
-
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
312
|
-
abi: lensAbi,
|
|
313
|
-
functionName: "getPoolData",
|
|
314
|
-
args: poolAddress ? [poolAddress] : void 0,
|
|
315
|
-
query: { enabled: !!poolAddress && !!client }
|
|
316
|
-
});
|
|
317
|
-
return data || {};
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
//#endregion
|
|
321
|
-
//#region src/hooks/market/useMintOption.ts
|
|
322
|
-
const useMintOption = (market) => {
|
|
323
|
-
const { timelockLens } = useLens();
|
|
324
|
-
const { payoutAsset, vault, pool, optionAssetIsToken0 } = useMarketData(market);
|
|
325
|
-
const { tickSpacing } = usePoolData(pool);
|
|
326
|
-
const { exact: currentTick } = useCurrentTick(pool);
|
|
327
|
-
const client = useClient();
|
|
328
|
-
const { address } = useAccount();
|
|
329
|
-
const { writeContractAsync, data: hash, isPending, error } = useWriteContract();
|
|
330
|
-
const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
|
|
331
|
-
const marketAddr = typeof market === "string" ? market : market === null || market === void 0 ? void 0 : market.address;
|
|
332
|
-
const askForApproval = async (premiumAmount) => {
|
|
333
|
-
if (!client || !address) throw new Error("Wallet not connected");
|
|
334
|
-
if (!payoutAsset || !marketAddr) throw new Error("Tokens not available");
|
|
335
|
-
if (await getErc20(payoutAsset, client).read.allowance([address, marketAddr]) < premiumAmount) await waitForTransactionReceipt(client, { hash: await writeContractAsync({
|
|
336
|
-
address: payoutAsset,
|
|
337
|
-
abi: erc20Abi,
|
|
338
|
-
functionName: "approve",
|
|
339
|
-
args: [marketAddr, maxUint256]
|
|
340
|
-
}) });
|
|
341
|
-
};
|
|
342
|
-
const mintOption = async (optionType, amount, duration, strikeTick) => {
|
|
343
|
-
if (!client) throw new Error("Wallet not connected");
|
|
344
|
-
if (!timelockLens || !vault || !marketAddr || !currentTick || !tickSpacing) throw new Error("Lowest tick lower not available");
|
|
345
|
-
strikeTick = roundTickDown(strikeTick ?? currentTick, tickSpacing);
|
|
346
|
-
if (optionType === "CALL" && optionAssetIsToken0 || optionType === "PUT" && !optionAssetIsToken0) strikeTick += tickSpacing;
|
|
347
|
-
await askForApproval(await getTimelockMarket(marketAddr, client).read.calculatePremium([
|
|
348
|
-
optionType === "CALL" ? 0 : 1,
|
|
349
|
-
amount,
|
|
350
|
-
strikeTick,
|
|
351
|
-
BigInt(duration)
|
|
352
|
-
]));
|
|
353
|
-
const hash$1 = await writeContractAsync({
|
|
354
|
-
address: marketAddr,
|
|
355
|
-
abi: optionsMarketAbi,
|
|
356
|
-
functionName: "mintOption",
|
|
357
|
-
args: [
|
|
358
|
-
optionType === "CALL" ? 0 : 1,
|
|
359
|
-
amount,
|
|
360
|
-
strikeTick,
|
|
361
|
-
BigInt(duration),
|
|
362
|
-
BigInt(1e69),
|
|
363
|
-
await timelockLens.read.getRefTick([vault, strikeTick])
|
|
364
|
-
]
|
|
365
|
-
});
|
|
366
|
-
await waitForTransactionReceipt(client, { hash: hash$1 });
|
|
367
|
-
return hash$1;
|
|
368
|
-
};
|
|
369
|
-
return {
|
|
370
|
-
mintOption,
|
|
371
|
-
hash,
|
|
372
|
-
isPending,
|
|
373
|
-
isConfirming,
|
|
374
|
-
isSuccess,
|
|
375
|
-
error,
|
|
376
|
-
isLoading: isPending || isConfirming
|
|
377
|
-
};
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
//#endregion
|
|
381
|
-
//#region src/hooks/market/useOptionPnl.ts
|
|
382
|
-
const useOptionPnl = (optionData) => {
|
|
383
|
-
const { marketAddr, optionType, entryTick, positionSizeCurrent } = optionData;
|
|
384
|
-
const { pool, optionAssetIsToken0, payoutAssetDecimals } = useMarketData(marketAddr);
|
|
385
|
-
const { exact: currentTick } = useCurrentTick(pool);
|
|
386
|
-
const { displayPnl, unrealizedPayout } = useMemo(() => {
|
|
387
|
-
if (optionAssetIsToken0 === void 0 || currentTick === void 0 || !positionSizeCurrent || !payoutAssetDecimals) return {};
|
|
388
|
-
const delta = optionAssetIsToken0 ? token0ToToken1(positionSizeCurrent, currentTick) - token0ToToken1(positionSizeCurrent, entryTick) : token1ToToken0(positionSizeCurrent, currentTick) - token1ToToken0(positionSizeCurrent, entryTick);
|
|
389
|
-
const displayPnl$1 = wrapAmount(optionType === "CALL" ? delta : -delta, payoutAssetDecimals);
|
|
390
|
-
return {
|
|
391
|
-
unrealizedPayout: wrapAmount(displayPnl$1.scaled < 0 ? 0n : displayPnl$1.scaled, payoutAssetDecimals),
|
|
392
|
-
displayPnl: displayPnl$1
|
|
393
|
-
};
|
|
394
|
-
}, [
|
|
395
|
-
optionData,
|
|
396
|
-
optionAssetIsToken0,
|
|
397
|
-
currentTick,
|
|
398
|
-
positionSizeCurrent
|
|
399
|
-
]);
|
|
400
|
-
return {
|
|
401
|
-
displayPnl,
|
|
402
|
-
unrealizedPayout
|
|
403
|
-
};
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
//#endregion
|
|
407
|
-
//#region src/hooks/market/useOptionPremium.ts
|
|
408
|
-
const useOptionPremium = (market, optionType, optionAmount, duration) => {
|
|
409
|
-
const { pool, payoutAssetDecimals } = useMarketData(market);
|
|
410
|
-
const { tickSpacing } = usePoolData(pool);
|
|
411
|
-
const currentTick = useCurrentTick(pool);
|
|
412
|
-
const marketAddr = typeof market === "string" ? market : market === null || market === void 0 ? void 0 : market.address;
|
|
413
|
-
const strikeTick = useMemo(() => {
|
|
414
|
-
if (currentTick.rounded === void 0 || tickSpacing === void 0) return;
|
|
415
|
-
return optionType === "CALL" ? currentTick.rounded + tickSpacing : currentTick.rounded;
|
|
416
|
-
}, [
|
|
417
|
-
currentTick.rounded,
|
|
418
|
-
tickSpacing,
|
|
419
|
-
optionType
|
|
420
|
-
]);
|
|
421
|
-
const { data: premiumData } = useReadContract({
|
|
422
|
-
address: marketAddr,
|
|
423
|
-
abi: optionsMarketAbi,
|
|
424
|
-
functionName: "calculatePremium",
|
|
425
|
-
args: strikeTick !== void 0 ? [
|
|
426
|
-
optionType === "CALL" ? 0 : 1,
|
|
427
|
-
optionAmount,
|
|
428
|
-
strikeTick,
|
|
429
|
-
BigInt(duration)
|
|
430
|
-
] : void 0,
|
|
431
|
-
query: { enabled: currentTick.rounded !== void 0 && payoutAssetDecimals !== void 0 && tickSpacing !== void 0 && strikeTick !== void 0 }
|
|
432
|
-
});
|
|
433
|
-
return useMemo(() => {
|
|
434
|
-
if (premiumData === void 0 || payoutAssetDecimals === void 0) return;
|
|
435
|
-
return wrapAmount(premiumData, payoutAssetDecimals);
|
|
436
|
-
}, [premiumData, payoutAssetDecimals]);
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
//#endregion
|
|
440
|
-
//#region src/hooks/market/useUserOptions.ts
|
|
441
|
-
const useUserOptions = (user, active = false) => {
|
|
442
|
-
const { graphqlClient } = useTimelockConfig();
|
|
443
|
-
const { data: options,...rest } = useQuery({
|
|
444
|
-
queryKey: [
|
|
445
|
-
"userTrades",
|
|
446
|
-
user || "--",
|
|
447
|
-
active
|
|
448
|
-
],
|
|
449
|
-
queryFn: () => active ? graphqlClient === null || graphqlClient === void 0 ? void 0 : graphqlClient.GetActiveUserOptions({ user: user.toLowerCase() }) : graphqlClient === null || graphqlClient === void 0 ? void 0 : graphqlClient.GetClosedUserOptions({ user: user.toLowerCase() }),
|
|
450
|
-
select: (data) => {
|
|
451
|
-
var _data$UserOption;
|
|
452
|
-
return data === null || data === void 0 || (_data$UserOption = data.UserOption) === null || _data$UserOption === void 0 ? void 0 : _data$UserOption.map((option) => ({
|
|
453
|
-
...option,
|
|
454
|
-
id: BigInt(option.id),
|
|
455
|
-
marketAddr: option.marketAddr,
|
|
456
|
-
optionType: option.optionType,
|
|
457
|
-
createdAt: /* @__PURE__ */ new Date(Number(option.createdAt) * 1e3),
|
|
458
|
-
expiresAt: /* @__PURE__ */ new Date(Number(option.expiresAt) * 1e3),
|
|
459
|
-
premiumPaid: BigInt(option.premiumPaid),
|
|
460
|
-
realizedPayout: BigInt(option.realizedPayout),
|
|
461
|
-
liquiditiesAtOpen: option.liquiditiesAtOpen.map((liquidity) => BigInt(liquidity)),
|
|
462
|
-
liquiditiesCurrent: option.liquiditiesCurrent.map((liquidity) => BigInt(liquidity)),
|
|
463
|
-
positionSizeAtOpen: BigInt(option.positionSizeAtOpen),
|
|
464
|
-
positionSizeCurrent: BigInt(option.positionSizeCurrent),
|
|
465
|
-
strikePrice: BigInt(option.strikePrice),
|
|
466
|
-
entryPrice: BigInt(option.entryPrice)
|
|
467
|
-
}));
|
|
468
|
-
},
|
|
469
|
-
enabled: !!user && !!graphqlClient
|
|
470
|
-
});
|
|
471
|
-
return {
|
|
472
|
-
data: useMemo(() => (options === null || options === void 0 ? void 0 : options.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())) || [], [options]),
|
|
473
|
-
...rest
|
|
474
|
-
};
|
|
475
|
-
};
|
|
476
|
-
const useActiveUserOptions = (user) => {
|
|
477
|
-
return useUserOptions(user, true);
|
|
478
|
-
};
|
|
479
|
-
const useClosedUserOptions = (user) => {
|
|
480
|
-
return useUserOptions(user, false);
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
//#endregion
|
|
484
|
-
//#region src/hooks/pool/usePriceAtTick.ts
|
|
485
|
-
const usePriceAtTick = (tick, pool) => {
|
|
486
|
-
const { token0Decimals, token1Decimals } = usePoolData(pool);
|
|
487
|
-
return useMemo(() => tick && token0Decimals && token1Decimals ? wrapPrice(getPriceAtTick(tick), token0Decimals, token1Decimals) : void 0, [
|
|
488
|
-
tick,
|
|
489
|
-
token0Decimals,
|
|
490
|
-
token1Decimals
|
|
491
|
-
]);
|
|
492
|
-
};
|
|
493
|
-
|
|
494
|
-
//#endregion
|
|
495
|
-
//#region src/hooks/pool/useCurrentPrice.ts
|
|
496
|
-
const useCurrentPrice = (pool) => {
|
|
497
|
-
const currentTick = useCurrentTick(pool);
|
|
498
|
-
return {
|
|
499
|
-
currentPrice: usePriceAtTick(currentTick.exact, pool),
|
|
500
|
-
currentTick
|
|
501
|
-
};
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
//#endregion
|
|
505
|
-
//#region src/hooks/vault/useVaultData.ts
|
|
506
|
-
const useVaultData = (vault) => {
|
|
507
|
-
const { data } = useReadContract({
|
|
508
|
-
address: typeof vault === "string" ? vault : vault === null || vault === void 0 ? void 0 : vault.address,
|
|
509
|
-
abi: singleOwnerVaultAbi,
|
|
510
|
-
functionName: "pool"
|
|
511
|
-
});
|
|
512
|
-
return { pool: data };
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
//#endregion
|
|
516
|
-
//#region src/hooks/vault/useBurnLiquidity.ts
|
|
517
|
-
const useBurnLiquidity = (vault) => {
|
|
518
|
-
const client = useClient();
|
|
519
|
-
const { pool } = useVaultData(vault);
|
|
520
|
-
const currentTick = useCurrentTick(pool);
|
|
521
|
-
const { timelockLens } = useLens();
|
|
522
|
-
const vaultAddr = typeof vault === "string" ? vault : vault === null || vault === void 0 ? void 0 : vault.address;
|
|
523
|
-
const { writeContractAsync, data: hash, isPending, error } = useWriteContract();
|
|
524
|
-
const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
|
|
525
|
-
const burn = async (tickLower, tickUpper, liquidity) => {
|
|
526
|
-
if (!client) throw new Error("Wallet not connected");
|
|
527
|
-
if (!vaultAddr || !timelockLens) throw new Error("Vault/lens not available");
|
|
528
|
-
const hash$1 = await writeContractAsync({
|
|
529
|
-
address: vaultAddr,
|
|
530
|
-
abi: singleOwnerVaultAbi,
|
|
531
|
-
functionName: "burn",
|
|
532
|
-
args: [
|
|
533
|
-
tickLower,
|
|
534
|
-
tickUpper,
|
|
535
|
-
liquidity,
|
|
536
|
-
await timelockLens.read.getRefTick([vaultAddr, tickLower])
|
|
537
|
-
]
|
|
538
|
-
});
|
|
539
|
-
await waitForTransactionReceipt(client, { hash: hash$1 });
|
|
540
|
-
return hash$1;
|
|
541
|
-
};
|
|
542
|
-
const burnMultiple = async (positions) => {
|
|
543
|
-
if (!client) throw new Error("Wallet not connected");
|
|
544
|
-
if (!currentTick.exact) throw new Error("Current tick not available");
|
|
545
|
-
if (positions.length === 0) throw new Error("No positions to burn");
|
|
546
|
-
if (!timelockLens || !vaultAddr) throw new Error("Vault/lens not available");
|
|
547
|
-
if (positions.length === 1) await burn(positions[0].tickLower, positions[0].tickUpper, positions[0].liquidity);
|
|
548
|
-
else {
|
|
549
|
-
const refTicks = await timelockLens.read.batchGetRefTick([vaultAddr, positions.map((position) => position.tickLower)]);
|
|
550
|
-
await waitForTransactionReceipt(client, { hash: await writeContractAsync({
|
|
551
|
-
address: vaultAddr,
|
|
552
|
-
abi: singleOwnerVaultAbi,
|
|
553
|
-
functionName: "multicall",
|
|
554
|
-
args: [positions.map((p, i) => encodeFunctionData({
|
|
555
|
-
abi: singleOwnerVaultAbi,
|
|
556
|
-
functionName: "burn",
|
|
557
|
-
args: [
|
|
558
|
-
p.tickLower,
|
|
559
|
-
p.tickUpper,
|
|
560
|
-
p.liquidity,
|
|
561
|
-
refTicks[i]
|
|
562
|
-
]
|
|
563
|
-
}))]
|
|
564
|
-
}) });
|
|
565
|
-
}
|
|
566
|
-
};
|
|
567
|
-
return {
|
|
568
|
-
burnMultiple,
|
|
569
|
-
burn,
|
|
570
|
-
hash,
|
|
571
|
-
isPending,
|
|
572
|
-
isConfirming,
|
|
573
|
-
isSuccess,
|
|
574
|
-
error,
|
|
575
|
-
isLoading: isPending || isConfirming
|
|
576
|
-
};
|
|
577
|
-
};
|
|
578
|
-
|
|
579
|
-
//#endregion
|
|
580
|
-
//#region src/hooks/vault/useLiquidityBlocks.ts
|
|
581
|
-
const useLiquidityBlocks = (vault) => {
|
|
582
|
-
const { timelockLens } = useLens();
|
|
583
|
-
const { pool } = useVaultData(vault);
|
|
584
|
-
const { token0Decimals, token1Decimals } = usePoolData(pool);
|
|
585
|
-
const { currentPrice } = useCurrentPrice(pool);
|
|
586
|
-
const vaultAddr = typeof vault === "string" ? vault : vault === null || vault === void 0 ? void 0 : vault.address;
|
|
587
|
-
const { data: blocks = [], refetch } = useQuery({
|
|
588
|
-
queryKey: ["liquidityBlocks", vaultAddr],
|
|
589
|
-
queryFn: () => timelockLens.read.getAllBlocks([vaultAddr]),
|
|
590
|
-
enabled: !!vaultAddr && !!timelockLens
|
|
591
|
-
});
|
|
592
|
-
const { totalAmount0, totalAmount1, borrowedAmount0, borrowedAmount1 } = useMemo(() => ({
|
|
593
|
-
totalAmount0: token0Decimals ? wrapAmount(blocks.reduce((sum, block) => sum + block.totalAmount0, 0n), token0Decimals) : void 0,
|
|
594
|
-
totalAmount1: token1Decimals ? wrapAmount(blocks.reduce((sum, block) => sum + block.totalAmount1, 0n), token1Decimals) : void 0,
|
|
595
|
-
borrowedAmount0: token0Decimals ? wrapAmount(blocks.reduce((sum, block) => sum + block.borrowedAmount0, 0n), token0Decimals) : void 0,
|
|
596
|
-
borrowedAmount1: token1Decimals ? wrapAmount(blocks.reduce((sum, block) => sum + block.borrowedAmount1, 0n), token1Decimals) : void 0
|
|
597
|
-
}), [
|
|
598
|
-
blocks,
|
|
599
|
-
token0Decimals,
|
|
600
|
-
token1Decimals
|
|
601
|
-
]);
|
|
602
|
-
return {
|
|
603
|
-
tvl1: useMemo(() => {
|
|
604
|
-
if (!totalAmount0 || !totalAmount1 || !currentPrice || !token1Decimals) return void 0;
|
|
605
|
-
const tvl0as1 = totalAmount0.scaled * currentPrice.scaled / 10n ** 18n;
|
|
606
|
-
const tvl1 = totalAmount1.scaled;
|
|
607
|
-
return wrapAmount(tvl0as1 + tvl1, token1Decimals);
|
|
608
|
-
}, [
|
|
609
|
-
totalAmount0,
|
|
610
|
-
totalAmount1,
|
|
611
|
-
currentPrice,
|
|
612
|
-
token1Decimals
|
|
613
|
-
]),
|
|
614
|
-
blocks,
|
|
615
|
-
totalAmount0,
|
|
616
|
-
totalAmount1,
|
|
617
|
-
borrowedAmount0,
|
|
618
|
-
borrowedAmount1,
|
|
619
|
-
refetch
|
|
620
|
-
};
|
|
621
|
-
};
|
|
622
|
-
|
|
623
|
-
//#endregion
|
|
624
|
-
//#region src/hooks/vault/useMintLiquidity.ts
|
|
625
|
-
const batchGetAmountsFromLiquidity = async (lens, tickLower, tickUpper, liquidity, currentTick) => {
|
|
626
|
-
const currentTicksArray = new Array(tickLower.length).fill(currentTick);
|
|
627
|
-
const amounts = await lens.read.batchGetAmountsForLiquidityTicks([
|
|
628
|
-
currentTicksArray,
|
|
629
|
-
tickLower,
|
|
630
|
-
tickUpper,
|
|
631
|
-
liquidity
|
|
632
|
-
]);
|
|
633
|
-
let totalAmount0 = 0n;
|
|
634
|
-
let totalAmount1 = 0n;
|
|
635
|
-
const amounts0 = [];
|
|
636
|
-
const amounts1 = [];
|
|
637
|
-
for (const scaled of amounts[0]) {
|
|
638
|
-
totalAmount0 += scaled;
|
|
639
|
-
amounts0.push(scaled);
|
|
640
|
-
}
|
|
641
|
-
for (const scaled of amounts[1]) {
|
|
642
|
-
totalAmount1 += scaled;
|
|
643
|
-
amounts1.push(scaled);
|
|
644
|
-
}
|
|
645
|
-
return {
|
|
646
|
-
totalAmount0,
|
|
647
|
-
totalAmount1,
|
|
648
|
-
amounts0,
|
|
649
|
-
amounts1
|
|
650
|
-
};
|
|
651
|
-
};
|
|
652
|
-
const useMintLiquidity = (vault) => {
|
|
653
|
-
const client = useClient();
|
|
654
|
-
const { address } = useAccount();
|
|
655
|
-
const { pool } = useVaultData(vault);
|
|
656
|
-
const { timelockLens, uniswapLens } = useLens();
|
|
657
|
-
const currentTick = useCurrentTick(pool);
|
|
658
|
-
const { token0, token1 } = usePoolData(pool);
|
|
659
|
-
const vaultAddr = typeof vault === "string" ? vault : vault === null || vault === void 0 ? void 0 : vault.address;
|
|
660
|
-
const { writeContractAsync, data: hash, isPending, error } = useWriteContract();
|
|
661
|
-
const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
|
|
662
|
-
const askForApproval = async (params) => {
|
|
663
|
-
if (!address || !client) throw new Error("Wallet not connected");
|
|
664
|
-
if (currentTick.exact === void 0 || !token0 || !token1 || !vaultAddr || !uniswapLens) throw new Error("Current tick not available");
|
|
665
|
-
const { totalAmount0, totalAmount1 } = await batchGetAmountsFromLiquidity(uniswapLens, params.map((p) => p.tickLower), params.map((p) => p.tickUpper), params.map((p) => p.liquidity), currentTick.exact);
|
|
666
|
-
const [allowance0, allowance1] = await Promise.all([getErc20(token0, client).read.allowance([address, vaultAddr]), getErc20(token1, client).read.allowance([address, vaultAddr])]);
|
|
667
|
-
const approvalPromises = [];
|
|
668
|
-
if (allowance0 <= totalAmount0) {
|
|
669
|
-
const approvalHash = await writeContractAsync({
|
|
670
|
-
address: token0,
|
|
671
|
-
abi: erc20Abi,
|
|
672
|
-
functionName: "approve",
|
|
673
|
-
args: [vaultAddr, maxUint256]
|
|
674
|
-
});
|
|
675
|
-
approvalPromises.push(waitForTransactionReceipt(client, { hash: approvalHash }));
|
|
676
|
-
}
|
|
677
|
-
if (allowance1 <= totalAmount1) {
|
|
678
|
-
const approvalHash1 = await writeContractAsync({
|
|
679
|
-
address: token1,
|
|
680
|
-
abi: erc20Abi,
|
|
681
|
-
functionName: "approve",
|
|
682
|
-
args: [vaultAddr, maxUint256]
|
|
683
|
-
});
|
|
684
|
-
approvalPromises.push(waitForTransactionReceipt(client, { hash: approvalHash1 }));
|
|
685
|
-
}
|
|
686
|
-
if (approvalPromises.length > 0) await Promise.all(approvalPromises);
|
|
687
|
-
};
|
|
688
|
-
const mint = async (tickLower, tickUpper, liquidity) => {
|
|
689
|
-
if (!client) throw new Error("Wallet not connected");
|
|
690
|
-
if (!vaultAddr || !timelockLens) throw new Error("Vault/lens not available");
|
|
691
|
-
await askForApproval([{
|
|
692
|
-
tickLower,
|
|
693
|
-
tickUpper,
|
|
694
|
-
liquidity
|
|
695
|
-
}]);
|
|
696
|
-
const hash$1 = await writeContractAsync({
|
|
697
|
-
address: vaultAddr,
|
|
698
|
-
abi: singleOwnerVaultAbi,
|
|
699
|
-
functionName: "mint",
|
|
700
|
-
args: [
|
|
701
|
-
tickLower,
|
|
702
|
-
tickUpper,
|
|
703
|
-
liquidity,
|
|
704
|
-
await timelockLens.read.getRefTick([vaultAddr, tickLower])
|
|
705
|
-
]
|
|
706
|
-
});
|
|
707
|
-
await waitForTransactionReceipt(client, { hash: hash$1 });
|
|
708
|
-
return hash$1;
|
|
709
|
-
};
|
|
710
|
-
const mintMultiple = async (positions) => {
|
|
711
|
-
if (!client) throw new Error("Wallet not connected");
|
|
712
|
-
if (!currentTick.exact) throw new Error("Current tick not available");
|
|
713
|
-
if (positions.length === 0) throw new Error("No positions to mint");
|
|
714
|
-
if (!timelockLens || !vaultAddr) throw new Error("Vault/lens not available");
|
|
715
|
-
if (positions.length === 1) await mint(positions[0].tickLower, positions[0].tickUpper, positions[0].liquidity);
|
|
716
|
-
else {
|
|
717
|
-
await askForApproval(positions);
|
|
718
|
-
const refTicks = await timelockLens.read.batchGetRefTick([vaultAddr, positions.map((position) => position.tickLower)]);
|
|
719
|
-
await waitForTransactionReceipt(client, { hash: await writeContractAsync({
|
|
720
|
-
address: vaultAddr,
|
|
721
|
-
abi: singleOwnerVaultAbi,
|
|
722
|
-
functionName: "multicall",
|
|
723
|
-
args: [positions.map((p, i) => encodeFunctionData({
|
|
724
|
-
abi: singleOwnerVaultAbi,
|
|
725
|
-
functionName: "mint",
|
|
726
|
-
args: [
|
|
727
|
-
p.tickLower,
|
|
728
|
-
p.tickUpper,
|
|
729
|
-
p.liquidity,
|
|
730
|
-
refTicks[i]
|
|
731
|
-
]
|
|
732
|
-
}))]
|
|
733
|
-
}) });
|
|
734
|
-
}
|
|
735
|
-
};
|
|
736
|
-
return {
|
|
737
|
-
mintMultiple,
|
|
738
|
-
mint,
|
|
739
|
-
hash,
|
|
740
|
-
isPending,
|
|
741
|
-
isConfirming,
|
|
742
|
-
isSuccess,
|
|
743
|
-
error,
|
|
744
|
-
isLoading: isPending || isConfirming
|
|
745
|
-
};
|
|
746
|
-
};
|
|
747
|
-
|
|
748
|
-
//#endregion
|
|
749
|
-
//#region src/hooks/vault/useVaultTVL.ts
|
|
750
|
-
const useVaultTVL = (vault) => {
|
|
751
|
-
const { timelockLens } = useLens();
|
|
752
|
-
const { pool } = useVaultData(vault);
|
|
753
|
-
const { token0Decimals, token1Decimals } = usePoolData(pool);
|
|
754
|
-
const vaultAddr = typeof vault === "string" ? vault : vault === null || vault === void 0 ? void 0 : vault.address;
|
|
755
|
-
const { data, refetch } = useReadContract({
|
|
756
|
-
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
757
|
-
abi: lensAbi,
|
|
758
|
-
functionName: "getVaultTVL",
|
|
759
|
-
args: [vaultAddr],
|
|
760
|
-
query: { enabled: !!vaultAddr && !!timelockLens }
|
|
761
|
-
});
|
|
762
|
-
const totalAmount0 = data && token0Decimals ? wrapAmount(data[0], token0Decimals) : void 0;
|
|
763
|
-
const totalAmount1 = data && token1Decimals ? wrapAmount(data[1], token1Decimals) : void 0;
|
|
764
|
-
const borrowedAmount0 = data && token0Decimals ? wrapAmount(data[2], token0Decimals) : void 0;
|
|
765
|
-
const borrowedAmount1 = data && token1Decimals ? wrapAmount(data[3], token1Decimals) : void 0;
|
|
766
|
-
return {
|
|
767
|
-
tvl0: data && token0Decimals ? wrapAmount(data[4], token0Decimals) : void 0,
|
|
768
|
-
tvl1: data && token1Decimals ? wrapAmount(data[5], token1Decimals) : void 0,
|
|
769
|
-
totalAmount0,
|
|
770
|
-
totalAmount1,
|
|
771
|
-
borrowedAmount0,
|
|
772
|
-
borrowedAmount1,
|
|
773
|
-
refetch
|
|
774
|
-
};
|
|
775
|
-
};
|
|
776
|
-
|
|
777
|
-
//#endregion
|
|
778
|
-
export { TimelockMarketProvider, batchGetAmountsFromLiquidity, getCurrentTick, useActiveUserOptions, useBurnLiquidity, useClosedUserOptions, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useLens, useLiquidityBlocks, useMarketData, useMaxPositionSize, useMintLiquidity, useMintOption, useOptionPnl, useOptionPremium, usePoolData, usePriceAtTick, useTimelockConfig, useVaultData, useVaultTVL };
|
|
83
|
+
`,I=(e,t,n,r)=>e();function L(e,t=I){return{GetActiveUserOptions(n,r,i){return t(t=>e.request({document:N,variables:n,requestHeaders:{...r,...t},signal:i}),`GetActiveUserOptions`,`query`,n)},GetClosedUserOptions(n,r,i){return t(t=>e.request({document:P,variables:n,requestHeaders:{...r,...t},signal:i}),`GetClosedUserOptions`,`query`,n)},GetMarketData(n,r,i){return t(t=>e.request({document:F,variables:n,requestHeaders:{...r,...t},signal:i}),`GetMarketData`,`query`,n)}}}const R=b(void 0),z=({children:e,marketData:t,envioGraphqlUrl:n})=>{let r=w(),i=c[r],a=d[r];return y.createElement(R.Provider,{value:{marketData:t||{},lensAddr:i,uniswapMathLensAddr:a,envioGraphqlUrl:n}},e)},B=()=>{let e=x(R);if(e===void 0)throw Error(`useCurrentMarket must be used within a TimelockMarketProvider`);return e.marketData},V=()=>{let e=x(R);if(e===void 0)throw Error(`useConfig must be used within a TimelockMarketProvider`);return{lensAddr:e.lensAddr,uniswapMathLensAddr:e.uniswapMathLensAddr,envioGraphqlUrl:e.envioGraphqlUrl,graphqlClient:e.envioGraphqlUrl?L(new k(e.envioGraphqlUrl)):void 0}},H=e=>{let{graphqlClient:t}=V(),{data:n}=M({queryKey:[`marketData`,e||`--`],queryFn:()=>t.GetMarketData({marketAddr:e.toLowerCase()}),select:e=>({...e.TimelockMarket[0],pool:e.TimelockMarket[0].pool,vault:e.TimelockMarket[0].vault,optionAsset:e.TimelockMarket[0].optionAsset,payoutAsset:e.TimelockMarket[0].payoutAsset,optionsCount:BigInt(e.TimelockMarket[0].optionsCount),tradersCount:BigInt(e.TimelockMarket[0].tradersCount)}),enabled:!!e&&!!t});return n||{}},U=e=>{let{vault:n}=H(e),r=T(),{data:i}=E({address:n,abi:m,functionName:`lowestTick`}),{writeContractAsync:a,data:o,isPending:s,error:c}=O(),{isLoading:l,isSuccess:u}=D({hash:o});return{exerciseOption:async(n,o)=>{if(!r)throw Error(`Wallet not connected`);if(i===void 0||!e)throw Error(`Lowest tick lower not available`);let s=await a({address:e,abi:t,functionName:`exerciseOption`,args:[n,o,0n,i]});return await j(r,{hash:s}),s},hash:o,isPending:s,isConfirming:l,isSuccess:u,error:c,isLoading:s||l}},W=()=>{let e=T(),t=e?i(e):void 0;return{uniswapLens:e?o(e):void 0,timelockLens:t}},G=(t,n,r=100)=>{let{timelockLens:i}=W(),{optionAssetDecimals:a}=H(t),{data:o,refetch:s}=E({address:i==null?void 0:i.address,abi:e,functionName:`getMaxPositionSizeAtCurrentTick`,args:[t,r],query:{enabled:!!t&&!!i},gas:100000000n}),{data:c,refetch:l}=E({address:i==null?void 0:i.address,abi:e,functionName:`getMaxPositionSize`,args:[t,n,r],query:{enabled:!!t&&!!i&&n!==void 0},gas:100000000n}),u=n===void 0?o:c,{maxCallSize:d,maxPutSize:p}=S(()=>u&&a?{maxCallSize:f(u[0],a),maxPutSize:f(u[1],a)}:{},[u,a]);return{maxCallSize:d,maxPutSize:p,refetch:()=>{s(),l()}}},K=t=>{let{timelockLens:n}=W(),r=T(),{data:i}=E({address:n==null?void 0:n.address,abi:e,functionName:`getPoolData`,args:t?[t]:void 0,query:{enabled:!!t&&!!r}});return i||{}},q=e=>{let{tickSpacing:t}=K(e),{data:n}=E({address:e,abi:h,functionName:`slot0`,args:[]}),r=n==null?void 0:n[1];return{exact:r,rounded:r&&t?Math.floor(r/t)*t:void 0}},J=e=>{let{timelockLens:r}=W(),{payoutAsset:i,vault:o,pool:c,optionAssetIsToken0:l}=H(e),{tickSpacing:u}=K(c),{exact:d}=q(c),f=T(),{address:p}=C(),{writeContractAsync:m,data:h,isPending:g,error:y}=O(),{isLoading:b,isSuccess:x}=D({hash:h}),S=async t=>{if(!f||!p)throw Error(`Wallet not connected`);if(!i||!e)throw Error(`Tokens not available`);await n(i,f).read.allowance([p,e])<t&&await j(f,{hash:await m({address:i,abi:_,functionName:`approve`,args:[e,v]})})};return{mintOption:async(n,i,c,p)=>{if(!f)throw Error(`Wallet not connected`);if(!r||!o||!e||!d||!u)throw Error(`Lowest tick lower not available`);p=s(p??d,u),(n===`CALL`&&l||n===`PUT`&&!l)&&(p+=u),await S(await a(e,f).read.calculatePremium([n===`CALL`?0:1,i,p,BigInt(c)]));let h=await m({address:e,abi:t,functionName:`mintOption`,args:[n===`CALL`?0:1,i,p,BigInt(c),BigInt(1e69),await r.read.getRefTick([o,p])]});return await j(f,{hash:h}),h},hash:h,isPending:g,isConfirming:b,isSuccess:x,error:y,isLoading:g||b}},ee=e=>{let{marketAddr:t,optionType:n,entryTick:r,positionSizeCurrent:i}=e,{pool:a,optionAssetIsToken0:o,payoutAssetDecimals:s}=H(t),{exact:c}=q(a),{displayPnl:d,unrealizedPayout:p}=S(()=>{if(o===void 0||c===void 0||!i||!s)return{};let e=o?l(i,c)-l(i,r):u(i,c)-u(i,r),t=f(n===`CALL`?e:-e,s);return{unrealizedPayout:f(t.scaled<0?0n:t.scaled,s),displayPnl:t}},[e,o,c,i]);return{displayPnl:d,unrealizedPayout:p}},te=(e,n,r,i)=>{let{pool:a,payoutAssetDecimals:o}=H(e),{tickSpacing:s}=K(a),c=q(a),l=S(()=>{if(!(c.rounded===void 0||s===void 0))return n===`CALL`?c.rounded+s:c.rounded},[c.rounded,s,n]),{data:u}=E({address:e,abi:t,functionName:`calculatePremium`,args:l===void 0?void 0:[n===`CALL`?0:1,r,l,BigInt(i)],query:{enabled:c.rounded!==void 0&&o!==void 0&&s!==void 0&&l!==void 0}});return S(()=>{if(!(u===void 0||o===void 0))return f(u,o)},[u,o])},Y=(e,t=!1)=>{let{graphqlClient:n}=V(),{data:r,...i}=M({queryKey:[`userTrades`,e||`--`,t],queryFn:()=>t?n==null?void 0:n.GetActiveUserOptions({user:e.toLowerCase()}):n==null?void 0:n.GetClosedUserOptions({user:e.toLowerCase()}),select:e=>{var t;return e==null||(t=e.UserOption)==null?void 0:t.map(e=>({...e,id:BigInt(e.id),marketAddr:e.marketAddr,optionType:e.optionType,createdAt:new Date(Number(e.createdAt)*1e3),expiresAt:new Date(Number(e.expiresAt)*1e3),premiumPaid:BigInt(e.premiumPaid),realizedPayout:BigInt(e.realizedPayout),liquiditiesAtOpen:e.liquiditiesAtOpen.map(e=>BigInt(e)),liquiditiesCurrent:e.liquiditiesCurrent.map(e=>BigInt(e)),positionSizeAtOpen:BigInt(e.positionSizeAtOpen),positionSizeCurrent:BigInt(e.positionSizeCurrent),strikePrice:BigInt(e.strikePrice),entryPrice:BigInt(e.entryPrice)}))},enabled:!!e&&!!n});return{data:S(()=>(r==null?void 0:r.sort((e,t)=>t.createdAt.getTime()-e.createdAt.getTime()))||[],[r]),...i}},ne=e=>Y(e,!0),re=e=>Y(e,!1),X=(e,t)=>{let{token0Decimals:n,token1Decimals:i}=K(t);return S(()=>e&&n&&i?p(r(e),n,i):void 0,[e,n,i])},Z=e=>{let t=q(e);return{currentPrice:X(t.exact,e),currentTick:t}},Q=e=>{let{data:t}=E({address:e,abi:m,functionName:`pool`});return{pool:t}},ie=e=>{let t=T(),{pool:n}=Q(e),r=q(n),{timelockLens:i}=W(),{writeContractAsync:a,data:o,isPending:s,error:c}=O(),{isLoading:l,isSuccess:u}=D({hash:o}),d=async(n,r,o)=>{if(!t)throw Error(`Wallet not connected`);if(!e||!i)throw Error(`Vault/lens not available`);let s=await a({address:e,abi:m,functionName:`burn`,args:[n,r,o,await i.read.getRefTick([e,n])]});return await j(t,{hash:s}),s};return{burnMultiple:async n=>{if(!t)throw Error(`Wallet not connected`);if(!r.exact)throw Error(`Current tick not available`);if(n.length===0)throw Error(`No positions to burn`);if(!i||!e)throw Error(`Vault/lens not available`);if(n.length===1)await d(n[0].tickLower,n[0].tickUpper,n[0].liquidity);else{let r=await i.read.batchGetRefTick([e,n.map(e=>e.tickLower)]);await j(t,{hash:await a({address:e,abi:m,functionName:`multicall`,args:[n.map((e,t)=>g({abi:m,functionName:`burn`,args:[e.tickLower,e.tickUpper,e.liquidity,r[t]]}))]})})}},burn:d,hash:o,isPending:s,isConfirming:l,isSuccess:u,error:c,isLoading:s||l}},ae=e=>{let{timelockLens:t}=W(),{pool:n}=Q(e),{token0Decimals:r,token1Decimals:i}=K(n),{currentPrice:a}=Z(n),{data:o=[],refetch:s}=M({queryKey:[`liquidityBlocks`,e],queryFn:()=>t.read.getAllBlocks([e]),enabled:!!e&&!!t}),{totalAmount0:c,totalAmount1:l,borrowedAmount0:u,borrowedAmount1:d}=S(()=>({totalAmount0:r?f(o.reduce((e,t)=>e+t.totalAmount0,0n),r):void 0,totalAmount1:i?f(o.reduce((e,t)=>e+t.totalAmount1,0n),i):void 0,borrowedAmount0:r?f(o.reduce((e,t)=>e+t.borrowedAmount0,0n),r):void 0,borrowedAmount1:i?f(o.reduce((e,t)=>e+t.borrowedAmount1,0n),i):void 0}),[o,r,i]);return{tvl1:S(()=>{if(!c||!l||!a||!i)return;let e=c.scaled*a.scaled/10n**18n,t=l.scaled;return f(e+t,i)},[c,l,a,i]),blocks:o,totalAmount0:c,totalAmount1:l,borrowedAmount0:u,borrowedAmount1:d,refetch:s}},$=async(e,t,n,r,i)=>{let a=Array(t.length).fill(i),o=await e.read.batchGetAmountsForLiquidityTicks([a,t,n,r]),s=0n,c=0n,l=[],u=[];for(let e of o[0])s+=e,l.push(e);for(let e of o[1])c+=e,u.push(e);return{totalAmount0:s,totalAmount1:c,amounts0:l,amounts1:u}},oe=e=>{let t=T(),{address:r}=C(),{pool:i}=Q(e),{timelockLens:a,uniswapLens:o}=W(),s=q(i),{token0:c,token1:l}=K(i),{writeContractAsync:u,data:d,isPending:f,error:p}=O(),{isLoading:h,isSuccess:y}=D({hash:d}),b=async i=>{if(!r||!t)throw Error(`Wallet not connected`);if(s.exact===void 0||!c||!l||!e||!o)throw Error(`Current tick not available`);let{totalAmount0:a,totalAmount1:d}=await $(o,i.map(e=>e.tickLower),i.map(e=>e.tickUpper),i.map(e=>e.liquidity),s.exact),[f,p]=await Promise.all([n(c,t).read.allowance([r,e]),n(l,t).read.allowance([r,e])]),m=[];if(f<=a){let n=await u({address:c,abi:_,functionName:`approve`,args:[e,v]});m.push(j(t,{hash:n}))}if(p<=d){let n=await u({address:l,abi:_,functionName:`approve`,args:[e,v]});m.push(j(t,{hash:n}))}m.length>0&&await Promise.all(m)},x=async(n,r,i)=>{if(!t)throw Error(`Wallet not connected`);if(!e||!a)throw Error(`Vault/lens not available`);await b([{tickLower:n,tickUpper:r,liquidity:i}]);let o=await u({address:e,abi:m,functionName:`mint`,args:[n,r,i,await a.read.getRefTick([e,n])]});return await j(t,{hash:o}),o};return{mintMultiple:async n=>{if(!t)throw Error(`Wallet not connected`);if(!s.exact)throw Error(`Current tick not available`);if(n.length===0)throw Error(`No positions to mint`);if(!a||!e)throw Error(`Vault/lens not available`);if(n.length===1)await x(n[0].tickLower,n[0].tickUpper,n[0].liquidity);else{await b(n);let r=await a.read.batchGetRefTick([e,n.map(e=>e.tickLower)]);await j(t,{hash:await u({address:e,abi:m,functionName:`multicall`,args:[n.map((e,t)=>g({abi:m,functionName:`mint`,args:[e.tickLower,e.tickUpper,e.liquidity,r[t]]}))]})})}},mint:x,hash:d,isPending:f,isConfirming:h,isSuccess:y,error:p,isLoading:f||h}},se=t=>{let{timelockLens:n}=W(),{pool:r}=Q(t),{token0Decimals:i,token1Decimals:a}=K(r),{data:o,refetch:s}=E({address:n==null?void 0:n.address,abi:e,functionName:`getVaultTVL`,args:[t],query:{enabled:!!t&&!!n}}),c=o&&i?f(o[0],i):void 0,l=o&&a?f(o[1],a):void 0,u=o&&i?f(o[2],i):void 0,d=o&&a?f(o[3],a):void 0;return{tvl0:o&&i?f(o[4],i):void 0,tvl1:o&&a?f(o[5],a):void 0,totalAmount0:c,totalAmount1:l,borrowedAmount0:u,borrowedAmount1:d,refetch:s}};export{z as TimelockMarketProvider,$ as batchGetAmountsFromLiquidity,ne as useActiveUserOptions,ie as useBurnLiquidity,re as useClosedUserOptions,B as useCurrentMarket,Z as useCurrentPrice,q as useCurrentTick,U as useExerciseOption,W as useLens,ae as useLiquidityBlocks,H as useMarketData,G as useMaxPositionSize,oe as useMintLiquidity,J as useMintOption,ee as useOptionPnl,te as useOptionPremium,K as usePoolData,X as usePriceAtTick,V as useTimelockConfig,Q as useVaultData,se as useVaultTVL};
|