timelock-sdk 0.0.241 → 0.0.243

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
@@ -1303,10 +1303,11 @@ const calculateDisplayPnl = (option, poolPrice, optionAssetIsToken0, payoutAsset
1303
1303
  return require_numberUtils.wrapAmount(option.optionType === "CALL" ? delta : -delta, payoutAssetDecimals);
1304
1304
  };
1305
1305
  const useOptionPnl = (option) => {
1306
- const client = (0, wagmi.usePublicClient)();
1307
- const { poolManager, poolKey, optionAssetIsToken0, payoutAssetDecimals } = useMarketData(option === null || option === void 0 ? void 0 : option.marketAddr);
1308
- const { currentPrice: poolPrice } = useCurrentPrice(poolManager, poolKey);
1309
- const { tickSpacing } = usePoolData(poolManager, poolKey);
1306
+ var _refTickSimulation$da, _refTickSimulation$da2;
1307
+ const chainId = (0, wagmi.useChainId)();
1308
+ const { timelockLens } = useLens();
1309
+ const { vault, poolManager, poolKey, optionAssetIsToken0, payoutAssetDecimals } = useMarketData(option === null || option === void 0 ? void 0 : option.marketAddr);
1310
+ const { currentPrice: poolPrice, sqrtPriceX96 } = useCurrentPrice(poolManager, poolKey);
1310
1311
  const displayPnl = (0, react.useMemo)(() => {
1311
1312
  if (!option || !poolPrice || !payoutAssetDecimals || optionAssetIsToken0 === void 0) return void 0;
1312
1313
  return calculateDisplayPnl(option, poolPrice.scaled, optionAssetIsToken0, payoutAssetDecimals);
@@ -1316,50 +1317,52 @@ const useOptionPnl = (option) => {
1316
1317
  poolPrice,
1317
1318
  payoutAssetDecimals
1318
1319
  ]);
1319
- const canQueryPayout = !!client && !!poolManager && !!poolKey && !!tickSpacing && !!poolPrice && !!payoutAssetDecimals && optionAssetIsToken0 !== void 0;
1320
- const { data: unrealizedPayout } = (0, _tanstack_react_query.useQuery)({
1321
- queryKey: [
1322
- "unrealizedPayout",
1323
- (option === null || option === void 0 ? void 0 : option.id) || "-",
1324
- (poolPrice === null || poolPrice === void 0 ? void 0 : poolPrice.scaled.toString()) || "-"
1325
- ],
1326
- queryFn: async () => {
1327
- if (!option || !poolPrice || !payoutAssetDecimals || !tickSpacing) return;
1328
- const { optionType, marketAddr } = option;
1329
- const entryPrice = optionAssetIsToken0 ? option.entryPrice : require_numberUtils.PRICE_PRECISION ** 2n / option.entryPrice;
1330
- const [borrowed0, borrowed1] = require_numberUtils.liquiditiesToAmounts(option.liquiditiesCurrent, option.startTick, entryPrice, tickSpacing);
1331
- const [repay0, repay1] = require_numberUtils.liquiditiesToAmounts(option.liquiditiesCurrent, option.startTick, poolPrice.scaled, tickSpacing);
1332
- const isLong0 = optionAssetIsToken0 && optionType === "CALL" || !optionAssetIsToken0 && optionType === "PUT";
1333
- const borrowedLong = isLong0 ? borrowed0 : borrowed1;
1334
- const repayLong = isLong0 ? repay0 : repay1;
1335
- const excessLong = borrowedLong > repayLong ? borrowedLong - repayLong : 0n;
1336
- const neededShort = isLong0 ? repay1 : repay0;
1337
- if (neededShort === 0n || excessLong === 0n) return require_numberUtils.wrapAmount(0n, payoutAssetDecimals);
1338
- const quoter = await require_numberUtils.getQuoter(client);
1339
- const output0 = !optionAssetIsToken0;
1340
- let payout;
1341
- const isExactOutput = isLong0 && output0 || !isLong0 && !output0;
1342
- const params = {
1343
- poolKey,
1344
- zeroForOne: isLong0,
1345
- exactAmount: isExactOutput ? neededShort : excessLong,
1346
- hookData: "0x"
1347
- };
1348
- if (isExactOutput) {
1349
- const { result: [swappedLong] } = await quoter.simulate.quoteExactOutputSingle([poolManager, params], { account: marketAddr });
1350
- payout = excessLong > swappedLong ? excessLong - swappedLong : 0n;
1351
- } else {
1352
- const { result: [receviedShort] } = await quoter.simulate.quoteExactInputSingle([poolManager, params], { account: marketAddr });
1353
- payout = receviedShort > neededShort ? receviedShort - neededShort : 0n;
1320
+ const swapperData = (0, react.useMemo)(() => {
1321
+ if (!sqrtPriceX96) return void 0;
1322
+ return (0, viem.encodeAbiParameters)([
1323
+ { type: "uint160" },
1324
+ { type: "uint160" },
1325
+ { type: "uint256" }
1326
+ ], [
1327
+ sqrtPriceX96 * 9n / 10n,
1328
+ sqrtPriceX96 * 11n / 10n,
1329
+ BigInt(Math.floor(Date.now() / 1e3) + 600)
1330
+ ]);
1331
+ }, [sqrtPriceX96]);
1332
+ const refTickSimulation = (0, wagmi.useSimulateContract)({
1333
+ address: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.address,
1334
+ abi: timelockLens === null || timelockLens === void 0 ? void 0 : timelockLens.abi,
1335
+ functionName: "getRefTick",
1336
+ args: vault && (option === null || option === void 0 ? void 0 : option.startTick) ? [vault, option.startTick] : void 0,
1337
+ query: { enabled: !!vault && !!(option === null || option === void 0 ? void 0 : option.startTick) && !!timelockLens }
1338
+ });
1339
+ const result = (0, wagmi.useSimulateContract)({
1340
+ address: option === null || option === void 0 ? void 0 : option.marketAddr,
1341
+ abi: require_stateView.optionsMarketAbi,
1342
+ functionName: "exerciseOption",
1343
+ args: option && swapperData && ((_refTickSimulation$da = refTickSimulation.data) === null || _refTickSimulation$da === void 0 ? void 0 : _refTickSimulation$da.result) ? [
1344
+ option.optionId,
1345
+ option.liquiditiesCurrent,
1346
+ 0n,
1347
+ require_numberUtils.swappers[chainId],
1348
+ swapperData,
1349
+ refTickSimulation.data.result
1350
+ ] : void 0,
1351
+ query: {
1352
+ enabled: !!option && !!swapperData && !!((_refTickSimulation$da2 = refTickSimulation.data) === null || _refTickSimulation$da2 === void 0 ? void 0 : _refTickSimulation$da2.result) && !!option.marketAddr,
1353
+ staleTime: 1e4,
1354
+ select: (data) => {
1355
+ if (!payoutAssetDecimals) return void 0;
1356
+ return require_numberUtils.wrapAmount(data.result, payoutAssetDecimals);
1354
1357
  }
1355
- return require_numberUtils.wrapAmount(payout, payoutAssetDecimals);
1356
- },
1357
- enabled: canQueryPayout,
1358
- staleTime: 1e4
1358
+ }
1359
1359
  });
1360
1360
  return {
1361
- unrealizedPayout,
1362
- displayPnl
1361
+ ...result,
1362
+ data: {
1363
+ displayPnl,
1364
+ unrealizedPayout: result.data
1365
+ }
1363
1366
  };
1364
1367
  };
1365
1368