wagmi-extended 2.0.1 → 2.1.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.
- package/README.md +153 -68
- package/dist/hooks/mutations/useContractWriteX.d.ts +21 -57
- package/dist/hooks/mutations/useHandleTransactionMutationX.d.ts +0 -1
- package/dist/hooks/mutations/useSendTransactionX.d.ts +26 -45
- package/dist/index.cjs.js +58 -106
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +58 -106
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/mutations/useContractWriteX.ts +27 -61
- package/src/hooks/mutations/useHandleTransactionMutationX.ts +0 -2
- package/src/hooks/mutations/useSendTransactionX.ts +32 -47
- package/src/hooks/queries/useERC20Data.ts +0 -1
|
@@ -6,65 +6,28 @@ import {
|
|
|
6
6
|
import { Address } from "viem";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Custom hook for writing to a smart contract using Wagmi.
|
|
9
|
+
* Custom hook for writing to a smart contract using Wagmi with optional simulation.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @param {
|
|
14
|
-
* @param {
|
|
15
|
-
* @param {
|
|
16
|
-
* @param {Function} [settings.
|
|
17
|
-
* @param {
|
|
18
|
-
* @param {Function} [settings.onSettled] - Callback function to be called after the transaction settles (whether success or failure).
|
|
19
|
-
* @param {QueryKey[]} [settings.queriesToInvalidate] - Array of query keys to invalidate after the transaction receives a receipt.
|
|
20
|
-
* @returns {Object} Object containing the following properties:
|
|
21
|
-
* - {boolean} isPending - Indicates whether the transaction is pending.
|
|
22
|
-
* - {string|undefined} errorMessage - The error message, if an error occurred during the transaction.
|
|
23
|
-
* - {Function} writeContractAsync - Function to trigger the write operation.
|
|
24
|
-
*
|
|
25
|
-
/**
|
|
26
|
-
* Custom hook for writing a contract using Wagmi with extended functionality.
|
|
27
|
-
*
|
|
28
|
-
* This hook wraps Wagmi’s `useContractWriteX` with additional handling for
|
|
29
|
-
* waiting for a transaction receipt, logging control, and invalidation of specified queries.
|
|
30
|
-
*
|
|
31
|
-
* @param {WriteExtendedAsyncParams} [settings] - Optional settings for handling the transaction.
|
|
11
|
+
* @param {WriteExtendedAsyncParams} settings - Settings for handling transaction lifecycle:
|
|
12
|
+
* @param {boolean} [settings.disableWaitingForReceipt] - Disable waiting for receipt.
|
|
13
|
+
* @param {boolean} [settings.disableLogging] - Disable logging.
|
|
14
|
+
* @param {Function} [settings.onSuccess] - Callback invoked on successful transaction receipt.
|
|
15
|
+
* @param {Function} [settings.onError] - Callback invoked on simulation or transaction error.
|
|
16
|
+
* @param {Function} [settings.onSettled] - Callback invoked after transaction settles.
|
|
17
|
+
* @param {Array<import('@tanstack/query-core').QueryKey>} [settings.queriesToInvalidate] - Query keys to invalidate after receipt.
|
|
32
18
|
* @returns {Object} An object containing:
|
|
33
|
-
* -
|
|
34
|
-
* -
|
|
35
|
-
* -
|
|
19
|
+
* - writeContract: Wagmi's writeContract function.
|
|
20
|
+
* - writeContractX: Wrapped writeContract with optional simulation.
|
|
21
|
+
* - isPending: Boolean indicating if transaction is in progress.
|
|
22
|
+
* - errorMessage: Error message if one occurred.
|
|
36
23
|
*
|
|
37
24
|
* @example
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* const handleWrite = async () => {
|
|
45
|
-
* try {
|
|
46
|
-
* const txHash = await writeContractAsync({ transaction params here.. }, {
|
|
47
|
-
* // use calbacks here in writeContractAsync or in useContractWriteX
|
|
48
|
-
* onSuccess: (txHash) => console.log("Transaction successful:", txHash),
|
|
49
|
-
* onError: (error) => console.error("Transaction error:", error),
|
|
50
|
-
* });
|
|
51
|
-
* console.log("Received txHash:", txHash);
|
|
52
|
-
* } catch (err) {
|
|
53
|
-
* console.error("Failed writing transaction:", err);`
|
|
54
|
-
* }
|
|
55
|
-
* };
|
|
56
|
-
*
|
|
57
|
-
* return (
|
|
58
|
-
* <div>
|
|
59
|
-
* <button onClick={handleWrite} disabled={isPending}>
|
|
60
|
-
* {isPending ? "Processing..." : "Write Transaction"}
|
|
61
|
-
* </button>
|
|
62
|
-
* {errorMessage && <p>Error: {errorMessage}</p>}
|
|
63
|
-
* </div>
|
|
64
|
-
* );
|
|
65
|
-
* }
|
|
25
|
+
* const { writeContractX, isPending, errorMessage } = useContractWriteX({ onSuccess: ..., onError: ... });
|
|
26
|
+
* await writeContractX(
|
|
27
|
+
* { abi, address, functionName, args, account, chain, value },
|
|
28
|
+
* disable simulation? = false
|
|
29
|
+
* );
|
|
66
30
|
*/
|
|
67
|
-
|
|
68
31
|
export function useContractWriteX(settings: WriteExtendedAsyncParams) {
|
|
69
32
|
const publicClient = usePublicClient();
|
|
70
33
|
|
|
@@ -76,8 +39,9 @@ export function useContractWriteX(settings: WriteExtendedAsyncParams) {
|
|
|
76
39
|
mutation: { onMutate, onSettled },
|
|
77
40
|
});
|
|
78
41
|
|
|
79
|
-
async function
|
|
80
|
-
params: Parameters<typeof wagmiWrite.writeContract>[0]
|
|
42
|
+
async function writeContractX(
|
|
43
|
+
params: Parameters<typeof wagmiWrite.writeContract>[0],
|
|
44
|
+
disableSimulation = false
|
|
81
45
|
) {
|
|
82
46
|
// 0) signal start
|
|
83
47
|
onMutate();
|
|
@@ -86,10 +50,12 @@ export function useContractWriteX(settings: WriteExtendedAsyncParams) {
|
|
|
86
50
|
// 1) optional dry-run
|
|
87
51
|
const { chain, ...others } = params;
|
|
88
52
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
53
|
+
if (!disableSimulation) {
|
|
54
|
+
await publicClient?.simulateContract({
|
|
55
|
+
...others,
|
|
56
|
+
...(chain != null ? { chain } : {}),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
93
59
|
|
|
94
60
|
wagmiWrite.writeContract(params);
|
|
95
61
|
} catch (err) {
|
|
@@ -99,7 +65,7 @@ export function useContractWriteX(settings: WriteExtendedAsyncParams) {
|
|
|
99
65
|
|
|
100
66
|
return {
|
|
101
67
|
...wagmiWrite,
|
|
102
|
-
|
|
68
|
+
writeContractX,
|
|
103
69
|
isPending,
|
|
104
70
|
errorMessage,
|
|
105
71
|
};
|
|
@@ -21,8 +21,6 @@ export type WriteExtendedAsyncParams = {
|
|
|
21
21
|
/** a predicate to decide which queries to invalidate */
|
|
22
22
|
invalidatePredicate?: (query: Query<unknown, unknown>) => boolean;
|
|
23
23
|
|
|
24
|
-
simulationOverrideAbis?: any;
|
|
25
|
-
|
|
26
24
|
disableLogging?: boolean;
|
|
27
25
|
disableWaitingForReceipt?: boolean;
|
|
28
26
|
};
|
|
@@ -6,52 +6,28 @@ import {
|
|
|
6
6
|
import { writeContract } from "wagmi/actions";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Custom hook for sending a transaction using Wagmi.
|
|
9
|
+
* Custom hook for sending a transaction using Wagmi with optional simulation.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
11
|
+
* @param {WriteExtendedAsyncParams} [settings] - Settings for handling transaction lifecycle:
|
|
12
|
+
* @param {boolean} [settings.disableWaitingForReceipt] - Disable waiting for receipt.
|
|
13
|
+
* @param {boolean} [settings.disableLogging] - Disable logging.
|
|
14
|
+
* @param {Function} [settings.onSuccess] - Callback on success.
|
|
15
|
+
* @param {Function} [settings.onError] - Callback on error.
|
|
16
|
+
* @param {Function} [settings.onSettled] - Callback after settlement.
|
|
17
|
+
* @param {Array<import('@tanstack/query-core').QueryKey>} [settings.queriesToInvalidate] - Query keys to invalidate after receipt.
|
|
18
|
+
* @returns {Object} An object containing:
|
|
19
|
+
* - sendTransaction: Wagmi's sendTransaction function.
|
|
20
|
+
* - sendTransactionX: Wrapped sendTransaction with optional simulation.
|
|
21
|
+
* - isPending: Boolean indicating if transaction is in progress.
|
|
22
|
+
* - errorMessage: Error message if one occurred.
|
|
12
23
|
*
|
|
13
|
-
* @param {WriteExtendedAsyncParams} [settings] - Optional settings for the write operation.
|
|
14
|
-
* @param {boolean} [settings.disableWaitingForReceipt] - Disables waiting for the transaction receipt.
|
|
15
|
-
* @param {boolean} [settings.disableLogging] - Disables logging the result of the transaction.
|
|
16
|
-
* @param {Function} [settings.onSuccess] - Callback function to be called on successful transaction.
|
|
17
|
-
* @param {Function} [settings.onError] - Callback function to be called on transaction error.
|
|
18
|
-
* @param {Function} [settings.onSettled] - Callback function to be called after the transaction settles (whether success or failure).
|
|
19
|
-
* @param {QueryKey[]} [settings.queriesToInvalidate] - Array of query keys to invalidate after the transaction receives a receipt.
|
|
20
|
-
* @returns {Object} Object containing the following properties:
|
|
21
|
-
* - {boolean} isPending - Indicates whether the transaction is pending.
|
|
22
|
-
* - {string|undefined} errorMessage - The error message, if an error occurred during the transaction.
|
|
23
|
-
* - {Function} sendTransactionAsync - Function to trigger the send transaction mutation.
|
|
24
|
-
|
|
25
24
|
* @example
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* onError: (error) => console.error("Transaction error:", error),
|
|
32
|
-
* queriesToInvalidate: [["userBalance"], ["userActivity"]],
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* const handleSend = async () => {
|
|
36
|
-
* try {
|
|
37
|
-
* const txHash = await sendTransactionAsync({ transaction params here.. });
|
|
38
|
-
* console.log("Received txHash:", txHash);
|
|
39
|
-
* } catch (err) {
|
|
40
|
-
* console.error("Failed sending transaction:", err);`
|
|
41
|
-
* }
|
|
42
|
-
* };
|
|
43
|
-
*
|
|
44
|
-
* return (
|
|
45
|
-
* <div>
|
|
46
|
-
* <button onClick={handleSend} disabled={isPending}>
|
|
47
|
-
* {isPending ? "Processing..." : "Send Transaction"}
|
|
48
|
-
* </button>
|
|
49
|
-
* {errorMessage && <p>Error: {errorMessage}</p>}
|
|
50
|
-
* </div>
|
|
51
|
-
* );
|
|
52
|
-
* }
|
|
25
|
+
* const { sendTransactionX, isPending, errorMessage } = useSendTransactionX({ onSuccess: ..., onError: ... });
|
|
26
|
+
* await sendTransactionX(
|
|
27
|
+
* { to, value, data, account, chain },
|
|
28
|
+
* { abi, functionName, args, chain }
|
|
29
|
+
* );
|
|
53
30
|
*/
|
|
54
|
-
|
|
55
31
|
export function useSendTransactionX(settings?: WriteExtendedAsyncParams) {
|
|
56
32
|
const publicClient = usePublicClient();
|
|
57
33
|
|
|
@@ -68,16 +44,24 @@ export function useSendTransactionX(settings?: WriteExtendedAsyncParams) {
|
|
|
68
44
|
});
|
|
69
45
|
|
|
70
46
|
/**
|
|
71
|
-
* Wraps sendTransaction with an optional simulation.
|
|
47
|
+
* Wraps sendTransaction with an optional simulation step.
|
|
48
|
+
*
|
|
49
|
+
* @param {import('viem').SendTransactionParameters} params - Parameters to sendTransaction.
|
|
50
|
+
* @param {import('viem').SimulateContractParameters} [simulationParams] - Optional parameters to simulate contract call:
|
|
51
|
+
* @param {Array|object} simulationParams.abi - Contract ABI for simulation.
|
|
52
|
+
* @param {string} simulationParams.functionName - Name of the contract function to simulate.
|
|
53
|
+
* @param {any[]} [simulationParams.args] - Arguments for the function call.
|
|
54
|
+
* @param {import('viem').Chain} [simulationParams.chain] - Chain to run the simulation on.
|
|
55
|
+
* @returns {Promise<void>}
|
|
72
56
|
*/
|
|
73
|
-
async function
|
|
57
|
+
async function sendTransactionX(
|
|
74
58
|
params: Parameters<typeof sendTransaction>[0],
|
|
75
|
-
simulationParams
|
|
59
|
+
simulationParams?: Parameters<typeof writeContract>[1]
|
|
76
60
|
) {
|
|
77
61
|
onMutate();
|
|
78
62
|
|
|
79
63
|
try {
|
|
80
|
-
if (params.to) {
|
|
64
|
+
if (params.to && simulationParams) {
|
|
81
65
|
//simulate!
|
|
82
66
|
await publicClient?.simulateContract({
|
|
83
67
|
address: params.to,
|
|
@@ -101,6 +85,7 @@ export function useSendTransactionX(settings?: WriteExtendedAsyncParams) {
|
|
|
101
85
|
...rest,
|
|
102
86
|
isPending,
|
|
103
87
|
errorMessage,
|
|
104
|
-
|
|
88
|
+
sendTransaction,
|
|
89
|
+
sendTransactionX,
|
|
105
90
|
};
|
|
106
91
|
}
|
|
@@ -3,7 +3,6 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
3
3
|
import { Address } from "viem";
|
|
4
4
|
import { useAccount, useConfig } from "wagmi";
|
|
5
5
|
import { fetchERC20DataX } from "../../fetch-functions/erc20/fetchERC20DataX.js";
|
|
6
|
-
import { queryConfig } from "../../query-config/index.js";
|
|
7
6
|
|
|
8
7
|
const HookFetchERC20DataQK = (
|
|
9
8
|
address?: Address,
|