timelock-sdk 0.0.81 → 0.0.83

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
@@ -18,10 +18,6 @@ let viem_actions = require("viem/actions");
18
18
  viem_actions = require_optionUtils.__toESM(viem_actions);
19
19
  let __tanstack_react_query = require("@tanstack/react-query");
20
20
  __tanstack_react_query = require_optionUtils.__toESM(__tanstack_react_query);
21
- let timelock_sdk = require("timelock-sdk");
22
- timelock_sdk = require_optionUtils.__toESM(timelock_sdk);
23
- let timelock_sdk_client = require("timelock-sdk/client");
24
- timelock_sdk_client = require_optionUtils.__toESM(timelock_sdk_client);
25
21
 
26
22
  //#region src/generated/graphql.ts
27
23
  const UserOptionFieldsFragmentDoc = graphql_tag.default`
@@ -889,69 +885,6 @@ const usePerpsOperator = () => {
889
885
  };
890
886
  };
891
887
 
892
- //#endregion
893
- //#region src/hooks/perps/useMintPerp.ts
894
- const useMintPerp = (marketAddr) => {
895
- const queryClient = (0, __tanstack_react_query.useQueryClient)();
896
- const client = (0, wagmi.useClient)();
897
- const { address } = (0, wagmi.useAccount)();
898
- const { operator, address: operatorAddr } = usePerpsOperator();
899
- const { askForApproval } = (0, timelock_sdk_client.useApproval)();
900
- const { data: operators } = (0, timelock_sdk_client.useUserOperators)(address, marketAddr);
901
- const { mutateAsync: setOperatorPerms } = (0, timelock_sdk_client.useSetOperatorPerms)(marketAddr);
902
- const { pool, optionAssetIsToken0, payoutAsset } = (0, timelock_sdk_client.useMarketData)(marketAddr);
903
- const { tickSpacing } = (0, timelock_sdk_client.usePoolData)(pool);
904
- const { exact: currentTick } = (0, timelock_sdk_client.useCurrentTick)(pool);
905
- const userPerms = operatorAddr ? operators.find((o) => o.operatorAddr.toLowerCase() === operatorAddr.toLowerCase()) : void 0;
906
- const hasEnoughPerms = userPerms && userPerms.canMint && userPerms.canExtend;
907
- const mintPerp = async (data) => {
908
- const { optionType, amount, duration, strikeTick } = data;
909
- if (!client || !address) throw new Error("Wallet not connected");
910
- if (!marketAddr) throw new Error("Market address not found");
911
- if (!operator || !operatorAddr) throw new Error("Operator address not found");
912
- if (!tickSpacing || currentTick === void 0) throw new Error("Pool data not found");
913
- if (optionAssetIsToken0 === void 0 || !payoutAsset) throw new Error("Market data not found");
914
- if (!hasEnoughPerms) await setOperatorPerms({
915
- operator: operatorAddr,
916
- canMint: true,
917
- canExtend: true,
918
- canExercise: (userPerms === null || userPerms === void 0 ? void 0 : userPerms.canExercise) || false,
919
- canTransfer: (userPerms === null || userPerms === void 0 ? void 0 : userPerms.canTransfer) || false,
920
- spendingApproval: viem.maxUint256
921
- });
922
- const market = (0, timelock_sdk.getTimelockMarket)(marketAddr, client);
923
- const validStrikeTick = (0, timelock_sdk.getNearestValidStrikeTick)(optionType, optionAssetIsToken0, tickSpacing, currentTick, strikeTick);
924
- const [premium, protocolFee] = await market.read.calculatePremium([
925
- optionType === "CALL" ? 0 : 1,
926
- amount,
927
- validStrikeTick,
928
- duration,
929
- 0
930
- ]);
931
- await askForApproval(payoutAsset, marketAddr, (premium + protocolFee) * 11n / 10n);
932
- await operator.mintPerp({
933
- marketAddr,
934
- userAddr: address,
935
- amount,
936
- optionType,
937
- duration,
938
- strikeTick: validStrikeTick
939
- });
940
- queryClient.invalidateQueries({ queryKey: ["userOptions", address.toLowerCase()] });
941
- };
942
- return (0, __tanstack_react_query.useMutation)({ mutationFn: mintPerp });
943
- };
944
-
945
- //#endregion
946
- //#region src/hooks/perps/useClosePerp.ts
947
- const useClosePerp = () => {
948
- const { operator } = usePerpsOperator();
949
- return (0, __tanstack_react_query.useMutation)({ mutationFn: (body) => {
950
- if (!operator) throw new Error("Operator not connected");
951
- return operator.exercisePerp(body);
952
- } });
953
- };
954
-
955
888
  //#endregion
956
889
  //#region src/hooks/operators/useUserOperators.ts
957
890
  const useUserOperators = (userAddr, marketAddr) => {
@@ -1015,6 +948,69 @@ const useSetOperatorPerms = (marketAddr) => {
1015
948
  return (0, __tanstack_react_query.useMutation)({ mutationFn: setOperatorPerms });
1016
949
  };
1017
950
 
951
+ //#endregion
952
+ //#region src/hooks/perps/useMintPerp.ts
953
+ const useMintPerp = (marketAddr) => {
954
+ const queryClient = (0, __tanstack_react_query.useQueryClient)();
955
+ const client = (0, wagmi.useClient)();
956
+ const { address } = (0, wagmi.useAccount)();
957
+ const { operator, address: operatorAddr } = usePerpsOperator();
958
+ const { askForApproval } = useApproval();
959
+ const { data: operators } = useUserOperators(address, marketAddr);
960
+ const { mutateAsync: setOperatorPerms } = useSetOperatorPerms(marketAddr);
961
+ const { pool, optionAssetIsToken0, payoutAsset } = useMarketData(marketAddr);
962
+ const { tickSpacing } = usePoolData(pool);
963
+ const { exact: currentTick } = useCurrentTick(pool);
964
+ const userPerms = operatorAddr ? operators.find((o) => o.operatorAddr.toLowerCase() === operatorAddr.toLowerCase()) : void 0;
965
+ const hasEnoughPerms = userPerms && userPerms.canMint && userPerms.canExtend;
966
+ const mintPerp = async (data) => {
967
+ const { optionType, amount, duration, strikeTick } = data;
968
+ if (!client || !address) throw new Error("Wallet not connected");
969
+ if (!marketAddr) throw new Error("Market address not found");
970
+ if (!operator || !operatorAddr) throw new Error("Operator address not found");
971
+ if (!tickSpacing || currentTick === void 0) throw new Error("Pool data not found");
972
+ if (optionAssetIsToken0 === void 0 || !payoutAsset) throw new Error("Market data not found");
973
+ if (!hasEnoughPerms) await setOperatorPerms({
974
+ operator: operatorAddr,
975
+ canMint: true,
976
+ canExtend: true,
977
+ canExercise: (userPerms === null || userPerms === void 0 ? void 0 : userPerms.canExercise) || false,
978
+ canTransfer: (userPerms === null || userPerms === void 0 ? void 0 : userPerms.canTransfer) || false,
979
+ spendingApproval: viem.maxUint256
980
+ });
981
+ const market = require_optionUtils.getTimelockMarket(marketAddr, client);
982
+ const validStrikeTick = require_optionUtils.getNearestValidStrikeTick(optionType, optionAssetIsToken0, tickSpacing, currentTick, strikeTick);
983
+ const [premium, protocolFee] = await market.read.calculatePremium([
984
+ optionType === "CALL" ? 0 : 1,
985
+ amount,
986
+ validStrikeTick,
987
+ duration,
988
+ 0
989
+ ]);
990
+ await askForApproval(payoutAsset, marketAddr, (premium + protocolFee) * 11n / 10n);
991
+ await operator.mintPerp({
992
+ marketAddr,
993
+ userAddr: address,
994
+ amount,
995
+ optionType,
996
+ duration,
997
+ strikeTick: validStrikeTick
998
+ });
999
+ queryClient.invalidateQueries({ queryKey: ["userOptions", address.toLowerCase()] });
1000
+ };
1001
+ return (0, __tanstack_react_query.useMutation)({ mutationFn: mintPerp });
1002
+ };
1003
+
1004
+ //#endregion
1005
+ //#region src/hooks/perps/useClosePerp.ts
1006
+ const useClosePerp = () => {
1007
+ const { operator } = usePerpsOperator();
1008
+ return (0, __tanstack_react_query.useMutation)({ mutationFn: (body) => {
1009
+ if (!operator) throw new Error("Operator not connected");
1010
+ return operator.exercisePerp(body);
1011
+ } });
1012
+ };
1013
+
1018
1014
  //#endregion
1019
1015
  //#region src/hooks/pool/usePriceAtTick.ts
1020
1016
  const usePriceAtTick = (tick, poolAddr) => {
@@ -1151,25 +1147,17 @@ const useLiquidityBlocks = (vaultAddr) => {
1151
1147
 
1152
1148
  //#endregion
1153
1149
  //#region src/hooks/vault/useMintLiquidity.ts
1154
- const batchGetAmountsFromLiquidity = async (lens, tickLower, tickUpper, liquidity, currentTick) => {
1155
- const currentTicksArray = new Array(tickLower.length).fill(currentTick);
1156
- const amounts = await lens.read.batchGetAmountsForLiquidityTicks([
1157
- currentTicksArray,
1158
- tickLower,
1159
- tickUpper,
1160
- liquidity
1161
- ]);
1150
+ const batchGetAmountsFromLiquidity = (tickLowers, tickUppers, liquidities, currentTick) => {
1162
1151
  let totalAmount0 = 0n;
1163
1152
  let totalAmount1 = 0n;
1164
1153
  const amounts0 = [];
1165
1154
  const amounts1 = [];
1166
- for (const scaled of amounts[0]) {
1167
- totalAmount0 += scaled;
1168
- amounts0.push(scaled);
1169
- }
1170
- for (const scaled of amounts[1]) {
1171
- totalAmount1 += scaled;
1172
- amounts1.push(scaled);
1155
+ for (const [i, tickLower] of tickLowers.entries()) {
1156
+ const [amount0, amount1] = require_optionUtils.getAmountsFromLiquidity(tickLower, tickUppers[i], liquidities[i], currentTick);
1157
+ totalAmount0 += amount0;
1158
+ totalAmount1 += amount1;
1159
+ amounts0.push(amount0);
1160
+ amounts1.push(amount1);
1173
1161
  }
1174
1162
  return {
1175
1163
  totalAmount0,
@@ -1179,98 +1167,45 @@ const batchGetAmountsFromLiquidity = async (lens, tickLower, tickUpper, liquidit
1179
1167
  };
1180
1168
  };
1181
1169
  const useMintLiquidity = (vaultAddr) => {
1170
+ const queryClient = (0, __tanstack_react_query.useQueryClient)();
1182
1171
  const client = (0, wagmi.useClient)();
1183
- const { address } = (0, wagmi.useAccount)();
1184
1172
  const { pool } = useVaultData(vaultAddr);
1185
- const { timelockLens, uniswapLens } = useLens();
1186
- const currentTick = useCurrentTick(pool);
1173
+ const { timelockLens } = useLens();
1174
+ const { exact: currentTick } = useCurrentTick(pool);
1187
1175
  const { token0, token1 } = usePoolData(pool);
1188
- const { writeContractAsync, data: hash, isPending, error } = (0, wagmi.useWriteContract)();
1189
- const { isLoading: isConfirming, isSuccess } = (0, wagmi.useWaitForTransactionReceipt)({ hash });
1190
- const askForApproval = async (params) => {
1191
- if (!address || !client) throw new Error("Wallet not connected");
1192
- if (currentTick.exact === void 0 || !token0 || !token1 || !vaultAddr || !uniswapLens) throw new Error("Current tick not available");
1193
- const { totalAmount0, totalAmount1 } = await batchGetAmountsFromLiquidity(uniswapLens, params.map((p) => p.tickLower), params.map((p) => p.tickUpper), params.map((p) => p.liquidity), currentTick.exact);
1194
- const [allowance0, allowance1] = await Promise.all([require_optionUtils.getErc20(token0, client).read.allowance([address, vaultAddr]), require_optionUtils.getErc20(token1, client).read.allowance([address, vaultAddr])]);
1195
- const approvalPromises = [];
1196
- if (allowance0 <= totalAmount0) {
1197
- const approvalHash = await writeContractAsync({
1198
- address: token0,
1199
- abi: viem.erc20Abi,
1200
- functionName: "approve",
1201
- args: [vaultAddr, viem.maxUint256]
1202
- });
1203
- approvalPromises.push((0, viem_actions.waitForTransactionReceipt)(client, { hash: approvalHash }));
1204
- }
1205
- if (allowance1 <= totalAmount1) {
1206
- const approvalHash1 = await writeContractAsync({
1207
- address: token1,
1208
- abi: viem.erc20Abi,
1209
- functionName: "approve",
1210
- args: [vaultAddr, viem.maxUint256]
1211
- });
1212
- approvalPromises.push((0, viem_actions.waitForTransactionReceipt)(client, { hash: approvalHash1 }));
1213
- }
1214
- if (approvalPromises.length > 0) await Promise.all(approvalPromises);
1176
+ const { askForApproval } = useApproval();
1177
+ const { writeContractAsync } = (0, wagmi.useWriteContract)();
1178
+ const processApproval = async (params) => {
1179
+ if (currentTick === void 0 || !token0 || !token1 || !vaultAddr) throw new Error("Current tick not available");
1180
+ const { totalAmount0, totalAmount1 } = batchGetAmountsFromLiquidity(params.map((p) => p.tickLower), params.map((p) => p.tickUpper), params.map((p) => p.liquidity), currentTick);
1181
+ await askForApproval(token0, vaultAddr, totalAmount0);
1182
+ await askForApproval(token1, vaultAddr, totalAmount1);
1215
1183
  };
1216
- const mint = async (tickLower, tickUpper, liquidity) => {
1184
+ return (0, __tanstack_react_query.useMutation)({ mutationFn: async (params) => {
1217
1185
  if (!client) throw new Error("Wallet not connected");
1218
- if (!vaultAddr || !timelockLens) throw new Error("Vault/lens not available");
1219
- await askForApproval([{
1220
- tickLower,
1221
- tickUpper,
1222
- liquidity
1223
- }]);
1224
- const hash$1 = await writeContractAsync({
1186
+ if (currentTick === void 0) throw new Error("Current tick not available");
1187
+ if (!timelockLens || !vaultAddr) throw new Error("Vault/lens not available");
1188
+ if (!Array.isArray(params)) params = [params];
1189
+ if (params.length === 0) throw new Error("No positions to mint");
1190
+ await processApproval(params);
1191
+ const refTicks = await timelockLens.read.batchGetRefTick([vaultAddr, params.map((position) => position.tickLower)]);
1192
+ await (0, viem_actions.waitForTransactionReceipt)(client, { hash: await writeContractAsync({
1225
1193
  address: vaultAddr,
1226
1194
  abi: require_singleOwnerVault.singleOwnerVaultAbi,
1227
- functionName: "mint",
1228
- args: [
1229
- tickLower,
1230
- tickUpper,
1231
- liquidity,
1232
- await timelockLens.read.getRefTick([vaultAddr, tickLower])
1233
- ]
1234
- });
1235
- await (0, viem_actions.waitForTransactionReceipt)(client, { hash: hash$1 });
1236
- return hash$1;
1237
- };
1238
- const mintMultiple = async (positions) => {
1239
- if (!client) throw new Error("Wallet not connected");
1240
- if (!currentTick.exact) throw new Error("Current tick not available");
1241
- if (positions.length === 0) throw new Error("No positions to mint");
1242
- if (!timelockLens || !vaultAddr) throw new Error("Vault/lens not available");
1243
- if (positions.length === 1) await mint(positions[0].tickLower, positions[0].tickUpper, positions[0].liquidity);
1244
- else {
1245
- await askForApproval(positions);
1246
- const refTicks = await timelockLens.read.batchGetRefTick([vaultAddr, positions.map((position) => position.tickLower)]);
1247
- await (0, viem_actions.waitForTransactionReceipt)(client, { hash: await writeContractAsync({
1248
- address: vaultAddr,
1195
+ functionName: "multicall",
1196
+ args: [params.map((p, i) => (0, viem.encodeFunctionData)({
1249
1197
  abi: require_singleOwnerVault.singleOwnerVaultAbi,
1250
- functionName: "multicall",
1251
- args: [positions.map((p, i) => (0, viem.encodeFunctionData)({
1252
- abi: require_singleOwnerVault.singleOwnerVaultAbi,
1253
- functionName: "mint",
1254
- args: [
1255
- p.tickLower,
1256
- p.tickUpper,
1257
- p.liquidity,
1258
- refTicks[i]
1259
- ]
1260
- }))]
1261
- }) });
1262
- }
1263
- };
1264
- return {
1265
- mintMultiple,
1266
- mint,
1267
- hash,
1268
- isPending,
1269
- isConfirming,
1270
- isSuccess,
1271
- error,
1272
- isLoading: isPending || isConfirming
1273
- };
1198
+ functionName: "mint",
1199
+ args: [
1200
+ p.tickLower,
1201
+ p.tickUpper,
1202
+ p.liquidity,
1203
+ refTicks[i]
1204
+ ]
1205
+ }))]
1206
+ }) });
1207
+ queryClient.invalidateQueries({ queryKey: ["readContract"] });
1208
+ } });
1274
1209
  };
1275
1210
 
1276
1211
  //#endregion