timelock-sdk 0.0.55 → 0.0.57

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.cjs CHANGED
@@ -353,33 +353,48 @@ const useCurrentTick = (poolAddr) => {
353
353
  }, [data, tickSpacing]);
354
354
  };
355
355
 
356
+ //#endregion
357
+ //#region src/hooks/useApproval.ts
358
+ const useApproval = () => {
359
+ const client = (0, wagmi.useClient)();
360
+ const { address } = (0, wagmi.useAccount)();
361
+ const { writeContractAsync, data: hash, isPending, error, reset } = (0, wagmi.useWriteContract)();
362
+ const askForApproval = async (tokenAddress, spenderAddress, amount) => {
363
+ if (!client || !address) throw new Error("Wallet not connected");
364
+ if (await require_numberUtils.getErc20(tokenAddress, client).read.allowance([address, spenderAddress]) < amount) await (0, viem_actions.waitForTransactionReceipt)(client, { hash: await writeContractAsync({
365
+ address: tokenAddress,
366
+ abi: viem.erc20Abi,
367
+ functionName: "approve",
368
+ args: [spenderAddress, viem.maxUint256]
369
+ }) });
370
+ };
371
+ return {
372
+ askForApproval,
373
+ hash,
374
+ isPending,
375
+ error,
376
+ reset
377
+ };
378
+ };
379
+
356
380
  //#endregion
357
381
  //#region src/hooks/market/useMintOption.ts
358
382
  const useMintOption = (marketAddr) => {
359
- const { timelockLens } = useLens();
360
383
  const { payoutAsset, vault, pool, optionAssetIsToken0 } = useMarketData(marketAddr);
361
384
  const { tickSpacing } = usePoolData(pool);
362
385
  const { exact: currentTick } = useCurrentTick(pool);
386
+ const queryClient = (0, __tanstack_react_query.useQueryClient)();
363
387
  const client = (0, wagmi.useClient)();
364
388
  const { address } = (0, wagmi.useAccount)();
365
- const queryClient = (0, __tanstack_react_query.useQueryClient)();
389
+ const { timelockLens } = useLens();
390
+ const { askForApproval, isPending: isApprovalPending, error: approvalError, reset: resetApproval } = useApproval();
366
391
  const { writeContractAsync, data: hash, isPending, error } = (0, wagmi.useWriteContract)();
367
392
  const { isLoading: isConfirming, isSuccess } = (0, wagmi.useWaitForTransactionReceipt)({ hash });
368
- const askForApproval = async (premiumAmount) => {
369
- if (!client || !address) throw new Error("Wallet not connected");
370
- if (!payoutAsset || !marketAddr) throw new Error("Tokens not available");
371
- if (await require_numberUtils.getErc20(payoutAsset, client).read.allowance([address, marketAddr]) < premiumAmount) await (0, viem_actions.waitForTransactionReceipt)(client, { hash: await writeContractAsync({
372
- address: payoutAsset,
373
- abi: viem.erc20Abi,
374
- functionName: "approve",
375
- args: [marketAddr, viem.maxUint256]
376
- }) });
377
- };
378
393
  const mintOption = async (optionType, amount, duration, strikeTick) => {
379
394
  if (!client || !address) throw new Error("Wallet not connected");
380
395
  if (!marketAddr) throw new Error("Market address not available");
381
396
  if (!timelockLens) throw new Error("Timelock lens not available");
382
- if (!vault) throw new Error("Vault not available");
397
+ if (!vault || !payoutAsset) throw new Error("Vault not available");
383
398
  if (currentTick === void 0 || !tickSpacing) throw new Error("Pool data not available");
384
399
  strikeTick = require_numberUtils.roundTickDown(strikeTick ?? currentTick, tickSpacing);
385
400
  if (optionType === "CALL" && optionAssetIsToken0 || optionType === "PUT" && !optionAssetIsToken0) strikeTick += tickSpacing;
@@ -387,9 +402,11 @@ const useMintOption = (marketAddr) => {
387
402
  optionType === "CALL" ? 0 : 1,
388
403
  amount,
389
404
  strikeTick,
390
- duration
405
+ duration,
406
+ 0
391
407
  ]);
392
- await askForApproval(premium + protocolFee);
408
+ const maxPremium = (premium + protocolFee * 11n) / 10n;
409
+ await askForApproval(payoutAsset, marketAddr, maxPremium);
393
410
  const hash$1 = await writeContractAsync({
394
411
  address: marketAddr,
395
412
  abi: require_optionsMarket.optionsMarketAbi,
@@ -399,7 +416,7 @@ const useMintOption = (marketAddr) => {
399
416
  amount,
400
417
  strikeTick,
401
418
  duration,
402
- BigInt(1e69),
419
+ maxPremium,
403
420
  false,
404
421
  await timelockLens.read.getRefTick([vault, strikeTick])
405
422
  ]
@@ -411,11 +428,14 @@ const useMintOption = (marketAddr) => {
411
428
  return {
412
429
  mintOption,
413
430
  hash,
414
- isPending,
431
+ isPending: isPending || isApprovalPending,
415
432
  isConfirming,
416
433
  isSuccess,
417
- error,
418
- isLoading: isPending || isConfirming
434
+ error: error || approvalError,
435
+ isLoading: isPending || isConfirming || isApprovalPending,
436
+ isApprovalPending,
437
+ approvalError,
438
+ resetApproval
419
439
  };
420
440
  };
421
441
 
@@ -544,6 +564,56 @@ const useClosedUserOptions = (user) => {
544
564
  return useUserOptions(user, false);
545
565
  };
546
566
 
567
+ //#endregion
568
+ //#region src/hooks/market/useExtendOption.ts
569
+ const useExtendOption = (marketAddr) => {
570
+ const { payoutAsset } = useMarketData(marketAddr);
571
+ const queryClient = (0, __tanstack_react_query.useQueryClient)();
572
+ const client = (0, wagmi.useClient)();
573
+ const { address } = (0, wagmi.useAccount)();
574
+ const { askForApproval, isPending: isApprovalPending, error: approvalError, reset: resetApproval } = useApproval();
575
+ const { writeContractAsync, data: hash, isPending, error } = (0, wagmi.useWriteContract)();
576
+ const { isLoading: isConfirming, isSuccess } = (0, wagmi.useWaitForTransactionReceipt)({ hash });
577
+ const extendOption = async (option, duration) => {
578
+ if (!client || !address) throw new Error("Wallet not connected");
579
+ if (!marketAddr || !payoutAsset) throw new Error("Market address not available");
580
+ const [premium, protocolFee] = await require_numberUtils.getTimelockMarket(marketAddr, client).read.calculatePremium([
581
+ option.optionType === "CALL" ? 0 : 1,
582
+ option.positionSizeCurrent,
583
+ option.strikeTick,
584
+ duration,
585
+ Math.floor((option.expiresAt.getTime() - Date.now()) / 1e3)
586
+ ]);
587
+ const maxPremium = (premium + protocolFee * 11n) / 10n;
588
+ await askForApproval(payoutAsset, marketAddr, maxPremium);
589
+ const hash$1 = await writeContractAsync({
590
+ address: marketAddr,
591
+ abi: require_optionsMarket.optionsMarketAbi,
592
+ functionName: "extendOption",
593
+ args: [
594
+ option.optionId,
595
+ duration,
596
+ maxPremium
597
+ ]
598
+ });
599
+ await (0, viem_actions.waitForTransactionReceipt)(client, { hash: hash$1 });
600
+ await queryClient.invalidateQueries({ queryKey: ["userOptions", address.toLowerCase()] });
601
+ return hash$1;
602
+ };
603
+ return {
604
+ extendOption,
605
+ hash,
606
+ isPending: isPending || isApprovalPending,
607
+ isConfirming,
608
+ isSuccess,
609
+ error: error || approvalError,
610
+ isLoading: isPending || isConfirming || isApprovalPending,
611
+ isApprovalPending,
612
+ approvalError,
613
+ resetApproval
614
+ };
615
+ };
616
+
547
617
  //#endregion
548
618
  //#region src/hooks/pool/usePriceAtTick.ts
549
619
  const usePriceAtTick = (tick, poolAddr) => {
@@ -924,6 +994,7 @@ exports.useCurrentMarket = useCurrentMarket;
924
994
  exports.useCurrentPrice = useCurrentPrice;
925
995
  exports.useCurrentTick = useCurrentTick;
926
996
  exports.useExerciseOption = useExerciseOption;
997
+ exports.useExtendOption = useExtendOption;
927
998
  exports.useLens = useLens;
928
999
  exports.useLiquidityBlocks = useLiquidityBlocks;
929
1000
  exports.useMarketData = useMarketData;