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