timelock-sdk 0.0.159 → 0.0.160
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-C5uUBjks.d.ts → client-DmXuKRhM.d.cts} +1938 -1101
- package/dist/{client-COCvyyDy.d.cts → client-Dx6YoDji.d.ts} +2438 -1601
- package/dist/client.cjs +362 -168
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +2 -2
- package/dist/client.d.ts +2 -2
- package/dist/client.js +359 -170
- package/dist/client.js.map +1 -1
- package/dist/package.d.cts +1 -1
- package/dist/package.d.ts +1 -1
- package/package.json +1 -1
package/dist/client.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import "./uniswapMathLens-BoQxBJoY.cjs";
|
|
2
|
-
import {
|
|
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, useFeeRates, useGuardianGlobalState, useLens, useLiquidityBlocks, useMarketData, useMarketPriceHistory, useMarketVolume, useMaxPositionSize, useMintLiquidity, useMintOption, useMintPerp, useOperatorPerms, useOptionPnl, useOptionPremium, useOptionTimeline, usePauseGlobalTrading, usePauseMarketTrading, usePerpsOperator, usePoolData, usePriceAtSqrtPriceX96, usePriceAtTick, usePriceHistory, useSetOperatorPerms, useTimelockConfig, useTokenBalance, useTokenData, useUpdateMarketFees, useUserOperators, useUserPerps, useVaultData, useVaultTVL };
|
|
2
|
+
import { A as useCurrentPrice, Bt as useMarketVolume, C as useLiquidityBlocks, Ct as useMintOption, D as usePriceAtSqrtPriceX96, E as usePriceHistory, Gt as useTimelockConfig, Ht as useMarketData, M as UniswapPoolData, N as usePoolData, O as usePriceAtTick, S as LiquidityBlockData, St as useClosedUserOptions, T as useMarketPriceHistory, Ut as TimelockProvider, Vt as useMarketState, Wt as useCurrentMarket, _ as useTokenData, _t as useOptionPremium, a as MarketPricingData, at as useActiveUserPerps, b as batchGetAmountsFromLiquidity, bt as OptionData, c as useMarketPricing, ct as usePerpsOperator, d as usePauseMarketTrading, dt as ExerciseOptionEvent, f as usePauseGlobalTrading, ft as ExtendEvent, g as TokenData, gt as useExtendOption, h as useTokenBalance, ht as useOptionTimeline, i as useOptionPricingParams, it as useOperatorPerms, j as PoolKey, k as useCurrentTick, l as useUpdateMarketFees, lt as useClosePerp, m as useApproval, mt as OptionEvent, n as useUpdateMarketPricing, nt as useSetOperatorPerms, o as OptionPricingData, ot as useClosedUserPerps, p as useGuardianGlobalState, pt as MintOptionEvent, r as useStaticPricingParams, rt as useUserOperators, s as StaticPricingData, st as useUserPerps, t as useLens, u as useFeeRates, ut as useMintPerp, v as useVaultTVL, vt as useOptionPnl, w as useBurnLiquidity, wt as useMaxPositionSize, x as useMintLiquidity, xt as useActiveUserOptions, y as useVaultData, yt as useExerciseOption } from "./client-DmXuKRhM.cjs";
|
|
3
|
+
export { ExerciseOptionEvent, ExtendEvent, LiquidityBlockData, MarketPricingData, MintOptionEvent, OptionData, OptionEvent, OptionPricingData, PoolKey, StaticPricingData, TimelockProvider, TokenData, UniswapPoolData, batchGetAmountsFromLiquidity, useActiveUserOptions, useActiveUserPerps, useApproval, useBurnLiquidity, useClosePerp, useClosedUserOptions, useClosedUserPerps, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useExtendOption, useFeeRates, useGuardianGlobalState, useLens, useLiquidityBlocks, useMarketData, useMarketPriceHistory, useMarketPricing, useMarketState, useMarketVolume, useMaxPositionSize, useMintLiquidity, useMintOption, useMintPerp, useOperatorPerms, useOptionPnl, useOptionPremium, useOptionPricingParams, useOptionTimeline, usePauseGlobalTrading, usePauseMarketTrading, usePerpsOperator, usePoolData, usePriceAtSqrtPriceX96, usePriceAtTick, usePriceHistory, useSetOperatorPerms, useStaticPricingParams, useTimelockConfig, useTokenBalance, useTokenData, useUpdateMarketFees, useUpdateMarketPricing, useUserOperators, useUserPerps, useVaultData, useVaultTVL };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import "./uniswapMathLens-Ds8UmCMU.js";
|
|
2
|
-
import {
|
|
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, useFeeRates, useGuardianGlobalState, useLens, useLiquidityBlocks, useMarketData, useMarketPriceHistory, useMarketVolume, useMaxPositionSize, useMintLiquidity, useMintOption, useMintPerp, useOperatorPerms, useOptionPnl, useOptionPremium, useOptionTimeline, usePauseGlobalTrading, usePauseMarketTrading, usePerpsOperator, usePoolData, usePriceAtSqrtPriceX96, usePriceAtTick, usePriceHistory, useSetOperatorPerms, useTimelockConfig, useTokenBalance, useTokenData, useUpdateMarketFees, useUserOperators, useUserPerps, useVaultData, useVaultTVL };
|
|
2
|
+
import { A as useCurrentPrice, Bt as useMarketVolume, C as useLiquidityBlocks, Ct as useMintOption, D as usePriceAtSqrtPriceX96, E as usePriceHistory, Gt as useTimelockConfig, Ht as useMarketData, M as UniswapPoolData, N as usePoolData, O as usePriceAtTick, S as LiquidityBlockData, St as useClosedUserOptions, T as useMarketPriceHistory, Ut as TimelockProvider, Vt as useMarketState, Wt as useCurrentMarket, _ as useTokenData, _t as useOptionPremium, a as MarketPricingData, at as useActiveUserPerps, b as batchGetAmountsFromLiquidity, bt as OptionData, c as useMarketPricing, ct as usePerpsOperator, d as usePauseMarketTrading, dt as ExerciseOptionEvent, f as usePauseGlobalTrading, ft as ExtendEvent, g as TokenData, gt as useExtendOption, h as useTokenBalance, ht as useOptionTimeline, i as useOptionPricingParams, it as useOperatorPerms, j as PoolKey, k as useCurrentTick, l as useUpdateMarketFees, lt as useClosePerp, m as useApproval, mt as OptionEvent, n as useUpdateMarketPricing, nt as useSetOperatorPerms, o as OptionPricingData, ot as useClosedUserPerps, p as useGuardianGlobalState, pt as MintOptionEvent, r as useStaticPricingParams, rt as useUserOperators, s as StaticPricingData, st as useUserPerps, t as useLens, u as useFeeRates, ut as useMintPerp, v as useVaultTVL, vt as useOptionPnl, w as useBurnLiquidity, wt as useMaxPositionSize, x as useMintLiquidity, xt as useActiveUserOptions, y as useVaultData, yt as useExerciseOption } from "./client-Dx6YoDji.js";
|
|
3
|
+
export { ExerciseOptionEvent, ExtendEvent, LiquidityBlockData, MarketPricingData, MintOptionEvent, OptionData, OptionEvent, OptionPricingData, PoolKey, StaticPricingData, TimelockProvider, TokenData, UniswapPoolData, batchGetAmountsFromLiquidity, useActiveUserOptions, useActiveUserPerps, useApproval, useBurnLiquidity, useClosePerp, useClosedUserOptions, useClosedUserPerps, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useExtendOption, useFeeRates, useGuardianGlobalState, useLens, useLiquidityBlocks, useMarketData, useMarketPriceHistory, useMarketPricing, useMarketState, useMarketVolume, useMaxPositionSize, useMintLiquidity, useMintOption, useMintPerp, useOperatorPerms, useOptionPnl, useOptionPremium, useOptionPricingParams, useOptionTimeline, usePauseGlobalTrading, usePauseMarketTrading, usePerpsOperator, usePoolData, usePriceAtSqrtPriceX96, usePriceAtTick, usePriceHistory, useSetOperatorPerms, useStaticPricingParams, useTimelockConfig, useTokenBalance, useTokenData, useUpdateMarketFees, useUpdateMarketPricing, useUserOperators, useUserPerps, useVaultData, useVaultTVL };
|
package/dist/client.js
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
import { i as erc20Abi$1, r as lensAbi, t as optionsMarketAbi } from "./optionsMarket-BbXpQnpV.js";
|
|
5
5
|
import { A as token1ToToken0, B as swappers, D as roundTick, F as getStateView, I as getTimelockLens, L as getTimelockMarket, N as getPriceHistory, O as token0ToToken1, P as getErc20, U as statelessStateViewAbi, V as timelockFactories, 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-CL-MSoFS.js";
|
|
6
6
|
import { t as singleOwnerVaultAbi } from "./singleOwnerVault-BJyEs_D_.js";
|
|
7
|
-
import { decodeEventLog, encodeAbiParameters, encodeFunctionData, erc20Abi, maxUint256, zeroAddress } from "viem";
|
|
7
|
+
import { decodeAbiParameters, decodeEventLog, 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";
|
|
11
|
-
import { waitForTransactionReceipt } from "viem/actions";
|
|
12
11
|
import { useChainId, useClient, useConnection, usePublicClient, useReadContract, useSignMessage, useWaitForTransactionReceipt, useWriteContract } from "wagmi";
|
|
13
12
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
13
|
+
import { waitForTransactionReceipt } from "viem/actions";
|
|
14
14
|
import { z } from "zod";
|
|
15
15
|
|
|
16
16
|
//#region src/generated/graphql.ts
|
|
@@ -459,6 +459,70 @@ const useMarketData = (marketAddr) => {
|
|
|
459
459
|
return data || {};
|
|
460
460
|
};
|
|
461
461
|
|
|
462
|
+
//#endregion
|
|
463
|
+
//#region src/hooks/options/useMarketState.ts
|
|
464
|
+
const useMarketState = (marketAddr) => {
|
|
465
|
+
const { timelockLens } = useLens();
|
|
466
|
+
const { data, ...rest } = useReadContract({
|
|
467
|
+
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
468
|
+
abi: lensAbi,
|
|
469
|
+
functionName: "getMarketState",
|
|
470
|
+
args: marketAddr ? [marketAddr] : void 0
|
|
471
|
+
});
|
|
472
|
+
return {
|
|
473
|
+
data: data || {},
|
|
474
|
+
...rest
|
|
475
|
+
};
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
//#endregion
|
|
479
|
+
//#region src/hooks/options/useMarketVolume.ts
|
|
480
|
+
const useMarketVolume = (marketAddr) => {
|
|
481
|
+
const { graphqlClient } = useTimelockConfig();
|
|
482
|
+
const { data, ...rest } = useQuery({
|
|
483
|
+
queryKey: ["marketVolume", (marketAddr === null || marketAddr === void 0 ? void 0 : marketAddr.toLowerCase()) || "--"],
|
|
484
|
+
queryFn: async () => {
|
|
485
|
+
const result = await graphqlClient.GetMarketVolume({ marketAddr: marketAddr.toLowerCase() });
|
|
486
|
+
return {
|
|
487
|
+
...result.TimelockMarket[0],
|
|
488
|
+
address: result.TimelockMarket[0].address,
|
|
489
|
+
volume: BigInt(result.TimelockMarket[0].volume),
|
|
490
|
+
optionsCount: BigInt(result.TimelockMarket[0].optionsCount),
|
|
491
|
+
tradersCount: BigInt(result.TimelockMarket[0].tradersCount)
|
|
492
|
+
};
|
|
493
|
+
},
|
|
494
|
+
enabled: !!marketAddr && !!graphqlClient
|
|
495
|
+
});
|
|
496
|
+
return {
|
|
497
|
+
data: data || {},
|
|
498
|
+
...rest
|
|
499
|
+
};
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
//#endregion
|
|
503
|
+
//#region src/hooks/options/useMaxPositionSize.ts
|
|
504
|
+
const useMaxPositionSize = (marketAddr, strikeTick, maxSteps = 100) => {
|
|
505
|
+
const { timelockLens } = useLens();
|
|
506
|
+
const { optionAssetDecimals } = useMarketData(marketAddr);
|
|
507
|
+
const { data, ...rest } = useReadContract({
|
|
508
|
+
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
509
|
+
abi: lensAbi,
|
|
510
|
+
functionName: strikeTick ? "getMaxPositionSizes" : "getMaxATMPositionSizes",
|
|
511
|
+
args: marketAddr ? strikeTick ? [
|
|
512
|
+
marketAddr,
|
|
513
|
+
strikeTick,
|
|
514
|
+
maxSteps
|
|
515
|
+
] : [marketAddr, maxSteps] : void 0,
|
|
516
|
+
query: { enabled: !!marketAddr && !!timelockLens },
|
|
517
|
+
gas: 100000000n
|
|
518
|
+
});
|
|
519
|
+
return {
|
|
520
|
+
maxCallSize: data && optionAssetDecimals ? wrapAmount(data[0], optionAssetDecimals) : void 0,
|
|
521
|
+
maxPutSize: data && optionAssetDecimals ? wrapAmount(data[1], optionAssetDecimals) : void 0,
|
|
522
|
+
...rest
|
|
523
|
+
};
|
|
524
|
+
};
|
|
525
|
+
|
|
462
526
|
//#endregion
|
|
463
527
|
//#region src/hooks/pool/usePoolData.ts
|
|
464
528
|
const usePoolData = (poolManager, poolKey) => {
|
|
@@ -479,27 +543,6 @@ const usePoolData = (poolManager, poolKey) => {
|
|
|
479
543
|
return data || _default;
|
|
480
544
|
};
|
|
481
545
|
|
|
482
|
-
//#endregion
|
|
483
|
-
//#region src/hooks/pool/usePriceAtTick.ts
|
|
484
|
-
const usePriceAtTick = (poolManager, poolKey, tick) => {
|
|
485
|
-
const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
|
|
486
|
-
const priceBigInt = useMemo(() => tick !== void 0 ? getPriceAtTick(tick) : void 0, [tick]);
|
|
487
|
-
return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
|
|
488
|
-
priceBigInt,
|
|
489
|
-
token0Decimals,
|
|
490
|
-
token1Decimals
|
|
491
|
-
]);
|
|
492
|
-
};
|
|
493
|
-
const usePriceAtSqrtPriceX96 = (poolManager, poolKey, sqrtPriceX96) => {
|
|
494
|
-
const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
|
|
495
|
-
const priceBigInt = useMemo(() => sqrtPriceX96 !== void 0 ? getPriceAtSqrtPriceX96(sqrtPriceX96) : void 0, [sqrtPriceX96]);
|
|
496
|
-
return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
|
|
497
|
-
priceBigInt,
|
|
498
|
-
token0Decimals,
|
|
499
|
-
token1Decimals
|
|
500
|
-
]);
|
|
501
|
-
};
|
|
502
|
-
|
|
503
546
|
//#endregion
|
|
504
547
|
//#region src/hooks/pool/useCurrentTick.ts
|
|
505
548
|
const useCurrentTick = (poolManager, poolKey) => {
|
|
@@ -532,6 +575,109 @@ const useCurrentTick = (poolManager, poolKey) => {
|
|
|
532
575
|
};
|
|
533
576
|
};
|
|
534
577
|
|
|
578
|
+
//#endregion
|
|
579
|
+
//#region src/hooks/tokens/useApproval.ts
|
|
580
|
+
const useApproval = () => {
|
|
581
|
+
const client = useClient();
|
|
582
|
+
const { address } = useConnection();
|
|
583
|
+
const { writeContractAsync, data: hash, isPending, error, reset } = useWriteContract();
|
|
584
|
+
const askForApproval = async (tokenAddress, spenderAddress, amount) => {
|
|
585
|
+
if (!client || !address) throw new Error("Wallet not connected");
|
|
586
|
+
if (await getErc20(tokenAddress, client).read.allowance([address, spenderAddress]) < amount) await waitForTransactionReceipt(client, { hash: await writeContractAsync({
|
|
587
|
+
address: tokenAddress,
|
|
588
|
+
abi: erc20Abi,
|
|
589
|
+
functionName: "approve",
|
|
590
|
+
args: [spenderAddress, maxUint256]
|
|
591
|
+
}) });
|
|
592
|
+
};
|
|
593
|
+
return {
|
|
594
|
+
askForApproval,
|
|
595
|
+
hash,
|
|
596
|
+
isPending,
|
|
597
|
+
error,
|
|
598
|
+
reset
|
|
599
|
+
};
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
//#endregion
|
|
603
|
+
//#region src/lib/utils.ts
|
|
604
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
605
|
+
|
|
606
|
+
//#endregion
|
|
607
|
+
//#region src/hooks/options/useMintOption.ts
|
|
608
|
+
const useMintOption = (marketAddr) => {
|
|
609
|
+
const { payoutAsset, vault, poolManager, poolKey, optionAssetIsToken0 } = useMarketData(marketAddr);
|
|
610
|
+
const { tickSpacing } = usePoolData(poolManager, poolKey);
|
|
611
|
+
const { refetch: refetchCurrentTick } = useCurrentTick(poolManager, poolKey);
|
|
612
|
+
const queryClient = useQueryClient();
|
|
613
|
+
const client = useClient();
|
|
614
|
+
const { address } = useConnection();
|
|
615
|
+
const { timelockLens } = useLens();
|
|
616
|
+
const { askForApproval } = useApproval();
|
|
617
|
+
const { writeContractAsync } = useWriteContract();
|
|
618
|
+
const mintOption = async ({ optionType, amount, duration, strikeTick, maxSteps = 100 }) => {
|
|
619
|
+
if (!client || !address) throw new Error("Wallet not connected");
|
|
620
|
+
if (!marketAddr) throw new Error("Market address not available");
|
|
621
|
+
if (!timelockLens) throw new Error("Timelock lens not available");
|
|
622
|
+
if (!tickSpacing) throw new Error("Pool data not available");
|
|
623
|
+
if (!vault || !payoutAsset || optionAssetIsToken0 === void 0) throw new Error("Market data not available");
|
|
624
|
+
const { data: { exact: currentTick } = {} } = await refetchCurrentTick();
|
|
625
|
+
if (currentTick === void 0) throw new Error("Could not fetch current tick");
|
|
626
|
+
strikeTick = getNearestValidStrikeTick(optionType, optionAssetIsToken0, tickSpacing, currentTick, strikeTick);
|
|
627
|
+
const [premium, protocolFee] = await getTimelockMarket(marketAddr, client).read.calculatePremium([
|
|
628
|
+
optionType === "CALL" ? 0 : 1,
|
|
629
|
+
amount,
|
|
630
|
+
strikeTick,
|
|
631
|
+
duration,
|
|
632
|
+
0
|
|
633
|
+
]);
|
|
634
|
+
await askForApproval(payoutAsset, marketAddr, (premium + protocolFee) * 11n / 10n);
|
|
635
|
+
const hash = await writeContractAsync({
|
|
636
|
+
address: marketAddr,
|
|
637
|
+
abi: optionsMarketAbi,
|
|
638
|
+
functionName: "mintOption",
|
|
639
|
+
args: [
|
|
640
|
+
address,
|
|
641
|
+
optionType === "CALL" ? 0 : 1,
|
|
642
|
+
amount,
|
|
643
|
+
strikeTick,
|
|
644
|
+
duration,
|
|
645
|
+
maxUint256,
|
|
646
|
+
maxSteps,
|
|
647
|
+
await timelockLens.read.getRefTick([vault, strikeTick])
|
|
648
|
+
]
|
|
649
|
+
});
|
|
650
|
+
await waitForTransactionReceipt(client, { hash });
|
|
651
|
+
await sleep(200);
|
|
652
|
+
queryClient.invalidateQueries({ queryKey: ["userOptions"] });
|
|
653
|
+
queryClient.invalidateQueries({ queryKey: ["userOptions"] });
|
|
654
|
+
queryClient.invalidateQueries({ queryKey: ["readContract"] });
|
|
655
|
+
return hash;
|
|
656
|
+
};
|
|
657
|
+
return useMutation({ mutationFn: mintOption });
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
//#endregion
|
|
661
|
+
//#region src/hooks/pool/usePriceAtTick.ts
|
|
662
|
+
const usePriceAtTick = (poolManager, poolKey, tick) => {
|
|
663
|
+
const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
|
|
664
|
+
const priceBigInt = useMemo(() => tick !== void 0 ? getPriceAtTick(tick) : void 0, [tick]);
|
|
665
|
+
return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
|
|
666
|
+
priceBigInt,
|
|
667
|
+
token0Decimals,
|
|
668
|
+
token1Decimals
|
|
669
|
+
]);
|
|
670
|
+
};
|
|
671
|
+
const usePriceAtSqrtPriceX96 = (poolManager, poolKey, sqrtPriceX96) => {
|
|
672
|
+
const { token0Decimals, token1Decimals } = usePoolData(poolManager, poolKey);
|
|
673
|
+
const priceBigInt = useMemo(() => sqrtPriceX96 !== void 0 ? getPriceAtSqrtPriceX96(sqrtPriceX96) : void 0, [sqrtPriceX96]);
|
|
674
|
+
return useMemo(() => priceBigInt && token0Decimals && token1Decimals ? wrapPrice(priceBigInt, token0Decimals, token1Decimals) : void 0, [
|
|
675
|
+
priceBigInt,
|
|
676
|
+
token0Decimals,
|
|
677
|
+
token1Decimals
|
|
678
|
+
]);
|
|
679
|
+
};
|
|
680
|
+
|
|
535
681
|
//#endregion
|
|
536
682
|
//#region src/hooks/pool/useCurrentPrice.ts
|
|
537
683
|
const useCurrentPrice = (poolManager, poolKey) => {
|
|
@@ -551,10 +697,6 @@ const useCurrentPrice = (poolManager, poolKey) => {
|
|
|
551
697
|
]);
|
|
552
698
|
};
|
|
553
699
|
|
|
554
|
-
//#endregion
|
|
555
|
-
//#region src/lib/utils.ts
|
|
556
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
557
|
-
|
|
558
700
|
//#endregion
|
|
559
701
|
//#region src/hooks/options/useExerciseOption.ts
|
|
560
702
|
const useExerciseOption = (marketAddr) => {
|
|
@@ -632,132 +774,6 @@ const useExerciseOption = (marketAddr) => {
|
|
|
632
774
|
return useMutation({ mutationFn: exerciseOption });
|
|
633
775
|
};
|
|
634
776
|
|
|
635
|
-
//#endregion
|
|
636
|
-
//#region src/hooks/options/useMarketVolume.ts
|
|
637
|
-
const useMarketVolume = (marketAddr) => {
|
|
638
|
-
const { graphqlClient } = useTimelockConfig();
|
|
639
|
-
const { data, ...rest } = useQuery({
|
|
640
|
-
queryKey: ["marketVolume", (marketAddr === null || marketAddr === void 0 ? void 0 : marketAddr.toLowerCase()) || "--"],
|
|
641
|
-
queryFn: async () => {
|
|
642
|
-
const result = await graphqlClient.GetMarketVolume({ marketAddr: marketAddr.toLowerCase() });
|
|
643
|
-
return {
|
|
644
|
-
...result.TimelockMarket[0],
|
|
645
|
-
address: result.TimelockMarket[0].address,
|
|
646
|
-
volume: BigInt(result.TimelockMarket[0].volume),
|
|
647
|
-
optionsCount: BigInt(result.TimelockMarket[0].optionsCount),
|
|
648
|
-
tradersCount: BigInt(result.TimelockMarket[0].tradersCount)
|
|
649
|
-
};
|
|
650
|
-
},
|
|
651
|
-
enabled: !!marketAddr && !!graphqlClient
|
|
652
|
-
});
|
|
653
|
-
return {
|
|
654
|
-
data: data || {},
|
|
655
|
-
...rest
|
|
656
|
-
};
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
//#endregion
|
|
660
|
-
//#region src/hooks/options/useMaxPositionSize.ts
|
|
661
|
-
const useMaxPositionSize = (marketAddr, strikeTick, maxSteps = 100) => {
|
|
662
|
-
const { timelockLens } = useLens();
|
|
663
|
-
const { optionAssetDecimals } = useMarketData(marketAddr);
|
|
664
|
-
const { data, ...rest } = useReadContract({
|
|
665
|
-
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
666
|
-
abi: lensAbi,
|
|
667
|
-
functionName: strikeTick ? "getMaxPositionSizes" : "getMaxATMPositionSizes",
|
|
668
|
-
args: marketAddr ? strikeTick ? [
|
|
669
|
-
marketAddr,
|
|
670
|
-
strikeTick,
|
|
671
|
-
maxSteps
|
|
672
|
-
] : [marketAddr, maxSteps] : void 0,
|
|
673
|
-
query: { enabled: !!marketAddr && !!timelockLens },
|
|
674
|
-
gas: 100000000n
|
|
675
|
-
});
|
|
676
|
-
return {
|
|
677
|
-
maxCallSize: data && optionAssetDecimals ? wrapAmount(data[0], optionAssetDecimals) : void 0,
|
|
678
|
-
maxPutSize: data && optionAssetDecimals ? wrapAmount(data[1], optionAssetDecimals) : void 0,
|
|
679
|
-
...rest
|
|
680
|
-
};
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
//#endregion
|
|
684
|
-
//#region src/hooks/tokens/useApproval.ts
|
|
685
|
-
const useApproval = () => {
|
|
686
|
-
const client = useClient();
|
|
687
|
-
const { address } = useConnection();
|
|
688
|
-
const { writeContractAsync, data: hash, isPending, error, reset } = useWriteContract();
|
|
689
|
-
const askForApproval = async (tokenAddress, spenderAddress, amount) => {
|
|
690
|
-
if (!client || !address) throw new Error("Wallet not connected");
|
|
691
|
-
if (await getErc20(tokenAddress, client).read.allowance([address, spenderAddress]) < amount) await waitForTransactionReceipt(client, { hash: await writeContractAsync({
|
|
692
|
-
address: tokenAddress,
|
|
693
|
-
abi: erc20Abi,
|
|
694
|
-
functionName: "approve",
|
|
695
|
-
args: [spenderAddress, maxUint256]
|
|
696
|
-
}) });
|
|
697
|
-
};
|
|
698
|
-
return {
|
|
699
|
-
askForApproval,
|
|
700
|
-
hash,
|
|
701
|
-
isPending,
|
|
702
|
-
error,
|
|
703
|
-
reset
|
|
704
|
-
};
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
//#endregion
|
|
708
|
-
//#region src/hooks/options/useMintOption.ts
|
|
709
|
-
const useMintOption = (marketAddr) => {
|
|
710
|
-
const { payoutAsset, vault, poolManager, poolKey, optionAssetIsToken0 } = useMarketData(marketAddr);
|
|
711
|
-
const { tickSpacing } = usePoolData(poolManager, poolKey);
|
|
712
|
-
const { refetch: refetchCurrentTick } = useCurrentTick(poolManager, poolKey);
|
|
713
|
-
const queryClient = useQueryClient();
|
|
714
|
-
const client = useClient();
|
|
715
|
-
const { address } = useConnection();
|
|
716
|
-
const { timelockLens } = useLens();
|
|
717
|
-
const { askForApproval } = useApproval();
|
|
718
|
-
const { writeContractAsync } = useWriteContract();
|
|
719
|
-
const mintOption = async ({ optionType, amount, duration, strikeTick, maxSteps = 100 }) => {
|
|
720
|
-
if (!client || !address) throw new Error("Wallet not connected");
|
|
721
|
-
if (!marketAddr) throw new Error("Market address not available");
|
|
722
|
-
if (!timelockLens) throw new Error("Timelock lens not available");
|
|
723
|
-
if (!tickSpacing) throw new Error("Pool data not available");
|
|
724
|
-
if (!vault || !payoutAsset || optionAssetIsToken0 === void 0) throw new Error("Market data not available");
|
|
725
|
-
const { data: { exact: currentTick } = {} } = await refetchCurrentTick();
|
|
726
|
-
if (currentTick === void 0) throw new Error("Could not fetch current tick");
|
|
727
|
-
strikeTick = getNearestValidStrikeTick(optionType, optionAssetIsToken0, tickSpacing, currentTick, strikeTick);
|
|
728
|
-
const [premium, protocolFee] = await getTimelockMarket(marketAddr, client).read.calculatePremium([
|
|
729
|
-
optionType === "CALL" ? 0 : 1,
|
|
730
|
-
amount,
|
|
731
|
-
strikeTick,
|
|
732
|
-
duration,
|
|
733
|
-
0
|
|
734
|
-
]);
|
|
735
|
-
await askForApproval(payoutAsset, marketAddr, (premium + protocolFee) * 11n / 10n);
|
|
736
|
-
const hash = await writeContractAsync({
|
|
737
|
-
address: marketAddr,
|
|
738
|
-
abi: optionsMarketAbi,
|
|
739
|
-
functionName: "mintOption",
|
|
740
|
-
args: [
|
|
741
|
-
address,
|
|
742
|
-
optionType === "CALL" ? 0 : 1,
|
|
743
|
-
amount,
|
|
744
|
-
strikeTick,
|
|
745
|
-
duration,
|
|
746
|
-
maxUint256,
|
|
747
|
-
maxSteps,
|
|
748
|
-
await timelockLens.read.getRefTick([vault, strikeTick])
|
|
749
|
-
]
|
|
750
|
-
});
|
|
751
|
-
await waitForTransactionReceipt(client, { hash });
|
|
752
|
-
await sleep(200);
|
|
753
|
-
queryClient.invalidateQueries({ queryKey: ["userOptions"] });
|
|
754
|
-
queryClient.invalidateQueries({ queryKey: ["userOptions"] });
|
|
755
|
-
queryClient.invalidateQueries({ queryKey: ["readContract"] });
|
|
756
|
-
return hash;
|
|
757
|
-
};
|
|
758
|
-
return useMutation({ mutationFn: mintOption });
|
|
759
|
-
};
|
|
760
|
-
|
|
761
777
|
//#endregion
|
|
762
778
|
//#region src/hooks/options/useOptionPnl.ts
|
|
763
779
|
const useOptionPnl = (option) => {
|
|
@@ -2196,22 +2212,6 @@ const useFeeRates = (feeStrategy) => {
|
|
|
2196
2212
|
return data || {};
|
|
2197
2213
|
};
|
|
2198
2214
|
|
|
2199
|
-
//#endregion
|
|
2200
|
-
//#region src/hooks/options/useMarketState.ts
|
|
2201
|
-
const useMarketState = (marketAddr) => {
|
|
2202
|
-
const { timelockLens } = useLens();
|
|
2203
|
-
const { data, ...rest } = useReadContract({
|
|
2204
|
-
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
2205
|
-
abi: lensAbi,
|
|
2206
|
-
functionName: "getMarketState",
|
|
2207
|
-
args: marketAddr ? [marketAddr] : void 0
|
|
2208
|
-
});
|
|
2209
|
-
return {
|
|
2210
|
-
data: data || {},
|
|
2211
|
-
...rest
|
|
2212
|
-
};
|
|
2213
|
-
};
|
|
2214
|
-
|
|
2215
2215
|
//#endregion
|
|
2216
2216
|
//#region src/abis/factory.ts
|
|
2217
2217
|
const factoryAbi = [
|
|
@@ -2518,5 +2518,194 @@ const useUpdateMarketFees = (marketAddr) => {
|
|
|
2518
2518
|
};
|
|
2519
2519
|
|
|
2520
2520
|
//#endregion
|
|
2521
|
-
|
|
2521
|
+
//#region src/hooks/pricing/useMarketPricing.ts
|
|
2522
|
+
const useMarketPricing = (marketAddr) => {
|
|
2523
|
+
const { data: { optionPricing } } = useMarketState(marketAddr);
|
|
2524
|
+
return useReadContract({
|
|
2525
|
+
address: optionPricing,
|
|
2526
|
+
abi: [{
|
|
2527
|
+
inputs: [],
|
|
2528
|
+
name: "readState",
|
|
2529
|
+
outputs: [{
|
|
2530
|
+
name: "",
|
|
2531
|
+
type: "bytes"
|
|
2532
|
+
}],
|
|
2533
|
+
stateMutability: "view",
|
|
2534
|
+
type: "function"
|
|
2535
|
+
}],
|
|
2536
|
+
functionName: "readState",
|
|
2537
|
+
query: {
|
|
2538
|
+
enabled: !!optionPricing,
|
|
2539
|
+
select: (rawData) => {
|
|
2540
|
+
const [pricingModel] = decodeAbiParameters([{
|
|
2541
|
+
name: "model",
|
|
2542
|
+
type: "uint8"
|
|
2543
|
+
}], rawData);
|
|
2544
|
+
if (pricingModel === 0) {
|
|
2545
|
+
const [, logicContract, iv, riskFreeRate, minPremiumDailyRate, minPremiumAmount] = decodeAbiParameters([
|
|
2546
|
+
{
|
|
2547
|
+
name: "pricingModel",
|
|
2548
|
+
type: "uint8"
|
|
2549
|
+
},
|
|
2550
|
+
{
|
|
2551
|
+
name: "logicContract",
|
|
2552
|
+
type: "address"
|
|
2553
|
+
},
|
|
2554
|
+
{
|
|
2555
|
+
name: "iv",
|
|
2556
|
+
type: "uint32"
|
|
2557
|
+
},
|
|
2558
|
+
{
|
|
2559
|
+
name: "riskFreeRate",
|
|
2560
|
+
type: "uint32"
|
|
2561
|
+
},
|
|
2562
|
+
{
|
|
2563
|
+
name: "minPremiumDailyRate",
|
|
2564
|
+
type: "uint32"
|
|
2565
|
+
},
|
|
2566
|
+
{
|
|
2567
|
+
name: "minPremiumAmount",
|
|
2568
|
+
type: "uint256"
|
|
2569
|
+
}
|
|
2570
|
+
], rawData);
|
|
2571
|
+
return {
|
|
2572
|
+
model: "option",
|
|
2573
|
+
logicContract,
|
|
2574
|
+
iv,
|
|
2575
|
+
riskFreeRate,
|
|
2576
|
+
minPremiumDailyRate,
|
|
2577
|
+
minPremiumAmount
|
|
2578
|
+
};
|
|
2579
|
+
} else if (pricingModel === 1) {
|
|
2580
|
+
const [, dailyFundingRate, minFundingAmount] = decodeAbiParameters([
|
|
2581
|
+
{
|
|
2582
|
+
name: "pricingModel",
|
|
2583
|
+
type: "uint8"
|
|
2584
|
+
},
|
|
2585
|
+
{
|
|
2586
|
+
name: "dailyFundingRate",
|
|
2587
|
+
type: "uint32"
|
|
2588
|
+
},
|
|
2589
|
+
{
|
|
2590
|
+
name: "minFundingAmount",
|
|
2591
|
+
type: "uint128"
|
|
2592
|
+
}
|
|
2593
|
+
], rawData);
|
|
2594
|
+
return {
|
|
2595
|
+
model: "static",
|
|
2596
|
+
dailyFundingRate,
|
|
2597
|
+
minFundingAmount
|
|
2598
|
+
};
|
|
2599
|
+
}
|
|
2600
|
+
throw new Error("Unknown pricing model");
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
});
|
|
2604
|
+
};
|
|
2605
|
+
|
|
2606
|
+
//#endregion
|
|
2607
|
+
//#region src/hooks/pricing/useOptionPricingParams.ts
|
|
2608
|
+
const useOptionPricingParams = (pricingAddr) => {
|
|
2609
|
+
const { timelockLens } = useLens();
|
|
2610
|
+
const { data } = useReadContract({
|
|
2611
|
+
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
2612
|
+
abi: lensAbi,
|
|
2613
|
+
args: pricingAddr ? [pricingAddr] : void 0,
|
|
2614
|
+
functionName: "getOptionPricingParams"
|
|
2615
|
+
});
|
|
2616
|
+
return data || {};
|
|
2617
|
+
};
|
|
2618
|
+
|
|
2619
|
+
//#endregion
|
|
2620
|
+
//#region src/hooks/pricing/useStaticPricingParams.ts
|
|
2621
|
+
const useStaticPricingParams = (pricingAddr) => {
|
|
2622
|
+
const { timelockLens } = useLens();
|
|
2623
|
+
const { data } = useReadContract({
|
|
2624
|
+
address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
|
|
2625
|
+
abi: lensAbi,
|
|
2626
|
+
args: pricingAddr ? [pricingAddr] : void 0,
|
|
2627
|
+
functionName: "getStaticPricingParams"
|
|
2628
|
+
});
|
|
2629
|
+
return data || {};
|
|
2630
|
+
};
|
|
2631
|
+
|
|
2632
|
+
//#endregion
|
|
2633
|
+
//#region src/hooks/pricing/useUpdateMarketPricing.ts
|
|
2634
|
+
const useUpdateMarketPricing = (marketAddr) => {
|
|
2635
|
+
const { writeContractAsync, ...rest } = useWriteContract();
|
|
2636
|
+
const publicClient = usePublicClient();
|
|
2637
|
+
const chainId = useChainId();
|
|
2638
|
+
const { data: { feeStrategy } } = useMarketState();
|
|
2639
|
+
const { data: pricingData } = useMarketPricing(marketAddr);
|
|
2640
|
+
const updateMarketPricing = async (data) => {
|
|
2641
|
+
if (!pricingData) throw new Error("Market pricing data not available");
|
|
2642
|
+
if (!publicClient) throw new Error("Public client not available");
|
|
2643
|
+
if (!feeStrategy) throw new Error("Fee strategy not available");
|
|
2644
|
+
const factoryAddr = timelockFactories[chainId].toLowerCase();
|
|
2645
|
+
if (data.model === "static" && pricingData.model === "static") {
|
|
2646
|
+
data.dailyFundingRate ?? (data.dailyFundingRate = pricingData.dailyFundingRate);
|
|
2647
|
+
data.minFundingAmount ?? (data.minFundingAmount = pricingData.minFundingAmount);
|
|
2648
|
+
}
|
|
2649
|
+
if (data.model === "option" && pricingData.model === "option") {
|
|
2650
|
+
data.logicContract ?? (data.logicContract = pricingData.logicContract);
|
|
2651
|
+
data.iv ?? (data.iv = pricingData.iv);
|
|
2652
|
+
data.riskFreeRate ?? (data.riskFreeRate = pricingData.riskFreeRate);
|
|
2653
|
+
data.minPremiumDailyRate ?? (data.minPremiumDailyRate = pricingData.minPremiumDailyRate);
|
|
2654
|
+
data.minPremiumAmount ?? (data.minPremiumAmount = pricingData.minPremiumAmount);
|
|
2655
|
+
}
|
|
2656
|
+
if (data.model !== pricingData.model) if (data.model === "static") {
|
|
2657
|
+
if (data.dailyFundingRate === void 0) throw new Error("dailyFundingRate is required when switching to static model");
|
|
2658
|
+
if (data.minFundingAmount === void 0) throw new Error("minFundingAmount is required when switching to static model");
|
|
2659
|
+
} else {
|
|
2660
|
+
if (data.logicContract === void 0) throw new Error("logicContract is required when switching to option model");
|
|
2661
|
+
if (data.iv === void 0) throw new Error("iv is required when switching to option model");
|
|
2662
|
+
if (data.riskFreeRate === void 0) throw new Error("riskFreeRate is required when switching to option model");
|
|
2663
|
+
if (data.minPremiumDailyRate === void 0) throw new Error("minPremiumDailyRate is required when switching to option model");
|
|
2664
|
+
if (data.minPremiumAmount === void 0) throw new Error("minPremiumAmount is required when switching to option model");
|
|
2665
|
+
}
|
|
2666
|
+
const hash = data.model === "static" ? await writeContractAsync({
|
|
2667
|
+
address: factoryAddr,
|
|
2668
|
+
abi: factoryAbi,
|
|
2669
|
+
functionName: "deployStaticPerpsPricing",
|
|
2670
|
+
args: [data.dailyFundingRate, data.minFundingAmount]
|
|
2671
|
+
}) : await writeContractAsync({
|
|
2672
|
+
address: factoryAddr,
|
|
2673
|
+
abi: factoryAbi,
|
|
2674
|
+
functionName: "deployOptionPricing",
|
|
2675
|
+
args: [
|
|
2676
|
+
data.logicContract,
|
|
2677
|
+
data.iv,
|
|
2678
|
+
data.riskFreeRate,
|
|
2679
|
+
data.minPremiumDailyRate,
|
|
2680
|
+
data.minPremiumAmount
|
|
2681
|
+
]
|
|
2682
|
+
});
|
|
2683
|
+
const deployEvent = (await publicClient.waitForTransactionReceipt({ hash })).logs.find((log) => log.address.toLowerCase() === factoryAddr);
|
|
2684
|
+
if (!deployEvent) throw new Error("DeployFeeStrategy event not found");
|
|
2685
|
+
const decodedEvent = decodeEventLog({
|
|
2686
|
+
abi: factoryAbi,
|
|
2687
|
+
data: deployEvent.data,
|
|
2688
|
+
topics: deployEvent.topics
|
|
2689
|
+
});
|
|
2690
|
+
if (decodedEvent.eventName !== "DeployStaticPerpsPricing" && decodedEvent.eventName !== "DeployOptionPricing") throw new Error("Unexpected event");
|
|
2691
|
+
const pricingAddr = decodedEvent.args.pricing;
|
|
2692
|
+
return {
|
|
2693
|
+
deployHash: hash,
|
|
2694
|
+
updateHash: await writeContractAsync({
|
|
2695
|
+
address: marketAddr,
|
|
2696
|
+
abi: optionsMarketAbi,
|
|
2697
|
+
functionName: "updateAddresses",
|
|
2698
|
+
args: [pricingAddr, feeStrategy]
|
|
2699
|
+
}),
|
|
2700
|
+
newPricingAddr: pricingAddr
|
|
2701
|
+
};
|
|
2702
|
+
};
|
|
2703
|
+
return {
|
|
2704
|
+
updateMarketPricing,
|
|
2705
|
+
...rest
|
|
2706
|
+
};
|
|
2707
|
+
};
|
|
2708
|
+
|
|
2709
|
+
//#endregion
|
|
2710
|
+
export { TimelockProvider, batchGetAmountsFromLiquidity, useActiveUserOptions, useActiveUserPerps, useApproval, useBurnLiquidity, useClosePerp, useClosedUserOptions, useClosedUserPerps, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useExtendOption, useFeeRates, useGuardianGlobalState, useLens, useLiquidityBlocks, useMarketData, useMarketPriceHistory, useMarketPricing, useMarketState, useMarketVolume, useMaxPositionSize, useMintLiquidity, useMintOption, useMintPerp, useOperatorPerms, useOptionPnl, useOptionPremium, useOptionPricingParams, useOptionTimeline, usePauseGlobalTrading, usePauseMarketTrading, usePerpsOperator, usePoolData, usePriceAtSqrtPriceX96, usePriceAtTick, usePriceHistory, useSetOperatorPerms, useStaticPricingParams, useTimelockConfig, useTokenBalance, useTokenData, useUpdateMarketFees, useUpdateMarketPricing, useUserOperators, useUserPerps, useVaultData, useVaultTVL };
|
|
2522
2711
|
//# sourceMappingURL=client.js.map
|