timelock-sdk 0.0.144 → 0.0.146

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.d.cts CHANGED
@@ -1,3 +1,3 @@
1
1
  import "./uniswapMathLens-X6H7QwrK.cjs";
2
- import { $ as ExerciseOptionEvent, At as TimelockProvider, Dt as OptionData, Et as useExerciseOption, G as useUserOperators, J as useClosedUserPerps, K as useOperatorPerms, Mt as useTimelockConfig, Ot as useActiveUserOptions, Q as useMintPerp, Tt as useMarketData, W as useSetOperatorPerms, X as usePerpsOperator, Y as useUserPerps, Z as useClosePerp, _ as useCurrentTick, a as useTokenData, at as useOptionPremium, b as UniswapPoolData, c as batchGetAmountsFromLiquidity, ct as useMaxPositionSize, d as useLiquidityBlocks, et as ExtendEvent, f as useBurnLiquidity, g as usePriceAtTick, h as usePriceAtSqrtPriceX96, i as TokenData, it as useExtendOption, jt as useCurrentMarket, kt as useClosedUserOptions, l as useMintLiquidity, m as usePriceHistory, n as useApproval, nt as OptionEvent, o as useVaultTVL, ot as useOptionPnl, p as useMarketPriceHistory, q as useActiveUserPerps, r as useTokenBalance, rt as useOptionTimeline, s as useVaultData, st as useMintOption, t as useLens, tt as MintOptionEvent, u as LiquidityBlockData, v as useCurrentPrice, wt as useMarketVolume, x as usePoolData, y as PoolKey } from "./client-BRzenCT7.cjs";
2
+ import { $ as ExerciseOptionEvent, At as TimelockProvider, Dt as OptionData, Et as useExerciseOption, G as useUserOperators, J as useClosedUserPerps, K as useOperatorPerms, Mt as useTimelockConfig, Ot as useActiveUserOptions, Q as useMintPerp, Tt as useMarketData, W as useSetOperatorPerms, X as usePerpsOperator, Y as useUserPerps, Z as useClosePerp, _ as useCurrentTick, a as useTokenData, at as useOptionPremium, b as UniswapPoolData, c as batchGetAmountsFromLiquidity, ct as useMaxPositionSize, d as useLiquidityBlocks, et as ExtendEvent, f as useBurnLiquidity, g as usePriceAtTick, h as usePriceAtSqrtPriceX96, i as TokenData, it as useExtendOption, jt as useCurrentMarket, kt as useClosedUserOptions, l as useMintLiquidity, m as usePriceHistory, n as useApproval, nt as OptionEvent, o as useVaultTVL, ot as useOptionPnl, p as useMarketPriceHistory, q as useActiveUserPerps, r as useTokenBalance, rt as useOptionTimeline, s as useVaultData, st as useMintOption, t as useLens, tt as MintOptionEvent, u as LiquidityBlockData, v as useCurrentPrice, wt as useMarketVolume, x as usePoolData, y as PoolKey } from "./client-jAKjSAlA.cjs";
3
3
  export { ExerciseOptionEvent, ExtendEvent, LiquidityBlockData, MintOptionEvent, OptionData, OptionEvent, PoolKey, TimelockProvider, TokenData, UniswapPoolData, batchGetAmountsFromLiquidity, useActiveUserOptions, useActiveUserPerps, useApproval, useBurnLiquidity, useClosePerp, useClosedUserOptions, useClosedUserPerps, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useExtendOption, useLens, useLiquidityBlocks, useMarketData, useMarketPriceHistory, useMarketVolume, useMaxPositionSize, useMintLiquidity, useMintOption, useMintPerp, useOperatorPerms, useOptionPnl, useOptionPremium, useOptionTimeline, usePerpsOperator, usePoolData, usePriceAtSqrtPriceX96, usePriceAtTick, usePriceHistory, useSetOperatorPerms, useTimelockConfig, useTokenBalance, useTokenData, useUserOperators, useUserPerps, useVaultData, useVaultTVL };
package/dist/client.js CHANGED
@@ -4,7 +4,7 @@
4
4
  import { i as erc20Abi$1, r as lensAbi, t as optionsMarketAbi } from "./optionsMarket-DpvbjVFL.js";
5
5
  import { A as token1ToToken0, B as swappers, D as roundTick, F as getStateView, H as statelessStateViewAbi, I as getTimelockLens, L as getTimelockMarket, N as getPriceHistory, O as token0ToToken1, P as getErc20, b as getPriceAtSqrtPriceX96, f as wrapAmount, j as token1ToToken0AtTick, k as token0ToToken1AtTick, m as wrapPrice, r as EMPTY_ARRAY, t as getPayoutAtPrice, v as getAmountsFromLiquidity, x as getPriceAtTick, y as getNearestValidStrikeTick } from "./optionUtils-DuJzVAs5.js";
6
6
  import { t as singleOwnerVaultAbi } from "./singleOwnerVault-BJyEs_D_.js";
7
- import { encodeAbiParameters, encodeFunctionData, erc20Abi, maxUint160, maxUint256, zeroAddress } from "viem";
7
+ import { encodeAbiParameters, encodeFunctionData, erc20Abi, maxUint256, zeroAddress } from "viem";
8
8
  import React, { createContext, useContext, useEffect, useMemo } from "react";
9
9
  import { GraphQLClient, RequestOptions } from "graphql-request";
10
10
  import gql from "graphql-tag";
@@ -487,6 +487,98 @@ const useMarketData = (marketAddr) => {
487
487
  return data || fallback || {};
488
488
  };
489
489
 
490
+ //#endregion
491
+ //#region src/hooks/pool/usePoolData.ts
492
+ const usePoolData = (poolManager, poolKey) => {
493
+ const { timelockLens } = useLens();
494
+ const { data } = useReadContract({
495
+ address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
496
+ abi: lensAbi,
497
+ functionName: "getPoolData",
498
+ args: poolManager && poolKey ? [poolManager, poolKey] : void 0,
499
+ query: { enabled: !!poolManager && !!poolKey }
500
+ });
501
+ const _default = useMemo(() => ({
502
+ token0: poolKey === null || poolKey === void 0 ? void 0 : poolKey.currency0,
503
+ token1: poolKey === null || poolKey === void 0 ? void 0 : poolKey.currency1,
504
+ tickSpacing: poolKey === null || poolKey === void 0 ? void 0 : poolKey.tickSpacing,
505
+ fee: poolKey === null || poolKey === void 0 ? void 0 : poolKey.fee
506
+ }), [poolKey]);
507
+ return data || _default;
508
+ };
509
+
510
+ //#endregion
511
+ //#region src/hooks/pool/usePriceAtTick.ts
512
+ const usePriceAtTick = (poolManager, poolKey, tick) => {
513
+ const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
514
+ const priceBigInt = useMemo(() => tick !== void 0 ? getPriceAtTick(tick) : void 0, [tick]);
515
+ return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
516
+ priceBigInt,
517
+ token0Decimals,
518
+ token1Decimals
519
+ ]);
520
+ };
521
+ const usePriceAtSqrtPriceX96 = (poolManager, poolKey, sqrtPriceX96) => {
522
+ const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
523
+ const priceBigInt = useMemo(() => sqrtPriceX96 !== void 0 ? getPriceAtSqrtPriceX96(sqrtPriceX96) : void 0, [sqrtPriceX96]);
524
+ return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
525
+ priceBigInt,
526
+ token0Decimals,
527
+ token1Decimals
528
+ ]);
529
+ };
530
+
531
+ //#endregion
532
+ //#region src/hooks/pool/useCurrentTick.ts
533
+ const useCurrentTick = (poolManager, poolKey) => {
534
+ const { stateView } = useLens();
535
+ const { tickSpacing } = usePoolData(poolManager, poolKey);
536
+ const { data, ...rest } = useReadContract({
537
+ address: stateView === null || stateView === void 0 ? void 0 : stateView.address,
538
+ abi: statelessStateViewAbi,
539
+ functionName: "getSlot0",
540
+ args: poolManager && poolKey ? [poolManager, poolKey] : void 0,
541
+ query: {
542
+ enabled: !!poolManager && !!poolKey,
543
+ refetchInterval: 3e3,
544
+ select: (raw) => {
545
+ const sqrtPriceX96 = raw[0];
546
+ const exact = raw[1];
547
+ return {
548
+ exact,
549
+ rounded: tickSpacing ? roundTick(exact, tickSpacing) : void 0,
550
+ sqrtPriceX96
551
+ };
552
+ }
553
+ }
554
+ });
555
+ return {
556
+ exact: data === null || data === void 0 ? void 0 : data.exact,
557
+ rounded: data === null || data === void 0 ? void 0 : data.rounded,
558
+ sqrtPriceX96: data === null || data === void 0 ? void 0 : data.sqrtPriceX96,
559
+ ...rest
560
+ };
561
+ };
562
+
563
+ //#endregion
564
+ //#region src/hooks/pool/useCurrentPrice.ts
565
+ const useCurrentPrice = (poolManager, poolKey) => {
566
+ const { sqrtPriceX96, exact, rounded } = useCurrentTick(poolManager, poolKey);
567
+ const currentPrice = usePriceAtSqrtPriceX96(poolManager, poolKey, sqrtPriceX96);
568
+ return useMemo(() => ({
569
+ currentPrice,
570
+ sqrtPriceX96,
571
+ currentTick: {
572
+ exact,
573
+ rounded
574
+ }
575
+ }), [
576
+ currentPrice,
577
+ exact,
578
+ rounded
579
+ ]);
580
+ };
581
+
490
582
  //#endregion
491
583
  //#region src/lib/utils.ts
492
584
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -494,20 +586,24 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
494
586
  //#endregion
495
587
  //#region src/hooks/options/useExerciseOption.ts
496
588
  const useExerciseOption = (marketAddr) => {
497
- const { vault, poolKey } = useMarketData(marketAddr);
589
+ const { vault, poolManager, poolKey } = useMarketData(marketAddr);
498
590
  const { timelockLens } = useLens();
499
591
  const queryClient = useQueryClient();
500
592
  const client = useClient();
501
593
  const { address } = useConnection();
502
594
  const { writeContractAsync } = useWriteContract();
595
+ const { sqrtPriceX96 } = useCurrentPrice(poolManager, poolKey);
503
596
  const exerciseOption = async ({ option, liquidities }) => {
504
597
  if (!client || !address) throw new Error("Wallet not connected");
505
598
  if (!marketAddr) throw new Error("Market address not available");
506
599
  if (!timelockLens) throw new Error("Timelock lens not available");
507
600
  if (!vault) throw new Error("Vault not available");
508
601
  if (!poolKey) throw new Error("Pool data not available");
602
+ if (!sqrtPriceX96) throw new Error("Current price not available");
509
603
  const swapper = swappers[client.chain.id];
510
604
  if (!swapper) throw new Error("Swapper not available");
605
+ const minSqrtPrice = sqrtPriceX96 * 9n / 10n;
606
+ const maxSqrtPrice = sqrtPriceX96 * 11n / 10n;
511
607
  const refTick = await timelockLens.read.getRefTick([vault, option.startTick]);
512
608
  const hash = await writeContractAsync({
513
609
  address: marketAddr,
@@ -548,8 +644,8 @@ const useExerciseOption = (marketAddr) => {
548
644
  { type: "uint160" }
549
645
  ], [
550
646
  poolKey,
551
- 0n,
552
- maxUint160 - 1n
647
+ minSqrtPrice,
648
+ maxSqrtPrice
553
649
  ]),
554
650
  refTick
555
651
  ]
@@ -609,58 +705,6 @@ const useMaxPositionSize = (marketAddr, maxBorrowableRange = 100) => {
609
705
  };
610
706
  };
611
707
 
612
- //#endregion
613
- //#region src/hooks/pool/usePoolData.ts
614
- const usePoolData = (poolManager, poolKey) => {
615
- const { timelockLens } = useLens();
616
- const { data } = useReadContract({
617
- address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
618
- abi: lensAbi,
619
- functionName: "getPoolData",
620
- args: poolManager && poolKey ? [poolManager, poolKey] : void 0,
621
- query: { enabled: !!poolManager && !!poolKey }
622
- });
623
- const _default = useMemo(() => ({
624
- token0: poolKey === null || poolKey === void 0 ? void 0 : poolKey.currency0,
625
- token1: poolKey === null || poolKey === void 0 ? void 0 : poolKey.currency1,
626
- tickSpacing: poolKey === null || poolKey === void 0 ? void 0 : poolKey.tickSpacing,
627
- fee: poolKey === null || poolKey === void 0 ? void 0 : poolKey.fee
628
- }), [poolKey]);
629
- return data || _default;
630
- };
631
-
632
- //#endregion
633
- //#region src/hooks/pool/useCurrentTick.ts
634
- const useCurrentTick = (poolManager, poolKey) => {
635
- const { stateView } = useLens();
636
- const { tickSpacing } = usePoolData(poolManager, poolKey);
637
- const { data, ...rest } = useReadContract({
638
- address: stateView === null || stateView === void 0 ? void 0 : stateView.address,
639
- abi: statelessStateViewAbi,
640
- functionName: "getSlot0",
641
- args: poolManager && poolKey ? [poolManager, poolKey] : void 0,
642
- query: {
643
- enabled: !!poolManager && !!poolKey,
644
- refetchInterval: 3e3,
645
- select: (raw) => {
646
- const sqrtPriceX96 = raw[0];
647
- const exact = raw[1];
648
- return {
649
- exact,
650
- rounded: tickSpacing ? roundTick(exact, tickSpacing) : void 0,
651
- sqrtPriceX96
652
- };
653
- }
654
- }
655
- });
656
- return {
657
- exact: data === null || data === void 0 ? void 0 : data.exact,
658
- rounded: data === null || data === void 0 ? void 0 : data.rounded,
659
- sqrtPriceX96: data === null || data === void 0 ? void 0 : data.sqrtPriceX96,
660
- ...rest
661
- };
662
- };
663
-
664
708
  //#endregion
665
709
  //#region src/hooks/tokens/useApproval.ts
666
710
  const useApproval = () => {
@@ -738,46 +782,6 @@ const useMintOption = (marketAddr) => {
738
782
  return useMutation({ mutationFn: mintOption });
739
783
  };
740
784
 
741
- //#endregion
742
- //#region src/hooks/pool/usePriceAtTick.ts
743
- const usePriceAtTick = (poolManager, poolKey, tick) => {
744
- const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
745
- const priceBigInt = useMemo(() => tick !== void 0 ? getPriceAtTick(tick) : void 0, [tick]);
746
- return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
747
- priceBigInt,
748
- token0Decimals,
749
- token1Decimals
750
- ]);
751
- };
752
- const usePriceAtSqrtPriceX96 = (poolManager, poolKey, sqrtPriceX96) => {
753
- const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
754
- const priceBigInt = useMemo(() => sqrtPriceX96 !== void 0 ? getPriceAtSqrtPriceX96(sqrtPriceX96) : void 0, [sqrtPriceX96]);
755
- return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
756
- priceBigInt,
757
- token0Decimals,
758
- token1Decimals
759
- ]);
760
- };
761
-
762
- //#endregion
763
- //#region src/hooks/pool/useCurrentPrice.ts
764
- const useCurrentPrice = (poolManager, poolKey) => {
765
- const { sqrtPriceX96, exact, rounded } = useCurrentTick(poolManager, poolKey);
766
- const currentPrice = usePriceAtSqrtPriceX96(poolManager, poolKey, sqrtPriceX96);
767
- return useMemo(() => ({
768
- currentPrice,
769
- sqrtPriceX96,
770
- currentTick: {
771
- exact,
772
- rounded
773
- }
774
- }), [
775
- currentPrice,
776
- exact,
777
- rounded
778
- ]);
779
- };
780
-
781
785
  //#endregion
782
786
  //#region src/hooks/options/useOptionPnl.ts
783
787
  const useOptionPnl = (option) => {