wagmi-extended 1.1.8 → 2.0.1

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.
Files changed (63) hide show
  1. package/README.md +1 -1
  2. package/dist/fetch-functions/erc20/fetchAllowanceX.d.ts +4 -0
  3. package/dist/fetch-functions/erc20/fetchBalanceOfX.d.ts +4 -0
  4. package/dist/fetch-functions/erc20/fetchERC20DataX.d.ts +18 -0
  5. package/dist/fetch-functions/erc20/fetchNameX.d.ts +4 -0
  6. package/dist/fetch-functions/erc20/fetchSymbolX.d.ts +4 -0
  7. package/dist/fetch-functions/erc20/fetchTotalSupplyX.d.ts +4 -0
  8. package/dist/fetch-functions/erc4626/fetchERC4626AssetX.d.ts +4 -0
  9. package/dist/fetch-functions/erc4626/fetchERC4626ConvertToAssetsX.d.ts +4 -0
  10. package/dist/fetch-functions/erc4626/fetchERC4626ConvertToSharesX.d.ts +4 -0
  11. package/dist/fetch-functions/erc4626/fetchERC4626DataX.d.ts +24 -0
  12. package/dist/fetch-functions/erc4626/fetchERC4626MaxDepositX.d.ts +4 -0
  13. package/dist/fetch-functions/erc4626/fetchERC4626MaxMintX.d.ts +4 -0
  14. package/dist/fetch-functions/erc4626/fetchERC4626MaxRedeemX.d.ts +4 -0
  15. package/dist/fetch-functions/erc4626/fetchERC4626MaxWithdrawX.d.ts +4 -0
  16. package/dist/fetch-functions/erc4626/fetchERC4626TotalAssetsX.d.ts +4 -0
  17. package/dist/hooks/{useContractWriteX.d.ts → mutations/useContractWriteX.d.ts} +40 -35
  18. package/dist/hooks/{useHandleTransactionMutationX.d.ts → mutations/useHandleTransactionMutationX.d.ts} +8 -1
  19. package/dist/hooks/{useSendTransactionX.d.ts → mutations/useSendTransactionX.d.ts} +14 -13
  20. package/dist/hooks/queries/useERC20Data.d.ts +22 -0
  21. package/dist/hooks/queries/useFetchERC4626DataX.d.ts +28 -0
  22. package/dist/hooks/{useTokenX.d.ts → queries/useTokenX.d.ts} +18 -18
  23. package/dist/index.cjs.js +348 -114
  24. package/dist/index.cjs.js.map +1 -1
  25. package/dist/index.d.ts +16 -5
  26. package/dist/index.esm.js +339 -116
  27. package/dist/index.esm.js.map +1 -1
  28. package/dist/query-config/index.d.ts +20 -2
  29. package/dist/utils/ensureClientAndConfig.d.ts +6 -0
  30. package/package.json +1 -1
  31. package/src/fetch-functions/erc20/fetchAllowanceX.ts +31 -0
  32. package/src/fetch-functions/erc20/fetchBalanceOfX.ts +28 -0
  33. package/src/fetch-functions/erc20/fetchERC20DataX.ts +52 -0
  34. package/src/fetch-functions/erc20/fetchNameX.ts +26 -0
  35. package/src/fetch-functions/erc20/fetchSymbolX.ts +26 -0
  36. package/src/fetch-functions/erc20/fetchTotalSupplyX.ts +26 -0
  37. package/src/fetch-functions/erc4626/fetchERC4626AssetX.ts +25 -0
  38. package/src/fetch-functions/erc4626/fetchERC4626ConvertToAssetsX.ts +27 -0
  39. package/src/fetch-functions/erc4626/fetchERC4626ConvertToSharesX.ts +27 -0
  40. package/src/fetch-functions/erc4626/fetchERC4626DataX.ts +60 -0
  41. package/src/fetch-functions/erc4626/fetchERC4626MaxDepositX.ts +27 -0
  42. package/src/fetch-functions/erc4626/fetchERC4626MaxMintX.ts +27 -0
  43. package/src/fetch-functions/erc4626/fetchERC4626MaxRedeemX.ts +27 -0
  44. package/src/fetch-functions/erc4626/fetchERC4626MaxWithdrawX.ts +27 -0
  45. package/src/fetch-functions/erc4626/fetchERC4626TotalAssetsX.ts +25 -0
  46. package/src/fetch-functions/fetchTokenX.ts +16 -30
  47. package/src/hooks/{useContractWriteX.ts → mutations/useContractWriteX.ts} +32 -12
  48. package/src/hooks/{useERC20ApproveX.ts → mutations/useERC20ApproveX.ts} +1 -1
  49. package/src/hooks/{useHandleTransactionMutationX.ts → mutations/useHandleTransactionMutationX.ts} +30 -4
  50. package/src/hooks/{useSendTransactionX.ts → mutations/useSendTransactionX.ts} +36 -3
  51. package/src/hooks/queries/useERC20Data.ts +40 -0
  52. package/src/hooks/{useFetchAssetAllowanceX.ts → queries/useFetchAssetAllowanceX.ts} +6 -4
  53. package/src/hooks/queries/useFetchERC4626DataX.ts +39 -0
  54. package/src/hooks/{useTokenX.ts → queries/useTokenX.ts} +1 -1
  55. package/src/index.ts +16 -5
  56. package/src/query-config/index.ts +13 -3
  57. package/src/utils/ensureClientAndConfig.ts +18 -0
  58. package/dist/fetch-functions/fetchAllowanceX.d.ts +0 -2
  59. package/src/fetch-functions/fetchAllowanceX.ts +0 -22
  60. /package/dist/hooks/{useERC20ApproveX.d.ts → mutations/useERC20ApproveX.d.ts} +0 -0
  61. /package/dist/hooks/{useInvalidateQueries.d.ts → mutations/useInvalidateQueries.d.ts} +0 -0
  62. /package/dist/hooks/{useFetchAssetAllowanceX.d.ts → queries/useFetchAssetAllowanceX.d.ts} +0 -0
  63. /package/src/hooks/{useInvalidateQueries.ts → mutations/useInvalidateQueries.ts} +0 -0
@@ -0,0 +1,27 @@
1
+ import { QueryClient } from "@tanstack/react-query";
2
+ import { Address, erc4626Abi } from "viem";
3
+ import { Config } from "wagmi";
4
+ import { readContractQueryOptions } from "wagmi/query";
5
+ import { queryConfig } from "../../query-config/index.js";
6
+ import { ensureClientAndConfig } from "../../utils/ensureClientAndConfig.js";
7
+
8
+ export async function fetchERC4626MaxDepositX(
9
+ vault: Address,
10
+ caller: Address,
11
+ queryClient?: QueryClient,
12
+ wagmiConfig?: Config
13
+ ): Promise<bigint> {
14
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
15
+ queryClient,
16
+ wagmiConfig
17
+ ));
18
+ return queryClient.fetchQuery({
19
+ ...readContractQueryOptions(wagmiConfig, {
20
+ address: vault,
21
+ abi: erc4626Abi,
22
+ functionName: "maxDeposit",
23
+ args: [caller],
24
+ }),
25
+ ...queryConfig.semiSensitiveQuery,
26
+ });
27
+ }
@@ -0,0 +1,27 @@
1
+ import { QueryClient } from "@tanstack/react-query";
2
+ import { Address, erc4626Abi } from "viem";
3
+ import { Config } from "wagmi";
4
+ import { readContractQueryOptions } from "wagmi/query";
5
+ import { queryConfig } from "../../query-config/index.js";
6
+ import { ensureClientAndConfig } from "../../utils/ensureClientAndConfig.js";
7
+
8
+ export async function fetchERC4626MaxMintX(
9
+ vault: Address,
10
+ caller: Address,
11
+ queryClient?: QueryClient,
12
+ wagmiConfig?: Config
13
+ ): Promise<bigint> {
14
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
15
+ queryClient,
16
+ wagmiConfig
17
+ ));
18
+ return queryClient.fetchQuery({
19
+ ...readContractQueryOptions(wagmiConfig, {
20
+ address: vault,
21
+ abi: erc4626Abi,
22
+ functionName: "maxMint",
23
+ args: [caller],
24
+ }),
25
+ ...queryConfig.semiSensitiveQuery,
26
+ });
27
+ }
@@ -0,0 +1,27 @@
1
+ import { QueryClient } from "@tanstack/react-query";
2
+ import { Address, erc4626Abi } from "viem";
3
+ import { Config } from "wagmi";
4
+ import { readContractQueryOptions } from "wagmi/query";
5
+ import { queryConfig } from "../../query-config/index.js";
6
+ import { ensureClientAndConfig } from "../../utils/ensureClientAndConfig.js";
7
+
8
+ export async function fetchERC4626MaxRedeemX(
9
+ vault: Address,
10
+ owner: Address,
11
+ queryClient?: QueryClient,
12
+ wagmiConfig?: Config
13
+ ): Promise<bigint> {
14
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
15
+ queryClient,
16
+ wagmiConfig
17
+ ));
18
+ return queryClient.fetchQuery({
19
+ ...readContractQueryOptions(wagmiConfig, {
20
+ address: vault,
21
+ abi: erc4626Abi,
22
+ functionName: "maxRedeem",
23
+ args: [owner],
24
+ }),
25
+ ...queryConfig.semiSensitiveQuery,
26
+ });
27
+ }
@@ -0,0 +1,27 @@
1
+ import { QueryClient } from "@tanstack/react-query";
2
+ import { Address, erc4626Abi } from "viem";
3
+ import { Config } from "wagmi";
4
+ import { readContractQueryOptions } from "wagmi/query";
5
+ import { queryConfig } from "../../query-config/index.js";
6
+ import { ensureClientAndConfig } from "../../utils/ensureClientAndConfig.js";
7
+
8
+ export async function fetchERC4626MaxWithdrawX(
9
+ vault: Address,
10
+ owner: Address,
11
+ queryClient?: QueryClient,
12
+ wagmiConfig?: Config
13
+ ): Promise<bigint> {
14
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
15
+ queryClient,
16
+ wagmiConfig
17
+ ));
18
+ return queryClient.fetchQuery({
19
+ ...readContractQueryOptions(wagmiConfig, {
20
+ address: vault,
21
+ abi: erc4626Abi,
22
+ functionName: "maxWithdraw",
23
+ args: [owner],
24
+ }),
25
+ ...queryConfig.semiSensitiveQuery,
26
+ });
27
+ }
@@ -0,0 +1,25 @@
1
+ import { QueryClient } from "@tanstack/react-query";
2
+ import { Address, erc4626Abi } from "viem";
3
+ import { Config } from "wagmi";
4
+ import { readContractQueryOptions } from "wagmi/query";
5
+ import { queryConfig } from "../../query-config/index.js";
6
+ import { ensureClientAndConfig } from "../../utils/ensureClientAndConfig.js";
7
+
8
+ export async function fetchERC4626TotalAssetsX(
9
+ vault: Address,
10
+ queryClient?: QueryClient,
11
+ wagmiConfig?: Config
12
+ ): Promise<bigint> {
13
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
14
+ queryClient,
15
+ wagmiConfig
16
+ ));
17
+ return queryClient.fetchQuery({
18
+ ...readContractQueryOptions(wagmiConfig, {
19
+ address: vault,
20
+ abi: erc4626Abi,
21
+ functionName: "totalAssets",
22
+ }),
23
+ ...queryConfig.semiSensitiveQuery,
24
+ });
25
+ }
@@ -2,8 +2,8 @@ import { QueryClient } from "@tanstack/react-query";
2
2
  import { readContractQueryOptions } from "wagmi/query";
3
3
  import { Address, zeroAddress, erc20Abi } from "viem";
4
4
  import { Config } from "wagmi";
5
- import { getDefaults } from "../config/defaults.js";
6
5
  import { queryConfig } from "../query-config/index.js";
6
+ import { ensureClientAndConfig } from "../utils/ensureClientAndConfig.js";
7
7
 
8
8
  export interface Token {
9
9
  symbol: string;
@@ -22,15 +22,10 @@ export async function fetchDecimalsX(
22
22
  queryClient?: QueryClient,
23
23
  wagmiConfig?: Config
24
24
  ): Promise<number | undefined> {
25
- if (!queryClient || !wagmiConfig) {
26
- ({ queryClient, wagmiConfig } = getDefaults());
27
- }
28
- if (!queryClient || !wagmiConfig) {
29
- throw new Error(
30
- "Could not find queryClient or wagmiConfig, either pass them as arguments or set them using setDefaults()"
31
- );
32
- }
33
-
25
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
26
+ queryClient,
27
+ wagmiConfig
28
+ ));
34
29
  if (token === zeroAddress) return EthTokenData.decimals;
35
30
 
36
31
  const decimals = await queryClient.fetchQuery({
@@ -39,7 +34,7 @@ export async function fetchDecimalsX(
39
34
  abi: erc20Abi,
40
35
  functionName: "decimals",
41
36
  }),
42
- ...queryConfig.metadataQueryConfig,
37
+ ...queryConfig.metaDataQuery,
43
38
  });
44
39
 
45
40
  return decimals;
@@ -50,15 +45,10 @@ export async function fetchSymbolX(
50
45
  queryClient?: QueryClient,
51
46
  wagmiConfig?: Config
52
47
  ): Promise<string> {
53
- if (!queryClient || !wagmiConfig) {
54
- ({ queryClient, wagmiConfig } = getDefaults());
55
- }
56
- if (!queryClient || !wagmiConfig) {
57
- throw new Error(
58
- "Could not find queryClient or wagmiConfig, either pass them as arguments or set them using setDefaults()"
59
- );
60
- }
61
-
48
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
49
+ queryClient,
50
+ wagmiConfig
51
+ ));
62
52
  if (token === zeroAddress) return EthTokenData.symbol;
63
53
 
64
54
  const symbol = await queryClient.fetchQuery({
@@ -67,7 +57,7 @@ export async function fetchSymbolX(
67
57
  abi: erc20Abi,
68
58
  functionName: "symbol",
69
59
  }),
70
- ...queryConfig.metadataQueryConfig,
60
+ ...queryConfig.metaDataQuery,
71
61
  });
72
62
 
73
63
  return symbol;
@@ -80,14 +70,10 @@ export async function fetchNameX(
80
70
  ): Promise<string> {
81
71
  if (token === zeroAddress) return EthTokenData.name;
82
72
 
83
- if (!queryClient || !wagmiConfig) {
84
- ({ queryClient, wagmiConfig } = getDefaults());
85
- }
86
- if (!queryClient || !wagmiConfig) {
87
- throw new Error(
88
- "Could not find queryClient or wagmiConfig, either pass them as arguments or set them using setDefaults()"
89
- );
90
- }
73
+ ({ queryClient, wagmiConfig } = ensureClientAndConfig(
74
+ queryClient,
75
+ wagmiConfig
76
+ ));
91
77
 
92
78
  const name = await queryClient.fetchQuery({
93
79
  ...readContractQueryOptions(wagmiConfig, {
@@ -95,7 +81,7 @@ export async function fetchNameX(
95
81
  abi: erc20Abi,
96
82
  functionName: "name",
97
83
  }),
98
- ...queryConfig.metadataQueryConfig,
84
+ ...queryConfig.metaDataQuery,
99
85
  });
100
86
 
101
87
  return name;
@@ -1,8 +1,9 @@
1
- import { useWriteContract } from "wagmi";
1
+ import { usePublicClient, useSimulateContract, useWriteContract } from "wagmi";
2
2
  import {
3
3
  WriteExtendedAsyncParams,
4
4
  useHandleTransactionMutationX,
5
5
  } from "./useHandleTransactionMutationX.js";
6
+ import { Address } from "viem";
6
7
 
7
8
  /**
8
9
  * Custom hook for writing to a smart contract using Wagmi.
@@ -64,23 +65,42 @@ import {
64
65
  * }
65
66
  */
66
67
 
67
- export function useContractWriteX(settings?: WriteExtendedAsyncParams) {
68
+ export function useContractWriteX(settings: WriteExtendedAsyncParams) {
69
+ const publicClient = usePublicClient();
70
+
68
71
  const { isPending, errorMessage, onMutate, onSettled } =
69
- useHandleTransactionMutationX({
70
- settings,
71
- });
72
+ useHandleTransactionMutationX({ settings });
72
73
 
73
- const { writeContractAsync, ...rest } = useWriteContract({
74
- mutation: {
75
- onMutate,
76
- onSettled,
77
- },
74
+ // Underlying Wagmi write hook:
75
+ const wagmiWrite = useWriteContract({
76
+ mutation: { onMutate, onSettled },
78
77
  });
79
78
 
79
+ async function simulateAsyncAndWriteContract(
80
+ params: Parameters<typeof wagmiWrite.writeContract>[0]
81
+ ) {
82
+ // 0) signal start
83
+ onMutate();
84
+
85
+ try {
86
+ // 1) optional dry-run
87
+ const { chain, ...others } = params;
88
+
89
+ await publicClient?.simulateContract({
90
+ ...others,
91
+ ...(chain != null ? { chain } : {}),
92
+ });
93
+
94
+ wagmiWrite.writeContract(params);
95
+ } catch (err) {
96
+ await onSettled(undefined, err, params);
97
+ }
98
+ }
99
+
80
100
  return {
81
- ...rest,
101
+ ...wagmiWrite,
102
+ simulateAsyncAndWriteContract,
82
103
  isPending,
83
104
  errorMessage,
84
- writeContractAsync,
85
105
  };
86
106
  }
@@ -1,6 +1,6 @@
1
1
  import { useState, useEffect } from "react";
2
2
  import { Address, maxUint256, erc20Abi } from "viem";
3
- import { useFetchAssetAllowanceX } from "./useFetchAssetAllowanceX.js";
3
+ import { useFetchAssetAllowanceX } from "../queries/useFetchAssetAllowanceX.js";
4
4
  import { useContractWriteX } from "./useContractWriteX.js";
5
5
 
6
6
  /**
@@ -1,16 +1,28 @@
1
1
  import { waitForTransactionReceipt } from "wagmi/actions";
2
2
  import { useConfig } from "wagmi";
3
- import { QueryKey } from "@tanstack/query-core";
3
+ import { Query, QueryKey } from "@tanstack/query-core";
4
4
  import { Address } from "viem";
5
5
  import { useState } from "react";
6
- import { getParsedErrorX } from "../utils/errorParserX.js";
7
6
  import { useInvalidateQueries } from "./useInvalidateQueries.js";
7
+ import { useQueryClient } from "@tanstack/react-query";
8
+ import { getParsedErrorX } from "../../utils/errorParserX.js";
8
9
 
9
10
  export type WriteExtendedAsyncParams = {
10
11
  onSuccess?: (txHash: Address) => void;
11
12
  onError?: (e: any) => void;
12
13
  onSettled?: () => void;
14
+
15
+ onSuccessAsync?: (txHash: Address) => Promise<void>;
16
+ onErrorAsync?: (e: any) => Promise<void>;
17
+ onSettledAsync?: () => Promise<void>;
18
+
19
+ /** simple list of keys to invalidate */
13
20
  queriesToInvalidate?: (QueryKey | undefined)[];
21
+ /** a predicate to decide which queries to invalidate */
22
+ invalidatePredicate?: (query: Query<unknown, unknown>) => boolean;
23
+
24
+ simulationOverrideAbis?: any;
25
+
14
26
  disableLogging?: boolean;
15
27
  disableWaitingForReceipt?: boolean;
16
28
  };
@@ -26,6 +38,7 @@ export function useHandleTransactionMutationX({
26
38
  settings?: WriteExtendedAsyncParams;
27
39
  }) {
28
40
  const wagmiConfig = useConfig();
41
+ const queryClient = useQueryClient();
29
42
 
30
43
  const { invalidateMany } = useInvalidateQueries();
31
44
  const [isPending, setIsPending] = useState(false);
@@ -60,11 +73,22 @@ export function useHandleTransactionMutationX({
60
73
  }
61
74
 
62
75
  // 3. invalidate queries
63
- if (settings?.queriesToInvalidate)
64
- await invalidateMany(settings?.queriesToInvalidate);
76
+ const { queriesToInvalidate, invalidatePredicate } = settings || {};
77
+
78
+ if (invalidatePredicate) {
79
+ // 1) predicate-based
80
+ await queryClient.invalidateQueries({
81
+ predicate: invalidatePredicate,
82
+ });
83
+ }
84
+ if (queriesToInvalidate) {
85
+ // 2) explicit key list
86
+ await invalidateMany(queriesToInvalidate);
87
+ }
65
88
 
66
89
  // 4. call onSuccess callback
67
90
  settings?.onSuccess?.(txHash!);
91
+ if (settings?.onSuccessAsync) await settings.onSuccessAsync(txHash!);
68
92
 
69
93
  if (!settings?.disableLogging) {
70
94
  // 5. log result
@@ -90,10 +114,12 @@ export function useHandleTransactionMutationX({
90
114
 
91
115
  // 3. call callback
92
116
  settings?.onError?.(error);
117
+ if (settings?.onErrorAsync) await settings.onErrorAsync(error);
93
118
  } finally {
94
119
  setIsPending(false);
95
120
  // 1. call callback
96
121
  settings?.onSettled?.();
122
+ if (settings?.onSettledAsync) await settings.onSettledAsync();
97
123
  }
98
124
  return undefined;
99
125
  };
@@ -1,8 +1,9 @@
1
- import { useSendTransaction } from "wagmi";
1
+ import { usePublicClient, useSendTransaction, useWriteContract } from "wagmi";
2
2
  import {
3
3
  useHandleTransactionMutationX,
4
4
  WriteExtendedAsyncParams,
5
5
  } from "./useHandleTransactionMutationX.js";
6
+ import { writeContract } from "wagmi/actions";
6
7
 
7
8
  /**
8
9
  * Custom hook for sending a transaction using Wagmi.
@@ -52,22 +53,54 @@ import {
52
53
  */
53
54
 
54
55
  export function useSendTransactionX(settings?: WriteExtendedAsyncParams) {
56
+ const publicClient = usePublicClient();
57
+
55
58
  const { isPending, errorMessage, onMutate, onSettled } =
56
59
  useHandleTransactionMutationX({
57
60
  settings,
58
61
  });
59
62
 
60
- const { sendTransactionAsync, ...rest } = useSendTransaction({
63
+ const { sendTransaction, ...rest } = useSendTransaction({
61
64
  mutation: {
62
65
  onMutate,
63
66
  onSettled,
64
67
  },
65
68
  });
66
69
 
70
+ /**
71
+ * Wraps sendTransaction with an optional simulation.
72
+ */
73
+ async function simulateAsyncAndSendTransaction(
74
+ params: Parameters<typeof sendTransaction>[0],
75
+ simulationParams: Parameters<typeof writeContract>[1]
76
+ ) {
77
+ onMutate();
78
+
79
+ try {
80
+ if (params.to) {
81
+ //simulate!
82
+ await publicClient?.simulateContract({
83
+ address: params.to,
84
+ abi: simulationParams.abi,
85
+ functionName: simulationParams.functionName,
86
+ args: simulationParams.args ?? [],
87
+ account: params.account,
88
+ ...(simulationParams.chain != null
89
+ ? { chain: simulationParams.chain }
90
+ : {}),
91
+ });
92
+ }
93
+ // actual send!
94
+ await sendTransaction(params);
95
+ } catch (err) {
96
+ await onSettled(undefined, err, params);
97
+ }
98
+ }
99
+
67
100
  return {
68
101
  ...rest,
69
102
  isPending,
70
103
  errorMessage,
71
- sendTransactionAsync,
104
+ simulateAsyncAndSendTransaction,
72
105
  };
73
106
  }
@@ -0,0 +1,40 @@
1
+ // hooks/useFetchERC20DataX.js
2
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
3
+ import { Address } from "viem";
4
+ import { useAccount, useConfig } from "wagmi";
5
+ import { fetchERC20DataX } from "../../fetch-functions/erc20/fetchERC20DataX.js";
6
+ import { queryConfig } from "../../query-config/index.js";
7
+
8
+ const HookFetchERC20DataQK = (
9
+ address?: Address,
10
+ userAddress?: Address,
11
+ spender?: Address
12
+ ) => ["HookFetchERC20Data", address, userAddress, spender] as const;
13
+
14
+ /**
15
+ * Hook to fetch ERC-20 summary data for a given token and optional user/spender context.
16
+ *
17
+ * @param {{ address?: Address; user?: Address; spender?: Address }} params
18
+ * @param {import('@tanstack/react-query').UseQueryOptions=} options
19
+ */
20
+ export function useFetchERC20DataX({
21
+ address,
22
+ user,
23
+ spender,
24
+ }: {
25
+ address?: Address;
26
+ user?: Address;
27
+ spender?: Address;
28
+ }) {
29
+ const config = useConfig();
30
+ const queryClient = useQueryClient();
31
+ const { address: account } = useAccount();
32
+ const userAddress = user || account;
33
+
34
+ return useQuery({
35
+ queryKey: HookFetchERC20DataQK(address, userAddress, spender),
36
+ queryFn: () =>
37
+ fetchERC20DataX(address!, userAddress, spender, queryClient, config),
38
+ enabled: Boolean(address),
39
+ });
40
+ }
@@ -1,8 +1,8 @@
1
1
  import { useQuery, useQueryClient } from "@tanstack/react-query";
2
2
  import { Address, erc20Abi } from "viem";
3
3
  import { useAccount, useConfig } from "wagmi";
4
- import { queryConfig } from "../query-config/index.js";
5
- import { fetchAllowance } from "../fetch-functions/fetchAllowanceX.js";
4
+ import { fetchAllowanceX } from "../../fetch-functions/erc20/fetchAllowanceX.js";
5
+ import { queryConfig } from "../../query-config/index.js";
6
6
 
7
7
  const HookFetchAssetAllowanceQK = (
8
8
  asset?: Address,
@@ -43,13 +43,15 @@ export const useFetchAssetAllowanceX = ({
43
43
  spender?: Address;
44
44
  }) => {
45
45
  const config = useConfig();
46
+ const queryClient = useQueryClient();
46
47
  const { address: userAddress } = useAccount();
47
48
 
48
49
  const { data, ...rest } = useQuery({
49
50
  queryKey: HookFetchAssetAllowanceQK(asset, spender, userAddress),
50
- queryFn: () => fetchAllowance(asset!, spender!, userAddress!, config),
51
+ queryFn: () =>
52
+ fetchAllowanceX(asset!, spender!, userAddress!, queryClient, config),
51
53
  enabled: Boolean(asset) && Boolean(spender) && Boolean(userAddress),
52
- ...queryConfig.sensitiveDataQueryConfig,
54
+ ...queryConfig.lowSensitiveQuery,
53
55
  });
54
56
 
55
57
  return {
@@ -0,0 +1,39 @@
1
+ // hooks/useFetchERC4626DataX.js
2
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
3
+ import { Address } from "viem";
4
+ import { useAccount, useConfig } from "wagmi";
5
+ import { fetchERC4626DataX } from "../../fetch-functions/erc4626/fetchERC4626DataX.js";
6
+
7
+ const HookFetchERC4626DataQK = (
8
+ vault?: Address,
9
+ userAddress?: Address,
10
+ spender?: Address
11
+ ) => ["HookFetchERC4626Data", vault, userAddress, spender] as const;
12
+
13
+ /**
14
+ * Hook to fetch ERC-4626 vault summary data for a given vault and user/spender.
15
+ *
16
+ * @param {{ vault?: Address; user?: Address; spender?: Address }} params
17
+ * @param {import('@tanstack/react-query').UseQueryOptions=} options
18
+ */
19
+ export function useFetchERC4626DataX({
20
+ vault,
21
+ user,
22
+ spender,
23
+ }: {
24
+ vault?: Address;
25
+ user?: Address;
26
+ spender?: Address;
27
+ }) {
28
+ const config = useConfig();
29
+ const queryClient = useQueryClient();
30
+ const { address: account } = useAccount();
31
+ const userAddress = user || account;
32
+
33
+ return useQuery({
34
+ queryKey: HookFetchERC4626DataQK(vault, userAddress, spender),
35
+ queryFn: () =>
36
+ fetchERC4626DataX(vault!, userAddress, spender, queryClient, config),
37
+ enabled: Boolean(vault),
38
+ });
39
+ }
@@ -1,7 +1,7 @@
1
1
  import { useQueryClient, useQuery } from "@tanstack/react-query";
2
2
  import { Address } from "viem";
3
3
  import { useConfig } from "wagmi";
4
- import { fetchTokenX } from "../fetch-functions/fetchTokenX.js";
4
+ import { fetchTokenX } from "../../fetch-functions/fetchTokenX.js";
5
5
 
6
6
  /**
7
7
  * Returns a query key for fetching token data.
package/src/index.ts CHANGED
@@ -1,20 +1,31 @@
1
1
  /* ------------- */
2
2
  /* Mutations */
3
3
  /* ------------- */
4
- export * from "./hooks/useContractWriteX.js";
5
- export * from "./hooks/useSendTransactionX.js";
6
- export * from "./hooks/useERC20ApproveX.js";
4
+ export * from "./hooks/mutations/useContractWriteX.js";
5
+ export * from "./hooks/mutations/useSendTransactionX.js";
6
+ export * from "./hooks/mutations/useERC20ApproveX.js";
7
7
 
8
8
  /* ------------- */
9
9
  /* Queries */
10
10
  /* ------------- */
11
- export * from "./hooks/useTokenX.js";
12
- export * from "./hooks/useFetchAssetAllowanceX.js";
11
+ export * from "./hooks/queries/useTokenX.js";
12
+ export * from "./hooks/queries/useFetchAssetAllowanceX.js";
13
13
 
14
14
  /* ------------- */
15
15
  /* Fetch */
16
16
  /* ------------- */
17
17
  export * from "./fetch-functions/fetchTokenX.js";
18
+ export * from "./fetch-functions/erc20/fetchAllowanceX.js";
19
+ export * from "./fetch-functions/erc20/fetchBalanceOfX.js";
20
+ export * from "./fetch-functions/erc20/fetchTotalSupplyX.js";
21
+ export * from "./fetch-functions/erc20/fetchERC20DataX.js";
22
+ export * from "./fetch-functions/erc4626/fetchERC4626AssetX.js";
23
+ export * from "./fetch-functions/erc4626/fetchERC4626TotalAssetsX.js";
24
+ export * from "./fetch-functions/erc4626/fetchERC4626MaxDepositX.js";
25
+ export * from "./fetch-functions/erc4626/fetchERC4626MaxMintX.js";
26
+ export * from "./fetch-functions/erc4626/fetchERC4626MaxRedeemX.js";
27
+ export * from "./fetch-functions/erc4626/fetchERC4626MaxWithdrawX.js";
28
+ export * from "./fetch-functions/erc4626/fetchERC4626DataX.js";
18
29
 
19
30
  /* ------------- */
20
31
  /* Utils */
@@ -1,8 +1,18 @@
1
1
  export const queryConfig = {
2
- metadataQueryConfig: {
3
- staleTime: Infinity,
2
+ metaDataQuery: {
3
+ staleTime: Number.POSITIVE_INFINITY,
4
+ meta: { category: "metadata" } as const,
4
5
  },
5
- sensitiveDataQueryConfig: {
6
+ lowSensitiveQuery: {
6
7
  staleTime: 60_000,
8
+ meta: { category: "lowSensitive" } as const,
9
+ },
10
+ semiSensitiveQuery: {
11
+ staleTime: 180_000,
12
+ meta: { category: "semiSensitive" } as const,
13
+ },
14
+ expensiveQuery: {
15
+ staleTime: 60 * 60 * 1000,
16
+ meta: { category: "expensive" } as const,
7
17
  },
8
18
  };
@@ -0,0 +1,18 @@
1
+ import { QueryClient } from "@tanstack/react-query";
2
+ import { Config } from "wagmi";
3
+ import { getDefaults } from "../config/defaults.js";
4
+
5
+ export function ensureClientAndConfig(
6
+ queryClient?: QueryClient,
7
+ wagmiConfig?: Config
8
+ ): { queryClient: QueryClient; wagmiConfig: Config } {
9
+ if (!queryClient || !wagmiConfig) {
10
+ ({ queryClient, wagmiConfig } = getDefaults());
11
+ }
12
+ if (!queryClient || !wagmiConfig) {
13
+ throw new Error(
14
+ "Could not find queryClient or wagmiConfig; pass them in or setDefaults() first"
15
+ );
16
+ }
17
+ return { queryClient, wagmiConfig };
18
+ }
@@ -1,2 +0,0 @@
1
- import { Address } from "viem";
2
- export declare const fetchAllowance: (asset: Address, spender: Address, userAddress: Address, config: any) => Promise<bigint>;
@@ -1,22 +0,0 @@
1
- import { Address, erc20Abi } from "viem";
2
- import { readContract } from "viem/actions";
3
-
4
- export const fetchAllowance = async (
5
- asset: Address,
6
- spender: Address,
7
- userAddress: Address,
8
- config: any
9
- ) => {
10
- const allowance = await readContract(config, {
11
- address: asset,
12
- abi: erc20Abi,
13
- functionName: "allowance",
14
- args: [userAddress, spender],
15
- });
16
-
17
- if (allowance == null) {
18
- throw new Error("Failed to fetch token data or allowance");
19
- }
20
-
21
- return allowance;
22
- };