timelock-sdk 0.0.177 → 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
@@ -1082,6 +1082,66 @@ const usePerpsOperator = () => {
1082
1082
  };
1083
1083
  };
1084
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
+
1085
1145
  //#endregion
1086
1146
  //#region src/hooks/perps/useMintPerp.ts
1087
1147
  const useMintPerp = (marketAddr) => {
@@ -1090,9 +1150,13 @@ const useMintPerp = (marketAddr) => {
1090
1150
  const { address } = (0, wagmi.useConnection)();
1091
1151
  const { operator, address: operatorAddr, signMessage: { mutateAsync: signMessage } } = usePerpsOperator();
1092
1152
  const { askForApproval } = useApproval();
1153
+ const { data: operators } = useUserOperators(address, marketAddr);
1154
+ const { mutateAsync: setOperatorPerms } = useSetOperatorPerms(marketAddr);
1093
1155
  const { poolManager, poolKey, optionAssetIsToken0, payoutAsset } = useMarketData(marketAddr);
1094
1156
  const { tickSpacing } = usePoolData(poolManager, poolKey);
1095
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;
1096
1160
  const mintPerp = async (data) => {
1097
1161
  const { optionType, amount, duration, strikeTick } = data;
1098
1162
  if (!client || !address) throw new Error("Wallet not connected");
@@ -1112,7 +1176,16 @@ const useMintPerp = (marketAddr) => {
1112
1176
  duration,
1113
1177
  0
1114
1178
  ]);
1115
- 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);
1116
1189
  await operator.mintPerp({
1117
1190
  marketAddr,
1118
1191
  amount,
@@ -1146,7 +1219,7 @@ const useClosePerp = () => {
1146
1219
 
1147
1220
  //#endregion
1148
1221
  //#region src/hooks/perps/useUserPerps.ts
1149
- const EMPTY_ARRAY$1 = [];
1222
+ const EMPTY_ARRAY = [];
1150
1223
  const useUserPerps = (marketAddr, userAddr, type) => {
1151
1224
  const { operator } = usePerpsOperator();
1152
1225
  userAddr = userAddr === null || userAddr === void 0 ? void 0 : userAddr.toLowerCase();
@@ -1164,7 +1237,7 @@ const useUserPerps = (marketAddr, userAddr, type) => {
1164
1237
  refetchInterval: 1e4
1165
1238
  });
1166
1239
  return {
1167
- data: data || EMPTY_ARRAY$1,
1240
+ data: data || EMPTY_ARRAY,
1168
1241
  ...rest
1169
1242
  };
1170
1243
  };
@@ -1245,66 +1318,6 @@ const useOperatorPerms = (marketAddr, userAddr, operatorAddr) => {
1245
1318
  };
1246
1319
  };
1247
1320
 
1248
- //#endregion
1249
- //#region src/hooks/operators/useUserOperators.ts
1250
- const useUserOperators = (userAddr, marketAddr) => {
1251
- const { graphqlClient } = useTimelockConfig();
1252
- const { data, ...rest } = (0, __tanstack_react_query.useQuery)({
1253
- queryKey: [
1254
- "userOperators",
1255
- (userAddr === null || userAddr === void 0 ? void 0 : userAddr.toLowerCase()) || "--",
1256
- (marketAddr === null || marketAddr === void 0 ? void 0 : marketAddr.toLowerCase()) || "--"
1257
- ],
1258
- queryFn: async () => {
1259
- if (!userAddr || !marketAddr) return void 0;
1260
- return (await graphqlClient.GetUserMarketOperators({
1261
- userAddr: userAddr.toLowerCase(),
1262
- marketAddr: marketAddr.toLowerCase()
1263
- })).UserMarketOperator.map((operator) => ({
1264
- ...operator,
1265
- spendingApproval: BigInt(operator.spendingApproval),
1266
- operatorAddr: operator.operator.address.toLowerCase()
1267
- }));
1268
- },
1269
- enabled: !!userAddr && !!marketAddr && !!graphqlClient
1270
- });
1271
- return {
1272
- ...rest,
1273
- data: data || require_optionUtils.EMPTY_ARRAY
1274
- };
1275
- };
1276
-
1277
- //#endregion
1278
- //#region src/hooks/operators/useSetOperatorPerms.ts
1279
- const useSetOperatorPerms = (marketAddr) => {
1280
- const queryClient = (0, __tanstack_react_query.useQueryClient)();
1281
- const client = (0, wagmi.useClient)();
1282
- const { address } = (0, wagmi.useConnection)();
1283
- const { writeContractAsync } = (0, wagmi.useWriteContract)();
1284
- const setOperatorPerms = async ({ operator, canExtend, canExercise, canTransfer, canMint, spendingApproval }) => {
1285
- if (!client || !address) throw new Error("Wallet not connected");
1286
- if (!marketAddr) throw new Error("Market address not available");
1287
- const hash = await writeContractAsync({
1288
- address: marketAddr,
1289
- abi: require_statelessStateView.optionsMarketAbi,
1290
- functionName: "setOperatorPerms",
1291
- args: [
1292
- operator,
1293
- canExtend,
1294
- canExercise,
1295
- canTransfer,
1296
- canMint,
1297
- spendingApproval
1298
- ]
1299
- });
1300
- await (0, viem_actions.waitForTransactionReceipt)(client, { hash });
1301
- queryClient.invalidateQueries({ queryKey: ["userOperators"] });
1302
- queryClient.invalidateQueries({ queryKey: ["readContract"] });
1303
- return hash;
1304
- };
1305
- return (0, __tanstack_react_query.useMutation)({ mutationFn: setOperatorPerms });
1306
- };
1307
-
1308
1321
  //#endregion
1309
1322
  //#region src/hooks/pool/usePriceHistory.ts
1310
1323
  const usePriceHistory = (pool, token, resolution, startTimestamp, endTimestamp) => {