@@ -63,6 +63,81 @@ export type CreateUseWriteContractReturnType<
6363 wagmi_UseWriteContractReturnType < config , context > ,
6464 'writeContract' | 'writeContractAsync'
6565 > & {
66+ mutate : <
67+ const abi2 extends abi ,
68+ name extends functionName extends ContractFunctionName <
69+ abi ,
70+ stateMutability
71+ >
72+ ? functionName
73+ : ContractFunctionName < abi , stateMutability > ,
74+ args extends ContractFunctionArgs < abi2 , stateMutability , name > ,
75+ chainId extends config [ 'chains' ] [ number ] [ 'id' ] ,
76+ > (
77+ variables : Variables <
78+ abi2 ,
79+ functionName ,
80+ name ,
81+ args ,
82+ config ,
83+ chainId ,
84+ address
85+ > ,
86+ options ?:
87+ | MutateOptions <
88+ WriteContractData ,
89+ WriteContractErrorType ,
90+ WriteContractVariables <
91+ abi2 ,
92+ name ,
93+ args ,
94+ config ,
95+ chainId ,
96+ // use `functionName` to make sure it's not union of all possible function names
97+ name
98+ > ,
99+ context
100+ >
101+ | undefined ,
102+ ) => void
103+ mutateAsync : <
104+ const abi2 extends abi ,
105+ name extends functionName extends ContractFunctionName <
106+ abi ,
107+ stateMutability
108+ >
109+ ? functionName
110+ : ContractFunctionName < abi , stateMutability > ,
111+ args extends ContractFunctionArgs < abi2 , stateMutability , name > ,
112+ chainId extends config [ 'chains' ] [ number ] [ 'id' ] ,
113+ > (
114+ variables : Variables <
115+ abi2 ,
116+ functionName ,
117+ name ,
118+ args ,
119+ config ,
120+ chainId ,
121+ address
122+ > ,
123+ options ?:
124+ | MutateOptions <
125+ WriteContractData ,
126+ WriteContractErrorType ,
127+ WriteContractVariables <
128+ abi2 ,
129+ name ,
130+ args ,
131+ config ,
132+ chainId ,
133+ // use `functionName` to make sure it's not union of all possible function names
134+ name
135+ > ,
136+ context
137+ >
138+ | undefined ,
139+ ) => Promise < WriteContractData >
140+ /** @deprecated use `mutate` instead */
66141 writeContract : <
67142 const abi2 extends abi ,
68143 name extends functionName extends ContractFunctionName <
@@ -100,6 +175,7 @@ export type CreateUseWriteContractReturnType<
100175 >
101176 | undefined ,
102177 ) => void
178+ /** @deprecated use `mutateAsync` instead */
103179 writeContractAsync : <
104180 const abi2 extends abi ,
105181 name extends functionName extends ContractFunctionName <
@@ -158,76 +234,78 @@ export function createUseWriteContract<
158234 const result = useWriteContract ( parameters )
159235 const configChainId = useChainId ( { config } )
160236 type Args = Parameters < wagmi_UseWriteContractReturnType [ 'writeContract' ] >
161- return {
162- ...( result as any ) ,
163- writeContract : useCallback (
164- ( ...args : Args ) => {
165- const chainId = ( ( ) => {
166- if ( args [ 0 ] . chainId ) return args [ 0 ] . chainId
167- return configChainId
168- } ) ( )
169- const variables = {
170- ...( args [ 0 ] as any ) ,
171- address : chainId ? props . address ?. [ chainId ] : undefined ,
172- ...( props . functionName
173- ? { functionName : props . functionName }
174- : { } ) ,
175- abi : props . abi ,
176- }
177- result . writeContract ( variables , args [ 1 ] as any )
178- } ,
179- [ props , configChainId , result . writeContract ] ,
180- ) ,
181- writeContractAsync : useCallback (
182- ( ...args : Args ) => {
183- const chainId = ( ( ) => {
184- if ( args [ 0 ] . chainId ) return args [ 0 ] . chainId
185- return configChainId
186- } ) ( )
187- const variables = {
188- ...( args [ 0 ] as any ) ,
189- address : chainId ? props . address ?. [ chainId ] : undefined ,
190- ...( props . functionName
191- ? { functionName : props . functionName }
192- : { } ) ,
193- abi : props . abi ,
194- }
195- return result . writeContractAsync ( variables , args [ 1 ] as any )
196- } ,
197- [ props , configChainId , result . writeContractAsync ] ,
198- ) ,
199- }
200- }
201-
202- return ( parameters ) => {
203- const result = useWriteContract ( parameters )
204- type Args = Parameters < wagmi_UseWriteContractReturnType [ 'writeContract' ] >
205- return {
206- ...( result as any ) ,
207- writeContract : useCallback (
237+ const mutate = useCallback (
208238 ( ...args : Args ) => {
239+ const chainId = ( ( ) => {
240+ if ( args [ 0 ] . chainId ) return args [ 0 ] . chainId
241+ return configChainId
242+ } ) ( )
209243 const variables = {
210244 ...( args [ 0 ] as any ) ,
211- ... ( props . address ? { address : props . address } : { } ) ,
245+ address : chainId ? props . address ?. [ chainId ] : undefined ,
212246 ...( props . functionName ? { functionName : props . functionName } : { } ) ,
213247 abi : props . abi ,
214248 }
215249 result . writeContract ( variables , args [ 1 ] as any )
216250 } ,
217- [ props , result . writeContract ] ,
218- ) ,
219- writeContractAsync : useCallback (
251+ [ props , configChainId , result . writeContract ] ,
252+ )
253+ const mutateAsync = useCallback (
220254 ( ...args : Args ) => {
255+ const chainId = ( ( ) => {
256+ if ( args [ 0 ] . chainId ) return args [ 0 ] . chainId
257+ return configChainId
258+ } ) ( )
221259 const variables = {
222260 ...( args [ 0 ] as any ) ,
223- ... ( props . address ? { address : props . address } : { } ) ,
261+ address : chainId ? props . address ?. [ chainId ] : undefined ,
224262 ...( props . functionName ? { functionName : props . functionName } : { } ) ,
225263 abi : props . abi ,
226264 }
227265 return result . writeContractAsync ( variables , args [ 1 ] as any )
228266 } ,
229- [ props , result . writeContractAsync ] ,
230- ) ,
267+ [ props , configChainId , result . writeContractAsync ] ,
268+ )
269+ return {
270+ ...( result as any ) ,
271+ mutate,
272+ mutateAsync,
273+ writeContract : mutate ,
274+ writeContractAsync : mutateAsync ,
275+ }
276+ }
277+
278+ return ( parameters ) => {
279+ const result = useWriteContract ( parameters )
280+ type Args = Parameters < wagmi_UseWriteContractReturnType [ 'writeContract' ] >
281+ const mutate = useCallback (
282+ ( ...args : Args ) => {
283+ const variables = {
284+ ...( args [ 0 ] as any ) ,
285+ ...( props . address ? { address : props . address } : { } ) ,
286+ ...( props . functionName ? { functionName : props . functionName } : { } ) ,
287+ abi : props . abi ,
288+ }
289+ result . mutate ( variables , args [ 1 ] as any )
290+ } ,
291+ [ props , result . mutate ] ,
292+ )
293+ const mutateAsync = useCallback (
294+ ( ...args : Args ) => {
295+ const variables = {
296+ ...( args [ 0 ] as any ) ,
297+ ...( props . address ? { address : props . address } : { } ) ,
298+ ...( props . functionName ? { functionName : props . functionName } : { } ) ,
299+ abi : props . abi ,
300+ }
301+ return result . mutateAsync ( variables , args [ 1 ] as any )
302+ } ,
303+ [ props , result . mutateAsync ] ,
304+ )
305+ return {
306+ ...( result as any ) ,
307+ writeContract : mutate ,
308+ writeContractAsync : mutateAsync ,
231309 }
232310 }
233311}
0 commit comments