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.cjs +88 -18
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +83 -62
- package/dist/client.d.ts +79 -58
- package/dist/client.js +88 -19
- package/dist/client.js.map +1 -1
- package/dist/{index-DegGZn5F.d.cts → index-3V8OBVyj.d.cts} +92 -92
- package/dist/{index-DGAwlJuA.d.ts → index-BCwVk3dz.d.ts} +92 -92
- package/dist/package.d.cts +1 -1
- package/dist/package.d.ts +1 -1
- package/package.json +1 -1
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
|
|
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;
|
|
@@ -390,7 +405,8 @@ const useMintOption = (marketAddr) => {
|
|
|
390
405
|
duration,
|
|
391
406
|
0
|
|
392
407
|
]);
|
|
393
|
-
|
|
408
|
+
const maxPremium = (premium + protocolFee * 11n) / 10n;
|
|
409
|
+
await askForApproval(payoutAsset, marketAddr, maxPremium);
|
|
394
410
|
const hash$1 = await writeContractAsync({
|
|
395
411
|
address: marketAddr,
|
|
396
412
|
abi: require_optionsMarket.optionsMarketAbi,
|
|
@@ -400,7 +416,7 @@ const useMintOption = (marketAddr) => {
|
|
|
400
416
|
amount,
|
|
401
417
|
strikeTick,
|
|
402
418
|
duration,
|
|
403
|
-
|
|
419
|
+
maxPremium,
|
|
404
420
|
false,
|
|
405
421
|
await timelockLens.read.getRefTick([vault, strikeTick])
|
|
406
422
|
]
|
|
@@ -412,11 +428,14 @@ const useMintOption = (marketAddr) => {
|
|
|
412
428
|
return {
|
|
413
429
|
mintOption,
|
|
414
430
|
hash,
|
|
415
|
-
isPending,
|
|
431
|
+
isPending: isPending || isApprovalPending,
|
|
416
432
|
isConfirming,
|
|
417
433
|
isSuccess,
|
|
418
|
-
error,
|
|
419
|
-
isLoading: isPending || isConfirming
|
|
434
|
+
error: error || approvalError,
|
|
435
|
+
isLoading: isPending || isConfirming || isApprovalPending,
|
|
436
|
+
isApprovalPending,
|
|
437
|
+
approvalError,
|
|
438
|
+
resetApproval
|
|
420
439
|
};
|
|
421
440
|
};
|
|
422
441
|
|
|
@@ -545,6 +564,56 @@ const useClosedUserOptions = (user) => {
|
|
|
545
564
|
return useUserOptions(user, false);
|
|
546
565
|
};
|
|
547
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
|
+
|
|
548
617
|
//#endregion
|
|
549
618
|
//#region src/hooks/pool/usePriceAtTick.ts
|
|
550
619
|
const usePriceAtTick = (tick, poolAddr) => {
|
|
@@ -925,6 +994,7 @@ exports.useCurrentMarket = useCurrentMarket;
|
|
|
925
994
|
exports.useCurrentPrice = useCurrentPrice;
|
|
926
995
|
exports.useCurrentTick = useCurrentTick;
|
|
927
996
|
exports.useExerciseOption = useExerciseOption;
|
|
997
|
+
exports.useExtendOption = useExtendOption;
|
|
928
998
|
exports.useLens = useLens;
|
|
929
999
|
exports.useLiquidityBlocks = useLiquidityBlocks;
|
|
930
1000
|
exports.useMarketData = useMarketData;
|