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.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;
@@ -380,9 +395,11 @@ const useMintOption = (marketAddr) => {
380
395
  optionType === "CALL" ? 0 : 1,
381
396
  amount,
382
397
  strikeTick,
383
- duration
398
+ duration,
399
+ 0
384
400
  ]);
385
- await askForApproval(premium + protocolFee);
401
+ const maxPremium = (premium + protocolFee * 11n) / 10n;
402
+ await askForApproval(payoutAsset, marketAddr, maxPremium);
386
403
  const hash$1 = await writeContractAsync({
387
404
  address: marketAddr,
388
405
  abi: optionsMarketAbi,
@@ -392,7 +409,7 @@ const useMintOption = (marketAddr) => {
392
409
  amount,
393
410
  strikeTick,
394
411
  duration,
395
- BigInt(1e69),
412
+ maxPremium,
396
413
  false,
397
414
  await timelockLens.read.getRefTick([vault, strikeTick])
398
415
  ]
@@ -404,11 +421,14 @@ const useMintOption = (marketAddr) => {
404
421
  return {
405
422
  mintOption,
406
423
  hash,
407
- isPending,
424
+ isPending: isPending || isApprovalPending,
408
425
  isConfirming,
409
426
  isSuccess,
410
- error,
411
- isLoading: isPending || isConfirming
427
+ error: error || approvalError,
428
+ isLoading: isPending || isConfirming || isApprovalPending,
429
+ isApprovalPending,
430
+ approvalError,
431
+ resetApproval
412
432
  };
413
433
  };
414
434
 
@@ -537,6 +557,56 @@ const useClosedUserOptions = (user) => {
537
557
  return useUserOptions(user, false);
538
558
  };
539
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
+
540
610
  //#endregion
541
611
  //#region src/hooks/pool/usePriceAtTick.ts
542
612
  const usePriceAtTick = (tick, poolAddr) => {
@@ -908,5 +978,5 @@ const useVaultTVL = (vaultAddr) => {
908
978
  };
909
979
 
910
980
  //#endregion
911
- 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 };
912
982
  //# sourceMappingURL=client.js.map