timelock-sdk 0.0.78 → 0.0.80

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
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
  import { r as lensAbi, t as optionsMarketAbi } from "./optionsMarket-6PlyceXp.js";
5
- import { A as timelockLenses, D as getTimelockLens, E as getErc20, O as getTimelockMarket, S as roundTickDown, T as token1ToToken0, _ as getPriceAtTick, d as wrapPrice, g as getNearestValidStrikeTick, j as uniswapMathLenses, k as getUniswapMathLens, l as wrapAmount, m as PRICE_PRECISION, w as token0ToToken1, x as liquiditiesToAmounts } from "./numberUtils--YU8VSJO.js";
5
+ import { A as getErc20, D as token1ToToken0, E as token0ToToken1, F as uniswapMathLenses, M as getTimelockMarket, N as getUniswapMathLens, P as timelockLenses, d as wrapAmount, j as getTimelockLens, k as getPriceHistory, n as EMPTY_ARRAY, p as wrapPrice, t as getPayoutAtTick, v as getNearestValidStrikeTick, w as roundTickDown, y as getPriceAtTick } from "./optionUtils-CoIk8zAr.js";
6
6
  import { n as uniswapV3PoolAbi, t as singleOwnerVaultAbi } from "./singleOwnerVault-BeJChjfJ.js";
7
7
  import { encodeAbiParameters, encodeFunctionData, erc20Abi, maxUint256, zeroAddress } from "viem";
8
8
  import React, { createContext, useContext, useMemo } from "react";
@@ -548,15 +548,16 @@ const useMintOption = (marketAddr) => {
548
548
  //#endregion
549
549
  //#region src/hooks/market/useOptionPnl.ts
550
550
  const useOptionPnl = (option) => {
551
- const { marketAddr, optionType, strikePrice, positionSizeCurrent } = option;
551
+ const { marketAddr, optionType, entryTick, positionSizeCurrent } = option;
552
552
  const { pool, optionAssetIsToken0, payoutAssetDecimals, tickSpacing } = useMarketData(marketAddr);
553
553
  const { exact: currentTick } = useCurrentTick(pool);
554
- const strikeSize = positionSizeCurrent * strikePrice / PRICE_PRECISION;
555
554
  const displayPnl = useMemo(() => {
556
- if (optionAssetIsToken0 === void 0 || currentTick === void 0 || !positionSizeCurrent || !payoutAssetDecimals) return void 0;
557
- const delta = (optionAssetIsToken0 ? token0ToToken1(positionSizeCurrent, currentTick) : token1ToToken0(positionSizeCurrent, currentTick)) - strikeSize;
555
+ if (optionAssetIsToken0 === void 0 || currentTick === void 0 || !payoutAssetDecimals) return void 0;
556
+ const entrySize = optionAssetIsToken0 ? token0ToToken1(positionSizeCurrent, entryTick) : token1ToToken0(positionSizeCurrent, entryTick);
557
+ const delta = (optionAssetIsToken0 ? token0ToToken1(positionSizeCurrent, currentTick) : token1ToToken0(positionSizeCurrent, currentTick)) - entrySize;
558
558
  return wrapAmount(optionType === "CALL" ? delta : -delta, payoutAssetDecimals);
559
559
  }, [
560
+ entryTick,
560
561
  optionType,
561
562
  optionAssetIsToken0,
562
563
  currentTick,
@@ -565,16 +566,14 @@ const useOptionPnl = (option) => {
565
566
  ]);
566
567
  return {
567
568
  unrealizedPayout: useMemo(() => {
568
- if (!payoutAssetDecimals || !currentTick || !tickSpacing) return void 0;
569
- const [amount0, amount1] = liquiditiesToAmounts(option.liquiditiesCurrent, option.startTick, currentTick, tickSpacing);
570
- const delta = (optionAssetIsToken0 ? amount1 + token0ToToken1(amount0, currentTick) : amount0 + token1ToToken0(amount1, currentTick)) - strikeSize;
571
- const pnl = optionType === "CALL" ? delta : -delta;
572
- return wrapAmount(pnl < 0 ? 0n : pnl, payoutAssetDecimals);
569
+ if (!payoutAssetDecimals || !currentTick || !tickSpacing || optionAssetIsToken0 === void 0) return void 0;
570
+ return wrapAmount(getPayoutAtTick(option, option.liquiditiesCurrent, currentTick, tickSpacing, optionAssetIsToken0), payoutAssetDecimals);
573
571
  }, [
574
572
  currentTick,
575
573
  tickSpacing,
576
574
  optionType,
577
- payoutAssetDecimals
575
+ payoutAssetDecimals,
576
+ optionAssetIsToken0
578
577
  ]),
579
578
  displayPnl
580
579
  };
@@ -667,7 +666,7 @@ const useUserOptions = (userAddr, marketAddr, active = false) => {
667
666
  enabled: !!userAddr && !!marketAddr && !!graphqlClient
668
667
  });
669
668
  return {
670
- data: data || [],
669
+ data: data || EMPTY_ARRAY,
671
670
  ...rest
672
671
  };
673
672
  };
@@ -742,7 +741,7 @@ const useUserOperators = (userAddr, marketAddr) => {
742
741
  });
743
742
  return {
744
743
  ...rest,
745
- data: data || []
744
+ data: data || EMPTY_ARRAY
746
745
  };
747
746
  };
748
747
 
@@ -784,17 +783,17 @@ const useSetOperatorPerms = (marketAddr) => {
784
783
  //#region src/hooks/market/useOptionTimeline.ts
785
784
  const useOptionTimeline = (marketAddr, optionId) => {
786
785
  const { graphqlClient } = useTimelockConfig();
787
- const normalizedMarketAddr = marketAddr === null || marketAddr === void 0 ? void 0 : marketAddr.toLowerCase();
786
+ marketAddr = marketAddr === null || marketAddr === void 0 ? void 0 : marketAddr.toLowerCase();
788
787
  const { data,...rest } = useQuery({
789
788
  queryKey: [
790
789
  "optionTimeline",
791
- normalizedMarketAddr || "--",
790
+ marketAddr || "--",
792
791
  (optionId === null || optionId === void 0 ? void 0 : optionId.toString()) || "--"
793
792
  ],
794
793
  queryFn: async () => {
795
- if (!graphqlClient || !normalizedMarketAddr || optionId === void 0) return [];
794
+ if (!graphqlClient || !marketAddr || optionId === void 0) return [];
796
795
  const result = await graphqlClient.GetOptionEvents({
797
- marketAddr: normalizedMarketAddr,
796
+ marketAddr,
798
797
  optionId: optionId.toString()
799
798
  });
800
799
  const mintEvents = result.MintOptionEvent.map((event) => ({
@@ -844,10 +843,10 @@ const useOptionTimeline = (marketAddr, optionId) => {
844
843
  }))
845
844
  ].sort((a, b) => a.data.timestamp.getTime() - b.data.timestamp.getTime());
846
845
  },
847
- enabled: !!normalizedMarketAddr && optionId !== void 0 && !!graphqlClient
846
+ enabled: !!marketAddr && optionId !== void 0 && !!graphqlClient
848
847
  });
849
848
  return {
850
- data: data || [],
849
+ data: data || EMPTY_ARRAY,
851
850
  ...rest
852
851
  };
853
852
  };
@@ -875,81 +874,6 @@ const useCurrentPrice = (poolAddr) => {
875
874
  }), [currentPrice, currentTick]);
876
875
  };
877
876
 
878
- //#endregion
879
- //#region src/lib/price.ts
880
- const getResolutionConfig = (resolution) => {
881
- return {
882
- "1m": {
883
- timeframe: "minute",
884
- aggregate: "1",
885
- seconds: 60
886
- },
887
- "5m": {
888
- timeframe: "minute",
889
- aggregate: "5",
890
- seconds: 300
891
- },
892
- "15m": {
893
- timeframe: "minute",
894
- aggregate: "15",
895
- seconds: 900
896
- },
897
- "1h": {
898
- timeframe: "hour",
899
- aggregate: "1",
900
- seconds: 3600
901
- },
902
- "4h": {
903
- timeframe: "hour",
904
- aggregate: "4",
905
- seconds: 14400
906
- },
907
- "1d": {
908
- timeframe: "day",
909
- aggregate: "1",
910
- seconds: 86400
911
- }
912
- }[resolution];
913
- };
914
- const fillGaps = (prices, start, end, intervalMs) => {
915
- if (prices.length === 0) return [];
916
- const priceMap = /* @__PURE__ */ new Map();
917
- for (const point of prices) {
918
- const alignedTime = Math.floor(point.timestamp.getTime() / intervalMs) * intervalMs;
919
- priceMap.set(alignedTime, point);
920
- }
921
- const filled = [];
922
- let currentTime = Math.floor(prices[0].timestamp.getTime() / intervalMs) * intervalMs;
923
- let lastKnownPrice = null;
924
- while (currentTime <= end.getTime()) {
925
- const existing = priceMap.get(currentTime);
926
- if (existing) {
927
- filled.push(existing);
928
- lastKnownPrice = existing;
929
- } else if (lastKnownPrice) filled.push({
930
- timestamp: new Date(currentTime),
931
- price: lastKnownPrice.price
932
- });
933
- currentTime += intervalMs;
934
- }
935
- return filled;
936
- };
937
- const getPriceHistory = async (poolAddress, resolution, start, end) => {
938
- const network = "monad-testnet";
939
- const { timeframe, aggregate, seconds } = getResolutionConfig(resolution);
940
- if (end.getTime() > Date.now()) end = new Date(Date.now());
941
- const startSecs = Math.floor(start.getTime() / 1e3);
942
- const endSecs = Math.floor(end.getTime() / 1e3);
943
- const diffSeconds = endSecs - startSecs;
944
- const url = `https://api.geckoterminal.com/api/v2/networks/${network}/pools/${poolAddress}/ohlcv/${timeframe}?aggregate=${aggregate}&limit=${Math.min(Math.ceil(diffSeconds / seconds), 1e3)}&token=quote&currency=usd&before_timestamp=${endSecs}`;
945
- const res = await fetch(url, { headers: { Accept: "application/json" } });
946
- if (!res.ok) throw new Error(`Failed to fetch price history: ${res.statusText}`);
947
- return fillGaps((await res.json()).data.attributes.ohlcv_list.map(([timestamp, , , , close]) => ({
948
- timestamp: /* @__PURE__ */ new Date(timestamp * 1e3),
949
- price: close
950
- })).sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()), start, end, seconds * 1e3).filter((point) => point.timestamp.getTime() / 1e3 >= startSecs && point.timestamp.getTime() / 1e3 <= endSecs);
951
- };
952
-
953
877
  //#endregion
954
878
  //#region src/hooks/pool/usePriceHistory.ts
955
879
  const usePriceHistory = (pool, resolution, startTimestamp, endTimestamp) => {