timelock-sdk 0.0.110 → 0.0.112

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
@@ -288,6 +288,102 @@ function getSdk(client, withWrapper = defaultWrapper) {
288
288
  };
289
289
  }
290
290
 
291
+ //#endregion
292
+ //#region src/lib/perpsOperator.ts
293
+ var PerpsOperator = class {
294
+ #baseUrl;
295
+ auth;
296
+ constructor(baseUrl) {
297
+ this.#baseUrl = baseUrl;
298
+ }
299
+ #request = async (path, body) => {
300
+ const url = new URL(path, this.#baseUrl);
301
+ const res = await fetch(url, {
302
+ method: body ? "POST" : "GET",
303
+ headers: {
304
+ Connection: "keep-alive",
305
+ "Content-Type": "application/json",
306
+ "Keep-Alive": "timeout=120"
307
+ },
308
+ body: body ? JSON.stringify(body) : void 0
309
+ });
310
+ if (res.ok) {
311
+ const { data } = await res.json();
312
+ return data;
313
+ }
314
+ const resText = await res.text();
315
+ try {
316
+ const error = JSON.parse(resText);
317
+ throw new Error(`${res.status} ${res.statusText}: ${error.error}`);
318
+ } catch (error) {
319
+ throw new Error(`${res.status} ${res.statusText}: ${resText}`);
320
+ }
321
+ };
322
+ getOperatorAddr = async () => {
323
+ const { address } = await this.#request("api/operator/address");
324
+ return address;
325
+ };
326
+ genAuthMessage = async (userAddr) => {
327
+ const { message } = await this.#request("api/auth/gen", { userAddr });
328
+ return message;
329
+ };
330
+ validateAuthMessage = async (message, signature) => {
331
+ const { address, createdAt, validUntil } = await this.#request("api/auth/validate", {
332
+ message,
333
+ signature
334
+ });
335
+ return {
336
+ address,
337
+ createdAt,
338
+ validUntil
339
+ };
340
+ };
341
+ setAuth = (message, signature) => {
342
+ this.auth = {
343
+ message,
344
+ signature
345
+ };
346
+ };
347
+ getUserPerps = async (userAddr, marketAddr, type, offset = 0, limit = 1e3) => {
348
+ const params = new URLSearchParams({
349
+ offset: offset.toString(),
350
+ limit: limit.toString()
351
+ });
352
+ if (type) params.append("type", type);
353
+ if (marketAddr) params.append("marketAddr", marketAddr);
354
+ const url = `api/positions/${userAddr}?${params.toString()}`;
355
+ return (await this.#request(url)).map((p) => ({
356
+ ...p,
357
+ optionId: BigInt(p.optionId)
358
+ }));
359
+ };
360
+ mintPerp = async (body) => {
361
+ if (!this.auth) throw new Error("Authentication required. Call setAuth() with authMessage and signature before exercising perps.");
362
+ const { txHash, optionId } = await this.#request("api/positions/mint", {
363
+ ...body,
364
+ amount: body.amount.toString(),
365
+ auth: this.auth
366
+ });
367
+ return {
368
+ txHash,
369
+ optionId: BigInt(optionId)
370
+ };
371
+ };
372
+ exercisePerp = async (body) => {
373
+ if (!this.auth) throw new Error("Authentication required. Call setAuth() with authMessage and signature before exercising perps.");
374
+ const { txHash, optionId } = await this.#request("api/positions/exercise", {
375
+ ...body,
376
+ optionId: body.optionId.toString(),
377
+ liquidities: body.liquidities.map((l) => l.toString()),
378
+ auth: this.auth
379
+ });
380
+ return {
381
+ txHash,
382
+ optionId: BigInt(optionId)
383
+ };
384
+ };
385
+ };
386
+
291
387
  //#endregion
292
388
  //#region src/providers/TimelockProvider.tsx
293
389
  const TimelockContext = (0, react.createContext)(void 0);
@@ -298,11 +394,15 @@ const TimelockProvider = ({ children, marketData, envioGraphqlUrl, perpsOperator
298
394
  const graphqlClient = (0, react.useMemo)(() => {
299
395
  if (envioGraphqlUrl) return getSdk(new graphql_request.GraphQLClient(envioGraphqlUrl));
300
396
  }, [envioGraphqlUrl]);
397
+ const perpsOperator = (0, react.useMemo)(() => {
398
+ if (perpsOperatorUrl) return new PerpsOperator(perpsOperatorUrl);
399
+ }, [perpsOperatorUrl]);
301
400
  const contextValue = (0, react.useMemo)(() => ({
302
401
  marketData: marketData || {},
303
402
  lensAddr,
304
403
  uniswapMathLensAddr,
305
404
  envioGraphqlUrl,
405
+ perpsOperator,
306
406
  graphqlClient,
307
407
  perpsOperatorUrl
308
408
  }), [
@@ -310,6 +410,7 @@ const TimelockProvider = ({ children, marketData, envioGraphqlUrl, perpsOperator
310
410
  lensAddr,
311
411
  uniswapMathLensAddr,
312
412
  envioGraphqlUrl,
413
+ perpsOperator,
313
414
  graphqlClient,
314
415
  perpsOperatorUrl
315
416
  ]);
@@ -819,102 +920,6 @@ const useOptionTimeline = (marketAddr, optionId) => {
819
920
  };
820
921
  };
821
922
 
822
- //#endregion
823
- //#region src/lib/perpsOperator.ts
824
- var PerpsOperator = class {
825
- #baseUrl;
826
- auth;
827
- constructor(baseUrl) {
828
- this.#baseUrl = baseUrl;
829
- }
830
- #request = async (path, body) => {
831
- const url = new URL(path, this.#baseUrl);
832
- const res = await fetch(url, {
833
- method: body ? "POST" : "GET",
834
- headers: {
835
- Connection: "keep-alive",
836
- "Content-Type": "application/json",
837
- "Keep-Alive": "timeout=120"
838
- },
839
- body: body ? JSON.stringify(body) : void 0
840
- });
841
- if (res.ok) {
842
- const { data } = await res.json();
843
- return data;
844
- }
845
- const resText = await res.text();
846
- try {
847
- const error = JSON.parse(resText);
848
- throw new Error(`${res.status} ${res.statusText}: ${error.error}`);
849
- } catch (error) {
850
- throw new Error(`${res.status} ${res.statusText}: ${resText}`);
851
- }
852
- };
853
- getOperatorAddr = async () => {
854
- const { address } = await this.#request("api/operator/address");
855
- return address;
856
- };
857
- genAuthMessage = async (userAddr) => {
858
- const { message } = await this.#request("api/auth/gen", { userAddr });
859
- return message;
860
- };
861
- validateAuthMessage = async (message, signature) => {
862
- const { address, createdAt, validUntil } = await this.#request("api/auth/validate", {
863
- message,
864
- signature
865
- });
866
- return {
867
- address,
868
- createdAt,
869
- validUntil
870
- };
871
- };
872
- setAuth = (message, signature) => {
873
- this.auth = {
874
- message,
875
- signature
876
- };
877
- };
878
- getUserPerps = async (userAddr, marketAddr, type, offset = 0, limit = 1e3) => {
879
- const params = new URLSearchParams({
880
- offset: offset.toString(),
881
- limit: limit.toString()
882
- });
883
- if (type) params.append("type", type);
884
- if (marketAddr) params.append("marketAddr", marketAddr);
885
- const url = `api/positions/${userAddr}?${params.toString()}`;
886
- return (await this.#request(url)).map((p) => ({
887
- ...p,
888
- optionId: BigInt(p.optionId)
889
- }));
890
- };
891
- mintPerp = async (body) => {
892
- if (!this.auth) throw new Error("Authentication required. Call setAuth() with authMessage and signature before exercising perps.");
893
- const { txHash, optionId } = await this.#request("api/positions/mint", {
894
- ...body,
895
- amount: body.amount.toString(),
896
- auth: this.auth
897
- });
898
- return {
899
- txHash,
900
- optionId: BigInt(optionId)
901
- };
902
- };
903
- exercisePerp = async (body) => {
904
- if (!this.auth) throw new Error("Authentication required. Call setAuth() with authMessage and signature before exercising perps.");
905
- const { txHash, optionId } = await this.#request("api/positions/exercise", {
906
- ...body,
907
- optionId: body.optionId.toString(),
908
- liquidities: body.liquidities.map((l) => l.toString()),
909
- auth: this.auth
910
- });
911
- return {
912
- txHash,
913
- optionId: BigInt(optionId)
914
- };
915
- };
916
- };
917
-
918
923
  //#endregion
919
924
  //#region src/hooks/perps/usePerpsOperator.ts
920
925
  const ZHex = zod.z.string().regex(/^0x[a-fA-F0-9]+$/, "Invalid hex string").transform((v) => v);
@@ -947,9 +952,8 @@ const clearSignature = (userAddr) => {
947
952
  };
948
953
  const usePerpsOperator = () => {
949
954
  const { address: userAddr } = (0, wagmi.useAccount)();
950
- const { perpsOperatorUrl } = useTimelockConfig();
955
+ const { perpsOperatorUrl, perpsOperator: operator } = useTimelockConfig();
951
956
  const { signMessageAsync } = (0, wagmi.useSignMessage)();
952
- const operator = (0, react.useMemo)(() => perpsOperatorUrl ? new PerpsOperator(perpsOperatorUrl) : void 0, [perpsOperatorUrl]);
953
957
  const { data: address } = (0, __tanstack_react_query.useQuery)({
954
958
  queryKey: ["perpsOperatorAddr", perpsOperatorUrl || "--"],
955
959
  queryFn: () => operator === null || operator === void 0 ? void 0 : operator.getOperatorAddr(),
@@ -1174,6 +1178,11 @@ const useClosedUserPerps = (marketAddr, userAddr) => {
1174
1178
  //#endregion
1175
1179
  //#region src/hooks/operators/useOperatorPerms.ts
1176
1180
  const useOperatorPerms = (marketAddr, userAddr, operatorAddr) => {
1181
+ const { payoutAsset } = useMarketData(marketAddr);
1182
+ const { data: payoutAssetBalance } = (0, wagmi.useBalance)({
1183
+ address: userAddr,
1184
+ token: payoutAsset
1185
+ });
1177
1186
  const { data,...rest } = (0, wagmi.useReadContract)({
1178
1187
  abi: require_optionsMarket.optionsMarketAbi,
1179
1188
  address: marketAddr,
@@ -1181,14 +1190,25 @@ const useOperatorPerms = (marketAddr, userAddr, operatorAddr) => {
1181
1190
  args: [userAddr, operatorAddr],
1182
1191
  query: { enabled: !!userAddr && !!operatorAddr }
1183
1192
  });
1193
+ const [canExtend, canExercise, canTransfer, canMint, spendingApproval] = data || [];
1194
+ const min = (a, b) => a < b ? a : b;
1195
+ const effectiveApproval = spendingApproval && payoutAssetBalance ? min(spendingApproval, payoutAssetBalance.value) : 0n;
1184
1196
  return {
1185
- data: data ? {
1186
- canExtend: data[0],
1187
- canExercise: data[1],
1188
- canTransfer: data[2],
1189
- canMint: data[3],
1190
- spendingApproval: data[4]
1191
- } : void 0,
1197
+ data: (0, react.useMemo)(() => ({
1198
+ canExtend,
1199
+ canExercise,
1200
+ canTransfer,
1201
+ canMint,
1202
+ spendingApproval,
1203
+ effectiveApproval
1204
+ }), [
1205
+ canExtend,
1206
+ canExercise,
1207
+ canTransfer,
1208
+ canMint,
1209
+ spendingApproval,
1210
+ effectiveApproval
1211
+ ]),
1192
1212
  ...rest
1193
1213
  };
1194
1214
  };