@zama-fhe/react-sdk 1.0.0-alpha.13 → 1.0.0-alpha.15

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/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use client";
2
- import { ZamaSDK, ReadonlyToken, DecryptionFailedError, sortByBlockNumber, parseActivityFeed, extractEncryptedHandles, applyDecryptedValues, totalSupplyContract, getWrapFeeContract, getUnwrapFeeContract, getBatchTransferFeeContract, getFeeRecipientContract } from '@zama-fhe/sdk';
2
+ import { ZamaSDK } from '@zama-fhe/sdk';
3
3
  export { ApprovalFailedError, BATCH_SWAP_ABI, ChromeSessionStorage, CredentialsManager, DEPLOYMENT_COORDINATOR_ABI, DecryptionFailedError, ENCRYPTION_ABI, ERC165_ABI, ERC20_ABI, ERC20_METADATA_ABI, ERC7984_INTERFACE_ID, ERC7984_WRAPPER_INTERFACE_ID, EncryptionFailedError, FEE_MANAGER_ABI, HardhatConfig, IndexedDBStorage, InvalidKeypairError, KeypairExpiredError, MainnetConfig, MemoryStorage, NoCiphertextError, ReadonlyToken, RelayerRequestFailedError, RelayerWeb, SepoliaConfig, SigningFailedError, SigningRejectedError, TOKEN_TOPICS, TRANSFER_BATCHER_ABI, Token, Topics, TransactionRevertedError, WRAPPER_ABI, ZERO_HANDLE, ZamaError, ZamaErrorCode, ZamaSDK, ZamaSDKEvents, allowanceContract, applyDecryptedValues, approveContract, balanceOfContract, chromeSessionStorage, clearPendingUnshield, confidentialBalanceOfContract, confidentialBatchTransferContract, confidentialTotalSupplyContract, confidentialTransferContract, confidentialTransferFromContract, decimalsContract, decodeConfidentialTransfer, decodeOnChainEvent, decodeOnChainEvents, decodeUnwrapRequested, decodeUnwrappedFinalized, decodeUnwrappedStarted, decodeWrapped, deploymentCoordinatorContract, extractEncryptedHandles, finalizeUnwrapContract, findUnwrapRequested, findWrapped, getBatchTransferFeeContract, getFeeRecipientContract, getUnwrapFeeContract, getWrapFeeContract, getWrapperContract, indexedDBStorage, isConfidentialTokenContract, isConfidentialWrapperContract, isFinalizeUnwrapOperatorContract, isOperatorContract, loadPendingUnshield, matchZamaError, nameContract, parseActivityFeed, rateContract, savePendingUnshield, setFinalizeUnwrapOperatorContract, setOperatorContract, sortByBlockNumber, supportsInterfaceContract, symbolContract, totalSupplyContract, underlyingContract, unwrapContract, unwrapFromBalanceContract, wrapContract, wrapETHContract, wrapperExistsContract } from '@zama-fhe/sdk';
4
+ import { invalidateWalletLifecycleQueries, encryptMutationOptions, zamaQueryKeys, hashFn, publicKeyQueryOptions, publicParamsQueryOptions, signerAddressQueryOptions, confidentialHandleQueryOptions, confidentialBalanceQueryOptions, confidentialHandlesQueryOptions, confidentialBalancesQueryOptions, allowMutationOptions, isAllowedQueryOptions, revokeMutationOptions, revokeSessionMutationOptions, invalidateAfterTransfer, confidentialTransferMutationOptions, confidentialTransferFromMutationOptions, invalidateAfterApprove, confidentialApproveMutationOptions, confidentialIsApprovedQueryOptions, invalidateAfterShield, shieldMutationOptions, shieldETHMutationOptions, invalidateAfterUnwrap, unwrapMutationOptions, unwrapAllMutationOptions, invalidateAfterUnshield, finalizeUnwrapMutationOptions, unshieldMutationOptions, unshieldAllMutationOptions, resumeUnshieldMutationOptions, underlyingAllowanceQueryOptions, wrapperDiscoveryQueryOptions, tokenMetadataQueryOptions, activityFeedQueryOptions, invalidateAfterApproveUnderlying, approveUnderlyingMutationOptions, isConfidentialQueryOptions, isWrapperQueryOptions, totalSupplyQueryOptions, shieldFeeQueryOptions, unshieldFeeQueryOptions, batchTransferFeeQueryOptions, feeRecipientQueryOptions } from '@zama-fhe/sdk/query';
5
+ export { activityFeedQueryOptions, allowMutationOptions, approveUnderlyingMutationOptions, batchTransferFeeQueryOptions, confidentialApproveMutationOptions, confidentialBalanceQueryOptions, confidentialBalancesQueryOptions, confidentialHandleQueryOptions, confidentialHandlesQueryOptions, confidentialIsApprovedQueryOptions, confidentialTransferFromMutationOptions, confidentialTransferMutationOptions, encryptMutationOptions, feeRecipientQueryOptions, filterQueryOptions, finalizeUnwrapMutationOptions, hashFn, isAllowedQueryOptions, isConfidentialQueryOptions, isWrapperQueryOptions, publicKeyQueryOptions, publicParamsQueryOptions, resumeUnshieldMutationOptions, revokeMutationOptions, revokeSessionMutationOptions, shieldETHMutationOptions, shieldFeeQueryOptions, shieldMutationOptions, signerAddressQueryOptions, tokenMetadataQueryOptions, totalSupplyQueryOptions, underlyingAllowanceQueryOptions, unshieldAllMutationOptions, unshieldFeeQueryOptions, unshieldMutationOptions, unwrapAllMutationOptions, unwrapMutationOptions, wrapperDiscoveryQueryOptions, zamaQueryKeys } from '@zama-fhe/sdk/query';
6
+ import { useQueryClient, useMutation, useQuery, useQueries, skipToken, useSuspenseQuery as useSuspenseQuery$1 } from '@tanstack/react-query';
4
7
  import { createContext, useRef, useEffect, useMemo, useContext } from 'react';
5
8
  import { jsx } from 'react/jsx-runtime';
6
- import { useMutation, useQueryClient, useQuery, useQueries, skipToken, useSuspenseQuery } from '@tanstack/react-query';
7
9
 
8
10
  // src/provider.tsx
9
11
  var ZamaSDKContext = createContext(null);
@@ -17,10 +19,19 @@ function ZamaProvider({
17
19
  sessionTTL,
18
20
  onEvent
19
21
  }) {
22
+ const queryClient = useQueryClient();
20
23
  const onEventRef = useRef(onEvent);
21
24
  useEffect(() => {
22
25
  onEventRef.current = onEvent;
23
26
  });
27
+ const signerLifecycleCallbacks = useMemo(
28
+ () => signer?.subscribe ? {
29
+ onDisconnect: () => invalidateWalletLifecycleQueries(queryClient),
30
+ onAccountChange: () => invalidateWalletLifecycleQueries(queryClient),
31
+ onChainChange: () => invalidateWalletLifecycleQueries(queryClient)
32
+ } : void 0,
33
+ [queryClient, signer]
34
+ );
24
35
  const sdk = useMemo(
25
36
  () => new ZamaSDK({
26
37
  relayer,
@@ -29,9 +40,10 @@ function ZamaProvider({
29
40
  sessionStorage,
30
41
  keypairTTL,
31
42
  sessionTTL,
32
- onEvent: onEventRef.current
43
+ onEvent: onEventRef.current,
44
+ signerLifecycleCallbacks
33
45
  }),
34
- [relayer, signer, storage, sessionStorage, keypairTTL, sessionTTL]
46
+ [relayer, signer, storage, sessionStorage, keypairTTL, sessionTTL, signerLifecycleCallbacks]
35
47
  );
36
48
  useEffect(() => () => sdk.dispose(), [sdk]);
37
49
  return /* @__PURE__ */ jsx(ZamaSDKContext.Provider, { value: sdk, children });
@@ -45,20 +57,12 @@ function useZamaSDK() {
45
57
  }
46
58
  return context;
47
59
  }
48
- function encryptMutationOptions(sdk) {
49
- return {
50
- mutationKey: ["encrypt"],
51
- mutationFn: (params) => sdk.relayer.encrypt(params)
52
- };
53
- }
54
60
  function useEncrypt() {
55
61
  const sdk = useZamaSDK();
56
62
  return useMutation(encryptMutationOptions(sdk));
57
63
  }
58
-
59
- // src/relayer/decryption-cache.ts
60
64
  var decryptionKeys = {
61
- value: (handle) => ["decryptedValue", handle]
65
+ value: (handle, contractAddress) => zamaQueryKeys.decryption.handle(handle, contractAddress)
62
66
  };
63
67
 
64
68
  // src/relayer/use-user-decrypt.ts
@@ -128,37 +132,19 @@ function useRequestZKProofVerification() {
128
132
  mutationFn: (zkProof) => sdk.relayer.requestZKProofVerification(zkProof)
129
133
  });
130
134
  }
131
- var publicKeyQueryKeys = {
132
- /** Match the public key query. */
133
- all: ["publicKey"]
134
- };
135
- function publicKeyQueryOptions(sdk) {
136
- return {
137
- queryKey: publicKeyQueryKeys.all,
138
- queryFn: () => sdk.relayer.getPublicKey(),
139
- staleTime: Infinity
140
- };
141
- }
142
135
  function usePublicKey() {
143
136
  const sdk = useZamaSDK();
144
- return useQuery(publicKeyQueryOptions(sdk));
145
- }
146
- var publicParamsQueryKeys = {
147
- /** Match all public params queries. */
148
- all: ["publicParams"],
149
- /** Match public params query for a specific bit size. */
150
- bits: (bits) => ["publicParams", bits]
151
- };
152
- function publicParamsQueryOptions(sdk, bits) {
153
- return {
154
- queryKey: publicParamsQueryKeys.bits(bits),
155
- queryFn: () => sdk.relayer.getPublicParams(bits),
156
- staleTime: Infinity
157
- };
137
+ return useQuery({
138
+ ...publicKeyQueryOptions(sdk),
139
+ queryKeyHashFn: hashFn
140
+ });
158
141
  }
159
142
  function usePublicParams(bits) {
160
143
  const sdk = useZamaSDK();
161
- return useQuery(publicParamsQueryOptions(sdk, bits));
144
+ return useQuery({
145
+ ...publicParamsQueryOptions(sdk, bits),
146
+ queryKeyHashFn: hashFn
147
+ });
162
148
  }
163
149
  function useUserDecryptFlow(config) {
164
150
  const sdk = useZamaSDK();
@@ -202,8 +188,9 @@ function useUserDecryptFlow(config) {
202
188
  });
203
189
  Object.assign(allResults, result);
204
190
  }
205
- callbacks?.onDecrypted?.(allResults);
206
- return allResults;
191
+ const results = allResults;
192
+ callbacks?.onDecrypted?.(results);
193
+ return results;
207
194
  },
208
195
  onSuccess: (data) => {
209
196
  for (const [handle, value] of Object.entries(data)) {
@@ -214,7 +201,8 @@ function useUserDecryptFlow(config) {
214
201
  }
215
202
  function useUserDecryptedValue(handle) {
216
203
  return useQuery({
217
- queryKey: decryptionKeys.value(handle ?? ""),
204
+ queryKey: decryptionKeys.value(handle ?? "0x"),
205
+ queryKeyHashFn: hashFn,
218
206
  queryFn: () => void 0,
219
207
  enabled: false
220
208
  });
@@ -223,6 +211,7 @@ function useUserDecryptedValues(handles) {
223
211
  const results = useQueries({
224
212
  queries: handles.map((handle) => ({
225
213
  queryKey: decryptionKeys.value(handle),
214
+ queryKeyHashFn: hashFn,
226
215
  queryFn: () => void 0,
227
216
  enabled: false
228
217
  }))
@@ -247,659 +236,429 @@ function useReadonlyToken(address) {
247
236
  const sdk = useZamaSDK();
248
237
  return useMemo(() => sdk.createReadonlyToken(address), [sdk, address]);
249
238
  }
250
-
251
- // src/token/balance-query-keys.ts
252
- var confidentialBalanceQueryKeys = {
253
- /** Match all single-token balance queries. */
254
- all: ["confidentialBalance"],
255
- /** Match balance queries for a specific token (any owner). */
256
- token: (tokenAddress) => ["confidentialBalance", tokenAddress],
257
- /** Match balance query for a specific token + owner. */
258
- owner: (tokenAddress, owner) => ["confidentialBalance", tokenAddress, owner]
259
- };
260
- var confidentialBalancesQueryKeys = {
261
- /** Match all batch balance queries. */
262
- all: ["confidentialBalances"],
263
- /** Match batch balance query for a specific token set + owner. */
264
- tokens: (tokenAddresses, owner) => ["confidentialBalances", tokenAddresses, owner]
265
- };
266
- var confidentialHandleQueryKeys = {
267
- /** Match all single-token handle queries. */
268
- all: ["confidentialHandle"],
269
- /** Match handle queries for a specific token (any owner). */
270
- token: (tokenAddress) => ["confidentialHandle", tokenAddress],
271
- /** Match handle query for a specific token + owner. */
272
- owner: (tokenAddress, owner) => ["confidentialHandle", tokenAddress, owner]
273
- };
274
- var confidentialHandlesQueryKeys = {
275
- /** Match all batch handle queries. */
276
- all: ["confidentialHandles"],
277
- /** Match batch handle query for a specific token set + owner. */
278
- tokens: (tokenAddresses, owner) => ["confidentialHandles", tokenAddresses, owner]
279
- };
280
- var wagmiBalancePredicates = {
281
- /** Match all wagmi balance queries. */
282
- balanceOf: (query) => Array.isArray(query.queryKey) && query.queryKey.some(
283
- (key) => typeof key === "object" && key !== null && "functionName" in key && key.functionName === "balanceOf"
284
- )};
285
-
286
- // src/token/use-confidential-balance.ts
287
- var DEFAULT_HANDLE_REFETCH_INTERVAL = 1e4;
239
+ function useQuery4(options) {
240
+ return useQuery({
241
+ ...options,
242
+ queryKeyHashFn: hashFn
243
+ });
244
+ }
245
+ function useSuspenseQuery(options) {
246
+ return useSuspenseQuery$1({
247
+ ...options,
248
+ queryKeyHashFn: hashFn
249
+ });
250
+ }
288
251
  function useConfidentialBalance(config, options) {
289
252
  const { tokenAddress, handleRefetchInterval } = config;
253
+ const userEnabled = options?.enabled;
290
254
  const token = useReadonlyToken(tokenAddress);
291
- const addressQuery = useQuery({
292
- queryKey: ["zama", "signer-address", tokenAddress],
293
- queryFn: () => token.signer.getAddress()
294
- });
295
- const signerAddress = addressQuery.data;
296
- const ownerKey = signerAddress ?? "";
297
- const handleQuery = useQuery({
298
- queryKey: confidentialHandleQueryKeys.owner(tokenAddress, ownerKey),
299
- queryFn: () => token.confidentialBalanceOf(),
300
- enabled: !!signerAddress,
301
- refetchInterval: handleRefetchInterval ?? DEFAULT_HANDLE_REFETCH_INTERVAL
255
+ const addressQuery = useQuery4({
256
+ ...signerAddressQueryOptions(token.signer)
257
+ });
258
+ const owner = addressQuery.data;
259
+ const baseHandleQueryOptions = confidentialHandleQueryOptions(token.signer, tokenAddress, {
260
+ owner,
261
+ pollingInterval: handleRefetchInterval
262
+ });
263
+ const handleFactoryEnabled = baseHandleQueryOptions.enabled ?? true;
264
+ const handleQuery = useQuery4({
265
+ ...baseHandleQueryOptions,
266
+ enabled: handleFactoryEnabled && (userEnabled ?? true)
302
267
  });
303
268
  const handle = handleQuery.data;
304
- const balanceQuery = useQuery({
305
- queryKey: [...confidentialBalanceQueryKeys.owner(tokenAddress, ownerKey), handle ?? ""],
306
- queryFn: () => token.decryptBalance(handle),
307
- enabled: !!signerAddress && !!handle,
308
- staleTime: Infinity,
309
- ...options
269
+ const baseBalanceQueryOptions = confidentialBalanceQueryOptions(token, {
270
+ handle,
271
+ owner
272
+ });
273
+ const factoryEnabled = baseBalanceQueryOptions.enabled ?? true;
274
+ const balanceQuery = useQuery4({
275
+ ...baseBalanceQueryOptions,
276
+ ...options,
277
+ enabled: factoryEnabled && (userEnabled ?? true)
310
278
  });
311
279
  return { ...balanceQuery, handleQuery };
312
280
  }
313
- var DEFAULT_HANDLE_REFETCH_INTERVAL2 = 1e4;
314
281
  function useConfidentialBalances(config, options) {
315
282
  const { tokenAddresses, handleRefetchInterval, maxConcurrency } = config;
283
+ const userEnabled = options?.enabled;
316
284
  const sdk = useZamaSDK();
317
- const addressQuery = useQuery({
318
- queryKey: ["zama", "signer-address"],
319
- queryFn: () => sdk.signer.getAddress()
285
+ const addressQuery = useQuery4({
286
+ ...signerAddressQueryOptions(sdk.signer)
320
287
  });
321
- const signerAddress = addressQuery.data;
322
- const ownerKey = signerAddress ?? "";
288
+ const owner = addressQuery.data;
323
289
  const tokens = useMemo(
324
290
  () => tokenAddresses.map((addr) => sdk.createReadonlyToken(addr)),
325
291
  [sdk, tokenAddresses]
326
292
  );
327
- const handlesQuery = useQuery({
328
- queryKey: confidentialHandlesQueryKeys.tokens(tokenAddresses, ownerKey),
329
- queryFn: () => Promise.all(tokens.map((t) => t.confidentialBalanceOf())),
330
- enabled: tokenAddresses.length > 0 && !!signerAddress,
331
- refetchInterval: handleRefetchInterval ?? DEFAULT_HANDLE_REFETCH_INTERVAL2
293
+ const baseHandlesQueryOptions = confidentialHandlesQueryOptions(sdk.signer, tokenAddresses, {
294
+ owner,
295
+ pollingInterval: handleRefetchInterval
296
+ });
297
+ const handlesFactoryEnabled = baseHandlesQueryOptions.enabled ?? true;
298
+ const handlesQuery = useQuery4({
299
+ ...baseHandlesQueryOptions,
300
+ enabled: handlesFactoryEnabled && (userEnabled ?? true)
332
301
  });
333
302
  const handles = handlesQuery.data;
334
- const handlesKey = handles?.join(",") ?? "";
335
- const balancesQuery = useQuery({
336
- queryKey: [...confidentialBalancesQueryKeys.tokens(tokenAddresses, ownerKey), handlesKey],
337
- queryFn: async () => {
338
- const perTokenErrors = /* @__PURE__ */ new Map();
339
- const raw = await ReadonlyToken.batchDecryptBalances(tokens, {
340
- handles,
341
- maxConcurrency,
342
- onError: (error, address) => {
343
- perTokenErrors.set(address, error);
344
- return 0n;
345
- }
346
- });
347
- const balances = /* @__PURE__ */ new Map();
348
- const errors = /* @__PURE__ */ new Map();
349
- for (let i = 0; i < tokens.length; i++) {
350
- const tokenAddr = tokens[i].address;
351
- const originalAddr = tokenAddresses[i];
352
- const tokenError = perTokenErrors.get(tokenAddr);
353
- if (tokenError) {
354
- errors.set(originalAddr, tokenError);
355
- } else {
356
- const balance = raw.get(tokenAddr);
357
- if (balance !== void 0) balances.set(originalAddr, balance);
358
- }
359
- }
360
- if (errors.size === tokens.length && tokens.length > 0) {
361
- throw errors.values().next().value ?? new DecryptionFailedError("All token balance decryptions failed");
362
- }
363
- return {
364
- balances,
365
- errors,
366
- isPartialError: errors.size > 0
367
- };
368
- },
369
- enabled: tokenAddresses.length > 0 && !!signerAddress && !!handles,
370
- staleTime: Infinity,
371
- ...options
303
+ const handlesReady = Array.isArray(handles) && handles.length === tokenAddresses.length;
304
+ const baseBalancesQueryOptions = confidentialBalancesQueryOptions(tokens, {
305
+ owner,
306
+ handles,
307
+ maxConcurrency,
308
+ resultAddresses: tokenAddresses
309
+ });
310
+ const factoryEnabled = baseBalancesQueryOptions.enabled ?? true;
311
+ const balancesQuery = useQuery4({
312
+ ...baseBalancesQueryOptions,
313
+ ...options,
314
+ enabled: factoryEnabled && handlesReady && (userEnabled ?? true)
372
315
  });
373
316
  return { ...balancesQuery, handlesQuery };
374
317
  }
375
- var isAllowedQueryKeys = {
376
- all: ["zama", "isAllowed"]
377
- };
378
- function isAllowedQueryOptions(sdk) {
379
- return {
380
- queryKey: isAllowedQueryKeys.all,
381
- queryFn: () => sdk.isAllowed()
382
- };
383
- }
384
- function useIsAllowed() {
318
+ function useAllow(options) {
385
319
  const sdk = useZamaSDK();
386
- return useQuery(isAllowedQueryOptions(sdk));
387
- }
388
-
389
- // src/token/use-allow.ts
390
- function allowMutationOptions(sdk) {
391
- return {
392
- mutationKey: ["allow"],
393
- mutationFn: async (tokenAddresses) => {
394
- await sdk.allow(...tokenAddresses);
395
- }
396
- };
397
- }
398
- function useAllow() {
399
- const sdk = useZamaSDK();
400
- const queryClient = useQueryClient();
401
320
  return useMutation({
402
321
  ...allowMutationOptions(sdk),
403
- onSuccess: () => {
404
- queryClient.invalidateQueries({ queryKey: isAllowedQueryKeys.all });
322
+ ...options,
323
+ onSuccess: (data, variables, onMutateResult, context) => {
324
+ options?.onSuccess?.(data, variables, onMutateResult, context);
325
+ context.client.invalidateQueries({ queryKey: zamaQueryKeys.isAllowed.all });
405
326
  }
406
327
  });
407
328
  }
408
- function revokeMutationOptions(sdk) {
409
- return {
410
- mutationKey: ["revoke"],
411
- mutationFn: async (tokenAddresses) => {
412
- await sdk.revoke(...tokenAddresses);
413
- }
329
+ function useIsAllowed() {
330
+ const sdk = useZamaSDK();
331
+ const addressQuery = useQuery4({
332
+ ...signerAddressQueryOptions(sdk.signer)
333
+ });
334
+ const account = addressQuery.data;
335
+ const baseOpts = account ? isAllowedQueryOptions(sdk, { account }) : {
336
+ queryKey: zamaQueryKeys.isAllowed.all,
337
+ queryFn: skipToken
414
338
  };
339
+ const factoryEnabled = "enabled" in baseOpts ? baseOpts.enabled ?? true : true;
340
+ return useQuery4({
341
+ ...baseOpts,
342
+ enabled: factoryEnabled
343
+ });
415
344
  }
416
- function useRevoke() {
345
+ function useRevoke(options) {
417
346
  const sdk = useZamaSDK();
418
- const queryClient = useQueryClient();
419
347
  return useMutation({
420
348
  ...revokeMutationOptions(sdk),
421
- onSuccess: () => {
422
- queryClient.invalidateQueries({ queryKey: isAllowedQueryKeys.all });
349
+ ...options,
350
+ onSuccess: (data, variables, onMutateResult, context) => {
351
+ options?.onSuccess?.(data, variables, onMutateResult, context);
352
+ context.client.invalidateQueries({ queryKey: zamaQueryKeys.isAllowed.all });
423
353
  }
424
354
  });
425
355
  }
426
- function revokeSessionMutationOptions(sdk) {
427
- return {
428
- mutationKey: ["revokeSession"],
429
- mutationFn: async () => {
430
- await sdk.revokeSession();
431
- }
432
- };
433
- }
434
- function useRevokeSession() {
356
+ function useRevokeSession(options) {
435
357
  const sdk = useZamaSDK();
436
- const queryClient = useQueryClient();
437
358
  return useMutation({
438
359
  ...revokeSessionMutationOptions(sdk),
439
- onSuccess: () => {
440
- queryClient.invalidateQueries({ queryKey: isAllowedQueryKeys.all });
360
+ ...options,
361
+ onSuccess: (data, variables, onMutateResult, context) => {
362
+ options?.onSuccess?.(data, variables, onMutateResult, context);
363
+ context.client.invalidateQueries({ queryKey: zamaQueryKeys.isAllowed.all });
441
364
  }
442
365
  });
443
366
  }
444
- function confidentialTransferMutationOptions(token) {
445
- return {
446
- mutationKey: ["confidentialTransfer", token.address],
447
- mutationFn: ({ to, amount, callbacks }) => token.confidentialTransfer(to, amount, callbacks)
448
- };
367
+ function unwrapOptimisticCallerContext(optimistic, rawContext) {
368
+ const wrappedContext = optimistic ? rawContext : void 0;
369
+ const callerContext = optimistic ? wrappedContext?.callerContext : rawContext;
370
+ return { wrappedContext, callerContext };
371
+ }
372
+ async function applyOptimisticBalanceDelta(queryClient, tokenAddress, amount, mode) {
373
+ const balanceKey = zamaQueryKeys.confidentialBalance.token(tokenAddress);
374
+ await queryClient.cancelQueries({ queryKey: balanceKey });
375
+ const previous = queryClient.getQueriesData({ queryKey: balanceKey });
376
+ for (const [key, value] of previous) {
377
+ if (value === void 0) continue;
378
+ queryClient.setQueryData(key, mode === "add" ? value + amount : value - amount);
379
+ }
380
+ return previous;
449
381
  }
382
+ function rollbackOptimisticBalanceDelta(queryClient, snapshot) {
383
+ for (const [key, value] of snapshot) {
384
+ queryClient.setQueryData(key, value);
385
+ }
386
+ }
387
+
388
+ // src/token/use-confidential-transfer.ts
450
389
  function useConfidentialTransfer(config, options) {
451
390
  const token = useToken(config);
452
391
  const queryClient = useQueryClient();
453
- return useMutation({
454
- mutationKey: ["confidentialTransfer", config.tokenAddress],
455
- mutationFn: ({ to, amount, callbacks }) => token.confidentialTransfer(to, amount, callbacks),
456
- ...options,
457
- onMutate: config.optimistic ? async (variables, mutationContext) => {
458
- const balanceKey = confidentialBalanceQueryKeys.token(config.tokenAddress);
459
- await queryClient.cancelQueries({ queryKey: balanceKey });
460
- const previous = queryClient.getQueriesData({ queryKey: balanceKey });
461
- for (const [key, value] of previous) {
462
- if (value !== void 0) {
463
- queryClient.setQueryData(key, value - variables.amount);
392
+ return useMutation(
393
+ {
394
+ ...confidentialTransferMutationOptions(token),
395
+ ...options,
396
+ onMutate: config.optimistic ? async (variables, mutationContext) => {
397
+ const snapshot = await applyOptimisticBalanceDelta(
398
+ queryClient,
399
+ config.tokenAddress,
400
+ variables.amount,
401
+ "subtract"
402
+ );
403
+ const callerContext = await options?.onMutate?.(variables, mutationContext);
404
+ return { snapshot, callerContext };
405
+ } : options?.onMutate,
406
+ onError: (error, variables, rawContext, context) => {
407
+ const { wrappedContext, callerContext } = unwrapOptimisticCallerContext(
408
+ config.optimistic,
409
+ rawContext
410
+ );
411
+ if (wrappedContext) {
412
+ rollbackOptimisticBalanceDelta(queryClient, wrappedContext.snapshot);
464
413
  }
414
+ options?.onError?.(
415
+ error,
416
+ variables,
417
+ callerContext,
418
+ context
419
+ );
420
+ },
421
+ onSuccess: (data, variables, rawContext, context) => {
422
+ const { callerContext } = unwrapOptimisticCallerContext(config.optimistic, rawContext);
423
+ options?.onSuccess?.(data, variables, callerContext, context);
424
+ invalidateAfterTransfer(context.client, config.tokenAddress);
425
+ },
426
+ onSettled: (data, error, variables, rawContext, context) => {
427
+ const { callerContext } = unwrapOptimisticCallerContext(config.optimistic, rawContext);
428
+ options?.onSettled?.(
429
+ data,
430
+ error,
431
+ variables,
432
+ callerContext,
433
+ context
434
+ );
465
435
  }
466
- return options?.onMutate?.(variables, mutationContext) ?? config.tokenAddress;
467
- } : options?.onMutate,
468
- onError: (error, variables, onMutateResult, context) => {
469
- if (config.optimistic) {
470
- queryClient.invalidateQueries({
471
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
472
- });
473
- }
474
- options?.onError?.(error, variables, onMutateResult, context);
475
- },
436
+ }
437
+ );
438
+ }
439
+ function useConfidentialTransferFrom(config, options) {
440
+ const token = useToken(config);
441
+ return useMutation({
442
+ ...confidentialTransferFromMutationOptions(token),
443
+ ...options,
476
444
  onSuccess: (data, variables, onMutateResult, context) => {
477
- context.client.invalidateQueries({
478
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
479
- });
480
- context.client.invalidateQueries({
481
- queryKey: confidentialHandlesQueryKeys.all
482
- });
483
- context.client.resetQueries({
484
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
485
- });
486
- context.client.invalidateQueries({
487
- queryKey: confidentialBalancesQueryKeys.all
488
- });
489
445
  options?.onSuccess?.(data, variables, onMutateResult, context);
446
+ invalidateAfterTransfer(context.client, config.tokenAddress);
490
447
  }
491
448
  });
492
449
  }
493
- function confidentialTransferFromMutationOptions(token) {
494
- return {
495
- mutationKey: ["confidentialTransferFrom", token.address],
496
- mutationFn: ({ from, to, amount, callbacks }) => token.confidentialTransferFrom(from, to, amount, callbacks)
497
- };
498
- }
499
- function useConfidentialTransferFrom(config, options) {
450
+ function useConfidentialApprove(config, options) {
500
451
  const token = useToken(config);
501
452
  return useMutation({
502
- mutationKey: ["confidentialTransferFrom", config.tokenAddress],
503
- mutationFn: ({ from, to, amount, callbacks }) => token.confidentialTransferFrom(from, to, amount, callbacks),
453
+ ...confidentialApproveMutationOptions(token),
504
454
  ...options,
505
455
  onSuccess: (data, variables, onMutateResult, context) => {
506
- context.client.invalidateQueries({
507
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
508
- });
509
- context.client.invalidateQueries({
510
- queryKey: confidentialHandlesQueryKeys.all
511
- });
512
- context.client.resetQueries({
513
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
514
- });
515
- context.client.invalidateQueries({
516
- queryKey: confidentialBalancesQueryKeys.all
517
- });
518
456
  options?.onSuccess?.(data, variables, onMutateResult, context);
457
+ invalidateAfterApprove(context.client, config.tokenAddress);
519
458
  }
520
459
  });
521
460
  }
522
- var confidentialIsApprovedQueryKeys = {
523
- /** Match all approval queries. */
524
- all: ["confidentialIsApproved"],
525
- /** Match approval queries for a specific token. */
526
- token: (tokenAddress) => ["confidentialIsApproved", tokenAddress],
527
- /** Match approval queries for a specific token + spender pair. */
528
- spender: (tokenAddress, spender, holder) => ["confidentialIsApproved", tokenAddress, spender, holder ?? ""]
529
- };
530
- function confidentialIsApprovedQueryOptions(token, spender, holder) {
531
- return {
532
- queryKey: confidentialIsApprovedQueryKeys.spender(token.address, spender, holder),
533
- queryFn: () => token.isApproved(spender, holder),
534
- staleTime: 3e4
535
- };
536
- }
537
461
  function useConfidentialIsApproved(config, options) {
538
462
  const { spender, holder, ...tokenConfig } = config;
463
+ const userEnabled = options?.enabled;
539
464
  const token = useToken(tokenConfig);
540
- return useQuery({
541
- ...spender ? confidentialIsApprovedQueryOptions(token, spender, holder) : {
542
- queryKey: confidentialIsApprovedQueryKeys.spender(config.tokenAddress, "", holder),
543
- queryFn: skipToken
544
- },
545
- ...options
465
+ const holderQuery = useQuery4({
466
+ ...signerAddressQueryOptions(token.signer),
467
+ enabled: holder === void 0
468
+ });
469
+ const resolvedHolder = holder ?? holderQuery.data;
470
+ const baseOpts = spender && resolvedHolder ? confidentialIsApprovedQueryOptions(token.signer, token.address, {
471
+ holder: resolvedHolder,
472
+ spender
473
+ }) : {
474
+ queryKey: zamaQueryKeys.confidentialIsApproved.token(config.tokenAddress),
475
+ queryFn: skipToken
476
+ };
477
+ const factoryEnabled = "enabled" in baseOpts ? baseOpts.enabled ?? true : true;
478
+ return useQuery4({
479
+ ...baseOpts,
480
+ ...options,
481
+ enabled: factoryEnabled && (userEnabled ?? true)
546
482
  });
547
483
  }
548
484
  function useConfidentialIsApprovedSuspense(config) {
549
485
  const { spender, holder, ...tokenConfig } = config;
550
486
  const token = useToken(tokenConfig);
551
- return useSuspenseQuery(
552
- confidentialIsApprovedQueryOptions(token, spender, holder)
553
- );
554
- }
555
-
556
- // src/token/use-confidential-approve.ts
557
- function confidentialApproveMutationOptions(token) {
558
- return {
559
- mutationKey: ["confidentialApprove", token.address],
560
- mutationFn: ({ spender, until }) => token.approve(spender, until)
561
- };
562
- }
563
- function useConfidentialApprove(config, options) {
564
- const token = useToken(config);
565
- return useMutation({
566
- mutationKey: ["confidentialApprove", config.tokenAddress],
567
- mutationFn: ({ spender, until }) => token.approve(spender, until),
568
- ...options,
569
- onSuccess: (data, variables, onMutateResult, context) => {
570
- context.client.invalidateQueries({
571
- queryKey: confidentialIsApprovedQueryKeys.token(config.tokenAddress)
572
- });
573
- options?.onSuccess?.(data, variables, onMutateResult, context);
574
- }
487
+ const addressQuery = useSuspenseQuery({
488
+ ...signerAddressQueryOptions(token.signer)
489
+ });
490
+ const resolvedHolder = holder ?? addressQuery.data;
491
+ return useSuspenseQuery({
492
+ ...confidentialIsApprovedQueryOptions(token.signer, token.address, {
493
+ holder: resolvedHolder,
494
+ spender
495
+ })
575
496
  });
576
- }
577
- function shieldMutationOptions(token) {
578
- return {
579
- mutationKey: ["shield", token.address],
580
- mutationFn: async ({ amount, fees, approvalStrategy, to, callbacks }) => token.shield(amount, { fees, approvalStrategy, to, callbacks })
581
- };
582
497
  }
583
498
  function useShield(config, options) {
584
499
  const token = useToken(config);
585
500
  const queryClient = useQueryClient();
586
501
  return useMutation({
587
- mutationKey: ["shield", config.tokenAddress],
588
- mutationFn: async ({ amount, fees, approvalStrategy, to, callbacks }) => token.shield(amount, { fees, approvalStrategy, to, callbacks }),
502
+ ...shieldMutationOptions(token),
589
503
  ...options,
590
504
  onMutate: config.optimistic ? async (variables, mutationContext) => {
591
- const balanceKey = confidentialBalanceQueryKeys.token(config.tokenAddress);
592
- await queryClient.cancelQueries({ queryKey: balanceKey });
593
- const previous = queryClient.getQueriesData({ queryKey: balanceKey });
594
- for (const [key, value] of previous) {
595
- if (value !== void 0) {
596
- queryClient.setQueryData(key, value + variables.amount);
597
- }
598
- }
599
- return options?.onMutate?.(variables, mutationContext) ?? config.tokenAddress;
505
+ const snapshot = await applyOptimisticBalanceDelta(
506
+ queryClient,
507
+ config.tokenAddress,
508
+ variables.amount,
509
+ "add"
510
+ );
511
+ const callerContext = await options?.onMutate?.(variables, mutationContext);
512
+ return { snapshot, callerContext };
600
513
  } : options?.onMutate,
601
- onError: (error, variables, onMutateResult, context) => {
602
- if (config.optimistic) {
603
- queryClient.invalidateQueries({
604
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
605
- });
514
+ onError: (error, variables, rawContext, context) => {
515
+ const { wrappedContext, callerContext } = unwrapOptimisticCallerContext(
516
+ config.optimistic,
517
+ rawContext
518
+ );
519
+ if (wrappedContext) {
520
+ rollbackOptimisticBalanceDelta(queryClient, wrappedContext.snapshot);
606
521
  }
607
- options?.onError?.(error, variables, onMutateResult, context);
522
+ options?.onError?.(
523
+ error,
524
+ variables,
525
+ callerContext,
526
+ context
527
+ );
608
528
  },
609
- onSuccess: (data, variables, onMutateResult, context) => {
610
- context.client.invalidateQueries({
611
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
612
- });
613
- context.client.invalidateQueries({
614
- queryKey: confidentialHandlesQueryKeys.all
615
- });
616
- context.client.resetQueries({
617
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
618
- });
619
- context.client.invalidateQueries({
620
- queryKey: confidentialBalancesQueryKeys.all
621
- });
622
- context.client.invalidateQueries({
623
- predicate: wagmiBalancePredicates.balanceOf
624
- });
625
- options?.onSuccess?.(data, variables, onMutateResult, context);
529
+ onSuccess: (data, variables, rawContext, context) => {
530
+ const { callerContext } = unwrapOptimisticCallerContext(config.optimistic, rawContext);
531
+ options?.onSuccess?.(data, variables, callerContext, context);
532
+ invalidateAfterShield(context.client, config.tokenAddress);
533
+ },
534
+ onSettled: (data, error, variables, rawContext, context) => {
535
+ const { callerContext } = unwrapOptimisticCallerContext(config.optimistic, rawContext);
536
+ options?.onSettled?.(
537
+ data,
538
+ error,
539
+ variables,
540
+ callerContext,
541
+ context
542
+ );
626
543
  }
627
544
  });
628
545
  }
629
- function shieldETHMutationOptions(token) {
630
- return {
631
- mutationKey: ["shieldETH", token.address],
632
- mutationFn: ({ amount, value }) => token.shieldETH(amount, value)
633
- };
634
- }
635
546
  function useShieldETH(config, options) {
636
547
  const token = useToken(config);
637
548
  return useMutation({
638
- mutationKey: ["shieldETH", config.tokenAddress],
639
- mutationFn: ({ amount, value }) => token.shieldETH(amount, value),
549
+ ...shieldETHMutationOptions(token),
640
550
  ...options,
641
551
  onSuccess: (data, variables, onMutateResult, context) => {
642
- context.client.invalidateQueries({
643
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
644
- });
645
- context.client.invalidateQueries({
646
- queryKey: confidentialHandlesQueryKeys.all
647
- });
648
- context.client.resetQueries({
649
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
650
- });
651
- context.client.invalidateQueries({
652
- queryKey: confidentialBalancesQueryKeys.all
653
- });
654
552
  options?.onSuccess?.(data, variables, onMutateResult, context);
553
+ invalidateAfterShield(context.client, config.tokenAddress);
655
554
  }
656
555
  });
657
556
  }
658
- function unwrapMutationOptions(token) {
659
- return {
660
- mutationKey: ["unwrap", token.address],
661
- mutationFn: ({ amount }) => token.unwrap(amount)
662
- };
663
- }
664
557
  function useUnwrap(config, options) {
665
558
  const token = useToken(config);
666
559
  return useMutation({
667
- mutationKey: ["unwrap", config.tokenAddress],
668
- mutationFn: ({ amount }) => token.unwrap(amount),
560
+ ...unwrapMutationOptions(token),
669
561
  ...options,
670
562
  onSuccess: (data, variables, onMutateResult, context) => {
671
- context.client.invalidateQueries({
672
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
673
- });
674
- context.client.invalidateQueries({
675
- queryKey: confidentialHandlesQueryKeys.all
676
- });
677
- context.client.resetQueries({
678
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
679
- });
680
- context.client.invalidateQueries({
681
- queryKey: confidentialBalancesQueryKeys.all
682
- });
683
563
  options?.onSuccess?.(data, variables, onMutateResult, context);
564
+ invalidateAfterUnwrap(context.client, config.tokenAddress);
684
565
  }
685
566
  });
686
567
  }
687
- function unwrapAllMutationOptions(token) {
688
- return {
689
- mutationKey: ["unwrapAll", token.address],
690
- mutationFn: () => token.unwrapAll()
691
- };
692
- }
693
568
  function useUnwrapAll(config, options) {
694
569
  const token = useToken(config);
695
570
  return useMutation({
696
- mutationKey: ["unwrapAll", config.tokenAddress],
697
- mutationFn: () => token.unwrapAll(),
571
+ ...unwrapAllMutationOptions(token),
698
572
  ...options,
699
573
  onSuccess: (data, variables, onMutateResult, context) => {
700
- context.client.invalidateQueries({
701
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
702
- });
703
- context.client.invalidateQueries({
704
- queryKey: confidentialHandlesQueryKeys.all
705
- });
706
- context.client.resetQueries({
707
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
708
- });
709
- context.client.invalidateQueries({
710
- queryKey: confidentialBalancesQueryKeys.all
711
- });
712
574
  options?.onSuccess?.(data, variables, onMutateResult, context);
575
+ invalidateAfterUnwrap(context.client, config.tokenAddress);
713
576
  }
714
577
  });
715
578
  }
716
- var underlyingAllowanceQueryKeys = {
717
- /** Match all underlying allowance queries. */
718
- all: ["underlyingAllowance"],
719
- /** Match allowance query for a specific token + wrapper pair. */
720
- token: (tokenAddress, wrapper) => ["underlyingAllowance", tokenAddress, wrapper]
721
- };
722
- function underlyingAllowanceQueryOptions(token, wrapperAddress) {
723
- return {
724
- queryKey: underlyingAllowanceQueryKeys.token(token.address, wrapperAddress),
725
- queryFn: () => token.allowance(wrapperAddress),
726
- staleTime: 3e4
727
- };
728
- }
729
- function useUnderlyingAllowance(config, options) {
730
- const { tokenAddress, wrapperAddress } = config;
731
- const token = useReadonlyToken(tokenAddress);
732
- return useQuery({
733
- ...underlyingAllowanceQueryOptions(token, wrapperAddress),
734
- ...options
735
- });
736
- }
737
- function useUnderlyingAllowanceSuspense(config) {
738
- const { tokenAddress, wrapperAddress } = config;
739
- const token = useReadonlyToken(tokenAddress);
740
- return useSuspenseQuery(underlyingAllowanceQueryOptions(token, wrapperAddress));
741
- }
742
-
743
- // src/token/use-finalize-unwrap.ts
744
- function finalizeUnwrapMutationOptions(token) {
745
- return {
746
- mutationKey: ["finalizeUnwrap", token.address],
747
- mutationFn: ({ burnAmountHandle }) => token.finalizeUnwrap(burnAmountHandle)
748
- };
749
- }
750
579
  function useFinalizeUnwrap(config, options) {
751
580
  const token = useToken(config);
752
581
  return useMutation({
753
- mutationKey: ["finalizeUnwrap", config.tokenAddress],
754
- mutationFn: ({ burnAmountHandle }) => token.finalizeUnwrap(burnAmountHandle),
582
+ ...finalizeUnwrapMutationOptions(token),
755
583
  ...options,
756
584
  onSuccess: (data, variables, onMutateResult, context) => {
757
- context.client.invalidateQueries({
758
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
759
- });
760
- context.client.invalidateQueries({
761
- queryKey: confidentialHandlesQueryKeys.all
762
- });
763
- context.client.resetQueries({
764
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
765
- });
766
- context.client.invalidateQueries({
767
- queryKey: confidentialBalancesQueryKeys.all
768
- });
769
- context.client.invalidateQueries({
770
- queryKey: underlyingAllowanceQueryKeys.all
771
- });
772
- context.client.invalidateQueries({ predicate: wagmiBalancePredicates.balanceOf });
773
585
  options?.onSuccess?.(data, variables, onMutateResult, context);
586
+ invalidateAfterUnshield(context.client, config.tokenAddress);
774
587
  }
775
588
  });
776
589
  }
777
- function unshieldMutationOptions(token) {
778
- return {
779
- mutationKey: ["unshield", token.address],
780
- mutationFn: ({ amount, callbacks }) => token.unshield(amount, callbacks)
781
- };
782
- }
783
590
  function useUnshield(config, options) {
784
591
  const token = useToken(config);
785
592
  return useMutation({
786
- mutationKey: ["unshield", config.tokenAddress],
787
- mutationFn: ({ amount, callbacks }) => token.unshield(amount, callbacks),
593
+ ...unshieldMutationOptions(token),
788
594
  ...options,
789
595
  onSuccess: (data, variables, onMutateResult, context) => {
790
- context.client.invalidateQueries({
791
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
792
- });
793
- context.client.invalidateQueries({
794
- queryKey: confidentialHandlesQueryKeys.all
795
- });
796
- context.client.resetQueries({
797
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
798
- });
799
- context.client.invalidateQueries({
800
- queryKey: confidentialBalancesQueryKeys.all
801
- });
802
- context.client.invalidateQueries({
803
- queryKey: underlyingAllowanceQueryKeys.all
804
- });
805
- context.client.invalidateQueries({
806
- predicate: wagmiBalancePredicates.balanceOf
807
- });
808
596
  options?.onSuccess?.(data, variables, onMutateResult, context);
597
+ invalidateAfterUnshield(context.client, config.tokenAddress);
809
598
  }
810
599
  });
811
600
  }
812
- function unshieldAllMutationOptions(token) {
813
- return {
814
- mutationKey: ["unshieldAll", token.address],
815
- mutationFn: (params) => token.unshieldAll(params?.callbacks)
816
- };
817
- }
818
601
  function useUnshieldAll(config, options) {
819
602
  const token = useToken(config);
820
603
  return useMutation({
821
- mutationKey: ["unshieldAll", config.tokenAddress],
822
- mutationFn: (params) => token.unshieldAll(params?.callbacks),
604
+ ...unshieldAllMutationOptions(token),
823
605
  ...options,
824
606
  onSuccess: (data, variables, onMutateResult, context) => {
825
- context.client.invalidateQueries({
826
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
827
- });
828
- context.client.invalidateQueries({
829
- queryKey: confidentialHandlesQueryKeys.all
830
- });
831
- context.client.resetQueries({
832
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
833
- });
834
- context.client.invalidateQueries({
835
- queryKey: confidentialBalancesQueryKeys.all
836
- });
837
- context.client.invalidateQueries({
838
- queryKey: underlyingAllowanceQueryKeys.all
839
- });
840
- context.client.invalidateQueries({
841
- predicate: wagmiBalancePredicates.balanceOf
842
- });
843
607
  options?.onSuccess?.(data, variables, onMutateResult, context);
608
+ invalidateAfterUnshield(context.client, config.tokenAddress);
844
609
  }
845
610
  });
846
611
  }
847
- function resumeUnshieldMutationOptions(token) {
848
- return {
849
- mutationKey: ["resumeUnshield", token.address],
850
- mutationFn: ({ unwrapTxHash, callbacks }) => token.resumeUnshield(unwrapTxHash, callbacks)
851
- };
852
- }
853
612
  function useResumeUnshield(config, options) {
854
613
  const token = useToken(config);
855
614
  return useMutation({
856
- mutationKey: ["resumeUnshield", config.tokenAddress],
857
- mutationFn: ({ unwrapTxHash, callbacks }) => token.resumeUnshield(unwrapTxHash, callbacks),
615
+ ...resumeUnshieldMutationOptions(token),
858
616
  ...options,
859
617
  onSuccess: (data, variables, onMutateResult, context) => {
860
- context.client.invalidateQueries({
861
- queryKey: confidentialHandleQueryKeys.token(config.tokenAddress)
862
- });
863
- context.client.invalidateQueries({
864
- queryKey: confidentialHandlesQueryKeys.all
865
- });
866
- context.client.resetQueries({
867
- queryKey: confidentialBalanceQueryKeys.token(config.tokenAddress)
868
- });
869
- context.client.invalidateQueries({
870
- queryKey: confidentialBalancesQueryKeys.all
871
- });
872
- context.client.invalidateQueries({
873
- queryKey: underlyingAllowanceQueryKeys.all
874
- });
875
- context.client.invalidateQueries({
876
- predicate: wagmiBalancePredicates.balanceOf
877
- });
878
618
  options?.onSuccess?.(data, variables, onMutateResult, context);
619
+ invalidateAfterUnshield(context.client, config.tokenAddress);
879
620
  }
880
621
  });
881
622
  }
882
- var wrapperDiscoveryQueryKeys = {
883
- /** Match all wrapper discovery queries. */
884
- all: ["wrapperDiscovery"],
885
- /** Match wrapper discovery queries for a specific token. */
886
- token: (tokenAddress) => ["wrapperDiscovery", tokenAddress],
887
- /** Match wrapper discovery query for a specific token + coordinator pair. */
888
- tokenCoordinator: (tokenAddress, coordinatorAddress) => ["wrapperDiscovery", tokenAddress, coordinatorAddress]
889
- };
890
- function wrapperDiscoveryQueryOptions(token, coordinatorAddress) {
891
- return {
892
- queryKey: wrapperDiscoveryQueryKeys.tokenCoordinator(token.address, coordinatorAddress),
893
- queryFn: () => token.discoverWrapper(coordinatorAddress),
894
- staleTime: Infinity
895
- };
623
+ function useUnderlyingAllowance(config, options) {
624
+ const { tokenAddress, wrapperAddress } = config;
625
+ const userEnabled = options?.enabled;
626
+ const token = useReadonlyToken(tokenAddress);
627
+ const addressQuery = useQuery4({
628
+ ...signerAddressQueryOptions(token.signer)
629
+ });
630
+ const owner = addressQuery.data;
631
+ const baseOpts = underlyingAllowanceQueryOptions(token.signer, tokenAddress, {
632
+ owner,
633
+ wrapperAddress
634
+ });
635
+ const factoryEnabled = baseOpts.enabled ?? true;
636
+ return useQuery4({
637
+ ...baseOpts,
638
+ ...options,
639
+ enabled: factoryEnabled && (userEnabled ?? true)
640
+ });
641
+ }
642
+ function useUnderlyingAllowanceSuspense(config) {
643
+ const { tokenAddress, wrapperAddress } = config;
644
+ const token = useReadonlyToken(tokenAddress);
645
+ const addressQuery = useSuspenseQuery({
646
+ ...signerAddressQueryOptions(token.signer)
647
+ });
648
+ const owner = addressQuery.data;
649
+ return useSuspenseQuery({
650
+ ...underlyingAllowanceQueryOptions(token.signer, tokenAddress, {
651
+ owner,
652
+ wrapperAddress
653
+ })
654
+ });
896
655
  }
897
656
  function useWrapperDiscovery(config, options) {
898
657
  const { tokenAddress, coordinatorAddress } = config;
899
658
  const token = useReadonlyToken(tokenAddress);
900
- return useQuery({
901
- ...coordinatorAddress ? wrapperDiscoveryQueryOptions(token, coordinatorAddress) : {
902
- queryKey: wrapperDiscoveryQueryKeys.tokenCoordinator(tokenAddress, ""),
659
+ return useQuery4({
660
+ ...coordinatorAddress ? wrapperDiscoveryQueryOptions(token.signer, tokenAddress, { coordinatorAddress }) : {
661
+ queryKey: zamaQueryKeys.wrapperDiscovery.all,
903
662
  queryFn: skipToken
904
663
  },
905
664
  ...options
@@ -908,236 +667,116 @@ function useWrapperDiscovery(config, options) {
908
667
  function useWrapperDiscoverySuspense(config) {
909
668
  const { tokenAddress, coordinatorAddress } = config;
910
669
  const token = useReadonlyToken(tokenAddress);
911
- return useSuspenseQuery(
912
- wrapperDiscoveryQueryOptions(token, coordinatorAddress)
913
- );
914
- }
915
- var metadataQueryKeys = {
916
- /** Match all token metadata queries. */
917
- all: ["tokenMetadata"],
918
- /** Match metadata query for a specific token. */
919
- token: (tokenAddress) => ["tokenMetadata", tokenAddress]
920
- };
921
- function metadataQueryOptions(token) {
922
- return {
923
- queryKey: metadataQueryKeys.token(token.address),
924
- queryFn: async () => {
925
- const [name, symbol, decimals] = await Promise.all([
926
- token.name(),
927
- token.symbol(),
928
- token.decimals()
929
- ]);
930
- return { name, symbol, decimals };
931
- },
932
- staleTime: Infinity
933
- };
670
+ return useSuspenseQuery({
671
+ ...wrapperDiscoveryQueryOptions(token.signer, tokenAddress, { coordinatorAddress })
672
+ });
934
673
  }
935
674
  function useMetadata(tokenAddress, options) {
936
675
  const token = useReadonlyToken(tokenAddress);
937
- return useQuery({
938
- ...metadataQueryOptions(token),
676
+ return useQuery4({
677
+ ...tokenMetadataQueryOptions(token.signer, tokenAddress),
939
678
  ...options
940
679
  });
941
680
  }
942
681
  function useMetadataSuspense(tokenAddress) {
943
682
  const token = useReadonlyToken(tokenAddress);
944
- return useSuspenseQuery(metadataQueryOptions(token));
683
+ return useSuspenseQuery({
684
+ ...tokenMetadataQueryOptions(token.signer, tokenAddress)
685
+ });
945
686
  }
946
- var activityFeedQueryKeys = {
947
- /** Match all activity feed queries. */
948
- all: ["activityFeed"],
949
- /** Match activity feed queries for a specific token. */
950
- token: (tokenAddress) => ["activityFeed", tokenAddress]
951
- };
952
687
  function useActivityFeed(config) {
953
688
  const { tokenAddress, userAddress, logs, decrypt: decryptOpt } = config;
954
689
  const token = useReadonlyToken(tokenAddress);
955
690
  const decrypt = decryptOpt ?? true;
956
- const enabled = logs !== void 0 && userAddress !== void 0;
957
- return useQuery({
958
- queryKey: [
959
- ...activityFeedQueryKeys.token(tokenAddress),
960
- userAddress ?? "",
961
- logs?.map((l) => `${l.transactionHash ?? ""}:${l.logIndex ?? ""}`).join(",") ?? ""
962
- ],
963
- queryFn: async () => {
964
- if (!logs || !userAddress) return [];
965
- const items = sortByBlockNumber(parseActivityFeed(logs, userAddress));
966
- if (!decrypt) return items;
967
- const handles = extractEncryptedHandles(items);
968
- if (handles.length === 0) return items;
969
- const decryptedMap = await token.decryptHandles(handles, userAddress);
970
- return applyDecryptedValues(items, decryptedMap);
971
- },
972
- enabled,
973
- staleTime: Infinity
691
+ const logsKey = logs?.map((log) => `${log.transactionHash ?? ""}:${log.logIndex ?? ""}`).join(",") ?? "";
692
+ return useQuery4({
693
+ ...activityFeedQueryOptions(token, {
694
+ userAddress,
695
+ logs,
696
+ decrypt,
697
+ logsKey
698
+ })
974
699
  });
975
700
  }
976
- function approveUnderlyingMutationOptions(token) {
977
- return {
978
- mutationKey: ["approveUnderlying", token.address],
979
- mutationFn: ({ amount }) => token.approveUnderlying(amount)
980
- };
981
- }
982
701
  function useApproveUnderlying(config, options) {
983
702
  const token = useToken(config);
984
703
  return useMutation({
985
- mutationKey: ["approveUnderlying", config.tokenAddress],
986
- mutationFn: ({ amount }) => token.approveUnderlying(amount),
704
+ ...approveUnderlyingMutationOptions(token),
987
705
  ...options,
988
706
  onSuccess: (data, variables, onMutateResult, context) => {
989
- context.client.invalidateQueries({
990
- queryKey: underlyingAllowanceQueryKeys.all
991
- });
992
707
  options?.onSuccess?.(data, variables, onMutateResult, context);
708
+ invalidateAfterApproveUnderlying(context.client, config.tokenAddress);
993
709
  }
994
710
  });
995
711
  }
996
- var isConfidentialQueryKeys = {
997
- /** Match all confidential interface queries. */
998
- all: ["isConfidential"],
999
- /** Match confidential interface query for a specific token. */
1000
- token: (tokenAddress) => ["isConfidential", tokenAddress]
1001
- };
1002
- var isWrapperQueryKeys = {
1003
- /** Match all wrapper interface queries. */
1004
- all: ["isWrapper"],
1005
- /** Match wrapper interface query for a specific token. */
1006
- token: (tokenAddress) => ["isWrapper", tokenAddress]
1007
- };
1008
- function isConfidentialQueryOptions(token) {
1009
- return {
1010
- queryKey: isConfidentialQueryKeys.token(token.address),
1011
- queryFn: () => token.isConfidential(),
1012
- staleTime: Infinity
1013
- };
1014
- }
1015
- function isWrapperQueryOptions(token) {
1016
- return {
1017
- queryKey: isWrapperQueryKeys.token(token.address),
1018
- queryFn: () => token.isWrapper(),
1019
- staleTime: Infinity
1020
- };
1021
- }
1022
712
  function useIsConfidential(tokenAddress, options) {
1023
713
  const token = useReadonlyToken(tokenAddress);
1024
- return useQuery({
1025
- ...isConfidentialQueryOptions(token),
714
+ return useQuery4({
715
+ ...isConfidentialQueryOptions(token.signer, tokenAddress),
1026
716
  ...options
1027
717
  });
1028
718
  }
1029
719
  function useIsConfidentialSuspense(tokenAddress) {
1030
720
  const token = useReadonlyToken(tokenAddress);
1031
- return useSuspenseQuery(isConfidentialQueryOptions(token));
721
+ return useSuspenseQuery({
722
+ ...isConfidentialQueryOptions(token.signer, tokenAddress)
723
+ });
1032
724
  }
1033
725
  function useIsWrapper(tokenAddress, options) {
1034
726
  const token = useReadonlyToken(tokenAddress);
1035
- return useQuery({
1036
- ...isWrapperQueryOptions(token),
727
+ return useQuery4({
728
+ ...isWrapperQueryOptions(token.signer, tokenAddress),
1037
729
  ...options
1038
730
  });
1039
731
  }
1040
732
  function useIsWrapperSuspense(tokenAddress) {
1041
733
  const token = useReadonlyToken(tokenAddress);
1042
- return useSuspenseQuery(isWrapperQueryOptions(token));
1043
- }
1044
- var totalSupplyQueryKeys = {
1045
- /** Match all total supply queries. */
1046
- all: ["totalSupply"],
1047
- /** Match total supply query for a specific token. */
1048
- token: (tokenAddress) => ["totalSupply", tokenAddress]
1049
- };
1050
- function totalSupplyQueryOptions(token) {
1051
- return {
1052
- queryKey: totalSupplyQueryKeys.token(token.address),
1053
- queryFn: () => token.signer.readContract(totalSupplyContract(token.address)),
1054
- staleTime: 3e4
1055
- };
734
+ return useSuspenseQuery({
735
+ ...isWrapperQueryOptions(token.signer, tokenAddress)
736
+ });
1056
737
  }
1057
738
  function useTotalSupply(tokenAddress, options) {
1058
739
  const token = useReadonlyToken(tokenAddress);
1059
- return useQuery({
1060
- ...totalSupplyQueryOptions(token),
740
+ return useQuery4({
741
+ ...totalSupplyQueryOptions(token.signer, tokenAddress),
1061
742
  ...options
1062
743
  });
1063
744
  }
1064
745
  function useTotalSupplySuspense(tokenAddress) {
1065
746
  const token = useReadonlyToken(tokenAddress);
1066
- return useSuspenseQuery(totalSupplyQueryOptions(token));
1067
- }
1068
- var feeQueryKeys = {
1069
- /** Match shield fee query for given parameters. */
1070
- shieldFee: (feeManagerAddress, amount, from, to) => ["shieldFee", feeManagerAddress, ...amount !== void 0 ? [amount, from, to] : []],
1071
- /** Match unshield fee query for given parameters. */
1072
- unshieldFee: (feeManagerAddress, amount, from, to) => [
1073
- "unshieldFee",
1074
- feeManagerAddress,
1075
- ...amount !== void 0 ? [amount, from, to] : []
1076
- ],
1077
- /** Match batch transfer fee query for a specific fee manager. */
1078
- batchTransferFee: (feeManagerAddress) => ["batchTransferFee", feeManagerAddress],
1079
- /** Match fee recipient query for a specific fee manager. */
1080
- feeRecipient: (feeManagerAddress) => ["feeRecipient", feeManagerAddress]
1081
- };
1082
- function shieldFeeQueryOptions(signer, config) {
1083
- const { feeManagerAddress, amount, from, to } = config;
1084
- return {
1085
- queryKey: feeQueryKeys.shieldFee(feeManagerAddress, amount.toString(), from, to),
1086
- queryFn: () => signer.readContract(getWrapFeeContract(feeManagerAddress, amount, from, to)),
1087
- staleTime: 3e4
1088
- };
1089
- }
1090
- function unshieldFeeQueryOptions(signer, config) {
1091
- const { feeManagerAddress, amount, from, to } = config;
1092
- return {
1093
- queryKey: feeQueryKeys.unshieldFee(feeManagerAddress, amount.toString(), from, to),
1094
- queryFn: () => signer.readContract(getUnwrapFeeContract(feeManagerAddress, amount, from, to)),
1095
- staleTime: 3e4
1096
- };
1097
- }
1098
- function batchTransferFeeQueryOptions(signer, feeManagerAddress) {
1099
- return {
1100
- queryKey: feeQueryKeys.batchTransferFee(feeManagerAddress),
1101
- queryFn: () => signer.readContract(getBatchTransferFeeContract(feeManagerAddress)),
1102
- staleTime: 3e4
1103
- };
1104
- }
1105
- function feeRecipientQueryOptions(signer, feeManagerAddress) {
1106
- return {
1107
- queryKey: feeQueryKeys.feeRecipient(feeManagerAddress),
1108
- queryFn: () => signer.readContract(getFeeRecipientContract(feeManagerAddress)),
1109
- staleTime: 3e4
1110
- };
747
+ return useSuspenseQuery({
748
+ ...totalSupplyQueryOptions(token.signer, tokenAddress)
749
+ });
1111
750
  }
1112
751
  function useShieldFee(config, options) {
1113
752
  const sdk = useZamaSDK();
1114
- return useQuery({
753
+ return useQuery4({
1115
754
  ...shieldFeeQueryOptions(sdk.signer, config),
1116
755
  ...options
1117
756
  });
1118
757
  }
1119
758
  function useUnshieldFee(config, options) {
1120
759
  const sdk = useZamaSDK();
1121
- return useQuery({
760
+ return useQuery4({
1122
761
  ...unshieldFeeQueryOptions(sdk.signer, config),
1123
762
  ...options
1124
763
  });
1125
764
  }
1126
765
  function useBatchTransferFee(feeManagerAddress, options) {
1127
766
  const sdk = useZamaSDK();
1128
- return useQuery({
767
+ return useQuery4({
1129
768
  ...batchTransferFeeQueryOptions(sdk.signer, feeManagerAddress),
1130
769
  ...options
1131
770
  });
1132
771
  }
1133
772
  function useFeeRecipient(feeManagerAddress, options) {
1134
773
  const sdk = useZamaSDK();
1135
- return useQuery({
774
+ return useQuery4({
1136
775
  ...feeRecipientQueryOptions(sdk.signer, feeManagerAddress),
1137
776
  ...options
1138
777
  });
1139
778
  }
1140
779
 
1141
- export { ZamaProvider, activityFeedQueryKeys, allowMutationOptions, approveUnderlyingMutationOptions, batchTransferFeeQueryOptions, confidentialApproveMutationOptions, confidentialBalanceQueryKeys, confidentialBalancesQueryKeys, confidentialHandleQueryKeys, confidentialHandlesQueryKeys, confidentialIsApprovedQueryKeys, confidentialIsApprovedQueryOptions, confidentialTransferFromMutationOptions, confidentialTransferMutationOptions, decryptionKeys, encryptMutationOptions, feeQueryKeys, feeRecipientQueryOptions, finalizeUnwrapMutationOptions, isAllowedQueryKeys, isAllowedQueryOptions, isConfidentialQueryKeys, isConfidentialQueryOptions, isWrapperQueryKeys, isWrapperQueryOptions, metadataQueryKeys, metadataQueryOptions, publicKeyQueryKeys, publicKeyQueryOptions, publicParamsQueryKeys, publicParamsQueryOptions, resumeUnshieldMutationOptions, revokeMutationOptions, revokeSessionMutationOptions, shieldETHMutationOptions, shieldFeeQueryOptions, shieldMutationOptions, totalSupplyQueryKeys, totalSupplyQueryOptions, underlyingAllowanceQueryKeys, underlyingAllowanceQueryOptions, unshieldAllMutationOptions, unshieldFeeQueryOptions, unshieldMutationOptions, unwrapAllMutationOptions, unwrapMutationOptions, useActivityFeed, useAllow, useApproveUnderlying, useBatchTransferFee, useConfidentialApprove, useConfidentialBalance, useConfidentialBalances, useConfidentialIsApproved, useConfidentialIsApprovedSuspense, useConfidentialTransfer, useConfidentialTransferFrom, useCreateDelegatedUserDecryptEIP712, useCreateEIP712, useDelegatedUserDecrypt, useEncrypt, useFeeRecipient, useFinalizeUnwrap, useGenerateKeypair, useIsAllowed, useIsConfidential, useIsConfidentialSuspense, useIsWrapper, useIsWrapperSuspense, useMetadata, useMetadataSuspense, usePublicDecrypt, usePublicKey, usePublicParams, useReadonlyToken, useRequestZKProofVerification, useResumeUnshield, useRevoke, useRevokeSession, useShield, useShieldETH, useShieldFee, useToken, useTotalSupply, useTotalSupplySuspense, useUnderlyingAllowance, useUnderlyingAllowanceSuspense, useUnshield, useUnshieldAll, useUnshieldFee, useUnwrap, useUnwrapAll, useUserDecrypt, useUserDecryptFlow, useUserDecryptedValue, useUserDecryptedValues, useWrapperDiscovery, useWrapperDiscoverySuspense, useZamaSDK, wrapperDiscoveryQueryKeys, wrapperDiscoveryQueryOptions };
780
+ export { ZamaProvider, decryptionKeys, useActivityFeed, useAllow, useApproveUnderlying, useBatchTransferFee, useConfidentialApprove, useConfidentialBalance, useConfidentialBalances, useConfidentialIsApproved, useConfidentialIsApprovedSuspense, useConfidentialTransfer, useConfidentialTransferFrom, useCreateDelegatedUserDecryptEIP712, useCreateEIP712, useDelegatedUserDecrypt, useEncrypt, useFeeRecipient, useFinalizeUnwrap, useGenerateKeypair, useIsAllowed, useIsConfidential, useIsConfidentialSuspense, useIsWrapper, useIsWrapperSuspense, useMetadata, useMetadataSuspense, usePublicDecrypt, usePublicKey, usePublicParams, useReadonlyToken, useRequestZKProofVerification, useResumeUnshield, useRevoke, useRevokeSession, useShield, useShieldETH, useShieldFee, useToken, useTotalSupply, useTotalSupplySuspense, useUnderlyingAllowance, useUnderlyingAllowanceSuspense, useUnshield, useUnshieldAll, useUnshieldFee, useUnwrap, useUnwrapAll, useUserDecrypt, useUserDecryptFlow, useUserDecryptedValue, useUserDecryptedValues, useWrapperDiscovery, useWrapperDiscoverySuspense, useZamaSDK };
1142
781
  //# sourceMappingURL=index.js.map
1143
782
  //# sourceMappingURL=index.js.map