timelock-sdk 0.0.56 → 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.js CHANGED
@@ -346,33 +346,48 @@ const useCurrentTick = (poolAddr) => {
346
346
  }, [data, tickSpacing]);
347
347
  };
348
348
 
349
+ //#endregion
350
+ //#region src/hooks/useApproval.ts
351
+ const useApproval = () => {
352
+ const client = useClient();
353
+ const { address } = useAccount();
354
+ const { writeContractAsync, data: hash, isPending, error, reset } = useWriteContract();
355
+ const askForApproval = async (tokenAddress, spenderAddress, amount) => {
356
+ if (!client || !address) throw new Error("Wallet not connected");
357
+ if (await getErc20(tokenAddress, client).read.allowance([address, spenderAddress]) < amount) await waitForTransactionReceipt(client, { hash: await writeContractAsync({
358
+ address: tokenAddress,
359
+ abi: erc20Abi,
360
+ functionName: "approve",
361
+ args: [spenderAddress, maxUint256]
362
+ }) });
363
+ };
364
+ return {
365
+ askForApproval,
366
+ hash,
367
+ isPending,
368
+ error,
369
+ reset
370
+ };
371
+ };
372
+
349
373
  //#endregion
350
374
  //#region src/hooks/market/useMintOption.ts
351
375
  const useMintOption = (marketAddr) => {
352
- const { timelockLens } = useLens();
353
376
  const { payoutAsset, vault, pool, optionAssetIsToken0 } = useMarketData(marketAddr);
354
377
  const { tickSpacing } = usePoolData(pool);
355
378
  const { exact: currentTick } = useCurrentTick(pool);
379
+ const queryClient = useQueryClient();
356
380
  const client = useClient();
357
381
  const { address } = useAccount();
358
- const queryClient = useQueryClient();
382
+ const { timelockLens } = useLens();
383
+ const { askForApproval, isPending: isApprovalPending, error: approvalError, reset: resetApproval } = useApproval();
359
384
  const { writeContractAsync, data: hash, isPending, error } = useWriteContract();
360
385
  const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
361
- const askForApproval = async (premiumAmount) => {
362
- if (!client || !address) throw new Error("Wallet not connected");
363
- if (!payoutAsset || !marketAddr) throw new Error("Tokens not available");
364
- if (await getErc20(payoutAsset, client).read.allowance([address, marketAddr]) < premiumAmount) await waitForTransactionReceipt(client, { hash: await writeContractAsync({
365
- address: payoutAsset,
366
- abi: erc20Abi,
367
- functionName: "approve",
368
- args: [marketAddr, maxUint256]
369
- }) });
370
- };
371
386
  const mintOption = async (optionType, amount, duration, strikeTick) => {
372
387
  if (!client || !address) throw new Error("Wallet not connected");
373
388
  if (!marketAddr) throw new Error("Market address not available");
374
389
  if (!timelockLens) throw new Error("Timelock lens not available");
375
- if (!vault) throw new Error("Vault not available");
390
+ if (!vault || !payoutAsset) throw new Error("Vault not available");
376
391
  if (currentTick === void 0 || !tickSpacing) throw new Error("Pool data not available");
377
392
  strikeTick = roundTickDown(strikeTick ?? currentTick, tickSpacing);
378
393
  if (optionType === "CALL" && optionAssetIsToken0 || optionType === "PUT" && !optionAssetIsToken0) strikeTick += tickSpacing;
@@ -383,7 +398,8 @@ const useMintOption = (marketAddr) => {
383
398
  duration,
384
399
  0
385
400
  ]);
386
- await askForApproval(premium + protocolFee);
401
+ const maxPremium = (premium + protocolFee * 11n) / 10n;
402
+ await askForApproval(payoutAsset, marketAddr, maxPremium);
387
403
  const hash$1 = await writeContractAsync({
388
404
  address: marketAddr,
389
405
  abi: optionsMarketAbi,
@@ -393,7 +409,7 @@ const useMintOption = (marketAddr) => {
393
409
  amount,
394
410
  strikeTick,
395
411
  duration,
396
- BigInt(1e69),
412
+ maxPremium,
397
413
  false,
398
414
  await timelockLens.read.getRefTick([vault, strikeTick])
399
415
  ]
@@ -405,11 +421,14 @@ const useMintOption = (marketAddr) => {
405
421
  return {
406
422
  mintOption,
407
423
  hash,
408
- isPending,
424
+ isPending: isPending || isApprovalPending,
409
425
  isConfirming,
410
426
  isSuccess,
411
- error,
412
- isLoading: isPending || isConfirming
427
+ error: error || approvalError,
428
+ isLoading: isPending || isConfirming || isApprovalPending,
429
+ isApprovalPending,
430
+ approvalError,
431
+ resetApproval
413
432
  };
414
433
  };
415
434
 
@@ -538,6 +557,56 @@ const useClosedUserOptions = (user) => {
538
557
  return useUserOptions(user, false);
539
558
  };
540
559
 
560
+ //#endregion
561
+ //#region src/hooks/market/useExtendOption.ts
562
+ const useExtendOption = (marketAddr) => {
563
+ const { payoutAsset } = useMarketData(marketAddr);
564
+ const queryClient = useQueryClient();
565
+ const client = useClient();
566
+ const { address } = useAccount();
567
+ const { askForApproval, isPending: isApprovalPending, error: approvalError, reset: resetApproval } = useApproval();
568
+ const { writeContractAsync, data: hash, isPending, error } = useWriteContract();
569
+ const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
570
+ const extendOption = async (option, duration) => {
571
+ if (!client || !address) throw new Error("Wallet not connected");
572
+ if (!marketAddr || !payoutAsset) throw new Error("Market address not available");
573
+ const [premium, protocolFee] = await getTimelockMarket(marketAddr, client).read.calculatePremium([
574
+ option.optionType === "CALL" ? 0 : 1,
575
+ option.positionSizeCurrent,
576
+ option.strikeTick,
577
+ duration,
578
+ Math.floor((option.expiresAt.getTime() - Date.now()) / 1e3)
579
+ ]);
580
+ const maxPremium = (premium + protocolFee * 11n) / 10n;
581
+ await askForApproval(payoutAsset, marketAddr, maxPremium);
582
+ const hash$1 = await writeContractAsync({
583
+ address: marketAddr,
584
+ abi: optionsMarketAbi,
585
+ functionName: "extendOption",
586
+ args: [
587
+ option.optionId,
588
+ duration,
589
+ maxPremium
590
+ ]
591
+ });
592
+ await waitForTransactionReceipt(client, { hash: hash$1 });
593
+ await queryClient.invalidateQueries({ queryKey: ["userOptions", address.toLowerCase()] });
594
+ return hash$1;
595
+ };
596
+ return {
597
+ extendOption,
598
+ hash,
599
+ isPending: isPending || isApprovalPending,
600
+ isConfirming,
601
+ isSuccess,
602
+ error: error || approvalError,
603
+ isLoading: isPending || isConfirming || isApprovalPending,
604
+ isApprovalPending,
605
+ approvalError,
606
+ resetApproval
607
+ };
608
+ };
609
+
541
610
  //#endregion
542
611
  //#region src/hooks/pool/usePriceAtTick.ts
543
612
  const usePriceAtTick = (tick, poolAddr) => {
@@ -909,5 +978,5 @@ const useVaultTVL = (vaultAddr) => {
909
978
  };
910
979
 
911
980
  //#endregion
912
- export { TimelockMarketProvider, batchGetAmountsFromLiquidity, useActiveUserOptions, useBurnLiquidity, useClosedUserOptions, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useLens, useLiquidityBlocks, useMarketData, useMaxPositionSize, useMintLiquidity, useMintOption, useOptionPnl, useOptionPremium, usePoolData, usePriceAtTick, usePriceHistory, useTimelockConfig, useVaultData, useVaultTVL };
981
+ export { TimelockMarketProvider, batchGetAmountsFromLiquidity, useActiveUserOptions, useBurnLiquidity, useClosedUserOptions, useCurrentMarket, useCurrentPrice, useCurrentTick, useExerciseOption, useExtendOption, useLens, useLiquidityBlocks, useMarketData, useMaxPositionSize, useMintLiquidity, useMintOption, useOptionPnl, useOptionPremium, usePoolData, usePriceAtTick, usePriceHistory, useTimelockConfig, useVaultData, useVaultTVL };
913
982
  //# sourceMappingURL=client.js.map