wagmi-extended 2.3.8 → 2.4.0

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.
@@ -1,29 +1,31 @@
1
- import { waitForTransactionReceipt } from "wagmi/actions";
2
- import { useConfig } from "wagmi";
3
- import { Query, QueryKey } from "@tanstack/query-core";
4
- import { Address } from "viem";
5
- import { useState } from "react";
6
- import { useInvalidateQueries } from "./useInvalidateQueries.js";
7
- import { useQueryClient } from "@tanstack/react-query";
8
- import { getParsedErrorX } from "../../utils/errorParserX.js";
1
+ import { waitForTransactionReceipt } from "wagmi/actions"
2
+ import { useConfig } from "wagmi"
3
+ import { Query, QueryKey } from "@tanstack/query-core"
4
+ import { Address, WaitForTransactionReceiptParameters } from "viem"
5
+ import { useState } from "react"
6
+ import { useInvalidateQueries } from "./useInvalidateQueries.js"
7
+ import { useQueryClient } from "@tanstack/react-query"
8
+ import { getParsedErrorX } from "../../utils/errorParserX.js"
9
9
 
10
10
  export type WriteExtendedAsyncParams = {
11
- onSuccess?: (txHash: Address) => void;
12
- onError?: (e: any) => void;
13
- onSettled?: () => void;
11
+ onSuccess?: (txHash: Address) => void
12
+ onError?: (e: any) => void
13
+ onSettled?: () => void
14
14
 
15
- onSuccessAsync?: (txHash: Address) => Promise<void>;
16
- onErrorAsync?: (e: any) => Promise<void>;
17
- onSettledAsync?: () => Promise<void>;
15
+ onSuccessAsync?: (txHash: Address) => Promise<void>
16
+ onErrorAsync?: (e: any) => Promise<void>
17
+ onSettledAsync?: () => Promise<void>
18
18
 
19
19
  /** simple list of keys to invalidate */
20
- queriesToInvalidate?: (QueryKey | undefined)[];
20
+ queriesToInvalidate?: (QueryKey | undefined)[]
21
21
  /** a predicate to decide which queries to invalidate */
22
- invalidatePredicate?: (query: Query<unknown, unknown>) => boolean;
22
+ invalidatePredicate?: (query: Query<unknown, unknown>) => boolean
23
23
 
24
- disableLogging?: boolean;
25
- disableWaitingForReceipt?: boolean;
26
- };
24
+ disableLogging?: boolean
25
+ disableWaitingForReceipt?: boolean
26
+
27
+ waitForTransactionReceiptParameters?: WaitForTransactionReceiptParameters
28
+ }
27
29
 
28
30
  /**
29
31
  * Custom hook to handle transaction mutations.
@@ -33,70 +35,63 @@ export type WriteExtendedAsyncParams = {
33
35
  export function useHandleTransactionMutationX({
34
36
  settings,
35
37
  }: {
36
- settings?: WriteExtendedAsyncParams;
38
+ settings?: WriteExtendedAsyncParams
37
39
  }) {
38
- const wagmiConfig = useConfig();
39
- const queryClient = useQueryClient();
40
+ const wagmiConfig = useConfig()
41
+ const queryClient = useQueryClient()
40
42
 
41
- const { invalidateMany } = useInvalidateQueries();
42
- const [isPending, setIsPending] = useState(false);
43
- const [errorMessage, setErrorMessage] = useState<string | undefined>(
44
- undefined
45
- );
43
+ const { invalidateMany } = useInvalidateQueries()
44
+ const [isPending, setIsPending] = useState(false)
45
+ const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined)
46
46
 
47
47
  const onMutate = () => {
48
- setIsPending(true);
49
- setErrorMessage(undefined);
50
- };
51
-
52
- const onSettled = async (
53
- txHash: Address | undefined,
54
- error: any,
55
- args: any
56
- ) => {
48
+ setIsPending(true)
49
+ setErrorMessage(undefined)
50
+ }
51
+
52
+ const onSettled = async (txHash: Address | undefined, error: any, args: any) => {
57
53
  try {
58
- if (error) throw error;
54
+ if (error) throw error
59
55
 
60
56
  if (!settings?.disableWaitingForReceipt) {
61
57
  // 1. wait for transaction receipt
62
58
  const txReceipt = await waitForTransactionReceipt(wagmiConfig, {
63
59
  hash: txHash!,
64
- });
60
+ ...settings?.waitForTransactionReceiptParameters,
61
+ })
65
62
 
66
63
  // 2. throw if receipt is not valid
67
- if (txReceipt.status === "reverted")
68
- throw new Error("Execution reverted.");
69
- if (txReceipt.status !== "success")
70
- throw new Error("Execution reverted.");
64
+ if (txReceipt.status === "reverted") throw new Error("Execution reverted.")
65
+ if (txReceipt.status !== "success") throw new Error("Execution reverted.")
71
66
  }
72
67
 
73
68
  // 3. invalidate queries
74
- const { queriesToInvalidate, invalidatePredicate } = settings || {};
69
+ const { queriesToInvalidate, invalidatePredicate } = settings || {}
75
70
 
76
71
  if (invalidatePredicate) {
77
72
  // 1) predicate-based
78
73
  await queryClient.invalidateQueries({
79
74
  predicate: invalidatePredicate,
80
- });
75
+ })
81
76
  }
82
77
  if (queriesToInvalidate) {
83
78
  // 2) explicit key list
84
- await invalidateMany(queriesToInvalidate);
79
+ await invalidateMany(queriesToInvalidate)
85
80
  }
86
81
 
87
82
  // 4. call onSuccess callback
88
- settings?.onSuccess?.(txHash!);
89
- if (settings?.onSuccessAsync) await settings.onSuccessAsync(txHash!);
83
+ settings?.onSuccess?.(txHash!)
84
+ if (settings?.onSuccessAsync) await settings.onSuccessAsync(txHash!)
90
85
 
91
86
  if (!settings?.disableLogging) {
92
87
  // 5. log result
93
88
  // eslint-disable-next-line no-console
94
- console.info("Operation successful:", txHash); // todo: add logging service
89
+ console.info("Operation successful:", txHash) // todo: add logging service
95
90
  }
96
91
  // 6. return result
97
- return txHash;
92
+ return txHash
98
93
  } catch (error) {
99
- const parsedError = getParsedErrorX(error);
94
+ const parsedError = getParsedErrorX(error)
100
95
 
101
96
  if (!settings?.disableLogging) {
102
97
  // 1. log error
@@ -104,28 +99,28 @@ export function useHandleTransactionMutationX({
104
99
  `ContractWriteExtended Operation failed with error(parsed): ${parsedError}`,
105
100
  { error },
106
101
  { args }
107
- );
108
- console.error({ error });
102
+ )
103
+ console.error({ error })
109
104
  }
110
105
  // 2. set error message
111
- setErrorMessage(parsedError);
106
+ setErrorMessage(parsedError)
112
107
 
113
108
  // 3. call callback
114
- settings?.onError?.(error);
115
- if (settings?.onErrorAsync) await settings.onErrorAsync(error);
109
+ settings?.onError?.(error)
110
+ if (settings?.onErrorAsync) await settings.onErrorAsync(error)
116
111
  } finally {
117
- setIsPending(false);
112
+ setIsPending(false)
118
113
  // 1. call callback
119
- settings?.onSettled?.();
120
- if (settings?.onSettledAsync) await settings.onSettledAsync();
114
+ settings?.onSettled?.()
115
+ if (settings?.onSettledAsync) await settings.onSettledAsync()
121
116
  }
122
- return undefined;
123
- };
117
+ return undefined
118
+ }
124
119
 
125
120
  return {
126
121
  onMutate,
127
122
  onSettled,
128
123
  isPending,
129
124
  errorMessage,
130
- };
125
+ }
131
126
  }