timelock-sdk 0.0.33 → 0.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.js CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  import { lensAbi, optionsMarketAbi } from "./optionsMarket-DyBxHplR.js";
5
5
  import { PRICE_PRECISION, getErc20, getPriceAtTick, getTimelockLens, getTimelockMarket, getUniswapMathLens, roundTickDown, timelockLenses, token0ToToken1, token1ToToken0, uniswapMathLenses, wrapAmount, wrapPrice } from "./numberUtils-CAMznXR5.js";
6
- import { singleOwnerVaultAbi, uniswapV3PoolAbi } from "./uniswapV3Pool-Copswrde.js";
6
+ import { singleOwnerVaultAbi, uniswapV3PoolAbi } from "./singleOwnerVault-DMu9pqN1.js";
7
7
  import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
8
8
  import React, { createContext, useContext, useMemo } from "react";
9
9
  import { useAccount, useChainId, useClient, useReadContract, useWaitForTransactionReceipt, useWriteContract } from "wagmi";
@@ -30,6 +30,7 @@ const GetActiveUserOptionsDocument = gql`
30
30
  optionType
31
31
  strikeTick
32
32
  entryTick
33
+ startTick
33
34
  strikePrice
34
35
  entryPrice
35
36
  expiresAt
@@ -62,6 +63,7 @@ const GetClosedUserOptionsDocument = gql`
62
63
  optionType
63
64
  strikeTick
64
65
  entryTick
66
+ startTick
65
67
  strikePrice
66
68
  entryPrice
67
69
  expiresAt
@@ -196,30 +198,36 @@ const useMarketData = (marketAddr) => {
196
198
  return data || {};
197
199
  };
198
200
 
201
+ //#endregion
202
+ //#region src/hooks/useLens.ts
203
+ const useLens = () => {
204
+ const client = useClient();
205
+ return useMemo(() => ({
206
+ timelockLens: client ? getTimelockLens(client) : void 0,
207
+ uniswapLens: client ? getUniswapMathLens(client) : void 0
208
+ }), [client]);
209
+ };
210
+
199
211
  //#endregion
200
212
  //#region src/hooks/market/useExerciseOption.ts
201
213
  const useExerciseOption = (marketAddr) => {
202
214
  const { vault } = useMarketData(marketAddr);
215
+ const { timelockLens } = useLens();
203
216
  const client = useClient();
204
- const { data: lowestTick } = useReadContract({
205
- address: vault,
206
- abi: singleOwnerVaultAbi,
207
- functionName: "lowestTick"
208
- });
209
217
  const { writeContractAsync, data: hash, isPending, error } = useWriteContract();
210
218
  const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
211
- const exerciseOption = async (optionId, liquidities) => {
212
- if (!client) throw new Error("Wallet not connected");
213
- if (lowestTick === void 0 || !marketAddr) throw new Error("Lowest tick lower not available");
219
+ const exerciseOption = async (option, liquidities) => {
220
+ if (!client || !timelockLens || !vault || !marketAddr) throw new Error("Wallet not connected");
221
+ const refTick = await timelockLens.read.getRefTick([vault, option.startTick]);
214
222
  const hash$1 = await writeContractAsync({
215
223
  address: marketAddr,
216
224
  abi: optionsMarketAbi,
217
225
  functionName: "exerciseOption",
218
226
  args: [
219
- optionId,
227
+ option.id,
220
228
  liquidities,
221
229
  0n,
222
- lowestTick
230
+ refTick
223
231
  ]
224
232
  });
225
233
  await waitForTransactionReceipt(client, { hash: hash$1 });
@@ -236,16 +244,6 @@ const useExerciseOption = (marketAddr) => {
236
244
  };
237
245
  };
238
246
 
239
- //#endregion
240
- //#region src/hooks/useLens.ts
241
- const useLens = () => {
242
- const client = useClient();
243
- return useMemo(() => ({
244
- timelockLens: client ? getTimelockLens(client) : void 0,
245
- uniswapLens: client ? getUniswapMathLens(client) : void 0
246
- }), [client]);
247
- };
248
-
249
247
  //#endregion
250
248
  //#region src/hooks/market/useMaxPositionSize.ts
251
249
  const useMaxPositionSize = (marketAddr, strikeTick, maxBorrowableRange = 100) => {
@@ -413,8 +411,9 @@ const useOptionPremium = (marketAddr, optionType, optionAmount, duration, strike
413
411
  const { exact: currentTick } = useCurrentTick(pool);
414
412
  const strikeTickRounded = useMemo(() => {
415
413
  if (!tickSpacing || currentTick === void 0) return;
416
- strikeTick = roundTickDown(strikeTick ?? currentTick, tickSpacing);
417
- if (optionType === "CALL" && optionAssetIsToken0 || optionType === "PUT" && !optionAssetIsToken0) strikeTick += tickSpacing;
414
+ let strikeTickRounded$1 = roundTickDown(strikeTick ?? currentTick, tickSpacing);
415
+ if (optionType === "CALL" && optionAssetIsToken0 || optionType === "PUT" && !optionAssetIsToken0) strikeTickRounded$1 += tickSpacing;
416
+ return strikeTickRounded$1;
418
417
  }, [
419
418
  currentTick,
420
419
  tickSpacing,
@@ -461,8 +460,8 @@ const useUserOptions = (user, active = false) => {
461
460
  expiresAt: /* @__PURE__ */ new Date(Number(option.expiresAt) * 1e3),
462
461
  premiumPaid: BigInt(option.premiumPaid),
463
462
  realizedPayout: BigInt(option.realizedPayout),
464
- liquiditiesAtOpen: option.liquiditiesAtOpen.map((liquidity) => BigInt(liquidity)),
465
- liquiditiesCurrent: option.liquiditiesCurrent.map((liquidity) => BigInt(liquidity)),
463
+ liquiditiesAtOpen: option.liquiditiesAtOpen.map((l) => BigInt(l)),
464
+ liquiditiesCurrent: option.liquiditiesCurrent.map((l) => BigInt(l)),
466
465
  positionSizeAtOpen: BigInt(option.positionSizeAtOpen),
467
466
  positionSizeCurrent: BigInt(option.positionSizeCurrent),
468
467
  strikePrice: BigInt(option.strikePrice),
@@ -506,6 +505,75 @@ const useCurrentPrice = (poolAddr) => {
506
505
  }), [currentPrice, currentTick]);
507
506
  };
508
507
 
508
+ //#endregion
509
+ //#region src/lib/price.ts
510
+ const resolutionToGeckoTerminal = (resolution) => {
511
+ return {
512
+ "1m": {
513
+ timeframe: "minute",
514
+ aggregate: "1",
515
+ seconds: 60
516
+ },
517
+ "5m": {
518
+ timeframe: "minute",
519
+ aggregate: "5",
520
+ seconds: 300
521
+ },
522
+ "15m": {
523
+ timeframe: "minute",
524
+ aggregate: "15",
525
+ seconds: 900
526
+ },
527
+ "1h": {
528
+ timeframe: "hour",
529
+ aggregate: "1",
530
+ seconds: 3600
531
+ },
532
+ "4h": {
533
+ timeframe: "hour",
534
+ aggregate: "4",
535
+ seconds: 14400
536
+ },
537
+ "1d": {
538
+ timeframe: "day",
539
+ aggregate: "1",
540
+ seconds: 86400
541
+ }
542
+ }[resolution];
543
+ };
544
+ const getPriceHistory = async (poolAddress, resolution, startTimestamp, endTimestamp) => {
545
+ const network = "monad-testnet";
546
+ const { timeframe, aggregate, seconds } = resolutionToGeckoTerminal(resolution);
547
+ const timeDiff = endTimestamp - startTimestamp;
548
+ const url = `https://api.geckoterminal.com/api/v2/networks/${network}/pools/${poolAddress}/ohlcv/${timeframe}?aggregate=${aggregate}&limit=${Math.min(Math.ceil(timeDiff / seconds), 1e3)}&token=quote&currency=usd&before_timestamp=${endTimestamp}`;
549
+ const res = await fetch(url, { headers: { Accept: "application/json" } });
550
+ if (!res.ok) throw new Error(`Failed to fetch price history: ${res.statusText}`);
551
+ return (await res.json()).data.attributes.ohlcv_list.map(({ 0: timestamp, 4: price }) => ({
552
+ timestamp,
553
+ price
554
+ })).filter((point) => point.timestamp >= startTimestamp && point.timestamp <= endTimestamp).sort((a, b) => a.timestamp - b.timestamp);
555
+ };
556
+
557
+ //#endregion
558
+ //#region src/hooks/pool/usePriceHistory.ts
559
+ const usePriceHistory = ({ poolAddress, resolution, startTimestamp, endTimestamp, enabled = true }) => {
560
+ return useQuery({
561
+ queryKey: [
562
+ "priceHistory",
563
+ poolAddress,
564
+ useChainId(),
565
+ resolution,
566
+ startTimestamp,
567
+ endTimestamp
568
+ ],
569
+ queryFn: () => getPriceHistory(poolAddress, resolution, startTimestamp, endTimestamp),
570
+ enabled: enabled && !!poolAddress && startTimestamp < endTimestamp,
571
+ staleTime: 60 * 1e3,
572
+ gcTime: 300 * 1e3,
573
+ retry: 2
574
+ });
575
+ };
576
+
509
577
  //#endregion
510
578
  //#region src/hooks/vault/useVaultData.ts
511
579
  const useVaultData = (vaultAddr) => {
@@ -759,5 +827,5 @@ const useVaultTVL = (vaultAddr) => {
759
827
  };
760
828
 
761
829
  //#endregion
762
- export { TimelockMarketProvider, batchGetAmountsFromLiquidity, useActiveUserOptions, useBurnLiquidity, useClosedUserOptions, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useLens, useLiquidityBlocks, useMarketData, useMaxPositionSize, useMintLiquidity, useMintOption, useOptionPnl, useOptionPremium, usePoolData, usePriceAtTick, useTimelockConfig, useVaultData, useVaultTVL };
830
+ export { TimelockMarketProvider, batchGetAmountsFromLiquidity, useActiveUserOptions, useBurnLiquidity, useClosedUserOptions, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useLens, useLiquidityBlocks, useMarketData, useMaxPositionSize, useMintLiquidity, useMintOption, useOptionPnl, useOptionPremium, usePoolData, usePriceAtTick, usePriceHistory, useTimelockConfig, useVaultData, useVaultTVL };
763
831
  //# sourceMappingURL=client.js.map