timelock-sdk 0.0.176 → 0.0.178

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
@@ -616,7 +616,7 @@ const useMintOption = (marketAddr) => {
616
616
  if (!timelockLens) throw new Error("Timelock lens not available");
617
617
  if (!tickSpacing) throw new Error("Pool data not available");
618
618
  if (!vault || !payoutAsset || optionAssetIsToken0 === void 0) throw new Error("Market data not available");
619
- const { data: { exact: currentTick } = {} } = await refetchCurrentTick();
619
+ const { data: { currentTick } = {} } = await refetchCurrentTick();
620
620
  if (currentTick === void 0) throw new Error("Could not fetch current tick");
621
621
  strikeTick = require_optionUtils.getNearestValidStrikeTick(optionType, optionAssetIsToken0, tickSpacing, currentTick, strikeTick);
622
622
  const [premium, protocolFee] = await require_optionUtils.getTimelockMarket(marketAddr, client).read.calculatePremium([
@@ -626,7 +626,8 @@ const useMintOption = (marketAddr) => {
626
626
  duration,
627
627
  0
628
628
  ]);
629
- await askForApproval(payoutAsset, marketAddr, (premium + protocolFee) * 11n / 10n);
629
+ const maxPremium = (premium + protocolFee) * 11n / 10n;
630
+ await askForApproval(payoutAsset, marketAddr, maxPremium);
630
631
  const hash = await writeContractAsync({
631
632
  address: marketAddr,
632
633
  abi: require_statelessStateView.optionsMarketAbi,
@@ -637,7 +638,7 @@ const useMintOption = (marketAddr) => {
637
638
  amount,
638
639
  strikeTick,
639
640
  duration,
640
- viem.maxUint256,
641
+ maxPremium,
641
642
  maxSteps,
642
643
  await timelockLens.read.getRefTick([vault, strikeTick])
643
644
  ]
@@ -1081,6 +1082,66 @@ const usePerpsOperator = () => {
1081
1082
  };
1082
1083
  };
1083
1084
 
1085
+ //#endregion
1086
+ //#region src/hooks/operators/useUserOperators.ts
1087
+ const useUserOperators = (userAddr, marketAddr) => {
1088
+ const { graphqlClient } = useTimelockConfig();
1089
+ const { data, ...rest } = (0, __tanstack_react_query.useQuery)({
1090
+ queryKey: [
1091
+ "userOperators",
1092
+ (userAddr === null || userAddr === void 0 ? void 0 : userAddr.toLowerCase()) || "--",
1093
+ (marketAddr === null || marketAddr === void 0 ? void 0 : marketAddr.toLowerCase()) || "--"
1094
+ ],
1095
+ queryFn: async () => {
1096
+ if (!userAddr || !marketAddr) return void 0;
1097
+ return (await graphqlClient.GetUserMarketOperators({
1098
+ userAddr: userAddr.toLowerCase(),
1099
+ marketAddr: marketAddr.toLowerCase()
1100
+ })).UserMarketOperator.map((operator) => ({
1101
+ ...operator,
1102
+ spendingApproval: BigInt(operator.spendingApproval),
1103
+ operatorAddr: operator.operator.address.toLowerCase()
1104
+ }));
1105
+ },
1106
+ enabled: !!userAddr && !!marketAddr && !!graphqlClient
1107
+ });
1108
+ return {
1109
+ ...rest,
1110
+ data: data || require_optionUtils.EMPTY_ARRAY
1111
+ };
1112
+ };
1113
+
1114
+ //#endregion
1115
+ //#region src/hooks/operators/useSetOperatorPerms.ts
1116
+ const useSetOperatorPerms = (marketAddr) => {
1117
+ const queryClient = (0, __tanstack_react_query.useQueryClient)();
1118
+ const client = (0, wagmi.useClient)();
1119
+ const { address } = (0, wagmi.useConnection)();
1120
+ const { writeContractAsync } = (0, wagmi.useWriteContract)();
1121
+ const setOperatorPerms = async ({ operator, canExtend, canExercise, canTransfer, canMint, spendingApproval }) => {
1122
+ if (!client || !address) throw new Error("Wallet not connected");
1123
+ if (!marketAddr) throw new Error("Market address not available");
1124
+ const hash = await writeContractAsync({
1125
+ address: marketAddr,
1126
+ abi: require_statelessStateView.optionsMarketAbi,
1127
+ functionName: "setOperatorPerms",
1128
+ args: [
1129
+ operator,
1130
+ canExtend,
1131
+ canExercise,
1132
+ canTransfer,
1133
+ canMint,
1134
+ spendingApproval
1135
+ ]
1136
+ });
1137
+ await (0, viem_actions.waitForTransactionReceipt)(client, { hash });
1138
+ queryClient.invalidateQueries({ queryKey: ["userOperators"] });
1139
+ queryClient.invalidateQueries({ queryKey: ["readContract"] });
1140
+ return hash;
1141
+ };
1142
+ return (0, __tanstack_react_query.useMutation)({ mutationFn: setOperatorPerms });
1143
+ };
1144
+
1084
1145
  //#endregion
1085
1146
  //#region src/hooks/perps/useMintPerp.ts
1086
1147
  const useMintPerp = (marketAddr) => {
@@ -1089,9 +1150,13 @@ const useMintPerp = (marketAddr) => {
1089
1150
  const { address } = (0, wagmi.useConnection)();
1090
1151
  const { operator, address: operatorAddr, signMessage: { mutateAsync: signMessage } } = usePerpsOperator();
1091
1152
  const { askForApproval } = useApproval();
1153
+ const { data: operators } = useUserOperators(address, marketAddr);
1154
+ const { mutateAsync: setOperatorPerms } = useSetOperatorPerms(marketAddr);
1092
1155
  const { poolManager, poolKey, optionAssetIsToken0, payoutAsset } = useMarketData(marketAddr);
1093
1156
  const { tickSpacing } = usePoolData(poolManager, poolKey);
1094
1157
  const { refetch: refetchCurrentTick } = useCurrentTick(poolManager, poolKey);
1158
+ const userPerms = operatorAddr ? operators.find((o) => o.operatorAddr.toLowerCase() === operatorAddr.toLowerCase()) : void 0;
1159
+ const hasEnoughPerms = userPerms && userPerms.canMint && userPerms.canExtend && userPerms.canExercise;
1095
1160
  const mintPerp = async (data) => {
1096
1161
  const { optionType, amount, duration, strikeTick } = data;
1097
1162
  if (!client || !address) throw new Error("Wallet not connected");
@@ -1111,7 +1176,16 @@ const useMintPerp = (marketAddr) => {
1111
1176
  duration,
1112
1177
  0
1113
1178
  ]);
1114
- await askForApproval(payoutAsset, marketAddr, (premium + protocolFee) * 11n / 10n);
1179
+ const maxPremium = (premium + protocolFee) * 11n / 10n;
1180
+ if (!hasEnoughPerms) await setOperatorPerms({
1181
+ operator: operatorAddr,
1182
+ canMint: true,
1183
+ canExtend: true,
1184
+ canExercise: true,
1185
+ canTransfer: (userPerms === null || userPerms === void 0 ? void 0 : userPerms.canTransfer) || false,
1186
+ spendingApproval: maxPremium
1187
+ });
1188
+ await askForApproval(payoutAsset, marketAddr, maxPremium);
1115
1189
  await operator.mintPerp({
1116
1190
  marketAddr,
1117
1191
  amount,
@@ -1145,7 +1219,7 @@ const useClosePerp = () => {
1145
1219
 
1146
1220
  //#endregion
1147
1221
  //#region src/hooks/perps/useUserPerps.ts
1148
- const EMPTY_ARRAY$1 = [];
1222
+ const EMPTY_ARRAY = [];
1149
1223
  const useUserPerps = (marketAddr, userAddr, type) => {
1150
1224
  const { operator } = usePerpsOperator();
1151
1225
  userAddr = userAddr === null || userAddr === void 0 ? void 0 : userAddr.toLowerCase();
@@ -1163,7 +1237,7 @@ const useUserPerps = (marketAddr, userAddr, type) => {
1163
1237
  refetchInterval: 1e4
1164
1238
  });
1165
1239
  return {
1166
- data: data || EMPTY_ARRAY$1,
1240
+ data: data || EMPTY_ARRAY,
1167
1241
  ...rest
1168
1242
  };
1169
1243
  };
@@ -1244,66 +1318,6 @@ const useOperatorPerms = (marketAddr, userAddr, operatorAddr) => {
1244
1318
  };
1245
1319
  };
1246
1320
 
1247
- //#endregion
1248
- //#region src/hooks/operators/useUserOperators.ts
1249
- const useUserOperators = (userAddr, marketAddr) => {
1250
- const { graphqlClient } = useTimelockConfig();
1251
- const { data, ...rest } = (0, __tanstack_react_query.useQuery)({
1252
- queryKey: [
1253
- "userOperators",
1254
- (userAddr === null || userAddr === void 0 ? void 0 : userAddr.toLowerCase()) || "--",
1255
- (marketAddr === null || marketAddr === void 0 ? void 0 : marketAddr.toLowerCase()) || "--"
1256
- ],
1257
- queryFn: async () => {
1258
- if (!userAddr || !marketAddr) return void 0;
1259
- return (await graphqlClient.GetUserMarketOperators({
1260
- userAddr: userAddr.toLowerCase(),
1261
- marketAddr: marketAddr.toLowerCase()
1262
- })).UserMarketOperator.map((operator) => ({
1263
- ...operator,
1264
- spendingApproval: BigInt(operator.spendingApproval),
1265
- operatorAddr: operator.operator.address.toLowerCase()
1266
- }));
1267
- },
1268
- enabled: !!userAddr && !!marketAddr && !!graphqlClient
1269
- });
1270
- return {
1271
- ...rest,
1272
- data: data || require_optionUtils.EMPTY_ARRAY
1273
- };
1274
- };
1275
-
1276
- //#endregion
1277
- //#region src/hooks/operators/useSetOperatorPerms.ts
1278
- const useSetOperatorPerms = (marketAddr) => {
1279
- const queryClient = (0, __tanstack_react_query.useQueryClient)();
1280
- const client = (0, wagmi.useClient)();
1281
- const { address } = (0, wagmi.useConnection)();
1282
- const { writeContractAsync } = (0, wagmi.useWriteContract)();
1283
- const setOperatorPerms = async ({ operator, canExtend, canExercise, canTransfer, canMint, spendingApproval }) => {
1284
- if (!client || !address) throw new Error("Wallet not connected");
1285
- if (!marketAddr) throw new Error("Market address not available");
1286
- const hash = await writeContractAsync({
1287
- address: marketAddr,
1288
- abi: require_statelessStateView.optionsMarketAbi,
1289
- functionName: "setOperatorPerms",
1290
- args: [
1291
- operator,
1292
- canExtend,
1293
- canExercise,
1294
- canTransfer,
1295
- canMint,
1296
- spendingApproval
1297
- ]
1298
- });
1299
- await (0, viem_actions.waitForTransactionReceipt)(client, { hash });
1300
- queryClient.invalidateQueries({ queryKey: ["userOperators"] });
1301
- queryClient.invalidateQueries({ queryKey: ["readContract"] });
1302
- return hash;
1303
- };
1304
- return (0, __tanstack_react_query.useMutation)({ mutationFn: setOperatorPerms });
1305
- };
1306
-
1307
1321
  //#endregion
1308
1322
  //#region src/hooks/pool/usePriceHistory.ts
1309
1323
  const usePriceHistory = (pool, token, resolution, startTimestamp, endTimestamp) => {